Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd09ae6

Browse files
committedApr 24, 2023
Add support for c_void as an opaque C++ type
1 parent 50c1622 commit dd09ae6

File tree

11 files changed

+31
-7
lines changed

11 files changed

+31
-7
lines changed
 

‎.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"search.exclude": {
33
"**/target": true
4-
}
4+
},
5+
"vscode-nmake-tools.workspaceBuildDirectories": [
6+
"."
7+
]
58
}

‎gen/src/write.rs

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

‎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
}

‎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
}

‎syntax/check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
126126
None | Some(Bool) | Some(Char) | Some(U8) | Some(U16) | Some(U32) | Some(U64)
127127
| Some(Usize) | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize)
128128
| Some(F32) | Some(F64) | Some(RustString) => return,
129-
Some(CxxString) => {}
129+
Some(CxxString) | Some(Cvoid) => {}
130130
}
131131
}
132132
Type::Str(_) => return,
@@ -165,7 +165,7 @@ fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) {
165165
None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
166166
| Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
167167
| Some(F64) | Some(CxxString) => return,
168-
Some(Char) | Some(RustString) => {}
168+
Some(Char) | Some(RustString) | Some(Cvoid) => {}
169169
}
170170
} else if let Type::CxxVector(_) = &ptr.inner {
171171
cx.error(ptr, "std::shared_ptr<std::vector> is not supported yet");
@@ -186,7 +186,7 @@ fn check_type_weak_ptr(cx: &mut Check, ptr: &Ty1) {
186186
None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
187187
| Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
188188
| Some(F64) | Some(CxxString) => return,
189-
Some(Char) | Some(RustString) => {}
189+
Some(Char) | Some(RustString) | Some(Cvoid) => {}
190190
}
191191
} else if let Type::CxxVector(_) = &ptr.inner {
192192
cx.error(ptr, "std::weak_ptr<std::vector> is not supported yet");
@@ -211,7 +211,7 @@ fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
211211
| Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
212212
| Some(CxxString) => return,
213213
Some(Char) => { /* todo */ }
214-
Some(Bool) | Some(RustString) => {}
214+
Some(Bool) | Some(RustString) | Some(Cvoid) => {}
215215
}
216216
}
217217

‎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)

‎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
}

‎tests/ffi/lib.rs

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

206206
fn c_get_use_count(weak: &WeakPtr<C>) -> usize;
207207

208+
unsafe fn c_takes_void_star(value: *const c_void);
209+
208210
#[rust_name = "i32_overloaded_method"]
209211
fn cOverloadedMethod(&self, x: i32) -> String;
210212
#[rust_name = "str_overloaded_method"]

‎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
}

‎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);

‎tests/test.rs

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

366368
#[test]

0 commit comments

Comments
 (0)
Please sign in to comment.