Support &mut [u8]
diff --git a/gen/src/builtin.rs b/gen/src/builtin.rs
index 9c5f4fd..53facbf 100644
--- a/gen/src/builtin.rs
+++ b/gen/src/builtin.rs
@@ -149,7 +149,7 @@
if builtin.ptr_len {
out.begin_block(Block::Namespace("repr"));
writeln!(out, "struct PtrLen final {{");
- writeln!(out, " const void *ptr;");
+ writeln!(out, " void *ptr;");
writeln!(out, " size_t len;");
writeln!(out, "}};");
out.end_block(Block::Namespace("repr"));
@@ -173,7 +173,10 @@
}
if builtin.rust_str_repr {
writeln!(out, " static repr::PtrLen repr(Str str) noexcept {{");
- writeln!(out, " return repr::PtrLen{{str.ptr, str.len}};");
+ writeln!(
+ out,
+ " return repr::PtrLen{{const_cast<char *>(str.ptr), str.len}};",
+ );
writeln!(out, " }}");
}
writeln!(out, "}};");
@@ -189,18 +192,21 @@
out,
" static Slice<T> slice(repr::PtrLen repr) noexcept {{",
);
- writeln!(
- out,
- " return {{static_cast<const T *>(repr.ptr), repr.len}};",
- );
+ writeln!(out, " return {{static_cast<T *>(repr.ptr), repr.len}};");
writeln!(out, " }}");
}
if builtin.rust_slice_repr {
+ include.type_traits = true;
writeln!(
out,
" static repr::PtrLen repr(Slice<T> slice) noexcept {{",
);
- writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};");
+ writeln!(out, " return repr::PtrLen{{");
+ writeln!(
+ out,
+ " const_cast<typename ::std::remove_const<T>::type *>(slice.ptr),",
+ );
+ writeln!(out, " slice.len}};");
writeln!(out, " }}");
}
writeln!(out, "}};");
diff --git a/gen/src/write.rs b/gen/src/write.rs
index d997786..8b512da 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -436,9 +436,11 @@
out.builtin.rust_str_repr = true;
write!(out, "::rust::impl<::rust::Str>::repr(");
}
- Some(Type::SliceRefU8(_)) if !indirect_return => {
+ Some(ty @ Type::SliceRefU8(_)) if !indirect_return => {
out.builtin.rust_slice_repr = true;
- write!(out, "::rust::impl<::rust::Slice<const uint8_t>>::repr(")
+ write!(out, "::rust::impl<");
+ write_type(out, ty);
+ write!(out, ">::repr(");
}
_ => {}
}
@@ -474,12 +476,13 @@
out.builtin.unsafe_bitcopy = true;
write_type(out, &arg.ty);
write!(out, "(::rust::unsafe_bitcopy, *{})", arg.ident);
- } else if let Type::SliceRefU8(_) = arg.ty {
- write!(
- out,
- "::rust::Slice<const uint8_t>(static_cast<const uint8_t *>({0}.ptr), {0}.len)",
- arg.ident,
- );
+ } else if let Type::SliceRefU8(slice) = &arg.ty {
+ write_type(out, &arg.ty);
+ write!(out, "(static_cast<");
+ if slice.mutability.is_none() {
+ write!(out, "const ");
+ }
+ write!(out, "uint8_t *>({0}.ptr), {0}.len)", arg.ident);
} else if out.types.needs_indirect_abi(&arg.ty) {
out.include.utility = true;
write!(out, "::std::move(*{})", arg.ident);
@@ -507,7 +510,7 @@
writeln!(out, " throw$.len = ::std::strlen(catch$);");
writeln!(
out,
- " throw$.ptr = ::cxxbridge1$exception(catch$, throw$.len);",
+ " throw$.ptr = const_cast<char *>(::cxxbridge1$exception(catch$, throw$.len));",
);
writeln!(out, " }});");
writeln!(out, " return throw$;");
@@ -694,7 +697,9 @@
}
Type::SliceRefU8(_) => {
out.builtin.rust_slice_new = true;
- write!(out, "::rust::impl<::rust::Slice<const uint8_t>>::slice(");
+ write!(out, "::rust::impl<");
+ write_type(out, ret);
+ write!(out, ">::slice(");
}
_ => {}
}
@@ -720,7 +725,9 @@
}
Type::SliceRefU8(_) => {
out.builtin.rust_slice_repr = true;
- write!(out, "::rust::impl<::rust::Slice<const uint8_t>>::repr(");
+ write!(out, "::rust::impl<");
+ write_type(out, &arg.ty);
+ write!(out, ">::repr(");
}
ty if out.types.needs_indirect_abi(ty) => write!(out, "&"),
_ => {}
@@ -890,8 +897,12 @@
Type::Str(_) => {
write!(out, "::rust::Str");
}
- Type::SliceRefU8(_) => {
- write!(out, "::rust::Slice<const uint8_t>");
+ Type::SliceRefU8(ty) => {
+ write!(out, "::rust::Slice<");
+ if ty.mutability.is_none() {
+ write!(out, "const ");
+ }
+ write!(out, "uint8_t>");
}
Type::Fn(f) => {
write!(out, "::rust::{}<", if f.throws { "TryFn" } else { "Fn" });
diff --git a/include/cxx.h b/include/cxx.h
index 8122786..1b30497 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -87,16 +87,28 @@
#endif // CXXBRIDGE1_RUST_STR
#ifndef CXXBRIDGE1_RUST_SLICE
-template <typename T>
-class Slice final {
- static_assert(std::is_const<T>::value,
- "&[T] needs to be written as rust::Slice<const T> in C++");
+namespace detail {
+template <bool cond>
+struct copy_assignable_if {};
+template <>
+struct copy_assignable_if<false> {
+ copy_assignable_if() noexcept = default;
+ copy_assignable_if(const copy_assignable_if &) noexcept = default;
+ copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
+ copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
+};
+} // namespace detail
+
+template <typename T>
+class Slice final
+ : private detail::copy_assignable_if<std::is_const<T>::value> {
public:
Slice() noexcept;
Slice(T *, size_t count) noexcept;
Slice &operator=(const Slice<T> &) noexcept = default;
+ Slice &operator=(Slice<T> &&) noexcept = default;
T *data() const noexcept;
size_t size() const noexcept;
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index f29c4cc..1fee928 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -330,7 +330,10 @@
_ => quote!(#var),
},
Type::Str(_) => quote!(::cxx::private::RustStr::from(#var)),
- Type::SliceRefU8(_) => quote!(::cxx::private::RustSliceU8::from(#var)),
+ Type::SliceRefU8(ty) => match ty.mutable {
+ false => quote!(::cxx::private::RustSliceU8::from_ref(#var)),
+ true => quote!(::cxx::private::RustSliceU8::from_mut(#var)),
+ },
ty if types.needs_indirect_abi(ty) => quote!(#var.as_mut_ptr()),
_ => quote!(#var),
}
@@ -423,7 +426,10 @@
_ => call,
},
Type::Str(_) => quote!(#call.as_str()),
- Type::SliceRefU8(_) => quote!(#call.as_slice()),
+ Type::SliceRefU8(ty) => match ty.mutable {
+ false => quote!(#call.as_slice()),
+ true => quote!(#call.as_mut_slice()),
+ },
_ => call,
},
};
@@ -610,7 +616,10 @@
_ => quote!(#ident),
},
Type::Str(_) => quote!(#ident.as_str()),
- Type::SliceRefU8(_) => quote!(#ident.as_slice()),
+ Type::SliceRefU8(ty) => match ty.mutable {
+ false => quote!(#ident.as_slice()),
+ true => quote!(#ident.as_mut_slice()),
+ },
ty if types.needs_indirect_abi(ty) => quote!(::std::ptr::read(#ident)),
_ => quote!(#ident),
}
@@ -654,7 +663,10 @@
_ => None,
},
Type::Str(_) => Some(quote!(::cxx::private::RustStr::from)),
- Type::SliceRefU8(_) => Some(quote!(::cxx::private::RustSliceU8::from)),
+ Type::SliceRefU8(ty) => match ty.mutable {
+ false => Some(quote!(::cxx::private::RustSliceU8::from_ref)),
+ true => Some(quote!(::cxx::private::RustSliceU8::from_mut)),
+ },
_ => None,
});
diff --git a/src/cxx.cc b/src/cxx.cc
index 923b641..28506ef 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -180,6 +180,17 @@
static_assert(std::is_trivially_destructible<Slice<const uint8_t>>::value,
"trivial ~Slice()");
+static_assert(std::is_trivially_copy_constructible<Slice<uint8_t>>::value,
+ "trivial Slice(const Slice &)");
+static_assert(std::is_trivially_move_constructible<Slice<uint8_t>>::value,
+ "trivial Slice(Slice &&)");
+static_assert(!std::is_copy_assignable<Slice<uint8_t>>::value,
+ "delete Slice::operator=(const Slice &) for mut slices");
+static_assert(std::is_trivially_move_assignable<Slice<uint8_t>>::value,
+ "trivial Slice::operator=(Slice &&)");
+static_assert(std::is_trivially_destructible<Slice<uint8_t>>::value,
+ "trivial ~Slice()");
+
extern "C" {
const char *cxxbridge1$error(const char *ptr, size_t len) {
char *copy = new char[len];
diff --git a/src/rust_sliceu8.rs b/src/rust_sliceu8.rs
index 32f8798..e14f988 100644
--- a/src/rust_sliceu8.rs
+++ b/src/rust_sliceu8.rs
@@ -11,16 +11,27 @@
}
impl RustSliceU8 {
- pub fn from(s: &[u8]) -> Self {
+ pub fn from_ref(s: &[u8]) -> Self {
RustSliceU8 {
ptr: NonNull::from(s).cast::<u8>(),
len: s.len(),
}
}
+ pub fn from_mut(s: &mut [u8]) -> Self {
+ RustSliceU8 {
+ len: s.len(),
+ ptr: NonNull::from(s).cast::<u8>(),
+ }
+ }
+
pub unsafe fn as_slice<'a>(self) -> &'a [u8] {
slice::from_raw_parts(self.ptr.as_ptr(), self.len)
}
+
+ pub unsafe fn as_mut_slice<'a>(self) -> &'a mut [u8] {
+ slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len)
+ }
}
const_assert_eq!(
diff --git a/syntax/check.rs b/syntax/check.rs
index 2e94f4d..1ec6178 100644
--- a/syntax/check.rs
+++ b/syntax/check.rs
@@ -178,7 +178,10 @@
}
fn check_type_slice(cx: &mut Check, ty: &Slice) {
- cx.error(ty, "only &[u8] is supported so far, not other slice types");
+ cx.error(
+ ty,
+ "only &[u8] and &mut [u8] are supported so far, not other slice types",
+ );
}
fn check_type_fn(cx: &mut Check, ty: &Signature) {
@@ -489,7 +492,10 @@
Type::Str(_) => "&str".to_owned(),
Type::CxxVector(_) => "C++ vector".to_owned(),
Type::Slice(_) => "slice".to_owned(),
- Type::SliceRefU8(_) => "&[u8]".to_owned(),
+ Type::SliceRefU8(ty) => match ty.mutable {
+ false => "&[u8]".to_owned(),
+ true => "&mut [u8]".to_owned(),
+ },
Type::Fn(_) => "function pointer".to_owned(),
Type::Void(_) => "()".to_owned(),
}
diff --git a/syntax/parse.rs b/syntax/parse.rs
index 38b3dcc..c6d6480 100644
--- a/syntax/parse.rs
+++ b/syntax/parse.rs
@@ -658,7 +658,7 @@
}
}
Type::Slice(slice) => match &slice.inner {
- Type::Ident(ident) if ident.rust == U8 && ty.mutability.is_none() => Type::SliceRefU8,
+ Type::Ident(ident) if ident.rust == U8 => Type::SliceRefU8,
_ => Type::Ref,
},
_ => Type::Ref,
diff --git a/tests/ffi/lib.rs b/tests/ffi/lib.rs
index 762af4d..af72fb4 100644
--- a/tests/ffi/lib.rs
+++ b/tests/ffi/lib.rs
@@ -77,6 +77,7 @@
fn c_return_mut(shared: &mut Shared) -> &mut usize;
fn c_return_str(shared: &Shared) -> &str;
fn c_return_sliceu8(shared: &Shared) -> &[u8];
+ fn c_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
fn c_return_rust_string() -> String;
fn c_return_unique_ptr_string() -> UniquePtr<CxxString>;
fn c_return_unique_ptr_vector_u8() -> UniquePtr<CxxVector<u8>>;
@@ -140,6 +141,7 @@
fn c_try_return_ref(s: &String) -> Result<&String>;
fn c_try_return_str(s: &str) -> Result<&str>;
fn c_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>;
+ fn c_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>;
fn c_try_return_rust_string() -> Result<String>;
fn c_try_return_unique_ptr_string() -> Result<UniquePtr<CxxString>>;
fn c_try_return_rust_vec() -> Result<Vec<u8>>;
@@ -187,6 +189,7 @@
fn r_return_mut(shared: &mut Shared) -> &mut usize;
fn r_return_str(shared: &Shared) -> &str;
fn r_return_sliceu8(shared: &Shared) -> &[u8];
+ fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
fn r_return_rust_string() -> String;
fn r_return_unique_ptr_string() -> UniquePtr<CxxString>;
fn r_return_rust_vec() -> Vec<u8>;
@@ -220,6 +223,7 @@
fn r_try_return_box() -> Result<Box<R>>;
fn r_fail_return_primitive() -> Result<usize>;
fn r_try_return_sliceu8(s: &[u8]) -> Result<&[u8]>;
+ fn r_try_return_mutsliceu8(s: &mut [u8]) -> Result<&mut [u8]>;
fn get(self: &R) -> usize;
fn set(self: &mut R, n: usize) -> usize;
@@ -371,6 +375,10 @@
b"2020"
}
+fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8] {
+ slice
+}
+
fn r_return_rust_string() -> String {
"2020".to_owned()
}
@@ -509,6 +517,10 @@
Ok(slice)
}
+fn r_try_return_mutsliceu8(slice: &mut [u8]) -> Result<&mut [u8], Error> {
+ Ok(slice)
+}
+
fn r_aliased_function(x: i32) -> String {
x.to_string()
}
diff --git a/tests/ffi/tests.cc b/tests/ffi/tests.cc
index f1c293d..238b145 100644
--- a/tests/ffi/tests.cc
+++ b/tests/ffi/tests.cc
@@ -77,6 +77,10 @@
reinterpret_cast<const uint8_t *>(SLICE_DATA), sizeof(SLICE_DATA));
}
+rust::Slice<uint8_t> c_return_mutsliceu8(rust::Slice<uint8_t> slice) {
+ return slice;
+}
+
rust::String c_return_rust_string() { return "2020"; }
std::unique_ptr<std::string> c_return_unique_ptr_string() {
@@ -435,6 +439,10 @@
return s;
}
+rust::Slice<uint8_t> c_try_return_mutsliceu8(rust::Slice<uint8_t> s) {
+ return s;
+}
+
rust::String c_try_return_rust_string() { return c_return_rust_string(); }
std::unique_ptr<std::string> c_try_return_unique_ptr_string() {
diff --git a/tests/ffi/tests.h b/tests/ffi/tests.h
index 5acf469..6c288d7 100644
--- a/tests/ffi/tests.h
+++ b/tests/ffi/tests.h
@@ -84,6 +84,7 @@
size_t &c_return_mut(Shared &shared);
rust::Str c_return_str(const Shared &shared);
rust::Slice<const uint8_t> c_return_sliceu8(const Shared &shared);
+rust::Slice<uint8_t> c_return_mutsliceu8(rust::Slice<uint8_t> slice);
rust::String c_return_rust_string();
std::unique_ptr<std::string> c_return_unique_ptr_string();
std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8();
@@ -148,6 +149,7 @@
const rust::String &c_try_return_ref(const rust::String &);
rust::Str c_try_return_str(rust::Str);
rust::Slice<const uint8_t> c_try_return_sliceu8(rust::Slice<const uint8_t>);
+rust::Slice<uint8_t> c_try_return_mutsliceu8(rust::Slice<uint8_t>);
rust::String c_try_return_rust_string();
std::unique_ptr<std::string> c_try_return_unique_ptr_string();
rust::Vec<uint8_t> c_try_return_rust_vec();