Skip to content

Commit ee07f01

Browse files
authored
Merge pull request #267 from dtolnay/vec
Implement CxxVector<CxxString>
2 parents 13e4d39 + 47e239d commit ee07f01

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

src/cxx.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ void cxxbridge03$unique_ptr$std$string$drop(
276276
#define FOR_EACH_STD_VECTOR(MACRO) \
277277
FOR_EACH_NUMERIC(MACRO) \
278278
MACRO(usize, size_t) \
279-
MACRO(isize, rust::isize)
279+
MACRO(isize, rust::isize) \
280+
MACRO(string, std::string)
280281

281282
#define FOR_EACH_RUST_VEC(MACRO) \
282283
FOR_EACH_NUMERIC(MACRO) \

src/cxx_vector.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cxx_string::CxxString;
12
use std::ffi::c_void;
23
use std::fmt::{self, Display};
34
use std::marker::PhantomData;
@@ -129,16 +130,16 @@ pub unsafe trait VectorElement: Sized {
129130
unsafe fn __unique_ptr_drop(repr: *mut c_void);
130131
}
131132

132-
macro_rules! impl_vector_element_for_primitive {
133-
($ty:ident) => {
133+
macro_rules! impl_vector_element {
134+
($segment:expr, $name:expr, $ty:ty) => {
134135
const_assert_eq!(1, mem::align_of::<CxxVector<$ty>>());
135136

136137
unsafe impl VectorElement for $ty {
137-
const __NAME: &'static dyn Display = &stringify!($ty);
138+
const __NAME: &'static dyn Display = &$name;
138139
fn __vector_size(v: &CxxVector<$ty>) -> usize {
139140
extern "C" {
140141
attr! {
141-
#[link_name = concat!("cxxbridge03$std$vector$", stringify!($ty), "$size")]
142+
#[link_name = concat!("cxxbridge03$std$vector$", $segment, "$size")]
142143
fn __vector_size(_: &CxxVector<$ty>) -> usize;
143144
}
144145
}
@@ -147,7 +148,7 @@ macro_rules! impl_vector_element_for_primitive {
147148
unsafe fn __get_unchecked(v: &CxxVector<$ty>, pos: usize) -> &$ty {
148149
extern "C" {
149150
attr! {
150-
#[link_name = concat!("cxxbridge03$std$vector$", stringify!($ty), "$get_unchecked")]
151+
#[link_name = concat!("cxxbridge03$std$vector$", $segment, "$get_unchecked")]
151152
fn __get_unchecked(_: &CxxVector<$ty>, _: usize) -> *const $ty;
152153
}
153154
}
@@ -156,7 +157,7 @@ macro_rules! impl_vector_element_for_primitive {
156157
fn __unique_ptr_null() -> *mut c_void {
157158
extern "C" {
158159
attr! {
159-
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", stringify!($ty), "$null")]
160+
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", $segment, "$null")]
160161
fn __unique_ptr_null(this: *mut *mut c_void);
161162
}
162163
}
@@ -167,7 +168,7 @@ macro_rules! impl_vector_element_for_primitive {
167168
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> *mut c_void {
168169
extern "C" {
169170
attr! {
170-
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", stringify!($ty), "$raw")]
171+
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", $segment, "$raw")]
171172
fn __unique_ptr_raw(this: *mut *mut c_void, raw: *mut CxxVector<$ty>);
172173
}
173174
}
@@ -178,7 +179,7 @@ macro_rules! impl_vector_element_for_primitive {
178179
unsafe fn __unique_ptr_get(repr: *mut c_void) -> *const CxxVector<Self> {
179180
extern "C" {
180181
attr! {
181-
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", stringify!($ty), "$get")]
182+
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", $segment, "$get")]
182183
fn __unique_ptr_get(this: *const *mut c_void) -> *const CxxVector<$ty>;
183184
}
184185
}
@@ -187,7 +188,7 @@ macro_rules! impl_vector_element_for_primitive {
187188
unsafe fn __unique_ptr_release(mut repr: *mut c_void) -> *mut CxxVector<Self> {
188189
extern "C" {
189190
attr! {
190-
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", stringify!($ty), "$release")]
191+
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", $segment, "$release")]
191192
fn __unique_ptr_release(this: *mut *mut c_void) -> *mut CxxVector<$ty>;
192193
}
193194
}
@@ -196,7 +197,7 @@ macro_rules! impl_vector_element_for_primitive {
196197
unsafe fn __unique_ptr_drop(mut repr: *mut c_void) {
197198
extern "C" {
198199
attr! {
199-
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", stringify!($ty), "$drop")]
200+
#[link_name = concat!("cxxbridge03$unique_ptr$std$vector$", $segment, "$drop")]
200201
fn __unique_ptr_drop(this: *mut *mut c_void);
201202
}
202203
}
@@ -206,6 +207,12 @@ macro_rules! impl_vector_element_for_primitive {
206207
};
207208
}
208209

210+
macro_rules! impl_vector_element_for_primitive {
211+
($ty:ident) => {
212+
impl_vector_element!(stringify!($ty), stringify!($ty), $ty);
213+
};
214+
}
215+
209216
impl_vector_element_for_primitive!(u8);
210217
impl_vector_element_for_primitive!(u16);
211218
impl_vector_element_for_primitive!(u32);
@@ -218,3 +225,5 @@ impl_vector_element_for_primitive!(i64);
218225
impl_vector_element_for_primitive!(isize);
219226
impl_vector_element_for_primitive!(f32);
220227
impl_vector_element_for_primitive!(f64);
228+
229+
impl_vector_element!("string", "CxxString", CxxString);

syntax/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
130130

131131
match Atom::from(ident) {
132132
None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
133-
| Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
134-
Some(CxxString) => { /* todo */ }
133+
| Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
134+
| Some(CxxString) => return,
135135
Some(Bool) | Some(RustString) => {}
136136
}
137137
}

tests/ffi/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod ffi {
3939
fn c_return_unique_ptr_string() -> UniquePtr<CxxString>;
4040
fn c_return_unique_ptr_vector_u8() -> UniquePtr<CxxVector<u8>>;
4141
fn c_return_unique_ptr_vector_f64() -> UniquePtr<CxxVector<f64>>;
42+
fn c_return_unique_ptr_vector_string() -> UniquePtr<CxxVector<CxxString>>;
4243
fn c_return_unique_ptr_vector_shared() -> UniquePtr<CxxVector<Shared>>;
4344
fn c_return_unique_ptr_vector_opaque() -> UniquePtr<CxxVector<C>>;
4445
fn c_return_ref_vector(c: &C) -> &CxxVector<u8>;
@@ -62,6 +63,7 @@ pub mod ffi {
6263
fn c_take_unique_ptr_string(s: UniquePtr<CxxString>);
6364
fn c_take_unique_ptr_vector_u8(v: UniquePtr<CxxVector<u8>>);
6465
fn c_take_unique_ptr_vector_f64(v: UniquePtr<CxxVector<f64>>);
66+
fn c_take_unique_ptr_vector_string(v: UniquePtr<CxxVector<CxxString>>);
6567
fn c_take_unique_ptr_vector_shared(v: UniquePtr<CxxVector<Shared>>);
6668
fn c_take_ref_vector(v: &CxxVector<u8>);
6769
fn c_take_rust_vec(v: Vec<u8>);

tests/ffi/tests.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64() {
8888
return vec;
8989
}
9090

91+
std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string() {
92+
return std::unique_ptr<std::vector<std::string>>(
93+
new std::vector<std::string>());
94+
}
95+
9196
std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared() {
9297
auto vec = std::unique_ptr<std::vector<Shared>>(new std::vector<Shared>());
9398
vec->push_back(Shared{1010});
@@ -210,6 +215,12 @@ void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v) {
210215
}
211216
}
212217

218+
void c_take_unique_ptr_vector_string(
219+
std::unique_ptr<std::vector<std::string>> v) {
220+
(void)v;
221+
cxx_test_suite_set_correct();
222+
}
223+
213224
void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v) {
214225
if (v->size() == 2) {
215226
cxx_test_suite_set_correct();

tests/ffi/tests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rust::String c_return_rust_string();
4343
std::unique_ptr<std::string> c_return_unique_ptr_string();
4444
std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8();
4545
std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64();
46+
std::unique_ptr<std::vector<std::string>> c_return_unique_ptr_vector_string();
4647
std::unique_ptr<std::vector<Shared>> c_return_unique_ptr_vector_shared();
4748
std::unique_ptr<std::vector<C>> c_return_unique_ptr_vector_opaque();
4849
const std::vector<uint8_t> &c_return_ref_vector(const C &c);
@@ -67,6 +68,8 @@ void c_take_rust_string(rust::String s);
6768
void c_take_unique_ptr_string(std::unique_ptr<std::string> s);
6869
void c_take_unique_ptr_vector_u8(std::unique_ptr<std::vector<uint8_t>> v);
6970
void c_take_unique_ptr_vector_f64(std::unique_ptr<std::vector<double>> v);
71+
void c_take_unique_ptr_vector_string(
72+
std::unique_ptr<std::vector<std::string>> v);
7073
void c_take_unique_ptr_vector_shared(std::unique_ptr<std::vector<Shared>> v);
7174
void c_take_ref_vector(const std::vector<uint8_t> &v);
7275
void c_take_rust_vec(rust::Vec<uint8_t> v);

0 commit comments

Comments
 (0)