File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -4384,6 +4384,10 @@ impl TryToRustTy for Type {
4384
4384
if inner_ty. canonical_type ( ctx) . is_function ( ) || is_objc_pointer
4385
4385
{
4386
4386
Ok ( ty)
4387
+ } else if ctx. options ( ) . generate_cxx_nonnull_references && matches ! ( self . kind( ) , TypeKind :: Reference ( _) ) {
4388
+ // It's UB to pass null values in place of C++ references
4389
+ let prefix = ctx. trait_prefix ( ) ;
4390
+ Ok ( syn:: parse_quote! { :: #prefix:: ptr:: NonNull <#ty> } )
4387
4391
} else {
4388
4392
Ok ( ty. to_ptr ( is_const) )
4389
4393
}
Original file line number Diff line number Diff line change @@ -258,6 +258,9 @@ struct BindgenCommand {
258
258
/// Use extern crate instead of use for objc.
259
259
#[ arg( long) ]
260
260
objc_extern_crate : bool ,
261
+ /// Use `NonNull` in place of raw pointer for C++ references.
262
+ #[ arg( long) ]
263
+ nonnull_references : bool ,
261
264
/// Generate block signatures instead of void pointers.
262
265
#[ arg( long) ]
263
266
generate_block : bool ,
@@ -590,6 +593,7 @@ where
590
593
no_doc_comments,
591
594
no_recursive_allowlist,
592
595
objc_extern_crate,
596
+ nonnull_references,
593
597
generate_block,
594
598
generate_cstr,
595
599
block_extern_crate,
@@ -921,6 +925,7 @@ where
921
925
no_doc_comments => |b, _| b. generate_comments( false ) ,
922
926
no_recursive_allowlist => |b, _| b. allowlist_recursively( false ) ,
923
927
objc_extern_crate,
928
+ nonnull_references => |b, _| b. generate_cxx_nonnull_references( true ) ,
924
929
generate_block,
925
930
generate_cstr,
926
931
block_extern_crate,
Original file line number Diff line number Diff line change @@ -1474,6 +1474,26 @@ options! {
1474
1474
} ,
1475
1475
as_args: |value, args| ( !value) . as_args( args, "--no-doc-comments" ) ,
1476
1476
} ,
1477
+ /// Whether to generate [`NonNull`] pointers for C++ references.
1478
+ ///
1479
+ /// [`NonNull`]: core::ptr::NonNull
1480
+ generate_cxx_nonnull_references: bool {
1481
+ default : false ,
1482
+ methods: {
1483
+ /// Generate `NonNull` pointers in place of raw pointers for C++
1484
+ /// references.
1485
+ ///
1486
+ /// This option is disabled by default:
1487
+ ///
1488
+ /// Enabling it erases information about constness in generated
1489
+ /// code, and `NonNull` is more cumbersome to use than raw pointers.
1490
+ pub fn generate_cxx_nonnull_references( mut self , doit: bool ) -> Self {
1491
+ self . options. generate_cxx_nonnull_references = doit;
1492
+ self
1493
+ }
1494
+ } ,
1495
+ as_args: |value, args| ( !value) . as_args( args, "--nonnull-references" ) ,
1496
+ } ,
1477
1497
/// Whether to generate inline functions.
1478
1498
generate_inline_functions: bool {
1479
1499
methods: {
You can’t perform that action at this time.
0 commit comments