Restrict CxxVector::as_slice to types where size is definite
&[T] where T is an opaque C++ type isn't meaningful because Rust
wouldn't know the size of the type and therefore how big of a stride to
take between slice elements. Opaque C++ types are implemented as Rust
ZSTs so the observed effect would be seeing N copies of just the first
vector element, which is memory safe but misleading.
diff --git a/src/cxx_vector.rs b/src/cxx_vector.rs
index d1611db..b8de942 100644
--- a/src/cxx_vector.rs
+++ b/src/cxx_vector.rs
@@ -1,4 +1,6 @@
use crate::cxx_string::CxxString;
+use crate::extern_type::ExternType;
+use crate::kind::Trivial;
use core::ffi::c_void;
use core::fmt::{self, Display};
use core::marker::{PhantomData, PhantomPinned};
@@ -67,7 +69,10 @@
}
/// Returns a slice to the underlying contiguous array of elements.
- pub fn as_slice(&self) -> &[T] {
+ pub fn as_slice(&self) -> &[T]
+ where
+ T: ExternType<Kind = Trivial>,
+ {
let len = self.len();
if len == 0 {
// The slice::from_raw_parts in the other branch requires a nonnull