generate some functions with macros, cbindgen 0.29

This commit is contained in:
Vinzenz Schroeter 2025-06-16 21:26:58 +02:00
parent db94fecbb3
commit 02f629c68b
23 changed files with 195 additions and 349 deletions

View file

@ -1,5 +1,6 @@
use crate::mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
use crate::{
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet};
use std::ptr::NonNull;
@ -44,21 +45,8 @@ pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
/// Clones an [BitmapCommand] instance.
///
/// returns: a new [BitmapCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitmap_clone(
command: NonNull<BitmapCommand>,
) -> NonNull<BitmapCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull<BitmapCommand>) {
unsafe { heap_drop(command) }
}
wrap_clone!(BitmapCommand, sp_cmd_bitmap);
wrap_free!(BitmapCommand, sp_cmd_bitmap);
/// Returns a pointer to the provided `BitmapCommand`.
///

View file

@ -1,5 +1,6 @@
use crate::mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
use crate::{
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset,
@ -44,21 +45,8 @@ pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
/// Clones an [BitVecCommand] instance.
///
/// returns: a new [BitVecCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_clone(
command: NonNull<BitVecCommand>,
) -> NonNull<BitVecCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitVecCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull<BitVecCommand>) {
unsafe { heap_drop(command) }
}
wrap_clone!(BitVecCommand, sp_cmd_bitvec);
wrap_free!(BitVecCommand, sp_cmd_bitvec);
/// Returns a pointer to the [BitVec] contained in the [BitVecCommand].
#[no_mangle]

View file

@ -1,9 +1,8 @@
use crate::mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
};
use servicepoint::{
BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet,
use crate::{
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet};
use std::ptr::NonNull;
/// Set the brightness of individual tiles in a rectangular area of the display.
@ -42,23 +41,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
/// Clones an [BrightnessGridCommand] instance.
///
/// returns: a new [BrightnessGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_clone(
command: NonNull<BrightnessGridCommand>,
) -> NonNull<BrightnessGridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
wrap_clone!(BrightnessGridCommand, sp_cmd_brightness_grid);
wrap_free!(BrightnessGridCommand, sp_cmd_brightness_grid);
/// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand].
#[no_mangle]

View file

@ -1,4 +1,4 @@
use crate::mem::{heap_drop, heap_move_nonnull};
use crate::{macros::wrap_free, mem::heap_move_nonnull};
use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand};
use std::ptr::NonNull;
@ -12,11 +12,7 @@ pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull<ClearCommand> {
heap_move_nonnull(ClearCommand)
}
/// Deallocates a [ClearCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull<ClearCommand>) {
unsafe { heap_drop(command) }
}
wrap_free!(ClearCommand, sp_cmd_clear);
/// Kills the udp daemon on the display, which usually results in a restart.
///
@ -28,13 +24,7 @@ pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull<HardResetCommand> {
heap_move_nonnull(HardResetCommand)
}
/// Deallocates a [HardResetCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_hard_reset_free(
command: NonNull<ClearCommand>,
) {
unsafe { heap_drop(command) }
}
wrap_free!(HardResetCommand, sp_cmd_hard_reset);
/// A yet-to-be-tested command.
///
@ -44,8 +34,4 @@ pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull<FadeOutCommand> {
heap_move_nonnull(FadeOutCommand)
}
/// Deallocates a [FadeOutCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull<ClearCommand>) {
unsafe { heap_drop(command) }
}
wrap_free!(FadeOutCommand, sp_cmd_fade_out);

View file

@ -1,7 +1,8 @@
use crate::mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
use crate::{
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet};
use servicepoint::{CharGrid, CharGridCommand, Origin, Packet};
use std::ptr::NonNull;
/// Show UTF-8 encoded text on the screen.
@ -40,23 +41,8 @@ pub unsafe extern "C" fn sp_cmd_char_grid_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
/// Clones an [CharGridCommand] instance.
///
/// returns: a new [CharGridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_clone(
command: NonNull<CharGridCommand>,
) -> NonNull<CharGridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [BitmapCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_char_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
wrap_clone!(CharGridCommand, sp_cmd_char_grid);
wrap_free!(CharGridCommand, sp_cmd_char_grid);
/// Moves the provided [CharGrid] to be contained in the [CharGridCommand].
#[no_mangle]

View file

@ -1,9 +1,8 @@
use crate::mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
};
use servicepoint::{
BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet,
use crate::{
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet};
use std::ptr::NonNull;
/// Show text on the screen.
@ -42,23 +41,8 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_try_into_packet(
heap_move_ok(unsafe { heap_remove(command) }.try_into())
}
/// Clones an [Cp437GridCommand] instance.
///
/// returns: a new [Cp437GridCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_clone(
command: NonNull<Cp437GridCommand>,
) -> NonNull<Cp437GridCommand> {
unsafe { heap_clone(command) }
}
/// Deallocates a [Cp437GridCommand].
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_cp437_grid_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
wrap_clone!(Cp437GridCommand, sp_cmd_cp437_grid);
wrap_free!(Cp437GridCommand, sp_cmd_cp437_grid);
/// Moves the provided bitmap into the provided command.
///

