diff --git a/gen/src/builtin.rs b/gen/src/builtin.rs index 2e75666f..9b8d45ee 100644 --- a/gen/src/builtin.rs +++ b/gen/src/builtin.rs @@ -279,12 +279,7 @@ pub(super) fn write(out: &mut OutFile) { out, " static repr::PtrLen repr(Slice slice) noexcept {{", ); - writeln!(out, " return repr::PtrLen{{"); - writeln!( - out, - " const_cast::type *>(slice.ptr),", - ); - writeln!(out, " slice.len}};"); + writeln!(out, " return repr::PtrLen{{slice.ptr, slice.len}};"); writeln!(out, " }}"); } writeln!(out, "}};"); diff --git a/include/cxx.h b/include/cxx.h index d7959184..a4901e16 100644 --- a/include/cxx.h +++ b/include/cxx.h @@ -175,7 +175,7 @@ private: friend impl; // Not necessarily ABI compatible with &[T]. Codegen will translate to // cxx::rust_slice::RustSlice which matches this layout. - T *ptr; + void *ptr; std::size_t len; }; @@ -522,14 +522,15 @@ inline std::size_t Str::length() const noexcept { return this->len; } #define CXXBRIDGE1_RUST_SLICE template Slice::Slice() noexcept - : ptr(reinterpret_cast(align_of())), len(0) {} + : ptr(reinterpret_cast(align_of())), len(0) {} template -Slice::Slice(T *s, std::size_t count) noexcept : ptr(s), len(count) {} +Slice::Slice(T *s, std::size_t count) noexcept + : ptr(const_cast::type *>(s)), len(count) {} template T *Slice::data() const noexcept { - return this->ptr; + return reinterpret_cast(this->ptr); } template @@ -550,7 +551,8 @@ bool Slice::empty() const noexcept { template T &Slice::operator[](std::size_t n) const noexcept { assert(n < this->size()); - return this->ptr[n]; + auto pos = static_cast(this->ptr) + size_of() * n; + return *reinterpret_cast(pos); } template @@ -564,13 +566,13 @@ T &Slice::at(std::size_t n) const { template T &Slice::front() const noexcept { assert(!this->empty()); - return this->ptr[0]; + return (*this)[0]; } template T &Slice::back() const noexcept { assert(!this->empty()); - return this->ptr[this->len - 1]; + return (*this)[this->len - 1]; } template @@ -689,7 +691,7 @@ bool Slice::iterator::operator>=(const iterator &other) const noexcept { template typename Slice::iterator Slice::begin() const noexcept { iterator it; - it.pos = const_cast::type *>(this->ptr); + it.pos = this->ptr; it.stride = size_of(); return it; }