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,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.
///