Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sometimes, we need customize the implement of `Default` for certain types, In these cases, the `nodefault` annotation can be used to prevent bindgen to autoderive the `Default` traits for a type.
- Loading branch information
Showing
16 changed files
with
234 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Preventing the Derivation of `Default` | ||
|
||
`bindgen` will attempt to derive/impl the `Default` traits on a best-effort basis. | ||
Sometimes, we need customize the implement of `Default` for certain types, | ||
In these cases, the `nodefault` annotation can be used to prevent bindgen | ||
to autoderive the `Default` traits for a type. | ||
|
||
### Library | ||
|
||
* [`bindgen::Builder::no_default`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.no_default) | ||
|
||
### Command Line | ||
|
||
* `--no-default <regex>` | ||
|
||
### Annotations | ||
|
||
```c | ||
/** | ||
* We need to specify some preset values as the Default of Header. | ||
* | ||
* for example: | ||
* | ||
* <div rustbindgen nodefault></div> | ||
*/ | ||
struct Header { | ||
unsigned int magic; | ||
unsigned char data[252]; | ||
}; | ||
|
||
... | ||
``` | ||
|
||
### Customize Implements | ||
|
||
```rust | ||
// Inlcude the generated bindings. | ||
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); | ||
|
||
impl Default for Header { | ||
fn default() -> Self { | ||
Self { | ||
magic: 0x10203040u32, | ||
data: [0; 252usize], | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![allow( | ||
dead_code, | ||
non_snake_case, | ||
non_camel_case_types, | ||
non_upper_case_globals | ||
)] | ||
|
||
/// <div rustbindgen nodefault></div> | ||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct DefaultButWait { | ||
pub whatever: ::std::os::raw::c_int, | ||
} | ||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct DefaultButWaitDerived { | ||
pub whatever: DefaultButWait, | ||
} | ||
impl Default for DefaultButWaitDerived { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/expectations/tests/no_default_bypass_derive_default.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![allow( | ||
dead_code, | ||
non_snake_case, | ||
non_camel_case_types, | ||
non_upper_case_globals | ||
)] | ||
|
||
#[repr(C)] | ||
pub struct Generic<T> { | ||
pub t: [T; 40usize], | ||
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>, | ||
} | ||
impl<T> Default for Generic<T> { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
#[repr(C)] | ||
pub struct NoDefault<T> { | ||
pub t: [T; 40usize], | ||
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![allow( | ||
dead_code, | ||
non_snake_case, | ||
non_camel_case_types, | ||
non_upper_case_globals | ||
)] | ||
|
||
#[repr(C)] | ||
#[repr(align(4))] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct NoDefault { | ||
pub _bindgen_opaque_blob: u32, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_NoDefault() { | ||
assert_eq!( | ||
::std::mem::size_of::<NoDefault>(), | ||
4usize, | ||
concat!("Size of: ", stringify!(NoDefault)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<NoDefault>(), | ||
4usize, | ||
concat!("Alignment of ", stringify!(NoDefault)) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![allow( | ||
dead_code, | ||
non_snake_case, | ||
non_camel_case_types, | ||
non_upper_case_globals | ||
)] | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct NoDefault { | ||
pub i: ::std::os::raw::c_int, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_NoDefault() { | ||
assert_eq!( | ||
::std::mem::size_of::<NoDefault>(), | ||
4usize, | ||
concat!("Size of: ", stringify!(NoDefault)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<NoDefault>(), | ||
4usize, | ||
concat!("Alignment of ", stringify!(NoDefault)) | ||
); | ||
assert_eq!( | ||
unsafe { &(*(::std::ptr::null::<NoDefault>())).i as *const _ as usize }, | ||
0usize, | ||
concat!( | ||
"Offset of field: ", | ||
stringify!(NoDefault), | ||
"::", | ||
stringify!(i) | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
/** <div rustbindgen nodefault></div> */ | ||
template<typename T> | ||
class DefaultButWait { | ||
int whatever; | ||
}; | ||
|
||
template<typename T> | ||
class DefaultButWaitDerived { | ||
DefaultButWait<T> whatever; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// bindgen-flags: --no-default "NoDefault" | ||
|
||
template<typename T> | ||
class Generic { | ||
T t[40]; | ||
}; | ||
|
||
template<typename T> | ||
class NoDefault { | ||
T t[40]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// bindgen-flags: --opaque-type "NoDefault" --no-default "NoDefault" | ||
|
||
class NoDefault { | ||
int i; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// bindgen-flags: --whitelist-type "NoDefault" --no-default "NoDefault" | ||
|
||
class NoDefault { | ||
int i; | ||
}; |