Skip to content

Commit 85ad77b

Browse files
committed
Add support for c_void as an opaque C++ type
1 parent eb06f52 commit 85ad77b

File tree

10 files changed

+27
-6
lines changed

10 files changed

+27
-6
lines changed

Diff for: gen/src/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
210210
Some(Isize) => out.builtin.rust_isize = true,
211211
Some(CxxString) => out.include.string = true,
212212
Some(RustString) => out.builtin.rust_string = true,
213-
Some(Bool | Char | F32 | F64) | None => {}
213+
Some(Bool | Char | F32 | F64 | Cvoid) | None => {}
214214
},
215215
Type::RustBox(_) => out.builtin.rust_box = true,
216216
Type::RustVec(_) => out.builtin.rust_vec = true,
@@ -1322,6 +1322,7 @@ fn write_atom(out: &mut OutFile, atom: Atom) {
13221322
F64 => write!(out, "double"),
13231323
CxxString => write!(out, "::std::string"),
13241324
RustString => write!(out, "::rust::String"),
1325+
Cvoid => write!(out, "void"),
13251326
}
13261327
}
13271328

Diff for: src/extern_type.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use self::kind::{Kind, Opaque, Trivial};
22
use crate::CxxString;
33
#[cfg(feature = "alloc")]
44
use alloc::string::String;
5+
use core::ffi::c_void;
56

67
/// A type for which the layout is determined by its C++ definition.
78
///
@@ -222,4 +223,5 @@ impl_extern_type! {
222223

223224
[Opaque]
224225
CxxString = "std::string"
226+
c_void = "void"
225227
}

Diff for: syntax/atom.rs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum Atom {
2020
F64,
2121
CxxString,
2222
RustString,
23+
Cvoid,
2324
}
2425

2526
impl Atom {
@@ -46,6 +47,7 @@ impl Atom {
4647
"f64" => Some(F64),
4748
"CxxString" => Some(CxxString),
4849
"String" => Some(RustString),
50+
"c_void" => Some(Cvoid),
4951
_ => None,
5052
}
5153
}
@@ -77,6 +79,7 @@ impl AsRef<str> for Atom {
7779
F64 => "f64",
7880
CxxString => "CxxString",
7981
RustString => "String",
82+
Cvoid => "c_void",
8083
}
8184
}
8285
}

Diff for: syntax/check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
128128
Bool | Char | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | F32
129129
| F64 | RustString,
130130
) => return,
131-
Some(CxxString) => {}
131+
Some(CxxString | Cvoid) => {}
132132
}
133133
}
134134
Type::Str(_) => return,
@@ -169,7 +169,7 @@ fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) {
169169
Bool | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | F32 | F64
170170
| CxxString,
171171
) => return,
172-
Some(Char | RustString) => {}
172+
Some(Char | RustString | Cvoid) => {}
173173
}
174174
} else if let Type::CxxVector(_) = &ptr.inner {
175175
cx.error(ptr, "std::shared_ptr<std::vector> is not supported yet");
@@ -192,7 +192,7 @@ fn check_type_weak_ptr(cx: &mut Check, ptr: &Ty1) {
192192
Bool | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | F32 | F64
193193
| CxxString,
194194
) => return,
195-
Some(Char | RustString) => {}
195+
Some(Char | RustString | Cvoid) => {}
196196
}
197197
} else if let Type::CxxVector(_) = &ptr.inner {
198198
cx.error(ptr, "std::weak_ptr<std::vector> is not supported yet");
@@ -218,7 +218,7 @@ fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
218218
U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | F32 | F64 | CxxString,
219219
) => return,
220220
Some(Char) => { /* todo */ }
221-
Some(Bool | RustString) => {}
221+
Some(Bool | RustString | Cvoid) => {}
222222
}
223223
}
224224

Diff for: syntax/pod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl<'a> Types<'a> {
1010
match atom {
1111
Bool | Char | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64
1212
| Isize | F32 | F64 => true,
13-
CxxString | RustString => false,
13+
CxxString | RustString | Cvoid => false,
1414
}
1515
} else if let Some(strct) = self.structs.get(ident) {
1616
derive::contains(&strct.derives, Trait::Copy)

Diff for: syntax/tokens.rs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ impl ToTokens for Type {
2020
} else if ident.rust == RustString {
2121
let span = ident.rust.span();
2222
tokens.extend(quote_spanned!(span=> ::cxx::alloc::string::));
23+
} else if ident.rust == Cvoid {
24+
let span = ident.rust.span();
25+
tokens.extend(quote_spanned!(span=> ::core::ffi::));
2326
}
2427
ident.to_tokens(tokens);
2528
}

Diff for: tests/ffi/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ pub mod ffi {
207207

208208
fn c_get_use_count(weak: &WeakPtr<C>) -> usize;
209209

210+
unsafe fn c_takes_void_star(value: *const c_void);
211+
210212
#[rust_name = "i32_overloaded_method"]
211213
fn cOverloadedMethod(&self, x: i32) -> String;
212214
#[rust_name = "str_overloaded_method"]

Diff for: tests/ffi/tests.cc

+6
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ size_t c_get_use_count(const std::weak_ptr<C> &weak) noexcept {
598598
return weak.use_count();
599599
}
600600

601+
void c_takes_void_star(const void *value) noexcept {
602+
if (value == nullptr) {
603+
cxx_test_suite_set_correct();
604+
}
605+
}
606+
601607
extern "C" C *cxx_test_suite_get_unique_ptr() noexcept {
602608
return std::unique_ptr<C>(new C{2020}).release();
603609
}

Diff for: tests/ffi/tests.h

+2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c);
190190

191191
size_t c_get_use_count(const std::weak_ptr<C> &weak) noexcept;
192192

193+
void c_takes_void_star(const void *value) noexcept;
194+
193195
void c_take_trivial_ptr(std::unique_ptr<D> d);
194196
void c_take_trivial_ref(const D &d);
195197
void c_take_trivial_mut_ref(D &d);

Diff for: tests/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ fn test_extern_opaque() {
362362
let f = ffi2::c_return_ns_opaque_ptr();
363363
check!(ffi2::c_take_opaque_ns_ref(f.as_ref().unwrap()));
364364
check!(ffi2::c_take_opaque_ns_ptr(f));
365+
366+
check!(unsafe { ffi::c_takes_void_star(core::ptr::null()) });
365367
}
366368

367369
#[test]

0 commit comments

Comments
 (0)