View file

@ -222,7 +222,7 @@ pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand {
}
/// Deallocates an [SPCommand].
///
///
/// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null.
///
/// # Examples

View file

@ -1,7 +1,8 @@
use crate::mem::{heap_clone, heap_drop, heap_move_nonnull, heap_remove};
use servicepoint::{
BitmapCommand, Brightness, GlobalBrightnessCommand, Packet,
use crate::{
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_remove},
};
use servicepoint::{Brightness, GlobalBrightnessCommand, Packet};
use std::ptr::NonNull;
/// Set the brightness of all tiles to the same value.
@ -21,22 +22,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet(
heap_move_nonnull(unsafe { heap_remove(command) }.into())
}
/// Clones an [GlobalBrightnessCommand] instance.
///
/// returns: a new [GlobalBrightnessCommand] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_global_clone(
command: NonNull<GlobalBrightnessCommand>,
) -> NonNull<GlobalBrightnessCommand> {
unsafe { heap_clone(command) }
}
#[no_mangle]
pub unsafe extern "C" fn sp_cmd_brightness_global_free(
command: NonNull<BitmapCommand>,
) {
unsafe { heap_drop(command) }
}
wrap_clone!(GlobalBrightnessCommand, sp_cmd_brightness_global);
wrap_free!(GlobalBrightnessCommand, sp_cmd_brightness_global);
/// Moves the provided bitmap to be contained in the command.
#[no_mangle]

View file

@ -1,9 +1,7 @@
use crate::{
containers::ByteSlice,
mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some,
heap_remove,
},
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid,
@ -82,19 +80,8 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec(
heap_move_ok(Bitmap::from_bitvec(width, bitvec))
}
/// Clones a [Bitmap].
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: NonNull<Bitmap>,
) -> NonNull<Bitmap> {
unsafe { heap_clone(bitmap) }
}
/// Deallocates a [Bitmap].
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull<Bitmap>) {
unsafe { heap_drop(bitmap) }
}
wrap_clone!(Bitmap, sp_bitmap);
wrap_free!(Bitmap, sp_bitmap);
/// Gets the current value at the specified position in the [Bitmap].
///

View file

@ -1,8 +1,7 @@
use crate::{
containers::ByteSlice,
mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
},
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{
BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet,
@ -36,19 +35,8 @@ pub unsafe extern "C" fn sp_bitvec_load(
heap_move_nonnull(DisplayBitVec::from_slice(data))
}
/// Clones a [DisplayBitVec].
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_clone(
bit_vec: NonNull<DisplayBitVec>,
) -> NonNull<DisplayBitVec> {
unsafe { heap_clone(bit_vec) }
}
/// Deallocates a [DisplayBitVec].
#[no_mangle]
pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull<DisplayBitVec>) {
unsafe { heap_drop(bit_vec) }
}
wrap_clone!(DisplayBitVec, sp_bitvec);
wrap_free!(DisplayBitVec, sp_bitvec);
/// Gets the value of a bit from the [DisplayBitVec].
///

View file

@ -1,16 +1,13 @@
use crate::{
containers::ByteSlice,
mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some,
heap_remove,
},
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid,
Origin, Packet,
};
use std::mem::transmute;
use std::ptr::NonNull;
use std::{mem::transmute, ptr::NonNull};
/// Creates a new [BrightnessGrid] with the specified dimensions.
///
@ -55,21 +52,8 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
)
}
/// Clones a [BrightnessGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_clone(
grid: NonNull<BrightnessGrid>,
) -> NonNull<BrightnessGrid> {
unsafe { heap_clone(grid) }
}
/// Deallocates a [BrightnessGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: NonNull<BrightnessGrid>,
) {
unsafe { heap_drop(brightness_grid) }
}
wrap_clone!(BrightnessGrid, sp_brightness_grid);
wrap_free!(BrightnessGrid, sp_brightness_grid);
/// Gets the current value at the specified position.
///

