Add stride accessor for Vec
Will be required for implementing an iterator.
diff --git a/gen/write.rs b/gen/write.rs
index 9192a7e..d963bb9 100644
--- a/gen/write.rs
+++ b/gen/write.rs
@@ -1026,6 +1026,11 @@
"const {} *cxxbridge02$rust_vec${}$data(const ::rust::Vec<{0}> *ptr) noexcept;",
inner, instance,
);
+ writeln!(
+ out,
+ "size_t cxxbridge02$rust_vec${}$stride() noexcept;",
+ instance,
+ );
writeln!(out, "#endif // CXXBRIDGE02_RUST_VEC_{}", instance);
}
@@ -1075,6 +1080,11 @@
instance,
);
writeln!(out, "}}");
+
+ writeln!(out, "template <>");
+ writeln!(out, "size_t Vec<{}>::stride() noexcept {{", inner);
+ writeln!(out, " return cxxbridge02$rust_vec${}$stride();", instance);
+ writeln!(out, "}}");
}
fn write_unique_ptr(out: &mut OutFile, ty: &Type, types: &Types) {
diff --git a/include/cxx.h b/include/cxx.h
index c3037bd..f40614b 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -212,6 +212,7 @@
const T *data() const noexcept;
private:
+ static size_t stride() noexcept;
void drop() noexcept;
// Size and alignment statically verified by rust_vec.rs.
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index 15351f0..7503f3a 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -541,11 +541,13 @@
let link_drop = format!("{}drop", link_prefix);
let link_len = format!("{}len", link_prefix);
let link_data = format!("{}data", link_prefix);
+ let link_stride = format!("{}stride", link_prefix);
let local_prefix = format_ident!("{}__vec_", ident);
let local_drop = format_ident!("{}drop", local_prefix);
let local_len = format_ident!("{}len", local_prefix);
let local_data = format_ident!("{}data", local_prefix);
+ let local_stride = format_ident!("{}stride", local_prefix);
let span = ty.span();
quote_spanned! {span=>
@@ -564,6 +566,11 @@
unsafe extern "C" fn #local_data(this: *const ::cxx::private::RustVec<#inner>) -> *const #inner {
(*this).as_ptr()
}
+ #[doc(hidden)]
+ #[export_name = #link_stride]
+ unsafe extern "C" fn #local_stride() -> usize {
+ ::std::mem::size_of::<#inner>()
+ }
}
}