Skip to content

Commit

Permalink
Update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviojs committed May 27, 2024
1 parent 1db5eb5 commit 5d2f513
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
19 changes: 2 additions & 17 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,24 +299,9 @@ fn bar() -> Foo { .. } // Will be emitted as `struct foo bar();`

### Volatile annotation

cbindgen will emit the C volatile type qualifier for types and variables that have the `volatile=true` annotation.
cbindgen will emit the C volatile type qualifier for transparent 1-field structs that have the `volatile` annotation.

There is no equivalent in rust. You should use `read_volatile` and `write_volatile` to get C-like behavior.

```rust
/// cbindgen:volatile=true
type volatile_type_t = u32;

#[repr(C)]
struct S {
/// cbindgen:volatile=true
volatile_variable: u32,
}

/// cbindgen:volatile=true
#[no_mangle]
static mut volatile_global_variable: u32 = 0;
```
Example usage can be found in `tests/rust/volatile.rs`.

### Struct Annotations

Expand Down
8 changes: 4 additions & 4 deletions tests/rust/volatile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This test uses the `cbindgen:volatile=true` annotation to generate volatile types and variables.
//! This test uses the `volatile` annotation on a wrapper to generate volatile types.
use std::ffi::c_int;
use std::mem::ManuallyDrop;
Expand All @@ -8,7 +8,7 @@ use std::ptr::null_mut;
use std::ptr::NonNull;
use std::cell::Cell;

/// cbindgen:volatile=true
/// cbindgen:volatile
#[repr(transparent)]
pub struct V<T>(T);
impl<T> V<T> {
Expand Down Expand Up @@ -45,8 +45,8 @@ pub type c_vci = V<Cell<c_int>>;

pub type c_vfn = V<Option<extern "C" fn()>>;

// TODO not FFI-safe
//pub type c_va2 = [V<c_int>; 2];
// TODO cannot mangle array
//pub type c_va2 = V<[c_int; 2]>;

#[repr(C)]
pub struct S {
Expand Down

0 comments on commit 5d2f513

Please sign in to comment.