View file

@ -10,9 +10,9 @@
/// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
/// the function returning this type.
/// - if `start` is NULL or `length` is 0, do not dereference `start`.
///
///
/// # Examples
///
///
/// ```c
/// ByteSlice empty = {.start: NULL, .length = 0};
/// ```

View file

@ -1,8 +1,7 @@
use crate::{
containers::ByteSlice,
mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove,
},
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_remove},
};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull;
@ -40,19 +39,8 @@ pub unsafe extern "C" fn sp_char_grid_load(
heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec()))
}
/// Clones a [CharGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_clone(
grid: NonNull<CharGrid>,
) -> NonNull<CharGrid> {
unsafe { heap_clone(grid) }
}
/// Deallocates a [CharGrid].
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull<CharGrid>) {
unsafe { heap_drop(char_grid) }
}
wrap_clone!(CharGrid, sp_char_grid);
wrap_free!(CharGrid, sp_char_grid);
/// Returns the current value at the specified position.
///

View file

@ -1,9 +1,7 @@
use crate::{
containers::ByteSlice,
mem::{
heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some,
heap_remove,
},
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove},
};
use servicepoint::{
Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet,
@ -32,19 +30,8 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
heap_move_some(Cp437Grid::load(width, height, data))
}
/// Clones a [Cp437Grid].
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_clone(
grid: NonNull<Cp437Grid>,
) -> NonNull<Cp437Grid> {
unsafe { heap_clone(grid) }
}
/// Deallocates a [Cp437Grid].
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull<Cp437Grid>) {
unsafe { heap_drop(cp437_grid) }
}
wrap_clone!(Cp437Grid, sp_cp437_grid);
wrap_free!(Cp437Grid, sp_cp437_grid);
/// Gets the current value at the specified position.
///

View file

@ -29,6 +29,7 @@
pub mod commands;
/// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends.
pub mod containers;
mod macros;
pub(crate) mod mem;
/// Functions related to [Packet].
pub mod packet;

25
src/macros.rs Normal file
View file

@ -0,0 +1,25 @@
macro_rules! wrap_free {
($typ:ident, $prefix:ident) => {
paste::paste! {
#[doc = concat!("Deallocates a [", stringify!($typ), "] instance.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _free>](instance: NonNull<$typ>) {
unsafe { crate::mem::heap_drop(instance) }
}
}
};
}
macro_rules! wrap_clone {
($typ:ident, $prefix:ident) => {
paste::paste! {
#[doc = concat!("Clones a [", stringify!($typ), "] instance.")]
#[no_mangle]
pub unsafe extern "C" fn [<$prefix _clone>](instance: NonNull<$typ>) -> NonNull<$typ> {
unsafe { crate::mem::heap_clone(instance) }
}
}
};
}
pub(crate) use {wrap_clone, wrap_free};

View file

@ -1,6 +1,7 @@
use crate::{
containers::ByteSlice,
mem::{heap_clone, heap_drop, heap_move_nonnull, heap_move_ok},
macros::{wrap_clone, wrap_free},
mem::{heap_move_nonnull, heap_move_ok},
};
use servicepoint::{CommandCode, Header, Packet};
use std::ptr::NonNull;
@ -90,21 +91,8 @@ pub unsafe extern "C" fn sp_packet_serialize_to(
}
}
/// Clones a [Packet].
///
/// returns: a new [Packet] instance.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_clone(
packet: NonNull<Packet>,
) -> NonNull<Packet> {
unsafe { heap_clone(packet) }
}
/// Deallocates a [Packet].
#[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
unsafe { heap_drop(packet) }
}
wrap_clone!(Packet, sp_packet);
wrap_free!(Packet, sp_packet);
/// Converts u16 into [CommandCode].
///

View file

@ -1,6 +1,7 @@
use crate::{
commands::{CommandTag, SPCommand},
mem::{heap_drop, heap_move_ok, heap_remove},
macros::wrap_free,
mem::{heap_move_ok, heap_remove},
};
use servicepoint::{Header, Packet, UdpSocketExt};
use std::{
@ -143,8 +144,4 @@ pub unsafe extern "C" fn sp_udp_send_header(
.is_ok()
}
/// Closes and deallocates a [UdpSocket].
#[no_mangle]
pub unsafe extern "C" fn sp_udp_free(connection: NonNull<UdpSocket>) {
unsafe { heap_drop(connection) }
}
wrap_free!(UdpSocket, sp_udp);