-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add API to get slice from CxxVector #321
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ use core::fmt::{self, Display}; | |
use core::marker::PhantomData; | ||
use core::mem; | ||
use core::ptr; | ||
use std::slice; | ||
|
||
|
||
/// Binding to C++ `std::vector<T, std::allocator<T>>`. | ||
/// | ||
|
@@ -63,6 +65,15 @@ where | |
pub unsafe fn get_unchecked(&self, pos: usize) -> &T { | ||
T::__get_unchecked(self, pos) | ||
} | ||
|
||
/// Returns a slice to the underlying vector data | ||
/// | ||
/// An alternative approach would be to base your FFI API directly on a | ||
/// rust::Slice<T> instead of a std::vector<T>. | ||
pub fn get_slice(&self) -> &[T] { | ||
let slice = T::__vector_slice(self); | ||
slice | ||
} | ||
} | ||
|
||
pub struct Iter<'a, T> { | ||
|
@@ -122,6 +133,7 @@ where | |
pub unsafe trait VectorElement: Sized { | ||
const __NAME: &'static dyn Display; | ||
fn __vector_size(v: &CxxVector<Self>) -> usize; | ||
fn __vector_slice(v: &CxxVector<Self>) -> &[Self]; | ||
unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self; | ||
fn __unique_ptr_null() -> *mut c_void; | ||
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void; | ||
|
@@ -145,6 +157,34 @@ macro_rules! impl_vector_element { | |
} | ||
unsafe { __vector_size(v) } | ||
} | ||
fn __vector_slice(v: &CxxVector<$ty>) -> &[$ty] { | ||
extern "C" { | ||
attr! { | ||
#[link_name = concat!("cxxbridge04$std$vector$", $segment, "$data")] | ||
fn __vector_data(_: &CxxVector<$ty>) -> &$ty; | ||
} | ||
attr! { | ||
#[link_name = concat!("cxxbridge04$std$vector$", $segment, "$size")] | ||
fn __vector_size(_: &CxxVector<$ty>) -> usize; | ||
} | ||
} | ||
let data : *const $ty = unsafe { __vector_data(v) }; | ||
if (data.is_null()) | ||
{ | ||
// Avoid undefined behaviour in slice::from_raw_parts() as | ||
// std::vector::data() returns nullptr if no capacity(). | ||
// Alternative: Use from_raw_parts(NonNull::dangling(), 0) | ||
let ret = <&[$ty]>::default(); // empty slice | ||
ret | ||
} | ||
else | ||
{ | ||
// std::vector with capacity() has valid data ptr, even if | ||
// size() == 0. | ||
let ret = unsafe { slice::from_raw_parts(data, __vector_size(v)) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still undefined behavior, I think. As far as I can tell nothing in https://en.cppreference.com/w/cpp/container/vector/data would guarantee that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. Went after the implementation behaviour that always has been instead of reading the standard. Fix would be to check the size() == 0 instead the ptr, which I see you have done in your implementation. Thanks for fixing my code up. ;) |
||
ret | ||
} | ||
} | ||
unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty { | ||
extern "C" { | ||
attr! { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -401,6 +401,29 @@ extern "C" const char *cxx_run_test() noexcept { | |
std::unique_ptr<std::string>(new std::string("2020"))); | ||
r_take_enum(Enum::AVal); | ||
|
||
// Caller owns data handed into rust APIs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test for that did not really fit into the "take" or "return" tests. I initially had a c_take...() test that would create a std::vector and then call into the r_access_...() test function, but I found that overly complex as the c_take...() test did not add anything. Hence, directly testing the API in r_access..() here. |
||
{ | ||
std::vector<std::uint8_t> v{86, 75, 30, 9}; | ||
r_access_vector_u8_as_slice(v); | ||
} | ||
{ | ||
// Empty std::vector has data() == nullptr. | ||
// This shall produce an empty slice. | ||
std::vector<std::uint8_t> v; | ||
ASSERT(v.data() == nullptr); | ||
ASSERT(v.size() == 0); | ||
r_access_vector_u8_as_slice_empty(v); | ||
} | ||
{ | ||
// Empty std::vector with capacity has data() != nullptr and size() == 0. | ||
// This shall produce an empty slice. | ||
std::vector<std::uint8_t> v; | ||
v.reserve(10); | ||
ASSERT(v.data() != nullptr); | ||
ASSERT(v.size() == 0); | ||
r_access_vector_u8_as_slice_empty(v); | ||
} | ||
|
||
ASSERT(r_try_return_primitive() == 2020); | ||
try { | ||
r_fail_return_primitive(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering whether I can or should generate src/cxx.h|cc from this. Need to look into that...