Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use *const void for opaque pointers #334

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/api-desc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

### Patch

- Use `*const void` instead of `*const u8` for opaque data
- Update dependencies
- Fix lints

Expand Down
4 changes: 2 additions & 2 deletions crates/api-desc/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ pub(crate) fn new() -> Item {
/// Function called on button events.
///
/// The function takes its opaque `data` and the new button `state` as arguments.
handler_func: fn { data: *const u8, state: usize },
handler_func: fn { data: *const void, state: usize },

/// The opaque data to use when calling the handler function.
handler_data: *const u8,
handler_data: *const void,
} -> {}
},
item! {
Expand Down
4 changes: 2 additions & 2 deletions crates/api-desc/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub(crate) fn new() -> Item {
/// Allocates a timer (initially stopped).
fn allocate "ta" {
/// Function called when the timer triggers.
handler_func: fn { data: *const u8 },
handler_func: fn { data: *const void },

/// The opaque data to use when calling the handler function.
handler_data: *const u8,
handler_data: *const void,
} -> {
/// Identifier for this timer.
id: usize,
Expand Down
2 changes: 1 addition & 1 deletion crates/api-desc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! type_ {
};
(*const $($type:tt)*) => (type_!(*false $($type)*));
(*mut $($type:tt)*) => (type_!(*true $($type)*));
(*$mut:tt u8) => (Type::Pointer { mutable: $mut, type_: None });
(*$mut:tt void) => (Type::Pointer { mutable: $mut, type_: None });
(*$mut:tt $($type:tt)*) => {
Type::Pointer { mutable: $mut, type_: Some(Box::new(type_!($($type)*))) }
};
Expand Down
4 changes: 2 additions & 2 deletions crates/api-desc/src/radio/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub(crate) fn new() -> Item {
/// Function called on radio events.
///
/// The function takes its opaque `data` as argument.
handler_func: fn { data: *mut u8 },
handler_func: fn { data: *const void },

/// The opaque data to use when calling the handler function.
handler_data: *mut u8,
handler_data: *const void,
} -> {}
},
item! {
Expand Down
4 changes: 2 additions & 2 deletions crates/api-desc/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub(crate) fn new() -> Item {
event: usize,

/// Function pointer of the closure to call on events.
handler_func: fn { data: *const u8 },
handler_func: fn { data: *const void },

/// Opaque data of the closure to call on events.
handler_data: *const u8,
handler_data: *const void,
} -> {}
},
item! {
Expand Down
4 changes: 2 additions & 2 deletions crates/api-desc/src/usb/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub(crate) fn new() -> Item {
/// It is possible that the callback is spuriously called.
fn register "use" {
event: usize,
handler_func: fn { data: *const u8 },
handler_data: *const u8,
handler_func: fn { data: *const void },
handler_data: *const void,
} -> {}
},
item! {
Expand Down
2 changes: 1 addition & 1 deletion crates/prelude/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@

## 0.1.0

<!-- Increment to skip CHANGELOG.md test: 17 -->
<!-- Increment to skip CHANGELOG.md test: 18 -->
6 changes: 3 additions & 3 deletions crates/prelude/src/radio/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<H: Handler> Listener<H> {
let event = event as u32;
let handler_func = Self::call;
let handler = Box::into_raw(Box::new(handler));
let handler_data = handler as *mut u8;
let handler_data = handler as *const u8;
unsafe { api::register(api::register::Params { event, handler_func, handler_data }) };
Listener { handler }
}
Expand All @@ -78,8 +78,8 @@ impl<H: Handler> Listener<H> {
core::mem::forget(self);
}

extern "C" fn call(data: *mut u8) {
let handler = unsafe { &mut *(data as *mut H) };
extern "C" fn call(data: *const u8) {
let handler = unsafe { &*(data as *const H) };
handler.event(Event::Advertisement);
}
}
Expand Down