Move unique_ptr<vector<>> implementation into cxx crate
diff --git a/src/cxx.cc b/src/cxx.cc
index b9e4826..aacea9c 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -212,20 +212,10 @@
std::vector<CXX_TYPE> &s, const CXX_TYPE &item) noexcept { \
s.push_back(item); \
} \
- static_assert( \
- sizeof(std::unique_ptr<std::vector<CXX_TYPE>>) == sizeof(void *), ""); \
- static_assert( \
- alignof(std::unique_ptr<std::vector<CXX_TYPE>>) == alignof(void *), ""); \
void cxxbridge02$unique_ptr$std$vector$##RUST_TYPE##$null( \
std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \
} \
- void cxxbridge02$unique_ptr$std$vector$##RUST_TYPE##$new( \
- std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \
- std::vector<CXX_TYPE> *value) noexcept { \
- new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>( \
- new std::vector<CXX_TYPE>(std::move(*value))); \
- } \
void cxxbridge02$unique_ptr$std$vector$##RUST_TYPE##$raw( \
std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \
std::vector<CXX_TYPE> *raw) noexcept { \
diff --git a/src/cxx_vector.rs b/src/cxx_vector.rs
index b47ec41..d9e8892 100644
--- a/src/cxx_vector.rs
+++ b/src/cxx_vector.rs
@@ -1,4 +1,8 @@
+use std::ffi::c_void;
+use std::fmt::{self, Display};
+use std::marker::PhantomData;
use std::mem;
+use std::ptr;
/// Binding to C++ `std::vector<T, std::allocator<T>>`.
///
@@ -94,18 +98,46 @@
}
}
+pub struct TypeName<T> {
+ element: PhantomData<T>,
+}
+
+impl<T> TypeName<T> {
+ pub const fn new() -> Self {
+ TypeName {
+ element: PhantomData,
+ }
+ }
+}
+
+impl<T> Display for TypeName<T>
+where
+ T: VectorElement,
+{
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ write!(formatter, "CxxVector<{}>", T::__NAME)
+ }
+}
+
// Methods are private; not intended to be implemented outside of cxxbridge
// codebase.
#[doc(hidden)]
pub unsafe trait VectorElement: Sized {
+ const __NAME: &'static dyn Display;
fn __vector_size(v: &CxxVector<Self>) -> usize;
unsafe fn __get_unchecked(v: &CxxVector<Self>, pos: usize) -> &Self;
fn __push_back(v: &CxxVector<Self>, item: &Self);
+ fn __unique_ptr_null() -> *mut c_void;
+ unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void;
+ unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self>;
+ unsafe fn __unique_ptr_release(repr: *mut c_void) -> *mut CxxVector<Self>;
+ unsafe fn __unique_ptr_drop(repr: *mut c_void);
}
macro_rules! impl_vector_element_for_primitive {
($ty:ident) => {
unsafe impl VectorElement for $ty {
+ const __NAME: &'static dyn Display = &stringify!($ty);
fn __vector_size(v: &CxxVector<$ty>) -> usize {
extern "C" {
attr! {
@@ -133,6 +165,55 @@
}
unsafe { __push_back(v, item) }
}
+ fn __unique_ptr_null() -> *mut c_void {
+ extern "C" {
+ attr! {
+ #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$null")]
+ fn __unique_ptr_null(this: *mut *mut c_void);
+ }
+ }
+ let mut repr = ptr::null_mut::<c_void>();
+ unsafe { __unique_ptr_null(&mut repr) }
+ repr
+ }
+ unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
+ extern "C" {
+ attr! {
+ #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$raw")]
+ fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
+ }
+ }
+ let mut repr = ptr::null_mut::<c_void>();
+ __unique_ptr_raw(&mut repr, raw);
+ repr
+ }
+ unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
+ extern "C" {
+ attr! {
+ #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$get")]
+ fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
+ }
+ }
+ __unique_ptr_get(&repr)
+ }
+ unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
+ extern "C" {
+ attr! {
+ #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$release")]
+ fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
+ }
+ }
+ __unique_ptr_release(&mut repr)
+ }
+ unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
+ extern "C" {
+ attr! {
+ #[link_name = concat!("cxxbridge02$unique_ptr$std$vector$", stringify!($ty), "$drop")]
+ fn __unique_ptr_drop(this: *mut *mut c_void);
+ }
+ }
+ __unique_ptr_drop(&mut repr);
+ }
}
};
}
diff --git a/src/unique_ptr.rs b/src/unique_ptr.rs
index b50e870..98b4b7d 100644
--- a/src/unique_ptr.rs
+++ b/src/unique_ptr.rs
@@ -1,4 +1,5 @@
use crate::cxx_string::CxxString;
+use crate::cxx_vector::{self, CxxVector, VectorElement};
use std::ffi::c_void;
use std::fmt::{self, Debug, Display};
use std::marker::PhantomData;
@@ -149,7 +150,7 @@
// codebase.
pub unsafe trait UniquePtrTarget {
#[doc(hidden)]
- const __NAME: &'static str;
+ const __NAME: &'static dyn Display;
#[doc(hidden)]
fn __null() -> *mut c_void;
#[doc(hidden)]
@@ -186,7 +187,7 @@
}
unsafe impl UniquePtrTarget for CxxString {
- const __NAME: &'static str = "CxxString";
+ const __NAME: &'static dyn Display = &"CxxString";
fn __null() -> *mut c_void {
let mut repr = ptr::null_mut::<c_void>();
unsafe { unique_ptr_std_string_null(&mut repr) }
@@ -207,3 +208,25 @@
unique_ptr_std_string_drop(&mut repr);
}
}
+
+unsafe impl<T> UniquePtrTarget for CxxVector<T>
+where
+ T: VectorElement + 'static,
+{
+ const __NAME: &'static dyn Display = &cxx_vector::TypeName::<T>::new();
+ fn __null() -> *mut c_void {
+ T::__unique_ptr_null()
+ }
+ unsafe fn __raw(raw: *mut Self) -> *mut c_void {
+ T::__unique_ptr_raw(raw)
+ }
+ unsafe fn __get(repr: *mut c_void) -> *const Self {
+ T::__unique_ptr_get(repr)
+ }
+ unsafe fn __release(repr: *mut c_void) -> *mut Self {
+ T::__unique_ptr_release(repr)
+ }
+ unsafe fn __drop(repr: *mut c_void) {
+ T::__unique_ptr_drop(repr);
+ }
+}