Touch up &[u8] PR
diff --git a/README.md b/README.md
index ad14b57..f51a1ac 100644
--- a/README.md
+++ b/README.md
@@ -299,7 +299,7 @@
<tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
<tr><td>String</td><td>rust::String</td><td></td></tr>
<tr><td>&str</td><td>rust::Str</td><td></td></tr>
-<tr><td>&[u8]</td><td>rust::Slice<uint8_t></td><td>(no other slice types currently supported)</td></tr>
+<tr><td>&[u8]</td><td>rust::Slice<uint8_t></td><td><sup><i>arbitrary &[T] not implemented yet</i></sup></td></tr>
<tr><td><a href="https://docs.rs/cxx/0.2/cxx/struct.CxxString.html">CxxString</a></td><td>std::string</td><td><sup><i>cannot be passed by value</i></sup></td></tr>
<tr><td>Box<T></td><td>rust::Box<T></td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
<tr><td><a href="https://docs.rs/cxx/0.2/cxx/struct.UniquePtr.html">UniquePtr<T></a></td><td>std::unique_ptr<T></td><td><sup><i>cannot hold opaque Rust type</i></sup></td></tr>
diff --git a/include/cxx.h b/include/cxx.h
index e63790b..cf79cfb 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -16,43 +16,6 @@
struct unsafe_bitcopy_t;
-#ifndef CXXBRIDGE02_RUST_SLICE
-#define CXXBRIDGE02_RUST_SLICE
-template <typename T>
-class Slice final {
-public:
- Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
- Slice(const Slice<T> &) noexcept = default;
-
- Slice(const T *s, size_t size) : repr(Repr{s, size}) {}
-
- Slice &operator=(Slice<T> other) noexcept {
- this->repr = other.repr;
- return *this;
- }
-
- const T *data() const noexcept { return this->repr.ptr; }
- size_t size() const noexcept { return this->repr.len; }
- size_t length() const noexcept { return this->repr.len; }
-
- // Repr is PRIVATE; must not be used other than by our generated code.
- //
- // At present this class is only used for &[u8] slices.
- // Not necessarily ABI compatible with &[u8]. Codegen will translate to
- // cxx::rust_slice_u8::RustSlice which matches this layout.
- struct Repr {
- const T *ptr;
- size_t len;
- };
- Slice(Repr repr_) noexcept : repr(repr_) {}
- explicit operator Repr() noexcept { return this->repr; }
-
-private:
- Repr repr;
-};
-
-#endif // CXXBRIDGE02_RUST_SLICE
-
#ifndef CXXBRIDGE02_RUST_STRING
#define CXXBRIDGE02_RUST_STRING
class String final {
@@ -120,6 +83,42 @@
};
#endif // CXXBRIDGE02_RUST_STR
+#ifndef CXXBRIDGE02_RUST_SLICE
+#define CXXBRIDGE02_RUST_SLICE
+template <typename T>
+class Slice final {
+public:
+ Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
+ Slice(const Slice<T> &) noexcept = default;
+
+ Slice(const T *s, size_t size) : repr(Repr{s, size}) {}
+
+ Slice &operator=(Slice<T> other) noexcept {
+ this->repr = other.repr;
+ return *this;
+ }
+
+ const T *data() const noexcept { return this->repr.ptr; }
+ size_t size() const noexcept { return this->repr.len; }
+ size_t length() const noexcept { return this->repr.len; }
+
+ // Repr is PRIVATE; must not be used other than by our generated code.
+ //
+ // At present this class is only used for &[u8] slices.
+ // Not necessarily ABI compatible with &[u8]. Codegen will translate to
+ // cxx::rust_slice_u8::RustSlice which matches this layout.
+ struct Repr {
+ const T *ptr;
+ size_t len;
+ };
+ Slice(Repr repr_) noexcept : repr(repr_) {}
+ explicit operator Repr() noexcept { return this->repr; }
+
+private:
+ Repr repr;
+};
+#endif // CXXBRIDGE02_RUST_SLICE
+
#ifndef CXXBRIDGE02_RUST_BOX
#define CXXBRIDGE02_RUST_BOX
template <typename T>
diff --git a/src/lib.rs b/src/lib.rs
index 1ceef52..6348cf4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -307,6 +307,7 @@
//! <tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
//! <tr><td>String</td><td>rust::String</td><td></td></tr>
//! <tr><td>&str</td><td>rust::Str</td><td></td></tr>
+//! <tr><td>&[u8]</td><td>rust::Slice<uint8_t></td><td><sup><i>arbitrary &[T] not implemented yet</i></sup></td></tr>
//! <tr><td><a href="https://docs.rs/cxx/0.2/cxx/struct.CxxString.html">CxxString</a></td><td>std::string</td><td><sup><i>cannot be passed by value</i></sup></td></tr>
//! <tr><td>Box<T></td><td>rust::Box<T></td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
//! <tr><td><a href="https://docs.rs/cxx/0.2/cxx/struct.UniquePtr.html">UniquePtr<T></a></td><td>std::unique_ptr<T></td><td><sup><i>cannot hold opaque Rust type</i></sup></td></tr>
@@ -324,7 +325,6 @@
//!
//! <table>
//! <tr><th>name in Rust</th><th>name in C++</th></tr>
-//! <tr><td>&[T]</td><td><sup><i>tbd</i></sup></td></tr>
//! <tr><td>Vec<T></td><td><sup><i>tbd</i></sup></td></tr>
//! <tr><td>BTreeMap<K, V></td><td><sup><i>tbd</i></sup></td></tr>
//! <tr><td>HashMap<K, V></td><td><sup><i>tbd</i></sup></td></tr>
diff --git a/tests/ffi/lib.rs b/tests/ffi/lib.rs
index adcd4fc..8afa841 100644
--- a/tests/ffi/lib.rs
+++ b/tests/ffi/lib.rs
@@ -166,7 +166,7 @@
fn r_take_sliceu8(s: &[u8]) {
assert_eq!(s.len(), 5);
- assert_eq!(std::str::from_utf8(s).unwrap(), "2020\u{0}");
+ assert_eq!(std::str::from_utf8(s).unwrap(), "2020\0");
}
fn r_take_unique_ptr_string(s: UniquePtr<CxxString>) {