diff --git a/README.md b/README.md
index a962b529..26a8ba7b 100644
--- a/README.md
+++ b/README.md
@@ -315,7 +315,7 @@ returns of functions.
name in Rust | name in C++ | restrictions |
String | rust::String | |
&str | rust::Str | |
-&[u8] | rust::Slice<uint8_t> | arbitrary &[T] not implemented yet |
+&[u8] | rust::Slice<const uint8_t> | arbitrary &[T] not implemented yet |
CxxString | std::string | cannot be passed by value |
Box<T> | rust::Box<T> | cannot hold opaque C++ type |
UniquePtr<T> | std::unique_ptr<T> | cannot hold opaque Rust type |
diff --git a/gen/src/write.rs b/gen/src/write.rs
index 5fe8ee30..5708b49d 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -432,7 +432,7 @@ fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
}
Some(Type::SliceRefU8(_)) if !indirect_return => {
out.builtin.rust_slice_repr = true;
- write!(out, "::rust::impl<::rust::Slice>::repr(")
+ write!(out, "::rust::impl<::rust::Slice>::repr(")
}
_ => {}
}
@@ -471,7 +471,7 @@ fn write_cxx_function_shim<'a>(out: &mut OutFile<'a>, efn: &'a ExternFn) {
} else if let Type::SliceRefU8(_) = arg.ty {
write!(
out,
- "::rust::Slice(static_cast({0}.ptr), {0}.len)",
+ "::rust::Slice(static_cast({0}.ptr), {0}.len)",
arg.ident,
);
} else if out.types.needs_indirect_abi(&arg.ty) {
@@ -688,7 +688,7 @@ fn write_rust_function_shim_impl(
}
Type::SliceRefU8(_) => {
out.builtin.rust_slice_new = true;
- write!(out, "::rust::impl<::rust::Slice>::slice(");
+ write!(out, "::rust::impl<::rust::Slice>::slice(");
}
_ => {}
}
@@ -714,7 +714,7 @@ fn write_rust_function_shim_impl(
}
Type::SliceRefU8(_) => {
out.builtin.rust_slice_repr = true;
- write!(out, "::rust::impl<::rust::Slice>::repr(");
+ write!(out, "::rust::impl<::rust::Slice>::repr(");
}
ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
_ => {}
@@ -885,7 +885,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
write!(out, "::rust::Str");
}
Type::SliceRefU8(_) => {
- write!(out, "::rust::Slice");
+ write!(out, "::rust::Slice");
}
Type::Fn(f) => {
write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
diff --git a/include/cxx.h b/include/cxx.h
index 7dfbbcaf..83a0ccd6 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -90,13 +90,16 @@ private:
#ifndef CXXBRIDGE05_RUST_SLICE
template
class Slice final {
+ static_assert(std::is_const::value,
+ "&[T] needs to be written as rust::Slice in C++");
+
public:
Slice() noexcept;
- Slice(const T *, size_t count) noexcept;
+ Slice(T *, size_t count) noexcept;
Slice &operator=(const Slice &) noexcept = default;
- const T *data() const noexcept;
+ T *data() const noexcept;
size_t size() const noexcept;
size_t length() const noexcept;
@@ -108,7 +111,7 @@ private:
friend impl;
// Not necessarily ABI compatible with &[T]. Codegen will translate to
// cxx::rust_sliceu8::RustSliceU8 which matches this layout.
- const T *ptr;
+ T *ptr;
size_t len;
};
#endif // CXXBRIDGE05_RUST_SLICE
@@ -358,13 +361,13 @@ inline size_t Str::length() const noexcept { return this->len; }
#ifndef CXXBRIDGE05_RUST_SLICE
#define CXXBRIDGE05_RUST_SLICE
template
-Slice::Slice() noexcept : ptr(reinterpret_cast(this)), len(0) {}
+Slice::Slice() noexcept : ptr(reinterpret_cast(this)), len(0) {}
template
-Slice::Slice(const T *s, size_t count) noexcept : ptr(s), len(count) {}
+Slice::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
template
-const T *Slice::data() const noexcept {
+T *Slice::data() const noexcept {
return this->ptr;
}
diff --git a/src/lib.rs b/src/lib.rs
index c72e3628..9a3e04e6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -324,7 +324,7 @@
//! name in Rust | name in C++ | restrictions |
//! String | rust::String | |
//! &str | rust::Str | |
-//! &[u8] | rust::Slice<uint8_t> | arbitrary &[T] not implemented yet |
+//! &[u8] | rust::Slice<const uint8_t> | arbitrary &[T] not implemented yet |
//! CxxString | std::string | cannot be passed by value |
//! Box<T> | rust::Box<T> | cannot hold opaque C++ type |
//! UniquePtr<T> | std::unique_ptr<T> | cannot hold opaque Rust type |
diff --git a/tests/ffi/tests.cc b/tests/ffi/tests.cc
index 2305766c..fdf7a149 100644
--- a/tests/ffi/tests.cc
+++ b/tests/ffi/tests.cc
@@ -76,10 +76,10 @@ rust::Str c_return_str(const Shared &shared) {
return "2020";
}
-rust::Slice c_return_sliceu8(const Shared &shared) {
+rust::Slice c_return_sliceu8(const Shared &shared) {
(void)shared;
- return rust::Slice(reinterpret_cast(SLICE_DATA),
- sizeof(SLICE_DATA));
+ return rust::Slice(
+ reinterpret_cast(SLICE_DATA), sizeof(SLICE_DATA));
}
rust::String c_return_rust_string() { return "2020"; }
@@ -240,7 +240,7 @@ void c_take_str(rust::Str s) {
}
}
-void c_take_sliceu8(rust::Slice s) {
+void c_take_sliceu8(rust::Slice s) {
if (std::string(reinterpret_cast(s.data()), s.size()) ==
"2020") {
cxx_test_suite_set_correct();
@@ -436,7 +436,9 @@ const rust::String &c_try_return_ref(const rust::String &s) { return s; }
rust::Str c_try_return_str(rust::Str s) { return s; }
-rust::Slice c_try_return_sliceu8(rust::Slice s) { return s; }
+rust::Slice c_try_return_sliceu8(rust::Slice s) {
+ return s;
+}
rust::String c_try_return_rust_string() { return c_return_rust_string(); }
@@ -608,7 +610,7 @@ extern "C" const char *cxx_run_test() noexcept {
r_take_unique_ptr(std::unique_ptr(new C{2020}));
r_take_ref_c(C{2020});
r_take_str(rust::Str("2020"));
- r_take_sliceu8(rust::Slice(
+ r_take_sliceu8(rust::Slice(
reinterpret_cast(SLICE_DATA), sizeof(SLICE_DATA)));
r_take_rust_string(rust::String("2020"));
r_take_unique_ptr_string(
diff --git a/tests/ffi/tests.h b/tests/ffi/tests.h
index 2217c361..d231939e 100644
--- a/tests/ffi/tests.h
+++ b/tests/ffi/tests.h
@@ -84,7 +84,7 @@ const size_t &c_return_ns_ref(const ::A::AShared &shared);
const size_t &c_return_nested_ns_ref(const ::A::B::ABShared &shared);
size_t &c_return_mut(Shared &shared);
rust::Str c_return_str(const Shared &shared);
-rust::Slice c_return_sliceu8(const Shared &shared);
+rust::Slice c_return_sliceu8(const Shared &shared);
rust::String c_return_rust_string();
std::unique_ptr c_return_unique_ptr_string();
std::unique_ptr> c_return_unique_ptr_vector_u8();
@@ -114,7 +114,7 @@ void c_take_ref_r(const R &r);
void c_take_ref_c(const C &c);
void c_take_ref_ns_c(const ::H::H &h);
void c_take_str(rust::Str s);
-void c_take_sliceu8(rust::Slice s);
+void c_take_sliceu8(rust::Slice s);
void c_take_rust_string(rust::String s);
void c_take_unique_ptr_string(std::unique_ptr s);
void c_take_unique_ptr_vector_u8(std::unique_ptr> v);
@@ -148,7 +148,7 @@ size_t c_fail_return_primitive();
rust::Box c_try_return_box();
const rust::String &c_try_return_ref(const rust::String &);
rust::Str c_try_return_str(rust::Str);
-rust::Slice c_try_return_sliceu8(rust::Slice);
+rust::Slice c_try_return_sliceu8(rust::Slice);
rust::String c_try_return_rust_string();
std::unique_ptr c_try_return_unique_ptr_string();
rust::Vec c_try_return_rust_vec();