From 84adf166a971b81ddb806b323ddccbda7af893df Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 5 May 2025 18:01:35 +0200 Subject: [PATCH 01/76] add heap_move_ok and heap_move_some helpers --- src/bitmap.rs | 30 +++++++++--------------------- src/bitvec.rs | 11 +++++------ src/brightness_grid.rs | 23 ++++++++++------------- src/char_grid.rs | 17 ++++++----------- src/cp437_grid.rs | 19 +++++++------------ src/lib.rs | 8 ++++++++ src/packet.rs | 15 +++++---------- src/typed_command.rs | 7 ++----- src/udp.rs | 14 +++----------- 9 files changed, 55 insertions(+), 89 deletions(-) diff --git a/src/bitmap.rs b/src/bitmap.rs index 5db4af0..b8b482f 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,5 +1,8 @@ use crate::byte_slice::ByteSlice; -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, SPBitVec}; +use crate::{ + heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, + SPBitVec, +}; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, }; @@ -33,11 +36,7 @@ pub unsafe extern "C" fn sp_bitmap_new( width: usize, height: usize, ) -> *mut Bitmap { - if let Some(bitmap) = Bitmap::new(width, height) { - heap_move(bitmap) - } else { - std::ptr::null_mut() - } + heap_move_some(Bitmap::new(width, height)) } /// Creates a new [Bitmap] with a size matching the screen. @@ -63,11 +62,7 @@ pub unsafe extern "C" fn sp_bitmap_load( data: ByteSlice, ) -> *mut Bitmap { let data = unsafe { data.as_slice() }; - if let Ok(bitmap) = Bitmap::load(width, height, data) { - heap_move(bitmap) - } else { - std::ptr::null_mut() - } + heap_move_ok(Bitmap::load(width, height, data)) } /// Tries to convert the BitVec to a Bitmap. @@ -81,11 +76,7 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( bitvec: NonNull, ) -> *mut Bitmap { let bitvec = unsafe { heap_remove(bitvec) }; - if let Ok(bitmap) = Bitmap::from_bitvec(width, bitvec.0) { - heap_move(bitmap) - } else { - std::ptr::null_mut() - } + heap_move_ok(Bitmap::from_bitvec(width, bitvec.0)) } /// Clones a [Bitmap]. @@ -215,12 +206,9 @@ pub unsafe extern "C" fn sp_bitmap_into_packet( compression: CompressionCode, ) -> *mut Packet { let bitmap = unsafe { heap_remove(bitmap) }; - match Packet::try_from(BitmapCommand { + heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), compression, - }) { - Ok(packet) => heap_move(packet), - Err(_) => std::ptr::null_mut(), - } + })) } diff --git a/src/bitvec.rs b/src/bitvec.rs index 0e7ee5e..b988df6 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -1,4 +1,6 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; +use crate::{ + heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, +}; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, }; @@ -162,13 +164,10 @@ pub unsafe extern "C" fn sp_bitvec_into_packet( compression: CompressionCode, ) -> *mut Packet { let bitvec = unsafe { heap_remove(bitvec) }.0; - match Packet::try_from(BitVecCommand { + heap_move_ok(Packet::try_from(BitVecCommand { bitvec, offset, operation, compression, - }) { - Ok(packet) => heap_move(packet), - Err(_) => std::ptr::null_mut(), - } + })) } diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index 1099be0..2dd985a 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -1,4 +1,7 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; +use crate::{ + heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, + ByteSlice, +}; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, Origin, Packet, @@ -43,13 +46,10 @@ pub unsafe extern "C" fn sp_brightness_grid_load( data: ByteSlice, ) -> *mut BrightnessGrid { let data = unsafe { data.as_slice() }; - - match ByteGrid::load(width, height, data) - .map(move |grid| grid.map(Brightness::saturating_from)) - { - None => std::ptr::null_mut(), - Some(grid) => heap_move(grid), - } + heap_move_some( + ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)), + ) } /// Clones a [BrightnessGrid]. @@ -187,11 +187,8 @@ pub unsafe extern "C" fn sp_brightness_grid_into_packet( y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; - match Packet::try_from(BrightnessGridCommand { + heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), - }) { - Ok(packet) => heap_move(packet), - Err(_) => std::ptr::null_mut(), - } + })) } diff --git a/src/char_grid.rs b/src/char_grid.rs index bfb2585..e07e8db 100644 --- a/src/char_grid.rs +++ b/src/char_grid.rs @@ -1,4 +1,6 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; +use crate::{ + heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, +}; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -32,11 +34,7 @@ pub unsafe extern "C" fn sp_char_grid_load( data: ByteSlice, ) -> *mut CharGrid { let data = unsafe { data.as_slice() }; - if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) { - heap_move(grid) - } else { - std::ptr::null_mut() - } + heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } /// Clones a [CharGrid]. @@ -145,11 +143,8 @@ pub unsafe extern "C" fn sp_char_grid_into_packet( y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; - match Packet::try_from(CharGridCommand { + heap_move_ok(Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), - }) { - Ok(packet) => heap_move(packet), - Err(_) => std::ptr::null_mut(), - } + })) } diff --git a/src/cp437_grid.rs b/src/cp437_grid.rs index ff70d2b..5a3daab 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -1,4 +1,7 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; +use crate::{ + heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, + ByteSlice, +}; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, }; @@ -23,12 +26,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load( data: ByteSlice, ) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; - let grid = Cp437Grid::load(width, height, data); - if let Some(grid) = grid { - heap_move(grid) - } else { - std::ptr::null_mut() - } + heap_move_some(Cp437Grid::load(width, height, data)) } /// Clones a [Cp437Grid]. @@ -147,11 +145,8 @@ pub unsafe extern "C" fn sp_cp437_grid_into_packet( y: usize, ) -> *mut Packet { let grid = unsafe { heap_remove(grid) }; - match Packet::try_from(Cp437GridCommand { + heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), - }) { - Ok(packet) => heap_move(packet), - Err(_) => std::ptr::null_mut(), - } + })) } diff --git a/src/lib.rs b/src/lib.rs index 60c42ab..f69f0b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,6 +60,14 @@ pub(crate) fn heap_move_nonnull(x: T) -> NonNull { NonNull::from(Box::leak(Box::new(x))) } +pub(crate) fn heap_move_ok(x: Result) -> *mut T { + x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) +} + +pub(crate) fn heap_move_some(x: Option) -> *mut T { + x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) +} + pub(crate) unsafe fn heap_drop(x: NonNull) { drop(unsafe { heap_remove(x) }); } diff --git a/src/packet.rs b/src/packet.rs index d8b1579..a454732 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,4 +1,6 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice}; +use crate::{ + heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, +}; use servicepoint::{CommandCode, Header, Packet, TypedCommand}; use std::ptr::NonNull; @@ -11,11 +13,7 @@ pub unsafe extern "C" fn sp_packet_from_command( command: NonNull, ) -> *mut Packet { let command = unsafe { heap_remove(command) }; - if let Ok(packet) = command.try_into() { - heap_move(packet) - } else { - std::ptr::null_mut() - } + heap_move_ok(command.try_into()) } /// Tries to load a [Packet] from the passed array with the specified length. @@ -24,10 +22,7 @@ pub unsafe extern "C" fn sp_packet_from_command( #[no_mangle] pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet { let data = unsafe { data.as_slice() }; - match servicepoint::Packet::try_from(data) { - Err(_) => std::ptr::null_mut(), - Ok(packet) => heap_move(packet), - } + heap_move_ok(servicepoint::Packet::try_from(data)) } /// Creates a raw [Packet] from parts. diff --git a/src/typed_command.rs b/src/typed_command.rs index 9e27540..df454c4 100644 --- a/src/typed_command.rs +++ b/src/typed_command.rs @@ -1,4 +1,4 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, SPBitVec}; +use crate::{heap_drop, heap_move, heap_move_nonnull, heap_move_ok, SPBitVec}; use servicepoint::{ BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid, CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand, @@ -15,10 +15,7 @@ pub unsafe extern "C" fn sp_command_try_from_packet( packet: NonNull, ) -> *mut TypedCommand { let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; - match servicepoint::TypedCommand::try_from(packet) { - Err(_) => std::ptr::null_mut(), - Ok(command) => heap_move(command), - } + heap_move_ok(servicepoint::TypedCommand::try_from(packet)) } /// Clones a [TypedCommand] instance. diff --git a/src/udp.rs b/src/udp.rs index f68aa16..bc8acc3 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,4 +1,4 @@ -use crate::{heap_drop, heap_move, heap_remove}; +use crate::{heap_drop, heap_move_ok, heap_remove}; use servicepoint::{Header, Packet, TypedCommand, UdpSocketExt}; use std::ffi::{c_char, CStr}; use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; @@ -20,12 +20,8 @@ pub unsafe extern "C" fn sp_udp_open(host: NonNull) -> *mut UdpSocket { let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); - let connection = match UdpSocket::bind_connect(host) { - Err(_) => return std::ptr::null_mut(), - Ok(value) => value, - }; - heap_move(connection) + heap_move_ok(UdpSocket::bind_connect(host)) } /// Creates a new instance of [UdpConnection]. @@ -48,11 +44,7 @@ pub unsafe extern "C" fn sp_udp_open_ipv4( port: u16, ) -> *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); - let connection = match UdpSocket::bind_connect(addr) { - Err(_) => return std::ptr::null_mut(), - Ok(value) => value, - }; - heap_move(connection) + heap_move_ok(UdpSocket::bind_connect(addr)) } /// Sends a [Packet] to the display using the [UdpConnection]. From 373725c648e104b55373fc6d0184c257ca022067 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 5 May 2025 22:55:18 +0200 Subject: [PATCH 02/76] add first specific commands --- cbindgen.toml | 2 +- include/servicepoint.h | 135 ++++++++++++++++++++++++++++++--- src/commands/bitmap_command.rs | 110 +++++++++++++++++++++++++++ src/commands/bitvec_command.rs | 115 ++++++++++++++++++++++++++++ src/commands/mod.rs | 2 + src/lib.rs | 4 + src/typed_command.rs | 58 +------------- 7 files changed, 360 insertions(+), 66 deletions(-) create mode 100644 src/commands/bitmap_command.rs create mode 100644 src/commands/bitvec_command.rs create mode 100644 src/commands/mod.rs diff --git a/cbindgen.toml b/cbindgen.toml index 221e915..68723da 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -32,7 +32,7 @@ features = ["full"] [export] include = [] -exclude = [] +exclude = ["BitVec"] [export.rename] "SpBitVec" = "BitVec" diff --git a/include/servicepoint.h b/include/servicepoint.h index 5863ef5..8923756 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -179,6 +179,22 @@ enum CompressionCode typedef uint16_t CompressionCode; #endif // __cplusplus +/** + * Set pixel data starting at the pixel offset on screen. + * + * The screen will continuously overwrite more pixel data without regarding the offset, meaning + * once the starting row is full, overwriting will continue on column 0. + * + * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. + * + * `new_bit = old_bit op sent_bit` + * + * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. + * + * The contained [`DisplayBitVec`] is always uncompressed. + */ +typedef struct BitVecCommand BitVecCommand; + /** * A fixed-size 2D grid of booleans. * @@ -196,6 +212,38 @@ typedef uint16_t CompressionCode; */ typedef struct Bitmap Bitmap; +/** + * Overwrites a rectangular region of pixels. + * + * Origin coordinates must be divisible by 8. + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * # + * let mut bitmap = Bitmap::max_sized(); + * // draw something to the pixels here + * # bitmap.set(2, 5, true); + * + * // create command to send pixels + * let command = BitmapCommand { + * bitmap, + * origin: Origin::ZERO, + * compression: CompressionCode::Uncompressed + * }; + * + * connection.send_command(command).expect("send failed"); + * ``` + */ +typedef struct BitmapCommand BitmapCommand; + +/** + * This is a type only used by cbindgen to have a type for pointers. + */ +typedef struct DisplayBitVec DisplayBitVec; + /** * The raw packet. * @@ -348,6 +396,11 @@ typedef uint8_t Brightness; */ typedef ValueGrid_char CharGrid; +/** + * Type alias for documenting the meaning of the u16 in enum values + */ +typedef size_t Offset; + /** * A grid containing codepage 437 characters. * @@ -908,17 +961,64 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid, */ size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); +BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); + +/** + * Returns a pointer to the provided `BitmapCommand`. + * + * # Safety + * + * - The returned bitmap inherits the lifetime of the command in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + */ +Bitmap */*notnull*/ sp_cmd_bitmap_get(BitmapCommand */*notnull*/ command); + +CompressionCode sp_cmd_bitmap_get_compression(BitmapCommand */*notnull*/ command); + +void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +Packet *sp_cmd_bitmap_into_packet(BitmapCommand */*notnull*/ command); + +TypedCommand */*notnull*/ sp_cmd_bitmap_into_typed(BitmapCommand */*notnull*/ command); + /** * Sets a window of pixels to the specified values. * * The passed [Bitmap] gets consumed. * - * Returns: a new [servicepoint::BitmapCommand] instance. + * Returns: a new [BitmapCommand] instance. */ -TypedCommand *sp_command_bitmap(size_t x, - size_t y, - Bitmap */*notnull*/ bitmap, - CompressionCode compression); +BitmapCommand */*notnull*/ sp_cmd_bitmap_new(Bitmap */*notnull*/ bitmap, + size_t origin_x, + size_t origin_y, + CompressionCode compression); + +/** + * Moves the provided bitmap to be contained in the command. + */ +void sp_cmd_bitmap_set(BitmapCommand */*notnull*/ command, + Bitmap */*notnull*/ bitmap); + +void sp_cmd_bitmap_set_compression(BitmapCommand */*notnull*/ command, + CompressionCode compression); + +void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +DisplayBitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); + +CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command); + +Offset sp_cmd_bitvec_get_offset(BitVecCommand */*notnull*/ command); + +BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); + +Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command); + +TypedCommand */*notnull*/ sp_cmd_bitvec_into_typed(BitVecCommand */*notnull*/ command); /** * Set pixel data starting at the pixel offset on screen. @@ -932,12 +1032,27 @@ TypedCommand *sp_command_bitmap(size_t x, * * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. * - * The contained [`BitVecU8Msb0`] is always uncompressed. + * The contained [`DisplayBitVec`] is always uncompressed. */ -TypedCommand *sp_command_bitvec(size_t offset, - SPBitVec */*notnull*/ bit_vec, - CompressionCode compression, - BinaryOperation operation); +BitVecCommand */*notnull*/ sp_cmd_bitvec_new(DisplayBitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + +/** + * Moves the provided bitmap to be contained in the command. + */ +void sp_cmd_bitvec_set(BitVecCommand */*notnull*/ command, + DisplayBitVec */*notnull*/ bitvec); + +void sp_cmd_bitvec_set_compression(BitVecCommand */*notnull*/ command, + CompressionCode compression); + +void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, + Offset offset); + +void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, + BinaryOperation operation); /** * Set the brightness of individual tiles in a rectangular area of the display. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs new file mode 100644 index 0000000..b1ab343 --- /dev/null +++ b/src/commands/bitmap_command.rs @@ -0,0 +1,110 @@ +use crate::{heap_move_nonnull, heap_move_ok, heap_remove}; +use servicepoint::{ + Bitmap, BitmapCommand, CompressionCode, Origin, Packet, TypedCommand, +}; +use std::ptr::NonNull; + +/// Sets a window of pixels to the specified values. +/// +/// The passed [Bitmap] gets consumed. +/// +/// Returns: a new [BitmapCommand] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_new( + bitmap: NonNull, + origin_x: usize, + origin_y: usize, + compression: CompressionCode, +) -> NonNull { + heap_move_nonnull(BitmapCommand { + bitmap: unsafe { heap_remove(bitmap) }, + origin: Origin::new(origin_x, origin_y), + compression, + }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( + bitmap: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_into_typed( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(command) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( + command: NonNull, +) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) +} + +/// Returns a pointer to the provided `BitmapCommand`. +/// +/// # Safety +/// +/// - The returned bitmap inherits the lifetime of the command in which it is contained. +/// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_get( + mut command: NonNull, +) -> NonNull { + unsafe { NonNull::from(&mut (command.as_mut().bitmap)) } +} + +/// Moves the provided bitmap to be contained in the command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_set( + mut command: NonNull, + bitmap: NonNull, +) { + unsafe { + command.as_mut().bitmap = heap_remove(bitmap); + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_get_origin( + command: NonNull, + origin_x: NonNull, + origin_y: NonNull, +) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( + mut command: NonNull, + origin_x: usize, + origin_y: usize, +) { + unsafe { + command.as_mut().origin = Origin::new(origin_x, origin_y); + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( + mut command: NonNull, + compression: CompressionCode, +) { + unsafe { + command.as_mut().compression = compression; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( + command: NonNull, +) -> CompressionCode { + unsafe { command.as_ref().compression } +} diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs new file mode 100644 index 0000000..39d5253 --- /dev/null +++ b/src/commands/bitvec_command.rs @@ -0,0 +1,115 @@ +use crate::{heap_move_nonnull, heap_move_ok, heap_remove}; +use servicepoint::{ + BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, + Packet, TypedCommand, +}; +use std::ptr::NonNull; + +/// Set pixel data starting at the pixel offset on screen. +/// +/// The screen will continuously overwrite more pixel data without regarding the offset, meaning +/// once the starting row is full, overwriting will continue on column 0. +/// +/// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. +/// +/// `new_bit = old_bit op sent_bit` +/// +/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. +/// +/// The contained [`DisplayBitVec`] is always uncompressed. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_new( + bitvec: NonNull, + offset: usize, + operation: BinaryOperation, + compression: CompressionCode, +) -> NonNull { + heap_move_nonnull(BitVecCommand { + bitvec: unsafe { heap_remove(bitvec) }, + offset, + operation, + compression, + }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_into_typed( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(command) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_into_packet( + command: NonNull, +) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_get( + mut command: NonNull, +) -> *mut DisplayBitVec { + &mut unsafe { command.as_mut() }.bitvec +} + +/// Moves the provided bitmap to be contained in the command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_set( + mut command: NonNull, + bitvec: NonNull, +) { + unsafe { + command.as_mut().bitvec = heap_remove(bitvec); + } +} +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( + command: NonNull, +) -> Offset { + unsafe { command.as_ref().offset } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( + mut command: NonNull, + offset: Offset, +) { + unsafe { + command.as_mut().offset = offset; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( + command: NonNull, +) -> BinaryOperation { + unsafe { command.as_ref().operation.clone() } // TODO remove clone +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( + mut command: NonNull, + operation: BinaryOperation, +) { + unsafe { + command.as_mut().operation = operation; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( + mut command: NonNull, + compression: CompressionCode, +) { + unsafe { + command.as_mut().compression = compression; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_get_compression( + command: NonNull, +) -> CompressionCode { + unsafe { command.as_ref().compression } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..45489df --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,2 @@ +mod bitmap_command; +mod bitvec_command; diff --git a/src/lib.rs b/src/lib.rs index f69f0b3..924a729 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ mod bitvec; mod brightness_grid; mod byte_slice; mod char_grid; +mod commands; mod cp437_grid; mod packet; mod typed_command; @@ -78,3 +79,6 @@ pub(crate) unsafe fn heap_remove(x: NonNull) -> T { /// This is a type only used by cbindgen to have a type for pointers. pub struct UdpSocket; + +/// This is a type only used by cbindgen to have a type for pointers. +pub struct DisplayBitVec; diff --git a/src/typed_command.rs b/src/typed_command.rs index df454c4..451c0b7 100644 --- a/src/typed_command.rs +++ b/src/typed_command.rs @@ -1,7 +1,7 @@ -use crate::{heap_drop, heap_move, heap_move_nonnull, heap_move_ok, SPBitVec}; +use crate::{heap_drop, heap_move_nonnull, heap_move_ok}; use servicepoint::{ - BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid, - CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand, + Brightness, BrightnessGrid, CharGrid, Cp437Grid, GlobalBrightnessCommand, + Packet, TypedCommand, }; use std::ptr::NonNull; @@ -92,36 +92,6 @@ pub unsafe extern "C" fn sp_command_brightness_grid( heap_move_nonnull(result) } -/// Set pixel data starting at the pixel offset on screen. -/// -/// The screen will continuously overwrite more pixel data without regarding the offset, meaning -/// once the starting row is full, overwriting will continue on column 0. -/// -/// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. -/// -/// `new_bit = old_bit op sent_bit` -/// -/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. -/// -/// The contained [`BitVecU8Msb0`] is always uncompressed. -#[no_mangle] -pub unsafe extern "C" fn sp_command_bitvec( - offset: usize, - bit_vec: NonNull, - compression: CompressionCode, - operation: BinaryOperation, -) -> *mut TypedCommand { - let bit_vec = unsafe { *Box::from_raw(bit_vec.as_ptr()) }; - let command = servicepoint::BitVecCommand { - offset, - operation, - bitvec: bit_vec.0, - compression, - } - .into(); - heap_move(command) -} - /// Show codepage 437 encoded text on the screen. /// /// The passed [Cp437Grid] gets consumed. @@ -162,28 +132,6 @@ pub unsafe extern "C" fn sp_command_char_grid( heap_move_nonnull(result) } -/// Sets a window of pixels to the specified values. -/// -/// The passed [Bitmap] gets consumed. -/// -/// Returns: a new [servicepoint::BitmapCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_bitmap( - x: usize, - y: usize, - bitmap: NonNull, - compression: CompressionCode, -) -> *mut TypedCommand { - let bitmap = unsafe { *Box::from_raw(bitmap.as_ptr()) }; - let command = servicepoint::BitmapCommand { - origin: servicepoint::Origin::new(x, y), - bitmap, - compression, - } - .into(); - heap_move(command) -} - /// Deallocates a [TypedCommand]. /// /// # Examples From 2165629befde7ef83f0ad69bd1c3d115f5489a7e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 5 May 2025 22:59:06 +0200 Subject: [PATCH 03/76] add badges --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4788c75..fa0a49b 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # servicepoint_binding_c +[![Release](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c/badges/release.svg)](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c/releases) [![crates.io](https://img.shields.io/crates/v/servicepoint_binding_c.svg)](https://crates.io/crates/servicepoint) [![Crates.io Total Downloads](https://img.shields.io/crates/d/servicepoint_binding_c)](https://crates.io/crates/servicepoint) [![docs.rs](https://img.shields.io/docsrs/servicepoint_binding_c)](https://docs.rs/servicepoint/latest/servicepoint/) [![GPLv3 licensed](https://img.shields.io/crates/l/servicepoint_binding_c)](./LICENSE) +[![CI](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c/badges/workflows/rust.yml/badge.svg)](https://git.berlin.ccc.de/servicepoint/servicepoint_binding_c) In [CCCB](https://berlin.ccc.de/), there is a big pixel matrix hanging on the wall. It is called "Service Point Display" or "Airport Display". @@ -12,7 +14,7 @@ This crate contains C bindings for the [servicepoint](https://git.berlin.ccc.de/ ## Examples -```c++ +```c #include #include "servicepoint.h" From 4f0eca3ea0ee4caed7ea907033450b68b37db42b Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 6 May 2025 21:12:37 +0200 Subject: [PATCH 04/76] more commands --- example/src/brightness_tester.c | 44 ++++--- include/servicepoint.h | 146 ++++++++++++++++++++++++ src/commands/bitmap_command.rs | 14 ++- src/commands/bitvec_command.rs | 15 ++- src/commands/brightness_grid_command.rs | 95 +++++++++++++++ src/commands/char_grid_command.rs | 94 +++++++++++++++ src/commands/cp437_grid_command.rs | 94 +++++++++++++++ src/commands/mod.rs | 3 + src/typed_command.rs | 62 +--------- 9 files changed, 489 insertions(+), 78 deletions(-) create mode 100644 src/commands/brightness_grid_command.rs create mode 100644 src/commands/char_grid_command.rs create mode 100644 src/commands/cp437_grid_command.rs diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index c3e42ba..9476422 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -1,30 +1,44 @@ #include "servicepoint.h" -int main(void) { - UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); - //UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return -1; +static UdpSocket *connection = NULL; +void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); sp_bitmap_fill(all_on, true); - Packet *packet = sp_bitmap_into_packet(all_on, 0, 0, COMPRESSION_CODE_UNCOMPRESSED); - if (packet == NULL) - return -1; - - sp_udp_send_packet(connection, packet); - - BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT); + BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on); + Packet *packet = sp_cmd_bitmap_into_packet(bitmapCommand); + if (packet != NULL) + sp_udp_send_packet(connection, packet); +} +void make_brightness_pattern(BrightnessGrid *grid) { ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid); for (size_t index = 0; index < slice.length; index++) { slice.start[index] = (uint8_t) (index % ((size_t) Brightness_MAX)); } +} - packet = sp_brightness_grid_into_packet(grid, 0, 0); - sp_udp_send_packet(connection, packet); - +void run_at_exit() { sp_udp_free(connection); +} + +int main(void) { + //UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); + if (connection == NULL) + return -1; + atexit(run_at_exit); + + enable_all_pixels(); + + BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT); + make_brightness_pattern(grid); + + Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid)); + if (packet == NULL) + return -2; + + sp_udp_send_packet(connection, packet); return 0; } diff --git a/include/servicepoint.h b/include/servicepoint.h index 8923756..805d84b 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -239,6 +239,54 @@ typedef struct Bitmap Bitmap; */ typedef struct BitmapCommand BitmapCommand; +/** + * Set the brightness of individual tiles in a rectangular area of the display. + */ +typedef struct BrightnessGridCommand BrightnessGridCommand; + +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of UTF-8 encoded characters (the default encoding in rust). + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * let grid = CharGrid::from("Hello,\nWorld!"); + * connection.send_command(CharGridCommand { origin: Origin::ZERO, grid }).expect("send failed"); + * ``` + */ +typedef struct CharGridCommand CharGridCommand; + +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of [CP-437] encoded characters. + * + *
You probably want to use [Command::Utf8Data] instead
+ * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * let grid = CharGrid::from("Hello,\nWorld!"); + * let grid = Cp437Grid::from(&grid); + * connection.send_command(Cp437GridCommand{ origin: Origin::ZERO, grid }).expect("send failed"); + * ``` + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * let grid = Cp437Grid::load_ascii("Hello\nWorld", 5, false).unwrap(); + * connection.send_command(Cp437GridCommand{ origin: Origin::new(2, 2), grid }).unwrap(); + * ``` + * [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 + */ +typedef struct Cp437GridCommand Cp437GridCommand; + /** * This is a type only used by cbindgen to have a type for pointers. */ @@ -961,6 +1009,10 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid, */ size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); +BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ command); + +void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); + BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); /** @@ -1008,6 +1060,10 @@ void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(BitVecCommand */*notnull*/ command); + +void sp_cmd_bitvec_free(BitVecCommand */*notnull*/ command); + DisplayBitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command); @@ -1054,6 +1110,96 @@ void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, BinaryOperation operation); +BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(BrightnessGridCommand */*notnull*/ command); + +void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command); + +BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); + +BrightnessGrid *sp_cmd_brightness_grid_get(BrightnessGridCommand */*notnull*/ command); + +void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command); + +TypedCommand */*notnull*/ sp_cmd_brightness_grid_into_typed(BrightnessGridCommand */*notnull*/ command); + +BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Moves the provided bitmap to be contained in the command. + */ +void sp_cmd_brightness_grid_set(BrightnessGridCommand */*notnull*/ command, + BrightnessGrid */*notnull*/ grid); + +void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(CharGridCommand */*notnull*/ command); + +void sp_cmd_char_grid_free(BitmapCommand */*notnull*/ command); + +CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); + +CharGrid *sp_cmd_char_grid_get(CharGridCommand */*notnull*/ command); + +void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +Packet *sp_cmd_char_grid_into_packet(CharGridCommand */*notnull*/ command); + +TypedCommand */*notnull*/ sp_cmd_char_grid_into_typed(CharGridCommand */*notnull*/ command); + +CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Moves the provided bitmap to be contained in the command. + */ +void sp_cmd_char_grid_set(CharGridCommand */*notnull*/ command, + CharGrid */*notnull*/ grid); + +void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(Cp437GridCommand */*notnull*/ command); + +void sp_cmd_cp437_grid_free(BitmapCommand */*notnull*/ command); + +Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); + +Cp437Grid *sp_cmd_cp437_grid_get(Cp437GridCommand */*notnull*/ command); + +void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +Packet *sp_cmd_cp437_grid_into_packet(Cp437GridCommand */*notnull*/ command); + +TypedCommand */*notnull*/ sp_cmd_cp437_grid_into_typed(Cp437GridCommand */*notnull*/ command); + +Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Moves the provided bitmap to be contained in the command. + */ +void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, + Cp437Grid */*notnull*/ grid); + +void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + /** * Set the brightness of individual tiles in a rectangular area of the display. * diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index b1ab343..4413a3c 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,4 +1,4 @@ -use crate::{heap_move_nonnull, heap_move_ok, heap_remove}; +use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, Origin, Packet, TypedCommand, }; @@ -44,6 +44,18 @@ pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_clone( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { command.as_ref().clone() }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull) { + unsafe { heap_drop(command) } +} + /// Returns a pointer to the provided `BitmapCommand`. /// /// # Safety diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 39d5253..5795017 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,4 +1,4 @@ -use crate::{heap_move_nonnull, heap_move_ok, heap_remove}; +use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, Packet, TypedCommand, @@ -46,6 +46,18 @@ pub unsafe extern "C" fn sp_cmd_bitvec_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_clone( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { command.as_ref().clone() }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { + unsafe { heap_drop(command) } +} + #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get( mut command: NonNull, @@ -63,6 +75,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set( command.as_mut().bitvec = heap_remove(bitvec); } } + #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( command: NonNull, diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs new file mode 100644 index 0000000..18a2836 --- /dev/null +++ b/src/commands/brightness_grid_command.rs @@ -0,0 +1,95 @@ +use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; +use servicepoint::{ + BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet, + TypedCommand, +}; +use std::ptr::NonNull; + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_new( + grid: NonNull, + origin_x: usize, + origin_y: usize, +) -> NonNull { + heap_move_nonnull(BrightnessGridCommand { + grid: unsafe { heap_remove(grid) }, + origin: Origin::new(origin_x, origin_y), + }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid( + grid: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(grid) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_into_typed( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(command) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( + command: NonNull, +) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_clone( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { command.as_ref().clone() }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_free( + command: NonNull, +) { + unsafe { heap_drop(command) } +} + +/// Moves the provided bitmap to be contained in the command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_set( + mut command: NonNull, + grid: NonNull, +) { + unsafe { + command.as_mut().grid = heap_remove(grid); + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_get( + mut command: NonNull, +) -> *mut BrightnessGrid { + &mut unsafe { command.as_mut() }.grid +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_get_origin( + command: NonNull, + origin_x: NonNull, + origin_y: NonNull, +) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_grid_set_origin( + mut command: NonNull, + origin_x: usize, + origin_y: usize, +) { + unsafe { + command.as_mut().origin = Origin::new(origin_x, origin_y); + } +} diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs new file mode 100644 index 0000000..933e4cd --- /dev/null +++ b/src/commands/char_grid_command.rs @@ -0,0 +1,94 @@ +use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; +use servicepoint::{ + BitmapCommand, CharGrid, CharGridCommand, Origin, Packet, TypedCommand, +}; +use std::ptr::NonNull; + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_new( + grid: NonNull, + origin_x: usize, + origin_y: usize, +) -> NonNull { + heap_move_nonnull(CharGridCommand { + grid: unsafe { heap_remove(grid) }, + origin: Origin::new(origin_x, origin_y), + }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_from_grid( + grid: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(grid) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_into_typed( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(command) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_into_packet( + command: NonNull, +) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_clone( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { command.as_ref().clone() }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_free( + command: NonNull, +) { + unsafe { heap_drop(command) } +} + +/// Moves the provided bitmap to be contained in the command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_set( + mut command: NonNull, + grid: NonNull, +) { + unsafe { + command.as_mut().grid = heap_remove(grid); + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_get( + mut command: NonNull, +) -> *mut CharGrid { + &mut unsafe { command.as_mut() }.grid +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_get_origin( + command: NonNull, + origin_x: NonNull, + origin_y: NonNull, +) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_char_grid_set_origin( + mut command: NonNull, + origin_x: usize, + origin_y: usize, +) { + unsafe { + command.as_mut().origin = Origin::new(origin_x, origin_y); + } +} diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs new file mode 100644 index 0000000..0f3d9e2 --- /dev/null +++ b/src/commands/cp437_grid_command.rs @@ -0,0 +1,94 @@ +use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; +use servicepoint::{ + BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, TypedCommand, +}; +use std::ptr::NonNull; + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_new( + grid: NonNull, + origin_x: usize, + origin_y: usize, +) -> NonNull { + heap_move_nonnull(Cp437GridCommand { + grid: unsafe { heap_remove(grid) }, + origin: Origin::new(origin_x, origin_y), + }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid( + grid: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(grid) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_into_typed( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(command) }.into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet( + command: NonNull, +) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_clone( + command: NonNull, +) -> NonNull { + heap_move_nonnull(unsafe { command.as_ref().clone() }) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_free( + command: NonNull, +) { + unsafe { heap_drop(command) } +} + +/// Moves the provided bitmap to be contained in the command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_set( + mut command: NonNull, + grid: NonNull, +) { + unsafe { + command.as_mut().grid = heap_remove(grid); + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_get( + mut command: NonNull, +) -> *mut Cp437Grid { + &mut unsafe { command.as_mut() }.grid +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin( + command: NonNull, + origin_x: NonNull, + origin_y: NonNull, +) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_cp437_grid_set_origin( + mut command: NonNull, + origin_x: usize, + origin_y: usize, +) { + unsafe { + command.as_mut().origin = Origin::new(origin_x, origin_y); + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 45489df..90422ff 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,5 @@ mod bitmap_command; mod bitvec_command; +mod brightness_grid_command; +mod char_grid_command; +mod cp437_grid_command; diff --git a/src/typed_command.rs b/src/typed_command.rs index 451c0b7..26c33f1 100644 --- a/src/typed_command.rs +++ b/src/typed_command.rs @@ -1,6 +1,6 @@ use crate::{heap_drop, heap_move_nonnull, heap_move_ok}; use servicepoint::{ - Brightness, BrightnessGrid, CharGrid, Cp437Grid, GlobalBrightnessCommand, + Brightness, GlobalBrightnessCommand, Packet, TypedCommand, }; use std::ptr::NonNull; @@ -72,66 +72,6 @@ pub unsafe extern "C" fn sp_command_global_brightness( heap_move_nonnull(GlobalBrightnessCommand::from(brightness).into()) } -/// Set the brightness of individual tiles in a rectangular area of the display. -/// -/// The passed [BrightnessGrid] gets consumed. -/// -/// Returns: a new [servicepoint::Command::CharBrightness] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_brightness_grid( - x: usize, - y: usize, - grid: NonNull, -) -> NonNull { - let grid = unsafe { *Box::from_raw(grid.as_ptr()) }; - let result = servicepoint::BrightnessGridCommand { - origin: servicepoint::Origin::new(x, y), - grid, - } - .into(); - heap_move_nonnull(result) -} - -/// Show codepage 437 encoded text on the screen. -/// -/// The passed [Cp437Grid] gets consumed. -/// -/// Returns: a new [servicepoint::Cp437GridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_cp437_grid( - x: usize, - y: usize, - grid: NonNull, -) -> NonNull { - let grid = *unsafe { Box::from_raw(grid.as_ptr()) }; - let result = servicepoint::Cp437GridCommand { - origin: servicepoint::Origin::new(x, y), - grid, - } - .into(); - heap_move_nonnull(result) -} - -/// Show UTF-8 encoded text on the screen. -/// -/// The passed [CharGrid] gets consumed. -/// -/// Returns: a new [servicepoint::CharGridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_char_grid( - x: usize, - y: usize, - grid: NonNull, -) -> NonNull { - let grid = unsafe { *Box::from_raw(grid.as_ptr()) }; - let result = servicepoint::CharGridCommand { - origin: servicepoint::Origin::new(x, y), - grid, - } - .into(); - heap_move_nonnull(result) -} - /// Deallocates a [TypedCommand]. /// /// # Examples From 32d39f8006623d5d0124a69e9b7b9e20fbcda1a0 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 6 May 2025 21:25:36 +0200 Subject: [PATCH 05/76] misc mod and doc changes --- include/servicepoint.h | 28 +++++++++++++++++----------- src/bitmap.rs | 3 +-- src/bitvec.rs | 2 +- src/brightness_grid.rs | 8 ++++---- src/char_grid.rs | 2 +- src/commands/bitmap_command.rs | 4 ++++ src/commands/mod.rs | 6 ++++++ src/cp437_grid.rs | 2 +- src/lib.rs | 32 +++++++++++--------------------- src/packet.rs | 2 +- src/udp.rs | 16 ++++++++-------- 11 files changed, 55 insertions(+), 50 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 805d84b..02f4676 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -845,15 +845,15 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * * # Examples * ```C - * UdpConnection connection = sp_udp_open("127.0.0.1:2342"); + * UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); * if (connection == NULL) * return 1; * - * BrightnessGrid grid = sp_brightness_grid_new(2, 2); + * BrightnessGrid *grid = sp_brightness_grid_new(2, 2); * sp_brightness_grid_set(grid, 0, 0, 0); * sp_brightness_grid_set(grid, 1, 1, 10); * - * TypedCommand command = sp_command_char_brightness(grid); + * TypedCommand *command = sp_command_char_brightness(grid); * sp_udp_free(connection); * ``` */ @@ -1013,6 +1013,12 @@ BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ comman void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); +/** + * Move the provided [Bitmap] into a new [BitmapCommand], + * leaving other fields as their default values. + * + * Rust equivalent: [`>::from`] + */ BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); /** @@ -1475,19 +1481,19 @@ bool sp_u16_to_command_code(uint16_t code, CommandCode *result); /** - * Closes and deallocates a [UdpConnection]. + * Closes and deallocates a [UdpSocket]. */ void sp_udp_free(UdpSocket */*notnull*/ connection); /** - * Creates a new instance of [UdpConnection]. + * Creates a new instance of [UdpSocket]. * * returns: NULL if connection fails, or connected instance * * # Examples * * ```C - * UdpConnection connection = sp_udp_open("172.23.42.29:2342"); + * UdpSocket connection = sp_udp_open("172.23.42.29:2342"); * if (connection != NULL) * sp_udp_send_command(connection, sp_command_clear()); * ``` @@ -1495,14 +1501,14 @@ void sp_udp_free(UdpSocket */*notnull*/ connection); UdpSocket *sp_udp_open(char */*notnull*/ host); /** - * Creates a new instance of [UdpConnection]. + * Creates a new instance of [UdpSocket]. * * returns: NULL if connection fails, or connected instance * * # Examples * * ```C - * UdpConnection connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + * UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); * if (connection != NULL) * sp_udp_send_command(connection, sp_command_clear()); * ``` @@ -1514,7 +1520,7 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint16_t port); /** - * Sends a [TypedCommand] to the display using the [UdpConnection]. + * Sends a [TypedCommand] to the display using the [UdpSocket]. * * The passed `command` gets consumed. * @@ -1530,7 +1536,7 @@ bool sp_udp_send_command(UdpSocket */*notnull*/ connection, TypedCommand */*notnull*/ command); /** - * Sends a [Header] to the display using the [UdpConnection]. + * Sends a [Header] to the display using the [UdpSocket]. * * returns: true in case of success * @@ -1543,7 +1549,7 @@ bool sp_udp_send_command(UdpSocket */*notnull*/ connection, bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header); /** - * Sends a [Packet] to the display using the [UdpConnection]. + * Sends a [Packet] to the display using the [UdpSocket]. * * The passed `packet` gets consumed. * diff --git a/src/bitmap.rs b/src/bitmap.rs index b8b482f..9e7f4bd 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,7 +1,6 @@ -use crate::byte_slice::ByteSlice; use crate::{ heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, - SPBitVec, + byte_slice::ByteSlice, bitvec::SPBitVec, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, diff --git a/src/bitvec.rs b/src/bitvec.rs index b988df6..b7743aa 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -1,5 +1,5 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, + heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, byte_slice::ByteSlice, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index 2dd985a..496c53f 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, - ByteSlice, + byte_slice::ByteSlice, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -15,15 +15,15 @@ use std::ptr::NonNull; /// /// # Examples /// ```C -/// UdpConnection connection = sp_udp_open("127.0.0.1:2342"); +/// UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); /// if (connection == NULL) /// return 1; /// -/// BrightnessGrid grid = sp_brightness_grid_new(2, 2); +/// BrightnessGrid *grid = sp_brightness_grid_new(2, 2); /// sp_brightness_grid_set(grid, 0, 0, 0); /// sp_brightness_grid_set(grid, 1, 1, 10); /// -/// TypedCommand command = sp_command_char_brightness(grid); +/// TypedCommand *command = sp_command_char_brightness(grid); /// sp_udp_free(connection); /// ``` #[no_mangle] diff --git a/src/char_grid.rs b/src/char_grid.rs index e07e8db..42502a7 100644 --- a/src/char_grid.rs +++ b/src/char_grid.rs @@ -1,5 +1,5 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, + heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, byte_slice::ByteSlice, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 4413a3c..a45a155 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -23,6 +23,10 @@ pub unsafe extern "C" fn sp_cmd_bitmap_new( }) } +/// Move the provided [Bitmap] into a new [BitmapCommand], +/// leaving other fields as their default values. +/// +/// Rust equivalent: [`>::from`] #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( bitmap: NonNull, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 90422ff..e4b1d4a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,3 +3,9 @@ mod bitvec_command; mod brightness_grid_command; mod char_grid_command; mod cp437_grid_command; + +pub use bitmap_command::*; +pub use bitvec_command::*; +pub use brightness_grid_command::*; +pub use char_grid_command::*; +pub use cp437_grid_command::*; diff --git a/src/cp437_grid.rs b/src/cp437_grid.rs index 5a3daab..c39118e 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, - ByteSlice, + byte_slice::ByteSlice, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, diff --git a/src/lib.rs b/src/lib.rs index 924a729..c98318a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ //! #include "servicepoint.h" //! //! int main(void) { -//! UdpConnection *connection = sp_udp_open("172.23.42.29:2342"); +//! UdpSocket *connection = sp_udp_open("172.23.42.29:2342"); //! if (connection == NULL) //! return 1; //! @@ -25,28 +25,18 @@ //! } //! ``` -pub use crate::bitmap::*; -pub use crate::bitvec::*; -pub use crate::brightness_grid::*; -pub use crate::byte_slice::*; -pub use crate::char_grid::*; -pub use crate::cp437_grid::*; -pub use crate::packet::*; -pub use crate::typed_command::*; -pub use crate::udp::*; -pub use servicepoint::CommandCode; use std::ptr::NonNull; -mod bitmap; -mod bitvec; -mod brightness_grid; -mod byte_slice; -mod char_grid; -mod commands; -mod cp437_grid; -mod packet; -mod typed_command; -mod udp; +pub mod bitmap; +pub mod bitvec; +pub mod brightness_grid; +pub mod byte_slice; +pub mod char_grid; +pub mod commands; +pub mod cp437_grid; +pub mod packet; +pub mod typed_command; +pub mod udp; use std::time::Duration; diff --git a/src/packet.rs b/src/packet.rs index a454732..ea41597 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,5 +1,5 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice, + heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, byte_slice::ByteSlice, }; use servicepoint::{CommandCode, Header, Packet, TypedCommand}; use std::ptr::NonNull; diff --git a/src/udp.rs b/src/udp.rs index bc8acc3..b75f13d 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -4,14 +4,14 @@ use std::ffi::{c_char, CStr}; use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; use std::ptr::NonNull; -/// Creates a new instance of [UdpConnection]. +/// Creates a new instance of [UdpSocket]. /// /// returns: NULL if connection fails, or connected instance /// /// # Examples /// /// ```C -/// UdpConnection connection = sp_udp_open("172.23.42.29:2342"); +/// UdpSocket connection = sp_udp_open("172.23.42.29:2342"); /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` @@ -24,14 +24,14 @@ pub unsafe extern "C" fn sp_udp_open(host: NonNull) -> *mut UdpSocket { heap_move_ok(UdpSocket::bind_connect(host)) } -/// Creates a new instance of [UdpConnection]. +/// Creates a new instance of [UdpSocket]. /// /// returns: NULL if connection fails, or connected instance /// /// # Examples /// /// ```C -/// UdpConnection connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); +/// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` @@ -47,7 +47,7 @@ pub unsafe extern "C" fn sp_udp_open_ipv4( heap_move_ok(UdpSocket::bind_connect(addr)) } -/// Sends a [Packet] to the display using the [UdpConnection]. +/// Sends a [Packet] to the display using the [UdpSocket]. /// /// The passed `packet` gets consumed. /// @@ -61,7 +61,7 @@ pub unsafe extern "C" fn sp_udp_send_packet( unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() } -/// Sends a [TypedCommand] to the display using the [UdpConnection]. +/// Sends a [TypedCommand] to the display using the [UdpSocket]. /// /// The passed `command` gets consumed. /// @@ -81,7 +81,7 @@ pub unsafe extern "C" fn sp_udp_send_command( unsafe { connection.as_ref().send_command(command) }.is_some() } -/// Sends a [Header] to the display using the [UdpConnection]. +/// Sends a [Header] to the display using the [UdpSocket]. /// /// returns: true in case of success /// @@ -104,7 +104,7 @@ pub unsafe extern "C" fn sp_udp_send_header( .is_ok() } -/// Closes and deallocates a [UdpConnection]. +/// Closes and deallocates a [UdpSocket]. #[no_mangle] pub unsafe extern "C" fn sp_udp_free(connection: NonNull) { unsafe { heap_drop(connection) } From 85ccf4123c36918c93aa7d4bffb85826027c858e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 6 May 2025 22:54:54 +0200 Subject: [PATCH 06/76] expose tagged union instead of TypedCommand to C --- cbindgen.toml | 1 + include/servicepoint.h | 280 +++++++++++++-------- src/bitmap.rs | 6 +- src/bitvec.rs | 5 +- src/brightness_grid.rs | 8 +- src/char_grid.rs | 7 +- src/commands/bitmap_command.rs | 15 +- src/commands/bitvec_command.rs | 15 +- src/commands/brightness_grid_command.rs | 14 +- src/commands/cc_only_commands.rs | 54 +++++ src/commands/char_grid_command.rs | 15 +- src/commands/cp437_grid_command.rs | 15 +- src/commands/generic_command.rs | 281 ++++++++++++++++++++++ src/commands/global_brightness_command.rs | 53 ++++ src/commands/mod.rs | 4 + src/cp437_grid.rs | 8 +- src/lib.rs | 5 +- src/packet.rs | 19 +- src/typed_command.rs | 86 ------- src/udp.rs | 43 +++- 20 files changed, 657 insertions(+), 277 deletions(-) create mode 100644 src/commands/cc_only_commands.rs create mode 100644 src/commands/generic_command.rs create mode 100644 src/commands/global_brightness_command.rs delete mode 100644 src/typed_command.rs diff --git a/cbindgen.toml b/cbindgen.toml index 68723da..eca3164 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -37,6 +37,7 @@ exclude = ["BitVec"] [export.rename] "SpBitVec" = "BitVec" "SpByteSlice" = "ByteSlice" +"SpCommand" = "Command" [enum] rename_variants = "QualifiedScreamingSnakeCase" diff --git a/include/servicepoint.h b/include/servicepoint.h index 02f4676..404e291 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -125,6 +125,27 @@ enum CommandCode typedef uint16_t CommandCode; #endif // __cplusplus +enum CommandTag +#ifdef __cplusplus + : uint8_t +#endif // __cplusplus + { + COMMAND_TAG_INVALID = 0, + COMMAND_TAG_BITMAP, + COMMAND_TAG_BIT_VEC, + COMMAND_TAG_BRIGHTNESS_GRID, + COMMAND_TAG_CHAR_GRID, + COMMAND_TAG_CP437_GRID, + COMMAND_TAG_GLOBAL_BRIGHTNESS, + COMMAND_TAG_CLEAR, + COMMAND_TAG_HARD_RESET, + COMMAND_TAG_FADE_OUT, + COMMAND_TAG_BITMAP_LEGACY, +}; +#ifndef __cplusplus +typedef uint8_t CommandTag; +#endif // __cplusplus + /** * Specifies the kind of compression to use. Availability depends on features. * @@ -239,6 +260,23 @@ typedef struct Bitmap Bitmap; */ typedef struct BitmapCommand BitmapCommand; +/** + * Legacy command code, gets ignored by the real display. + * + * Might be useful as a noop package. + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * // this sends a packet that does nothing + * # #[allow(deprecated)] + * connection.send_command(BitmapLegacyCommand).unwrap(); + * ``` + */ +typedef struct BitmapLegacyCommand BitmapLegacyCommand; + /** * Set the brightness of individual tiles in a rectangular area of the display. */ @@ -260,6 +298,19 @@ typedef struct BrightnessGridCommand BrightnessGridCommand; */ typedef struct CharGridCommand CharGridCommand; +/** + * Set all pixels to the off state. Does not affect brightness. + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * connection.send_command(ClearCommand).unwrap(); + * ``` + */ +typedef struct ClearCommand ClearCommand; + /** * Show text on the screen. * @@ -292,6 +343,50 @@ typedef struct Cp437GridCommand Cp437GridCommand; */ typedef struct DisplayBitVec DisplayBitVec; +/** + *
Untested
+ * + * Slowly decrease brightness until off or something like that? + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * connection.send_command(FadeOutCommand).unwrap(); + * ``` + */ +typedef struct FadeOutCommand FadeOutCommand; + +/** + * Set the brightness of all tiles to the same value. + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * let command = GlobalBrightnessCommand { brightness: Brightness::MAX }; + * connection.send_command(command).unwrap(); + * ``` + */ +typedef struct GlobalBrightnessCommand GlobalBrightnessCommand; + +/** + * Kills the udp daemon on the display, which usually results in a restart. + * + * Please do not send this in your normal program flow. + * + * # Examples + * + * ```rust + * # use servicepoint::*; + * # let connection = FakeConnection; + * connection.send_command(HardResetCommand).unwrap(); + * ``` + */ +typedef struct HardResetCommand HardResetCommand; + /** * The raw packet. * @@ -313,14 +408,6 @@ typedef struct Packet Packet; */ typedef struct SPBitVec SPBitVec; -/** - * This enum contains all commands provided by the library. - * This is useful in case you want one data type for all kinds of commands without using `dyn`. - * - * Please look at the contained structs for documentation per command. - */ -typedef struct TypedCommand TypedCommand; - /** * This is a type only used by cbindgen to have a type for pointers. */ @@ -456,6 +543,31 @@ typedef size_t Offset; */ typedef ValueGrid_u8 Cp437Grid; +typedef union { + uint8_t *null; + BitmapCommand */*notnull*/ bitmap; + BitVecCommand */*notnull*/ bitvec; + BrightnessGridCommand */*notnull*/ brightness_grid; + CharGridCommand */*notnull*/ char_grid; + Cp437GridCommand */*notnull*/ cp437_grid; + GlobalBrightnessCommand */*notnull*/ global_brightness; + ClearCommand */*notnull*/ clear; + BitmapLegacyCommand */*notnull*/ bitmap_legacy; + HardResetCommand */*notnull*/ hard_reset; + FadeOutCommand */*notnull*/ fade_out; +} CommandUnion; + +typedef struct { + /** + * Specifies which kind of command struct is contained in `data` + */ + CommandTag tag; + /** + * The pointer to the command struct + */ + CommandUnion data; +} SPCommand; + /** * A raw header. * @@ -770,7 +882,7 @@ ByteSlice sp_bitvec_unsafe_data_ref(SPBitVec */*notnull*/ bit_vec); /** * Clones a [BrightnessGrid]. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ brightness_grid); +BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ grid); /** * Sets the value of all cells in the [BrightnessGrid]. @@ -906,7 +1018,7 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); /** * Clones a [CharGrid]. */ -CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ char_grid); +CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid); /** * Sets the value of all cells in the [CharGrid]. @@ -1039,8 +1151,6 @@ void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, Packet *sp_cmd_bitmap_into_packet(BitmapCommand */*notnull*/ command); -TypedCommand */*notnull*/ sp_cmd_bitmap_into_typed(BitmapCommand */*notnull*/ command); - /** * Sets a window of pixels to the specified values. * @@ -1080,8 +1190,6 @@ BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command); -TypedCommand */*notnull*/ sp_cmd_bitvec_into_typed(BitVecCommand */*notnull*/ command); - /** * Set pixel data starting at the pixel offset on screen. * @@ -1116,6 +1224,22 @@ void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, BinaryOperation operation); +GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(GlobalBrightnessCommand */*notnull*/ command); + +void sp_cmd_brightness_global_free(BitmapCommand */*notnull*/ command); + +Brightness *sp_cmd_brightness_global_get(GlobalBrightnessCommand */*notnull*/ command); + +Packet *sp_cmd_brightness_global_into_packet(GlobalBrightnessCommand */*notnull*/ command); + +GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); + +/** + * Moves the provided bitmap to be contained in the command. + */ +void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, + Brightness brightness); + BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(BrightnessGridCommand */*notnull*/ command); void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command); @@ -1130,8 +1254,6 @@ void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ comman Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command); -TypedCommand */*notnull*/ sp_cmd_brightness_grid_into_typed(BrightnessGridCommand */*notnull*/ command); - BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, size_t origin_x, size_t origin_y); @@ -1160,8 +1282,6 @@ void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, Packet *sp_cmd_char_grid_into_packet(CharGridCommand */*notnull*/ command); -TypedCommand */*notnull*/ sp_cmd_char_grid_into_typed(CharGridCommand */*notnull*/ command); - CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, size_t origin_x, size_t origin_y); @@ -1176,6 +1296,23 @@ void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +void sp_cmd_clear_free(ClearCommand */*notnull*/ command); + +/** + * Set all pixels to the off state. + * + * Does not affect brightness. + * + * Returns: a new [ClearCommand] instance. + * + * # Examples + * + * ```C + * sp_udp_send_command(connection, sp_cmd_clear()); + * ``` + */ +ClearCommand */*notnull*/ sp_cmd_clear_new(void); + Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(Cp437GridCommand */*notnull*/ command); void sp_cmd_cp437_grid_free(BitmapCommand */*notnull*/ command); @@ -1190,8 +1327,6 @@ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, Packet *sp_cmd_cp437_grid_into_packet(Cp437GridCommand */*notnull*/ command); -TypedCommand */*notnull*/ sp_cmd_cp437_grid_into_typed(Cp437GridCommand */*notnull*/ command); - Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, size_t origin_x, size_t origin_y); @@ -1206,70 +1341,24 @@ void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); -/** - * Set the brightness of individual tiles in a rectangular area of the display. - * - * The passed [BrightnessGrid] gets consumed. - * - * Returns: a new [servicepoint::Command::CharBrightness] instance. - */ -TypedCommand */*notnull*/ sp_command_brightness_grid(size_t x, - size_t y, - BrightnessGrid */*notnull*/ grid); - -/** - * Show UTF-8 encoded text on the screen. - * - * The passed [CharGrid] gets consumed. - * - * Returns: a new [servicepoint::CharGridCommand] instance. - */ -TypedCommand */*notnull*/ sp_command_char_grid(size_t x, - size_t y, - CharGrid */*notnull*/ grid); - -/** - * Set all pixels to the off state. - * - * Does not affect brightness. - * - * Returns: a new [servicepoint::Command::Clear] instance. - * - * # Examples - * - * ```C - * sp_udp_send_command(connection, sp_command_clear()); - * ``` - */ -TypedCommand */*notnull*/ sp_command_clear(void); - -/** - * Clones a [TypedCommand] instance. - * - * returns: new [TypedCommand] instance. - */ -TypedCommand */*notnull*/ sp_command_clone(TypedCommand */*notnull*/ command); - -/** - * Show codepage 437 encoded text on the screen. - * - * The passed [Cp437Grid] gets consumed. - * - * Returns: a new [servicepoint::Cp437GridCommand] instance. - */ -TypedCommand */*notnull*/ sp_command_cp437_grid(size_t x, - size_t y, - Cp437Grid */*notnull*/ grid); +void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command); /** * A yet-to-be-tested command. * - * Returns: a new [servicepoint::Command::FadeOut] instance. + * Returns: a new [FadeOutCommand] instance. */ -TypedCommand */*notnull*/ sp_command_fade_out(void); +FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); /** - * Deallocates a [TypedCommand]. + * Clones a [SPCommand] instance. + * + * returns: new [SPCommand] instance. + */ +SPCommand sp_cmd_generic_clone(SPCommand command); + +/** + * Deallocates a [SPCommand]. * * # Examples * @@ -1278,37 +1367,31 @@ TypedCommand */*notnull*/ sp_command_fade_out(void); * sp_command_free(c); * ``` */ -void sp_command_free(TypedCommand */*notnull*/ command); +void sp_cmd_generic_free(SPCommand command); /** - * Set the brightness of all tiles to the same value. + * Turns a [TypedCommand] into a [Packet]. + * The [TypedCommand] gets consumed. * - * Returns: a new [servicepoint::Command::Brightness] instance. + * Returns NULL in case of an error. */ -TypedCommand */*notnull*/ sp_command_global_brightness(Brightness brightness); +Packet *sp_cmd_generic_into_packet(SPCommand command); + +void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); /** * Kills the udp daemon on the display, which usually results in a restart. * * Please do not send this in your normal program flow. * - * Returns: a new [servicepoint::Command::HardReset] instance. + * Returns: a new [HardResetCommand] instance. */ -TypedCommand */*notnull*/ sp_command_hard_reset(void); - -/** - * Tries to turn a [Packet] into a [TypedCommand]. - * - * The packet is deallocated in the process. - * - * Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. - */ -TypedCommand *sp_command_try_from_packet(Packet */*notnull*/ packet); +HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** * Clones a [Cp437Grid]. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ cp437_grid); +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid); /** * Sets the value of all cells in the [Cp437Grid]. @@ -1419,14 +1502,6 @@ Packet */*notnull*/ sp_packet_clone(Packet */*notnull*/ packet); */ void sp_packet_free(Packet */*notnull*/ packet); -/** - * Turns a [TypedCommand] into a [Packet]. - * The [TypedCommand] gets consumed. - * - * Returns NULL in case of an error. - */ -Packet *sp_packet_from_command(TypedCommand */*notnull*/ command); - /** * Creates a raw [Packet] from parts. * @@ -1532,8 +1607,7 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_command(UdpSocket */*notnull*/ connection, - TypedCommand */*notnull*/ command); +bool sp_udp_send_command(UdpSocket */*notnull*/ connection, SPCommand command); /** * Sends a [Header] to the display using the [UdpSocket]. diff --git a/src/bitmap.rs b/src/bitmap.rs index 9e7f4bd..8341c59 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, - byte_slice::ByteSlice, bitvec::SPBitVec, + bitvec::SPBitVec, byte_slice::ByteSlice, heap_clone, heap_drop, + heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, @@ -83,7 +83,7 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( pub unsafe extern "C" fn sp_bitmap_clone( bitmap: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { bitmap.as_ref().clone() }) + unsafe { heap_clone(bitmap) } } /// Deallocates a [Bitmap]. diff --git a/src/bitvec.rs b/src/bitvec.rs index b7743aa..3fa7412 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -1,5 +1,6 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, byte_slice::ByteSlice, + byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + heap_move_ok, heap_remove, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -52,7 +53,7 @@ pub unsafe extern "C" fn sp_bitvec_load(data: ByteSlice) -> NonNull { pub unsafe extern "C" fn sp_bitvec_clone( bit_vec: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { bit_vec.as_ref().clone() }) + unsafe { heap_clone(bit_vec) } } /// Deallocates a [SPBitVec]. diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index 496c53f..dc3b2f7 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, - byte_slice::ByteSlice, + byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + heap_move_ok, heap_move_some, heap_remove, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -55,9 +55,9 @@ pub unsafe extern "C" fn sp_brightness_grid_load( /// Clones a [BrightnessGrid]. #[no_mangle] pub unsafe extern "C" fn sp_brightness_grid_clone( - brightness_grid: NonNull, + grid: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { brightness_grid.as_ref().clone() }) + unsafe { heap_clone(grid) } } /// Deallocates a [BrightnessGrid]. diff --git a/src/char_grid.rs b/src/char_grid.rs index 42502a7..62aba87 100644 --- a/src/char_grid.rs +++ b/src/char_grid.rs @@ -1,5 +1,6 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, byte_slice::ByteSlice, + byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + heap_move_ok, heap_remove, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -40,9 +41,9 @@ pub unsafe extern "C" fn sp_char_grid_load( /// Clones a [CharGrid]. #[no_mangle] pub unsafe extern "C" fn sp_char_grid_clone( - char_grid: NonNull, + grid: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { char_grid.as_ref().clone() }) + unsafe { heap_clone(grid) } } /// Deallocates a [CharGrid]. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index a45a155..b2ff3d8 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,7 +1,7 @@ -use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; -use servicepoint::{ - Bitmap, BitmapCommand, CompressionCode, Origin, Packet, TypedCommand, +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; +use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use std::ptr::NonNull; /// Sets a window of pixels to the specified values. @@ -34,13 +34,6 @@ pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) } -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_into_typed( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) -} - #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( command: NonNull, @@ -52,7 +45,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( pub unsafe extern "C" fn sp_cmd_bitmap_clone( command: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { command.as_ref().clone() }) + unsafe { heap_clone(command) } } #[no_mangle] diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 5795017..bfe2d33 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,7 +1,9 @@ -use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +}; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, - Packet, TypedCommand, + Packet, }; use std::ptr::NonNull; @@ -32,13 +34,6 @@ pub unsafe extern "C" fn sp_cmd_bitvec_new( }) } -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_into_typed( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) -} - #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_into_packet( command: NonNull, @@ -50,7 +45,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_into_packet( pub unsafe extern "C" fn sp_cmd_bitvec_clone( command: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { command.as_ref().clone() }) + unsafe { heap_clone(command) } } #[no_mangle] diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 18a2836..bd975d6 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,7 +1,8 @@ -use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +}; use servicepoint::{ BitmapCommand, BrightnessGrid, BrightnessGridCommand, Origin, Packet, - TypedCommand, }; use std::ptr::NonNull; @@ -24,13 +25,6 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid( heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_into_typed( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) -} - #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( command: NonNull, @@ -42,7 +36,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( pub unsafe extern "C" fn sp_cmd_brightness_grid_clone( command: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { command.as_ref().clone() }) + unsafe { heap_clone(command) } } #[no_mangle] diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs new file mode 100644 index 0000000..751a6ab --- /dev/null +++ b/src/commands/cc_only_commands.rs @@ -0,0 +1,54 @@ +use crate::{heap_drop, heap_move_nonnull}; +use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; +use std::ptr::NonNull; + +/// Set all pixels to the off state. +/// +/// Does not affect brightness. +/// +/// Returns: a new [ClearCommand] instance. +/// +/// # Examples +/// +/// ```C +/// sp_udp_send_command(connection, sp_cmd_clear()); +/// ``` +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull { + heap_move_nonnull(ClearCommand) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull) { + unsafe { heap_drop(command) } +} + +/// Kills the udp daemon on the display, which usually results in a restart. +/// +/// Please do not send this in your normal program flow. +/// +/// Returns: a new [HardResetCommand] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull { + heap_move_nonnull(HardResetCommand) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_hard_reset_free( + command: NonNull, +) { + unsafe { heap_drop(command) } +} + +/// A yet-to-be-tested command. +/// +/// Returns: a new [FadeOutCommand] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull { + heap_move_nonnull(FadeOutCommand) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull) { + unsafe { heap_drop(command) } +} diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 933e4cd..bc078cd 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,7 +1,7 @@ -use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; -use servicepoint::{ - BitmapCommand, CharGrid, CharGridCommand, Origin, Packet, TypedCommand, +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; +use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet}; use std::ptr::NonNull; #[no_mangle] @@ -23,13 +23,6 @@ pub unsafe extern "C" fn sp_cmd_char_grid_from_grid( heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_into_typed( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) -} - #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_into_packet( command: NonNull, @@ -41,7 +34,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_into_packet( pub unsafe extern "C" fn sp_cmd_char_grid_clone( command: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { command.as_ref().clone() }) + unsafe { heap_clone(command) } } #[no_mangle] diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 0f3d9e2..1e1598a 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,6 +1,8 @@ -use crate::{heap_drop, heap_move_nonnull, heap_move_ok, heap_remove}; +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +}; use servicepoint::{ - BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, TypedCommand, + BitmapCommand, Cp437Grid, Cp437GridCommand, Origin, Packet, }; use std::ptr::NonNull; @@ -23,13 +25,6 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid( heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_into_typed( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) -} - #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet( command: NonNull, @@ -41,7 +36,7 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet( pub unsafe extern "C" fn sp_cmd_cp437_grid_clone( command: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { command.as_ref().clone() }) + unsafe { heap_clone(command) } } #[no_mangle] diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs new file mode 100644 index 0000000..5066022 --- /dev/null +++ b/src/commands/generic_command.rs @@ -0,0 +1,281 @@ +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +}; +use servicepoint::{ + BitVecCommand, BitmapCommand, BitmapLegacyCommand, BrightnessGridCommand, + CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, + GlobalBrightnessCommand, HardResetCommand, Packet, TypedCommand, +}; +use std::ptr::{null_mut, NonNull}; + +#[repr(C)] +pub union CommandUnion { + pub null: *mut u8, + pub bitmap: NonNull, + pub bitvec: NonNull, + pub brightness_grid: NonNull, + pub char_grid: NonNull, + pub cp437_grid: NonNull, + pub global_brightness: NonNull, + pub clear: NonNull, + #[allow(deprecated)] + pub bitmap_legacy: NonNull, + pub hard_reset: NonNull, + pub fade_out: NonNull, +} + +#[repr(u8)] +pub enum CommandTag { + Invalid = 0, + Bitmap, + BitVec, + BrightnessGrid, + CharGrid, + Cp437Grid, + GlobalBrightness, + Clear, + HardReset, + FadeOut, + BitmapLegacy, +} + +#[repr(C)] +pub struct SPCommand { + /// Specifies which kind of command struct is contained in `data` + pub tag: CommandTag, + /// The pointer to the command struct + pub data: CommandUnion, +} + +impl SPCommand { + const INVALID: SPCommand = SPCommand { + tag: CommandTag::Invalid, + data: CommandUnion { null: null_mut() }, + }; +} + +/// Tries to turn a [Packet] into a [TypedCommand]. +/// +/// The packet is deallocated in the process. +/// +/// Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. +/// #[no_mangle] +pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( + packet: NonNull, +) -> *mut SPCommand { + let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; + heap_move_ok(servicepoint::TypedCommand::try_from(packet).map(|value| { + match value { + TypedCommand::Clear(clear) => SPCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_move_nonnull(clear), + }, + }, + TypedCommand::CharGrid(char_grid) => SPCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_move_nonnull(char_grid), + }, + }, + TypedCommand::Cp437Grid(cp437_grid) => SPCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_move_nonnull(cp437_grid), + }, + }, + TypedCommand::Bitmap(bitmap) => SPCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_move_nonnull(bitmap), + }, + }, + TypedCommand::Brightness(global_brightness) => SPCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_move_nonnull(global_brightness), + }, + }, + TypedCommand::BrightnessGrid(brightness_grid) => SPCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_move_nonnull(brightness_grid), + }, + }, + TypedCommand::BitVec(bitvec) => SPCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bitvec: heap_move_nonnull(bitvec), + }, + }, + TypedCommand::HardReset(hard_reset) => SPCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_move_nonnull(hard_reset), + }, + }, + TypedCommand::FadeOut(fade_out) => SPCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_move_nonnull(fade_out), + }, + }, + #[allow(deprecated)] + TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_move_nonnull(bitmap_legacy), + }, + }, + } + })) +} + +/// Clones a [SPCommand] instance. +/// +/// returns: new [SPCommand] instance. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { + unsafe { + match command.tag { + CommandTag::Clear => SPCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_clone(command.data.clear), + }, + }, + CommandTag::CharGrid => SPCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_clone(command.data.char_grid), + }, + }, + CommandTag::Cp437Grid => SPCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_clone(command.data.cp437_grid), + }, + }, + CommandTag::Bitmap => SPCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_clone(command.data.bitmap), + }, + }, + CommandTag::GlobalBrightness => SPCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_clone( + command.data.global_brightness, + ), + }, + }, + CommandTag::BrightnessGrid => SPCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_clone(command.data.brightness_grid), + }, + }, + CommandTag::BitVec => SPCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bitvec: heap_clone(command.data.bitvec), + }, + }, + CommandTag::HardReset => SPCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_clone(command.data.hard_reset), + }, + }, + CommandTag::FadeOut => SPCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_clone(command.data.fade_out), + }, + }, + #[allow(deprecated)] + CommandTag::BitmapLegacy => SPCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_clone(command.data.bitmap_legacy), + }, + }, + CommandTag::Invalid => SPCommand::INVALID, + } + } +} + +/// Deallocates a [SPCommand]. +/// +/// # Examples +/// +/// ```C +/// TypedCommand c = sp_command_clear(); +/// sp_command_free(c); +/// ``` +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) { + unsafe { + match command.tag { + CommandTag::Invalid => return, + CommandTag::Bitmap => heap_drop(command.data.bitmap), + CommandTag::BitVec => heap_drop(command.data.bitvec), + CommandTag::BrightnessGrid => { + heap_drop(command.data.brightness_grid) + } + CommandTag::CharGrid => heap_drop(command.data.char_grid), + CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid), + CommandTag::GlobalBrightness => { + heap_drop(command.data.global_brightness) + } + CommandTag::Clear => heap_drop(command.data.clear), + CommandTag::HardReset => heap_drop(command.data.hard_reset), + CommandTag::FadeOut => heap_drop(command.data.fade_out), + CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy), + } + } +} + +/// Turns a [TypedCommand] into a [Packet]. +/// The [TypedCommand] gets consumed. +/// +/// Returns NULL in case of an error. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_generic_into_packet( + command: SPCommand, +) -> *mut Packet { + match command.tag { + CommandTag::Invalid => null_mut(), + CommandTag::Bitmap => { + heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) + } + CommandTag::BitVec => { + heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() }) + } + CommandTag::BrightnessGrid => heap_move_ok(unsafe { + heap_remove(command.data.brightness_grid).try_into() + }), + CommandTag::CharGrid => heap_move_ok(unsafe { + heap_remove(command.data.char_grid).try_into() + }), + CommandTag::Cp437Grid => heap_move_ok(unsafe { + heap_remove(command.data.cp437_grid).try_into() + }), + CommandTag::GlobalBrightness => heap_move_ok(unsafe { + heap_remove(command.data.global_brightness).try_into() + }), + CommandTag::Clear => { + heap_move_ok(unsafe { heap_remove(command.data.clear).try_into() }) + } + CommandTag::HardReset => heap_move_ok(unsafe { + heap_remove(command.data.hard_reset).try_into() + }), + CommandTag::FadeOut => heap_move_ok(unsafe { + heap_remove(command.data.fade_out).try_into() + }), + CommandTag::BitmapLegacy => heap_move_ok(unsafe { + heap_remove(command.data.bitmap_legacy).try_into() + }), + } +} diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs new file mode 100644 index 0000000..29e7c19 --- /dev/null +++ b/src/commands/global_brightness_command.rs @@ -0,0 +1,53 @@ +use crate::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, +}; +use servicepoint::{ + BitmapCommand, Brightness, GlobalBrightnessCommand, Packet, +}; +use std::ptr::NonNull; + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_global_new( + brightness: Brightness, +) -> NonNull { + heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( + command: NonNull, +) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_global_clone( + command: NonNull, +) -> NonNull { + unsafe { heap_clone(command) } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_global_free( + command: NonNull, +) { + unsafe { heap_drop(command) } +} + +/// Moves the provided bitmap to be contained in the command. +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_global_set( + mut command: NonNull, + brightness: Brightness, +) { + unsafe { + command.as_mut().brightness = brightness; + } +} + +#[no_mangle] +pub unsafe extern "C" fn sp_cmd_brightness_global_get( + mut command: NonNull, +) -> *mut Brightness { + &mut unsafe { command.as_mut() }.brightness +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e4b1d4a..3cbb99a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,11 +1,15 @@ mod bitmap_command; mod bitvec_command; mod brightness_grid_command; +mod cc_only_commands; mod char_grid_command; mod cp437_grid_command; +mod generic_command; +mod global_brightness_command; pub use bitmap_command::*; pub use bitvec_command::*; pub use brightness_grid_command::*; pub use char_grid_command::*; pub use cp437_grid_command::*; +pub use generic_command::*; diff --git a/src/cp437_grid.rs b/src/cp437_grid.rs index c39118e..b4d449e 100644 --- a/src/cp437_grid.rs +++ b/src/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, - byte_slice::ByteSlice, + byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + heap_move_ok, heap_move_some, heap_remove, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -32,9 +32,9 @@ pub unsafe extern "C" fn sp_cp437_grid_load( /// Clones a [Cp437Grid]. #[no_mangle] pub unsafe extern "C" fn sp_cp437_grid_clone( - cp437_grid: NonNull, + grid: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { cp437_grid.as_ref().clone() }) + unsafe { heap_clone(grid) } } /// Deallocates a [Cp437Grid]. diff --git a/src/lib.rs b/src/lib.rs index c98318a..de22866 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,6 @@ pub mod char_grid; pub mod commands; pub mod cp437_grid; pub mod packet; -pub mod typed_command; pub mod udp; use std::time::Duration; @@ -67,6 +66,10 @@ pub(crate) unsafe fn heap_remove(x: NonNull) -> T { unsafe { *Box::from_raw(x.as_ptr()) } } +unsafe fn heap_clone(source: NonNull) -> NonNull { + heap_move_nonnull(unsafe { source.as_ref().clone() }) +} + /// This is a type only used by cbindgen to have a type for pointers. pub struct UdpSocket; diff --git a/src/packet.rs b/src/packet.rs index ea41597..16509be 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,21 +1,10 @@ use crate::{ - heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, byte_slice::ByteSlice, + byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + heap_move_ok, }; -use servicepoint::{CommandCode, Header, Packet, TypedCommand}; +use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -/// Turns a [TypedCommand] into a [Packet]. -/// The [TypedCommand] gets consumed. -/// -/// Returns NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_from_command( - command: NonNull, -) -> *mut Packet { - let command = unsafe { heap_remove(command) }; - heap_move_ok(command.try_into()) -} - /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise @@ -94,7 +83,7 @@ pub unsafe extern "C" fn sp_packet_serialize_to( pub unsafe extern "C" fn sp_packet_clone( packet: NonNull, ) -> NonNull { - heap_move_nonnull(unsafe { packet.as_ref().clone() }) + unsafe { heap_clone(packet) } } /// Deallocates a [Packet]. diff --git a/src/typed_command.rs b/src/typed_command.rs deleted file mode 100644 index 26c33f1..0000000 --- a/src/typed_command.rs +++ /dev/null @@ -1,86 +0,0 @@ -use crate::{heap_drop, heap_move_nonnull, heap_move_ok}; -use servicepoint::{ - Brightness, GlobalBrightnessCommand, - Packet, TypedCommand, -}; -use std::ptr::NonNull; - -/// Tries to turn a [Packet] into a [TypedCommand]. -/// -/// The packet is deallocated in the process. -/// -/// Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. -#[no_mangle] -pub unsafe extern "C" fn sp_command_try_from_packet( - packet: NonNull, -) -> *mut TypedCommand { - let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; - heap_move_ok(servicepoint::TypedCommand::try_from(packet)) -} - -/// Clones a [TypedCommand] instance. -/// -/// returns: new [TypedCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_clone( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { command.as_ref().clone() }) -} - -/// Set all pixels to the off state. -/// -/// Does not affect brightness. -/// -/// Returns: a new [servicepoint::Command::Clear] instance. -/// -/// # Examples -/// -/// ```C -/// sp_udp_send_command(connection, sp_command_clear()); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_command_clear() -> NonNull { - heap_move_nonnull(servicepoint::ClearCommand.into()) -} - -/// Kills the udp daemon on the display, which usually results in a restart. -/// -/// Please do not send this in your normal program flow. -/// -/// Returns: a new [servicepoint::Command::HardReset] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull { - heap_move_nonnull(servicepoint::HardResetCommand.into()) -} - -/// A yet-to-be-tested command. -/// -/// Returns: a new [servicepoint::Command::FadeOut] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_fade_out() -> NonNull { - heap_move_nonnull(servicepoint::FadeOutCommand.into()) -} - -/// Set the brightness of all tiles to the same value. -/// -/// Returns: a new [servicepoint::Command::Brightness] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_command_global_brightness( - brightness: Brightness, -) -> NonNull { - heap_move_nonnull(GlobalBrightnessCommand::from(brightness).into()) -} - -/// Deallocates a [TypedCommand]. -/// -/// # Examples -/// -/// ```C -/// TypedCommand c = sp_command_clear(); -/// sp_command_free(c); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_command_free(command: NonNull) { - unsafe { heap_drop(command) } -} diff --git a/src/udp.rs b/src/udp.rs index b75f13d..7b659cf 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,5 +1,6 @@ +use crate::commands::{CommandTag, SPCommand}; use crate::{heap_drop, heap_move_ok, heap_remove}; -use servicepoint::{Header, Packet, TypedCommand, UdpSocketExt}; +use servicepoint::{Header, Packet, UdpSocketExt}; use std::ffi::{c_char, CStr}; use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; use std::ptr::NonNull; @@ -75,10 +76,44 @@ pub unsafe extern "C" fn sp_udp_send_packet( #[no_mangle] pub unsafe extern "C" fn sp_udp_send_command( connection: NonNull, - command: NonNull, + command: SPCommand, ) -> bool { - let command = unsafe { heap_remove(command) }; - unsafe { connection.as_ref().send_command(command) }.is_some() + unsafe { + match command.tag { + CommandTag::Invalid => return false, + CommandTag::Bitmap => connection + .as_ref() + .send_command(heap_remove(command.data.bitmap)), + CommandTag::BitVec => connection + .as_ref() + .send_command(heap_remove(command.data.bitvec)), + CommandTag::BrightnessGrid => connection + .as_ref() + .send_command(heap_remove(command.data.brightness_grid)), + CommandTag::CharGrid => connection + .as_ref() + .send_command(heap_remove(command.data.char_grid)), + CommandTag::Cp437Grid => connection + .as_ref() + .send_command(heap_remove(command.data.cp437_grid)), + CommandTag::GlobalBrightness => connection + .as_ref() + .send_command(heap_remove(command.data.global_brightness)), + CommandTag::Clear => connection + .as_ref() + .send_command(heap_remove(command.data.clear)), + CommandTag::HardReset => connection + .as_ref() + .send_command(heap_remove(command.data.hard_reset)), + CommandTag::FadeOut => connection + .as_ref() + .send_command(heap_remove(command.data.fade_out)), + CommandTag::BitmapLegacy => connection + .as_ref() + .send_command(heap_remove(command.data.bitmap_legacy)), + } + } + .is_some() } /// Sends a [Header] to the display using the [UdpSocket]. From a4bacd53a2b5c6df7cb80d755044f252585461ea Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 6 May 2025 23:05:06 +0200 Subject: [PATCH 07/76] remove SPBitVec wrapper type --- cbindgen.toml | 1 - include/servicepoint.h | 40 ++++++++------------- src/bitmap.rs | 15 ++++---- src/bitvec.rs | 62 +++++++++++++-------------------- src/commands/generic_command.rs | 4 +-- 5 files changed, 48 insertions(+), 74 deletions(-) diff --git a/cbindgen.toml b/cbindgen.toml index eca3164..4aabb7e 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -35,7 +35,6 @@ include = [] exclude = ["BitVec"] [export.rename] -"SpBitVec" = "BitVec" "SpByteSlice" = "ByteSlice" "SpCommand" = "Command" diff --git a/include/servicepoint.h b/include/servicepoint.h index 404e291..ec722f5 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -396,18 +396,6 @@ typedef struct HardResetCommand HardResetCommand; */ typedef struct Packet Packet; -/** - * A vector of bits - * - * # Examples - * ```C - * SPBitVec vec = sp_bitvec_new(8); - * sp_bitvec_set(vec, 5, true); - * sp_bitvec_free(vec); - * ``` - */ -typedef struct SPBitVec SPBitVec; - /** * This is a type only used by cbindgen to have a type for pointers. */ @@ -634,7 +622,7 @@ void sp_bitmap_free(Bitmap */*notnull*/ bitmap); * * Returns NULL in case of error. */ -Bitmap *sp_bitmap_from_bitvec(size_t width, SPBitVec */*notnull*/ bitvec); +Bitmap *sp_bitmap_from_bitvec(size_t width, DisplayBitVec */*notnull*/ bitvec); /** * Gets the current value at the specified position in the [Bitmap]. @@ -662,7 +650,7 @@ size_t sp_bitmap_height(Bitmap */*notnull*/ bitmap); /** * Consumes the Bitmap and returns the contained BitVec */ -SPBitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); +DisplayBitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); /** * Creates a [BitmapCommand] and immediately turns that into a [Packet]. @@ -768,7 +756,7 @@ size_t sp_bitmap_width(Bitmap */*notnull*/ bitmap); /** * Clones a [SPBitVec]. */ -SPBitVec */*notnull*/ sp_bitvec_clone(SPBitVec */*notnull*/ bit_vec); +DisplayBitVec */*notnull*/ sp_bitvec_clone(DisplayBitVec */*notnull*/ bit_vec); /** * Sets the value of all bits in the [SPBitVec]. @@ -778,12 +766,12 @@ SPBitVec */*notnull*/ sp_bitvec_clone(SPBitVec */*notnull*/ bit_vec); * - `bit_vec`: instance to write to * - `value`: the value to set all bits to */ -void sp_bitvec_fill(SPBitVec */*notnull*/ bit_vec, bool value); +void sp_bitvec_fill(DisplayBitVec */*notnull*/ bit_vec, bool value); /** * Deallocates a [SPBitVec]. */ -void sp_bitvec_free(SPBitVec */*notnull*/ bit_vec); +void sp_bitvec_free(DisplayBitVec */*notnull*/ bit_vec); /** * Gets the value of a bit from the [SPBitVec]. @@ -799,7 +787,7 @@ void sp_bitvec_free(SPBitVec */*notnull*/ bit_vec); * * - when accessing `index` out of bounds */ -bool sp_bitvec_get(SPBitVec */*notnull*/ bit_vec, size_t index); +bool sp_bitvec_get(DisplayBitVec */*notnull*/ bit_vec, size_t index); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -808,7 +796,7 @@ bool sp_bitvec_get(SPBitVec */*notnull*/ bit_vec, size_t index); * * Returns NULL in case of an error. */ -Packet *sp_bitvec_into_packet(SPBitVec */*notnull*/ bitvec, +Packet *sp_bitvec_into_packet(DisplayBitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -820,7 +808,7 @@ Packet *sp_bitvec_into_packet(SPBitVec */*notnull*/ bitvec, * * - `bit_vec`: instance to write to */ -bool sp_bitvec_is_empty(SPBitVec */*notnull*/ bit_vec); +bool sp_bitvec_is_empty(DisplayBitVec */*notnull*/ bit_vec); /** * Gets the length of the [SPBitVec] in bits. @@ -829,14 +817,14 @@ bool sp_bitvec_is_empty(SPBitVec */*notnull*/ bit_vec); * * - `bit_vec`: instance to write to */ -size_t sp_bitvec_len(SPBitVec */*notnull*/ bit_vec); +size_t sp_bitvec_len(DisplayBitVec */*notnull*/ bit_vec); /** * Interpret the data as a series of bits and load then into a new [SPBitVec] instance. * * returns: [SPBitVec] instance containing data. */ -SPBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); +DisplayBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); /** * Creates a new [SPBitVec] instance. @@ -851,7 +839,7 @@ SPBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); * * - when `size` is not divisible by 8. */ -SPBitVec */*notnull*/ sp_bitvec_new(size_t size); +DisplayBitVec */*notnull*/ sp_bitvec_new(size_t size); /** * Sets the value of a bit in the [SPBitVec]. @@ -866,7 +854,9 @@ SPBitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds */ -void sp_bitvec_set(SPBitVec */*notnull*/ bit_vec, size_t index, bool value); +void sp_bitvec_set(DisplayBitVec */*notnull*/ bit_vec, + size_t index, + bool value); /** * Gets an unsafe reference to the data of the [SPBitVec] instance. @@ -877,7 +867,7 @@ void sp_bitvec_set(SPBitVec */*notnull*/ bit_vec, size_t index, bool value); * * - `bit_vec`: instance to write to */ -ByteSlice sp_bitvec_unsafe_data_ref(SPBitVec */*notnull*/ bit_vec); +ByteSlice sp_bitvec_unsafe_data_ref(DisplayBitVec */*notnull*/ bit_vec); /** * Clones a [BrightnessGrid]. diff --git a/src/bitmap.rs b/src/bitmap.rs index 8341c59..3393671 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -1,9 +1,6 @@ -use crate::{ - bitvec::SPBitVec, byte_slice::ByteSlice, heap_clone, heap_drop, - heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, -}; +use crate::{byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}; use servicepoint::{ - Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, + Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, DisplayBitVec }; use std::ptr::NonNull; @@ -72,10 +69,10 @@ pub unsafe extern "C" fn sp_bitmap_load( #[no_mangle] pub unsafe extern "C" fn sp_bitmap_from_bitvec( width: usize, - bitvec: NonNull, + bitvec: NonNull, ) -> *mut Bitmap { let bitvec = unsafe { heap_remove(bitvec) }; - heap_move_ok(Bitmap::from_bitvec(width, bitvec.0)) + heap_move_ok(Bitmap::from_bitvec(width, bitvec)) } /// Clones a [Bitmap]. @@ -187,9 +184,9 @@ pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref( #[no_mangle] pub unsafe extern "C" fn sp_bitmap_into_bitvec( bitmap: NonNull, -) -> NonNull { +) -> NonNull { let bitmap = unsafe { heap_remove(bitmap) }; - heap_move_nonnull(SPBitVec(bitmap.into())) + heap_move_nonnull(bitmap.into()) } /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. diff --git a/src/bitvec.rs b/src/bitvec.rs index 3fa7412..1ba49a7 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -7,22 +7,6 @@ use servicepoint::{ }; use std::ptr::NonNull; -/// A vector of bits -/// -/// # Examples -/// ```C -/// SPBitVec vec = sp_bitvec_new(8); -/// sp_bitvec_set(vec, 5, true); -/// sp_bitvec_free(vec); -/// ``` -pub struct SPBitVec(pub(crate) DisplayBitVec); - -impl Clone for SPBitVec { - fn clone(&self) -> Self { - SPBitVec(self.0.clone()) - } -} - /// Creates a new [SPBitVec] instance. /// /// # Arguments @@ -35,30 +19,32 @@ impl Clone for SPBitVec { /// /// - when `size` is not divisible by 8. #[no_mangle] -pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull { - heap_move_nonnull(SPBitVec(DisplayBitVec::repeat(false, size))) +pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull { + heap_move_nonnull(DisplayBitVec::repeat(false, size)) } /// Interpret the data as a series of bits and load then into a new [SPBitVec] instance. /// /// returns: [SPBitVec] instance containing data. #[no_mangle] -pub unsafe extern "C" fn sp_bitvec_load(data: ByteSlice) -> NonNull { +pub unsafe extern "C" fn sp_bitvec_load( + data: ByteSlice, +) -> NonNull { let data = unsafe { data.as_slice() }; - heap_move_nonnull(SPBitVec(DisplayBitVec::from_slice(data))) + heap_move_nonnull(DisplayBitVec::from_slice(data)) } /// Clones a [SPBitVec]. #[no_mangle] pub unsafe extern "C" fn sp_bitvec_clone( - bit_vec: NonNull, -) -> NonNull { + bit_vec: NonNull, +) -> NonNull { unsafe { heap_clone(bit_vec) } } /// Deallocates a [SPBitVec]. #[no_mangle] -pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull) { +pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull) { unsafe { heap_drop(bit_vec) } } @@ -76,10 +62,10 @@ pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull) { /// - when accessing `index` out of bounds #[no_mangle] pub unsafe extern "C" fn sp_bitvec_get( - bit_vec: NonNull, + bit_vec: NonNull, index: usize, ) -> bool { - unsafe { *bit_vec.as_ref().0.get(index).unwrap() } + unsafe { *bit_vec.as_ref().get(index).unwrap() } } /// Sets the value of a bit in the [SPBitVec]. @@ -95,11 +81,11 @@ pub unsafe extern "C" fn sp_bitvec_get( /// - when accessing `index` out of bounds #[no_mangle] pub unsafe extern "C" fn sp_bitvec_set( - bit_vec: NonNull, + bit_vec: NonNull, index: usize, value: bool, ) { - unsafe { (*bit_vec.as_ptr()).0.set(index, value) } + unsafe { (*bit_vec.as_ptr()).set(index, value) } } /// Sets the value of all bits in the [SPBitVec]. @@ -110,10 +96,10 @@ pub unsafe extern "C" fn sp_bitvec_set( /// - `value`: the value to set all bits to #[no_mangle] pub unsafe extern "C" fn sp_bitvec_fill( - bit_vec: NonNull, + bit_vec: NonNull, value: bool, ) { - unsafe { (*bit_vec.as_ptr()).0.fill(value) } + unsafe { (*bit_vec.as_ptr()).fill(value) } } /// Gets the length of the [SPBitVec] in bits. @@ -122,8 +108,10 @@ pub unsafe extern "C" fn sp_bitvec_fill( /// /// - `bit_vec`: instance to write to #[no_mangle] -pub unsafe extern "C" fn sp_bitvec_len(bit_vec: NonNull) -> usize { - unsafe { bit_vec.as_ref().0.len() } +pub unsafe extern "C" fn sp_bitvec_len( + bit_vec: NonNull, +) -> usize { + unsafe { bit_vec.as_ref().len() } } /// Returns true if length is 0. @@ -133,9 +121,9 @@ pub unsafe extern "C" fn sp_bitvec_len(bit_vec: NonNull) -> usize { /// - `bit_vec`: instance to write to #[no_mangle] pub unsafe extern "C" fn sp_bitvec_is_empty( - bit_vec: NonNull, + bit_vec: NonNull, ) -> bool { - unsafe { bit_vec.as_ref().0.is_empty() } + unsafe { bit_vec.as_ref().is_empty() } } /// Gets an unsafe reference to the data of the [SPBitVec] instance. @@ -147,9 +135,9 @@ pub unsafe extern "C" fn sp_bitvec_is_empty( /// - `bit_vec`: instance to write to #[no_mangle] pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref( - bit_vec: NonNull, + bit_vec: NonNull, ) -> ByteSlice { - unsafe { ByteSlice::from_slice((*bit_vec.as_ptr()).0.as_raw_mut_slice()) } + unsafe { ByteSlice::from_slice((*bit_vec.as_ptr()).as_raw_mut_slice()) } } /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -159,12 +147,12 @@ pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref( /// Returns NULL in case of an error. #[no_mangle] pub unsafe extern "C" fn sp_bitvec_into_packet( - bitvec: NonNull, + bitvec: NonNull, offset: usize, operation: BinaryOperation, compression: CompressionCode, ) -> *mut Packet { - let bitvec = unsafe { heap_remove(bitvec) }.0; + let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Packet::try_from(BitVecCommand { bitvec, offset, diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 5066022..e3da8fd 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -2,7 +2,7 @@ use crate::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ - BitVecCommand, BitmapCommand, BitmapLegacyCommand, BrightnessGridCommand, + BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, HardResetCommand, Packet, TypedCommand, }; @@ -19,7 +19,7 @@ pub union CommandUnion { pub global_brightness: NonNull, pub clear: NonNull, #[allow(deprecated)] - pub bitmap_legacy: NonNull, + pub bitmap_legacy: NonNull, pub hard_reset: NonNull, pub fade_out: NonNull, } From e7cad5b5a3a79e6e1cf6fd4dcfdbbbf03fecdddd Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 7 May 2025 08:29:16 +0200 Subject: [PATCH 08/76] doc changes --- include/servicepoint.h | 66 +++++++++++++++++++++++------- src/bitvec.rs | 24 +++++------ src/commands/bitmap_command.rs | 2 +- src/commands/cp437_grid_command.rs | 11 ++++- src/commands/generic_command.rs | 13 +++++- src/udp.rs | 2 +- 6 files changed, 87 insertions(+), 31 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index ec722f5..131fe41 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -125,6 +125,11 @@ enum CommandCode typedef uint16_t CommandCode; #endif // __cplusplus +/** + * Specifies the kind of command struct. + * + * This is _not_ equivalent to the [servicepoint::CommandCode]s. + */ enum CommandTag #ifdef __cplusplus : uint8_t @@ -531,6 +536,9 @@ typedef size_t Offset; */ typedef ValueGrid_u8 Cp437Grid; +/** + * Pointer to one of the available command structs. + */ typedef union { uint8_t *null; BitmapCommand */*notnull*/ bitmap; @@ -545,6 +553,13 @@ typedef union { FadeOutCommand */*notnull*/ fade_out; } CommandUnion; +/** + * This struct represents a pointer to one of the possible command structs. + * + * Only ever access `data` with the correct data type as specified by `tag`! + * + * Rust equivalent: [TypedCommand]. + */ typedef struct { /** * Specifies which kind of command struct is contained in `data` @@ -754,12 +769,12 @@ ByteSlice sp_bitmap_unsafe_data_ref(Bitmap */*notnull*/ bitmap); size_t sp_bitmap_width(Bitmap */*notnull*/ bitmap); /** - * Clones a [SPBitVec]. + * Clones a [DisplayBitVec]. */ DisplayBitVec */*notnull*/ sp_bitvec_clone(DisplayBitVec */*notnull*/ bit_vec); /** - * Sets the value of all bits in the [SPBitVec]. + * Sets the value of all bits in the [DisplayBitVec]. * * # Arguments * @@ -769,12 +784,12 @@ DisplayBitVec */*notnull*/ sp_bitvec_clone(DisplayBitVec */*notnull*/ bit_vec); void sp_bitvec_fill(DisplayBitVec */*notnull*/ bit_vec, bool value); /** - * Deallocates a [SPBitVec]. + * Deallocates a [DisplayBitVec]. */ void sp_bitvec_free(DisplayBitVec */*notnull*/ bit_vec); /** - * Gets the value of a bit from the [SPBitVec]. + * Gets the value of a bit from the [DisplayBitVec]. * * # Arguments * @@ -792,7 +807,7 @@ bool sp_bitvec_get(DisplayBitVec */*notnull*/ bit_vec, size_t index); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. * - * The provided [SPBitVec] gets consumed. + * The provided [DisplayBitVec] gets consumed. * * Returns NULL in case of an error. */ @@ -811,7 +826,7 @@ Packet *sp_bitvec_into_packet(DisplayBitVec */*notnull*/ bitvec, bool sp_bitvec_is_empty(DisplayBitVec */*notnull*/ bit_vec); /** - * Gets the length of the [SPBitVec] in bits. + * Gets the length of the [DisplayBitVec] in bits. * * # Arguments * @@ -820,20 +835,20 @@ bool sp_bitvec_is_empty(DisplayBitVec */*notnull*/ bit_vec); size_t sp_bitvec_len(DisplayBitVec */*notnull*/ bit_vec); /** - * Interpret the data as a series of bits and load then into a new [SPBitVec] instance. + * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. * - * returns: [SPBitVec] instance containing data. + * returns: [DisplayBitVec] instance containing data. */ DisplayBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); /** - * Creates a new [SPBitVec] instance. + * Creates a new [DisplayBitVec] instance. * * # Arguments * * - `size`: size in bits. * - * returns: [SPBitVec] with all bits set to false. + * returns: [DisplayBitVec] with all bits set to false. * * # Panics * @@ -842,7 +857,7 @@ DisplayBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); DisplayBitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Sets the value of a bit in the [SPBitVec]. + * Sets the value of a bit in the [DisplayBitVec]. * * # Arguments * @@ -859,7 +874,7 @@ void sp_bitvec_set(DisplayBitVec */*notnull*/ bit_vec, bool value); /** - * Gets an unsafe reference to the data of the [SPBitVec] instance. + * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * * The returned memory is valid for the lifetime of the bitvec. * @@ -1119,7 +1134,7 @@ void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); * Move the provided [Bitmap] into a new [BitmapCommand], * leaving other fields as their default values. * - * Rust equivalent: [`>::from`] + * Rust equivalent: `BitmapCommand::from(bitmap)` */ BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); @@ -1311,6 +1326,11 @@ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ Cp437Grid *sp_cmd_cp437_grid_get(Cp437GridCommand */*notnull*/ command); +/** + * Gets the origin field of the [Cp437GridCommand]. + * + * Rust equivalent: `cp437_command.origin` + */ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1322,11 +1342,18 @@ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, size_t origin_y); /** - * Moves the provided bitmap to be contained in the command. + * Moves the provided bitmap into the provided command. + * + * This drops the previously contained [Cp437Grid]. */ void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, Cp437Grid */*notnull*/ grid); +/** + * Sets the origin field of the [Cp437GridCommand]. + * + * Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` + */ void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1367,6 +1394,15 @@ void sp_cmd_generic_free(SPCommand command); */ Packet *sp_cmd_generic_into_packet(SPCommand command); +/** + * Tries to turn a [Packet] into a [TypedCommand]. + * + * The packet is deallocated in the process. + * + * Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. + */ +SPCommand *sp_cmd_generic_try_from_packet(Packet */*notnull*/ packet); + void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); /** @@ -1585,7 +1621,7 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint16_t port); /** - * Sends a [TypedCommand] to the display using the [UdpSocket]. + * Sends a [SPCommand] to the display using the [UdpSocket]. * * The passed `command` gets consumed. * diff --git a/src/bitvec.rs b/src/bitvec.rs index 1ba49a7..ec5aa4c 100644 --- a/src/bitvec.rs +++ b/src/bitvec.rs @@ -7,13 +7,13 @@ use servicepoint::{ }; use std::ptr::NonNull; -/// Creates a new [SPBitVec] instance. +/// Creates a new [DisplayBitVec] instance. /// /// # Arguments /// /// - `size`: size in bits. /// -/// returns: [SPBitVec] with all bits set to false. +/// returns: [DisplayBitVec] with all bits set to false. /// /// # Panics /// @@ -23,9 +23,9 @@ pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull { heap_move_nonnull(DisplayBitVec::repeat(false, size)) } -/// Interpret the data as a series of bits and load then into a new [SPBitVec] instance. +/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// -/// returns: [SPBitVec] instance containing data. +/// returns: [DisplayBitVec] instance containing data. #[no_mangle] pub unsafe extern "C" fn sp_bitvec_load( data: ByteSlice, @@ -34,7 +34,7 @@ pub unsafe extern "C" fn sp_bitvec_load( heap_move_nonnull(DisplayBitVec::from_slice(data)) } -/// Clones a [SPBitVec]. +/// Clones a [DisplayBitVec]. #[no_mangle] pub unsafe extern "C" fn sp_bitvec_clone( bit_vec: NonNull, @@ -42,13 +42,13 @@ pub unsafe extern "C" fn sp_bitvec_clone( unsafe { heap_clone(bit_vec) } } -/// Deallocates a [SPBitVec]. +/// Deallocates a [DisplayBitVec]. #[no_mangle] pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull) { unsafe { heap_drop(bit_vec) } } -/// Gets the value of a bit from the [SPBitVec]. +/// Gets the value of a bit from the [DisplayBitVec]. /// /// # Arguments /// @@ -68,7 +68,7 @@ pub unsafe extern "C" fn sp_bitvec_get( unsafe { *bit_vec.as_ref().get(index).unwrap() } } -/// Sets the value of a bit in the [SPBitVec]. +/// Sets the value of a bit in the [DisplayBitVec]. /// /// # Arguments /// @@ -88,7 +88,7 @@ pub unsafe extern "C" fn sp_bitvec_set( unsafe { (*bit_vec.as_ptr()).set(index, value) } } -/// Sets the value of all bits in the [SPBitVec]. +/// Sets the value of all bits in the [DisplayBitVec]. /// /// # Arguments /// @@ -102,7 +102,7 @@ pub unsafe extern "C" fn sp_bitvec_fill( unsafe { (*bit_vec.as_ptr()).fill(value) } } -/// Gets the length of the [SPBitVec] in bits. +/// Gets the length of the [DisplayBitVec] in bits. /// /// # Arguments /// @@ -126,7 +126,7 @@ pub unsafe extern "C" fn sp_bitvec_is_empty( unsafe { bit_vec.as_ref().is_empty() } } -/// Gets an unsafe reference to the data of the [SPBitVec] instance. +/// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. /// @@ -142,7 +142,7 @@ pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref( /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// -/// The provided [SPBitVec] gets consumed. +/// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. #[no_mangle] diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index b2ff3d8..2adf9a8 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -26,7 +26,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_new( /// Move the provided [Bitmap] into a new [BitmapCommand], /// leaving other fields as their default values. /// -/// Rust equivalent: [`>::from`] +/// Rust equivalent: `BitmapCommand::from(bitmap)` #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( bitmap: NonNull, diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 1e1598a..2ccb000 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -46,7 +46,9 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_free( unsafe { heap_drop(command) } } -/// Moves the provided bitmap to be contained in the command. +/// Moves the provided bitmap into the provided command. +/// +/// This drops the previously contained [Cp437Grid]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_set( mut command: NonNull, @@ -64,6 +66,9 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_get( &mut unsafe { command.as_mut() }.grid } +/// Gets the origin field of the [Cp437GridCommand]. +/// +/// Rust equivalent: `cp437_command.origin` #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin( command: NonNull, @@ -77,6 +82,10 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin( } } + +/// Sets the origin field of the [Cp437GridCommand]. +/// +/// Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_set_origin( mut command: NonNull, diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index e3da8fd..561d041 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -8,7 +8,9 @@ use servicepoint::{ }; use std::ptr::{null_mut, NonNull}; +/// Pointer to one of the available command structs. #[repr(C)] +#[allow(missing_docs)] pub union CommandUnion { pub null: *mut u8, pub bitmap: NonNull, @@ -24,7 +26,11 @@ pub union CommandUnion { pub fade_out: NonNull, } +/// Specifies the kind of command struct. +/// +/// This is _not_ equivalent to the [servicepoint::CommandCode]s. #[repr(u8)] +#[allow(missing_docs)] pub enum CommandTag { Invalid = 0, Bitmap, @@ -39,6 +45,11 @@ pub enum CommandTag { BitmapLegacy, } +/// This struct represents a pointer to one of the possible command structs. +/// +/// Only ever access `data` with the correct data type as specified by `tag`! +/// +/// Rust equivalent: [TypedCommand]. #[repr(C)] pub struct SPCommand { /// Specifies which kind of command struct is contained in `data` @@ -59,7 +70,7 @@ impl SPCommand { /// The packet is deallocated in the process. /// /// Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. -/// #[no_mangle] +#[no_mangle] pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( packet: NonNull, ) -> *mut SPCommand { diff --git a/src/udp.rs b/src/udp.rs index 7b659cf..4f778b0 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -62,7 +62,7 @@ pub unsafe extern "C" fn sp_udp_send_packet( unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() } -/// Sends a [TypedCommand] to the display using the [UdpSocket]. +/// Sends a [SPCommand] to the display using the [UdpSocket]. /// /// The passed `command` gets consumed. /// From cf6e6385ec502788a0ce2cb6973189f6dd525336 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 7 May 2025 08:43:13 +0200 Subject: [PATCH 09/76] move containers to own mod --- src/commands/cp437_grid_command.rs | 1 - src/commands/generic_command.rs | 6 +++--- src/{ => containers}/bitmap.rs | 8 ++++++-- src/{ => containers}/bitvec.rs | 2 +- src/{ => containers}/brightness_grid.rs | 2 +- src/{ => containers}/byte_slice.rs | 0 src/{ => containers}/char_grid.rs | 2 +- src/{ => containers}/cp437_grid.rs | 2 +- src/containers/mod.rs | 13 +++++++++++++ src/lib.rs | 7 +------ src/packet.rs | 2 +- 11 files changed, 28 insertions(+), 17 deletions(-) rename src/{ => containers}/bitmap.rs (95%) rename src/{ => containers}/bitvec.rs (98%) rename src/{ => containers}/brightness_grid.rs (98%) rename src/{ => containers}/byte_slice.rs (100%) rename src/{ => containers}/char_grid.rs (98%) rename src/{ => containers}/cp437_grid.rs (98%) create mode 100644 src/containers/mod.rs diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 2ccb000..eb42d15 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -82,7 +82,6 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin( } } - /// Sets the origin field of the [Cp437GridCommand]. /// /// Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 561d041..2b5327d 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -2,9 +2,9 @@ use crate::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ - BitVecCommand, BitmapCommand, BrightnessGridCommand, - CharGridCommand, ClearCommand, Cp437GridCommand, FadeOutCommand, - GlobalBrightnessCommand, HardResetCommand, Packet, TypedCommand, + BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand, + ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, + HardResetCommand, Packet, TypedCommand, }; use std::ptr::{null_mut, NonNull}; diff --git a/src/bitmap.rs b/src/containers/bitmap.rs similarity index 95% rename from src/bitmap.rs rename to src/containers/bitmap.rs index 3393671..f928596 100644 --- a/src/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,10 @@ -use crate::{byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}; +use crate::{ + containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + heap_move_ok, heap_move_some, heap_remove, +}; use servicepoint::{ - Bitmap, BitmapCommand, CompressionCode, DataRef, Grid, Origin, Packet, DisplayBitVec + Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, + Origin, Packet, }; use std::ptr::NonNull; diff --git a/src/bitvec.rs b/src/containers/bitvec.rs similarity index 98% rename from src/bitvec.rs rename to src/containers/bitvec.rs index ec5aa4c..fd1132d 100644 --- a/src/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,5 +1,5 @@ use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ diff --git a/src/brightness_grid.rs b/src/containers/brightness_grid.rs similarity index 98% rename from src/brightness_grid.rs rename to src/containers/brightness_grid.rs index dc3b2f7..b6b3f04 100644 --- a/src/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,5 +1,5 @@ use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, }; use servicepoint::{ diff --git a/src/byte_slice.rs b/src/containers/byte_slice.rs similarity index 100% rename from src/byte_slice.rs rename to src/containers/byte_slice.rs diff --git a/src/char_grid.rs b/src/containers/char_grid.rs similarity index 98% rename from src/char_grid.rs rename to src/containers/char_grid.rs index 62aba87..4e11e19 100644 --- a/src/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,5 +1,5 @@ use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; diff --git a/src/cp437_grid.rs b/src/containers/cp437_grid.rs similarity index 98% rename from src/cp437_grid.rs rename to src/containers/cp437_grid.rs index b4d449e..3d3885a 100644 --- a/src/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,5 +1,5 @@ use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove, }; use servicepoint::{ diff --git a/src/containers/mod.rs b/src/containers/mod.rs new file mode 100644 index 0000000..62d51d0 --- /dev/null +++ b/src/containers/mod.rs @@ -0,0 +1,13 @@ +mod bitmap; +mod bitvec; +mod brightness_grid; +mod byte_slice; +mod char_grid; +mod cp437_grid; + +pub use bitmap::*; +pub use bitvec::*; +pub use brightness_grid::*; +pub use byte_slice::*; +pub use char_grid::*; +pub use cp437_grid::*; diff --git a/src/lib.rs b/src/lib.rs index de22866..8196e3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,13 +27,8 @@ use std::ptr::NonNull; -pub mod bitmap; -pub mod bitvec; -pub mod brightness_grid; -pub mod byte_slice; -pub mod char_grid; pub mod commands; -pub mod cp437_grid; +pub mod containers; pub mod packet; pub mod udp; diff --git a/src/packet.rs b/src/packet.rs index 16509be..a6fde16 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,5 +1,5 @@ use crate::{ - byte_slice::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, + containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, }; use servicepoint::{CommandCode, Header, Packet}; From 626a887480c3e823323f51d82713c830d754f5b6 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 7 May 2025 08:53:49 +0200 Subject: [PATCH 10/76] move heap functions to own mod --- src/commands/bitmap_command.rs | 2 +- src/commands/bitvec_command.rs | 2 +- src/commands/brightness_grid_command.rs | 2 +- src/commands/cc_only_commands.rs | 2 +- src/commands/char_grid_command.rs | 2 +- src/commands/cp437_grid_command.rs | 2 +- src/commands/generic_command.rs | 22 ++++++++-------- src/commands/global_brightness_command.rs | 2 +- src/containers/bitmap.rs | 7 +++-- src/containers/bitvec.rs | 6 +++-- src/containers/brightness_grid.rs | 7 +++-- src/containers/char_grid.rs | 6 +++-- src/containers/cp437_grid.rs | 7 +++-- src/lib.rs | 31 +---------------------- src/mem.rs | 29 +++++++++++++++++++++ src/packet.rs | 4 +-- src/udp.rs | 14 ++++++---- 17 files changed, 82 insertions(+), 65 deletions(-) create mode 100644 src/mem.rs diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 2adf9a8..d67a59f 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index bfe2d33..a717e0c 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index bd975d6..ac4b302 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 751a6ab..ca24f7d 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,4 +1,4 @@ -use crate::{heap_drop, heap_move_nonnull}; +use crate::mem::{heap_drop, heap_move_nonnull}; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index bc078cd..d3f6816 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet}; diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index eb42d15..2ab4e54 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 2b5327d..59eb8f8 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ @@ -65,11 +65,11 @@ impl SPCommand { }; } -/// Tries to turn a [Packet] into a [TypedCommand]. +/// Tries to turn a [Packet] into a [SPCommand]. /// -/// The packet is deallocated in the process. +/// The packet is dropped in the process. /// -/// Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. +/// Returns: pointer to new [SPCommand] instance or NULL if parsing failed. #[no_mangle] pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( packet: NonNull, @@ -142,9 +142,9 @@ pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( })) } -/// Clones a [SPCommand] instance. +/// Clones an [SPCommand] instance. /// -/// returns: new [SPCommand] instance. +/// returns: a new [SPCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { unsafe { @@ -217,12 +217,12 @@ pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { } } -/// Deallocates a [SPCommand]. +/// Deallocates an [SPCommand]. /// /// # Examples /// /// ```C -/// TypedCommand c = sp_command_clear(); +/// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); /// sp_command_free(c); /// ``` #[no_mangle] @@ -248,10 +248,10 @@ pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) { } } -/// Turns a [TypedCommand] into a [Packet]. -/// The [TypedCommand] gets consumed. +/// Tries to turn a [SPCommand] into a [Packet]. +/// The [SPCommand] gets consumed. /// -/// Returns NULL in case of an error. +/// Returns tag [CommandTag::Invalid] in case of an error. #[no_mangle] pub unsafe extern "C" fn sp_cmd_generic_into_packet( command: SPCommand, diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 29e7c19..9d1690c 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,4 +1,4 @@ -use crate::{ +use crate::mem::{ heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, }; use servicepoint::{ diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index f928596..7a1b633 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,9 @@ use crate::{ - containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_move_some, heap_remove, + containers::ByteSlice, + mem::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, + heap_remove, + }, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index fd1132d..e9eeb68 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,8 @@ use crate::{ - containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_remove, + containers::ByteSlice, + mem::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, + }, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index b6b3f04..3a7ae5f 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,9 @@ use crate::{ - containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_move_some, heap_remove, + containers::ByteSlice, + mem::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, + heap_remove, + }, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 4e11e19..3363d43 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,8 @@ use crate::{ - containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_remove, + containers::ByteSlice, + mem::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, + }, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 3d3885a..a636295 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,9 @@ use crate::{ - containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, heap_move_some, heap_remove, + containers::ByteSlice, + mem::{ + heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, + heap_remove, + }, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, diff --git a/src/lib.rs b/src/lib.rs index 8196e3b..14b172d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,10 +25,9 @@ //! } //! ``` -use std::ptr::NonNull; - pub mod commands; pub mod containers; +pub mod mem; pub mod packet; pub mod udp; @@ -37,34 +36,6 @@ use std::time::Duration; /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis(); -pub(crate) fn heap_move(x: T) -> *mut T { - Box::into_raw(Box::new(x)) -} - -pub(crate) fn heap_move_nonnull(x: T) -> NonNull { - NonNull::from(Box::leak(Box::new(x))) -} - -pub(crate) fn heap_move_ok(x: Result) -> *mut T { - x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) -} - -pub(crate) fn heap_move_some(x: Option) -> *mut T { - x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) -} - -pub(crate) unsafe fn heap_drop(x: NonNull) { - drop(unsafe { heap_remove(x) }); -} - -pub(crate) unsafe fn heap_remove(x: NonNull) -> T { - unsafe { *Box::from_raw(x.as_ptr()) } -} - -unsafe fn heap_clone(source: NonNull) -> NonNull { - heap_move_nonnull(unsafe { source.as_ref().clone() }) -} - /// This is a type only used by cbindgen to have a type for pointers. pub struct UdpSocket; diff --git a/src/mem.rs b/src/mem.rs new file mode 100644 index 0000000..6555999 --- /dev/null +++ b/src/mem.rs @@ -0,0 +1,29 @@ +use std::ptr::NonNull; + +pub(crate) fn heap_move(x: T) -> *mut T { + Box::into_raw(Box::new(x)) +} + +pub(crate) fn heap_move_nonnull(x: T) -> NonNull { + NonNull::from(Box::leak(Box::new(x))) +} + +pub(crate) fn heap_move_ok(x: Result) -> *mut T { + x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) +} + +pub(crate) fn heap_move_some(x: Option) -> *mut T { + x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) +} + +pub(crate) unsafe fn heap_drop(x: NonNull) { + drop(unsafe { heap_remove(x) }); +} + +pub(crate) unsafe fn heap_remove(x: NonNull) -> T { + unsafe { *Box::from_raw(x.as_ptr()) } +} + +pub(crate) unsafe fn heap_clone(source: NonNull) -> NonNull { + heap_move_nonnull(unsafe { source.as_ref().clone() }) +} diff --git a/src/packet.rs b/src/packet.rs index a6fde16..5fe7572 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,6 +1,6 @@ use crate::{ - containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, - heap_move_ok, + containers::ByteSlice, + mem::{heap_clone, heap_drop, heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; diff --git a/src/udp.rs b/src/udp.rs index 4f778b0..7c2ab2f 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,9 +1,13 @@ -use crate::commands::{CommandTag, SPCommand}; -use crate::{heap_drop, heap_move_ok, heap_remove}; +use crate::{ + commands::{CommandTag, SPCommand}, + mem::{heap_drop, heap_move_ok, heap_remove}, +}; use servicepoint::{Header, Packet, UdpSocketExt}; -use std::ffi::{c_char, CStr}; -use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; -use std::ptr::NonNull; +use std::{ + ffi::{c_char, CStr}, + net::{Ipv4Addr, SocketAddrV4, UdpSocket}, + ptr::NonNull, +}; /// Creates a new instance of [UdpSocket]. /// From 36f3d84dc8e8f9aa07b384a5e98d4a0303876abe Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 7 May 2025 22:27:06 +0200 Subject: [PATCH 11/76] a bunch of docs --- cbindgen.toml | 4 +- include/servicepoint.h | 175 ++++++++++++++++------ src/commands/bitmap_command.rs | 3 + src/commands/bitvec_command.rs | 4 + src/commands/brightness_grid_command.rs | 11 ++ src/commands/cc_only_commands.rs | 9 +- src/commands/char_grid_command.rs | 11 ++ src/commands/cp437_grid_command.rs | 17 +++ src/commands/generic_command.rs | 29 ++-- src/commands/global_brightness_command.rs | 14 +- src/lib.rs | 6 +- src/packet.rs | 2 + 12 files changed, 215 insertions(+), 70 deletions(-) diff --git a/cbindgen.toml b/cbindgen.toml index 4aabb7e..7841755 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -35,8 +35,8 @@ include = [] exclude = ["BitVec"] [export.rename] -"SpByteSlice" = "ByteSlice" -"SpCommand" = "Command" +"SPCommand" = "Command" +"DisplayBitVec" = "BitVec" [enum] rename_variants = "QualifiedScreamingSnakeCase" diff --git a/include/servicepoint.h b/include/servicepoint.h index 131fe41..a5b3ac7 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -346,7 +346,7 @@ typedef struct Cp437GridCommand Cp437GridCommand; /** * This is a type only used by cbindgen to have a type for pointers. */ -typedef struct DisplayBitVec DisplayBitVec; +typedef struct BitVec BitVec; /** *
Untested
@@ -569,7 +569,7 @@ typedef struct { * The pointer to the command struct */ CommandUnion data; -} SPCommand; +} Command; /** * A raw header. @@ -637,7 +637,7 @@ void sp_bitmap_free(Bitmap */*notnull*/ bitmap); * * Returns NULL in case of error. */ -Bitmap *sp_bitmap_from_bitvec(size_t width, DisplayBitVec */*notnull*/ bitvec); +Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** * Gets the current value at the specified position in the [Bitmap]. @@ -665,7 +665,7 @@ size_t sp_bitmap_height(Bitmap */*notnull*/ bitmap); /** * Consumes the Bitmap and returns the contained BitVec */ -DisplayBitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); +BitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); /** * Creates a [BitmapCommand] and immediately turns that into a [Packet]. @@ -771,7 +771,7 @@ size_t sp_bitmap_width(Bitmap */*notnull*/ bitmap); /** * Clones a [DisplayBitVec]. */ -DisplayBitVec */*notnull*/ sp_bitvec_clone(DisplayBitVec */*notnull*/ bit_vec); +BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ bit_vec); /** * Sets the value of all bits in the [DisplayBitVec]. @@ -781,12 +781,12 @@ DisplayBitVec */*notnull*/ sp_bitvec_clone(DisplayBitVec */*notnull*/ bit_vec); * - `bit_vec`: instance to write to * - `value`: the value to set all bits to */ -void sp_bitvec_fill(DisplayBitVec */*notnull*/ bit_vec, bool value); +void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); /** * Deallocates a [DisplayBitVec]. */ -void sp_bitvec_free(DisplayBitVec */*notnull*/ bit_vec); +void sp_bitvec_free(BitVec */*notnull*/ bit_vec); /** * Gets the value of a bit from the [DisplayBitVec]. @@ -802,7 +802,7 @@ void sp_bitvec_free(DisplayBitVec */*notnull*/ bit_vec); * * - when accessing `index` out of bounds */ -bool sp_bitvec_get(DisplayBitVec */*notnull*/ bit_vec, size_t index); +bool sp_bitvec_get(BitVec */*notnull*/ bit_vec, size_t index); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -811,7 +811,7 @@ bool sp_bitvec_get(DisplayBitVec */*notnull*/ bit_vec, size_t index); * * Returns NULL in case of an error. */ -Packet *sp_bitvec_into_packet(DisplayBitVec */*notnull*/ bitvec, +Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -823,7 +823,7 @@ Packet *sp_bitvec_into_packet(DisplayBitVec */*notnull*/ bitvec, * * - `bit_vec`: instance to write to */ -bool sp_bitvec_is_empty(DisplayBitVec */*notnull*/ bit_vec); +bool sp_bitvec_is_empty(BitVec */*notnull*/ bit_vec); /** * Gets the length of the [DisplayBitVec] in bits. @@ -832,14 +832,14 @@ bool sp_bitvec_is_empty(DisplayBitVec */*notnull*/ bit_vec); * * - `bit_vec`: instance to write to */ -size_t sp_bitvec_len(DisplayBitVec */*notnull*/ bit_vec); +size_t sp_bitvec_len(BitVec */*notnull*/ bit_vec); /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. * * returns: [DisplayBitVec] instance containing data. */ -DisplayBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); +BitVec */*notnull*/ sp_bitvec_load(ByteSlice data); /** * Creates a new [DisplayBitVec] instance. @@ -854,7 +854,7 @@ DisplayBitVec */*notnull*/ sp_bitvec_load(ByteSlice data); * * - when `size` is not divisible by 8. */ -DisplayBitVec */*notnull*/ sp_bitvec_new(size_t size); +BitVec */*notnull*/ sp_bitvec_new(size_t size); /** * Sets the value of a bit in the [DisplayBitVec]. @@ -869,9 +869,7 @@ DisplayBitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds */ -void sp_bitvec_set(DisplayBitVec */*notnull*/ bit_vec, - size_t index, - bool value); +void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value); /** * Gets an unsafe reference to the data of the [DisplayBitVec] instance. @@ -882,7 +880,7 @@ void sp_bitvec_set(DisplayBitVec */*notnull*/ bit_vec, * * - `bit_vec`: instance to write to */ -ByteSlice sp_bitvec_unsafe_data_ref(DisplayBitVec */*notnull*/ bit_vec); +ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); /** * Clones a [BrightnessGrid]. @@ -1126,6 +1124,11 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid, */ size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); +/** + * Clones an [BitmapCommand] instance. + * + * returns: a new [BitmapCommand] instance. + */ BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ command); void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); @@ -1181,11 +1184,19 @@ void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + * Clones an [BitVecCommand] instance. + * + * returns: a new [BitVecCommand] instance. + */ BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(BitVecCommand */*notnull*/ command); +/** + * Deallocates a [BitVecCommand]. + */ void sp_cmd_bitvec_free(BitVecCommand */*notnull*/ command); -DisplayBitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); +BitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command); @@ -1209,7 +1220,7 @@ Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command); * * The contained [`DisplayBitVec`] is always uncompressed. */ -BitVecCommand */*notnull*/ sp_cmd_bitvec_new(DisplayBitVec */*notnull*/ bitvec, +BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -1218,7 +1229,7 @@ BitVecCommand */*notnull*/ sp_cmd_bitvec_new(DisplayBitVec */*notnull*/ bitvec, * Moves the provided bitmap to be contained in the command. */ void sp_cmd_bitvec_set(BitVecCommand */*notnull*/ command, - DisplayBitVec */*notnull*/ bitvec); + BitVec */*notnull*/ bitvec); void sp_cmd_bitvec_set_compression(BitVecCommand */*notnull*/ command, CompressionCode compression); @@ -1229,14 +1240,24 @@ void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, BinaryOperation operation); +/** + * Clones an [GlobalBrightnessCommand] instance. + * + * returns: a new [GlobalBrightnessCommand] instance. + */ GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(GlobalBrightnessCommand */*notnull*/ command); void sp_cmd_brightness_global_free(BitmapCommand */*notnull*/ command); Brightness *sp_cmd_brightness_global_get(GlobalBrightnessCommand */*notnull*/ command); -Packet *sp_cmd_brightness_global_into_packet(GlobalBrightnessCommand */*notnull*/ command); +Packet */*notnull*/ sp_cmd_brightness_global_into_packet(GlobalBrightnessCommand */*notnull*/ command); +/** + * Set the brightness of all tiles to the same value. + * + * Returns: a new [GlobalBrightnessCommand] instance. + */ GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** @@ -1245,10 +1266,22 @@ GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness bri void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, Brightness brightness); +/** + * Clones an [BrightnessGridCommand] instance. + * + * returns: a new [BrightnessGridCommand] instance. + */ BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(BrightnessGridCommand */*notnull*/ command); +/** + * Deallocates a [BitmapCommand]. + */ void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command); +/** + * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + * leaving other fields as their default values. + */ BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); BrightnessGrid *sp_cmd_brightness_grid_get(BrightnessGridCommand */*notnull*/ command); @@ -1259,6 +1292,13 @@ void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ comman Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command); +/** + * Set the brightness of individual tiles in a rectangular area of the display. + * + * The passed [BrightnessGrid] gets consumed. + * + * Returns: a new [BrightnessGridCommand] instance. + */ BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, size_t origin_x, size_t origin_y); @@ -1273,10 +1313,22 @@ void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ comman size_t origin_x, size_t origin_y); +/** + * Clones an [CharGridCommand] instance. + * + * returns: a new [CharGridCommand] instance. + */ CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(CharGridCommand */*notnull*/ command); +/** + * Deallocates a [BitmapCommand]. + */ void sp_cmd_char_grid_free(BitmapCommand */*notnull*/ command); +/** + * Moves the provided [CharGrid] into a new [CharGridCommand], + * leaving other fields as their default values. + */ CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); CharGrid *sp_cmd_char_grid_get(CharGridCommand */*notnull*/ command); @@ -1287,6 +1339,13 @@ void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, Packet *sp_cmd_char_grid_into_packet(CharGridCommand */*notnull*/ command); +/** + * Show UTF-8 encoded text on the screen. + * + * The passed [CharGrid] gets consumed. + * + * Returns: a new [CharGridCommand] instance. + */ CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, size_t origin_x, size_t origin_y); @@ -1301,6 +1360,9 @@ void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + * Deallocates a [ClearCommand]. + */ void sp_cmd_clear_free(ClearCommand */*notnull*/ command); /** @@ -1309,21 +1371,35 @@ void sp_cmd_clear_free(ClearCommand */*notnull*/ command); * Does not affect brightness. * * Returns: a new [ClearCommand] instance. - * - * # Examples - * - * ```C - * sp_udp_send_command(connection, sp_cmd_clear()); - * ``` */ ClearCommand */*notnull*/ sp_cmd_clear_new(void); +/** + * Clones an [Cp437GridCommand] instance. + * + * returns: a new [Cp437GridCommand] instance. + */ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(Cp437GridCommand */*notnull*/ command); +/** + * Deallocates a [Cp437GridCommand]. + */ void sp_cmd_cp437_grid_free(BitmapCommand */*notnull*/ command); +/** + * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], + * leaving other fields as their default values. + */ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of [CP-437] encoded characters. + * For sending UTF-8 encoded characters, see [servicepoint::CharGridCommand]. + * + * [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 + */ Cp437Grid *sp_cmd_cp437_grid_get(Cp437GridCommand */*notnull*/ command); /** @@ -1337,6 +1413,13 @@ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, Packet *sp_cmd_cp437_grid_into_packet(Cp437GridCommand */*notnull*/ command); +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of [CP-437] encoded characters. + * + * The origin is relative to the top-left of the display. + */ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, size_t origin_x, size_t origin_y); @@ -1358,6 +1441,9 @@ void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + * Deallocates a [FadeOutCommand]. + */ void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command); /** @@ -1368,41 +1454,44 @@ void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command); FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); /** - * Clones a [SPCommand] instance. + * Clones an [SPCommand] instance. * - * returns: new [SPCommand] instance. + * returns: a new [SPCommand] instance. */ -SPCommand sp_cmd_generic_clone(SPCommand command); +Command sp_cmd_generic_clone(Command command); /** - * Deallocates a [SPCommand]. + * Deallocates an [SPCommand]. * * # Examples * * ```C - * TypedCommand c = sp_command_clear(); + * SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); * sp_command_free(c); * ``` */ -void sp_cmd_generic_free(SPCommand command); +void sp_cmd_generic_free(Command command); /** - * Turns a [TypedCommand] into a [Packet]. - * The [TypedCommand] gets consumed. + * Tries to turn a [SPCommand] into a [Packet]. + * The [SPCommand] gets consumed. * - * Returns NULL in case of an error. + * Returns tag [CommandTag::Invalid] in case of an error. */ -Packet *sp_cmd_generic_into_packet(SPCommand command); +Packet *sp_cmd_generic_into_packet(Command command); /** - * Tries to turn a [Packet] into a [TypedCommand]. + * Tries to turn a [Packet] into a [SPCommand]. * - * The packet is deallocated in the process. + * The packet is dropped in the process. * - * Returns: pointer to new [TypedCommand] instance or NULL if parsing failed. + * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. */ -SPCommand *sp_cmd_generic_try_from_packet(Packet */*notnull*/ packet); +Command *sp_cmd_generic_try_from_packet(Packet */*notnull*/ packet); +/** + * Deallocates a [HardResetCommand]. + */ void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); /** @@ -1520,6 +1609,8 @@ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); /** * Clones a [Packet]. + * + * returns: a new [Packet] instance. */ Packet */*notnull*/ sp_packet_clone(Packet */*notnull*/ packet); @@ -1633,7 +1724,7 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_command(UdpSocket */*notnull*/ connection, SPCommand command); +bool sp_udp_send_command(UdpSocket */*notnull*/ connection, Command command); /** * Sends a [Header] to the display using the [UdpSocket]. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index d67a59f..731a644 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -41,6 +41,9 @@ pub unsafe extern "C" fn sp_cmd_bitmap_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, diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index a717e0c..7734c05 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -41,6 +41,9 @@ pub unsafe extern "C" fn sp_cmd_bitvec_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, @@ -48,6 +51,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_clone( unsafe { heap_clone(command) } } +/// Deallocates a [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { unsafe { heap_drop(command) } diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index ac4b302..aa1caab 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -6,6 +6,11 @@ use servicepoint::{ }; use std::ptr::NonNull; +/// Set the brightness of individual tiles in a rectangular area of the display. +/// +/// The passed [BrightnessGrid] gets consumed. +/// +/// Returns: a new [BrightnessGridCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_new( grid: NonNull, @@ -18,6 +23,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_new( }) } +/// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], +/// leaving other fields as their default values. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid( grid: NonNull, @@ -32,6 +39,9 @@ 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, @@ -39,6 +49,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_clone( unsafe { heap_clone(command) } } +/// Deallocates a [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_free( command: NonNull, diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index ca24f7d..2a48c12 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -7,17 +7,12 @@ use std::ptr::NonNull; /// Does not affect brightness. /// /// Returns: a new [ClearCommand] instance. -/// -/// # Examples -/// -/// ```C -/// sp_udp_send_command(connection, sp_cmd_clear()); -/// ``` #[no_mangle] pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull { heap_move_nonnull(ClearCommand) } +/// Deallocates a [ClearCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull) { unsafe { heap_drop(command) } @@ -33,6 +28,7 @@ pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull { heap_move_nonnull(HardResetCommand) } +/// Deallocates a [HardResetCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_hard_reset_free( command: NonNull, @@ -48,6 +44,7 @@ pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull { heap_move_nonnull(FadeOutCommand) } +/// Deallocates a [FadeOutCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull) { unsafe { heap_drop(command) } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index d3f6816..c9e5d9b 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -4,6 +4,11 @@ use crate::mem::{ use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet}; use std::ptr::NonNull; +/// Show UTF-8 encoded text on the screen. +/// +/// The passed [CharGrid] gets consumed. +/// +/// Returns: a new [CharGridCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_new( grid: NonNull, @@ -16,6 +21,8 @@ pub unsafe extern "C" fn sp_cmd_char_grid_new( }) } +/// Moves the provided [CharGrid] into a new [CharGridCommand], +/// leaving other fields as their default values. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_from_grid( grid: NonNull, @@ -30,6 +37,9 @@ pub unsafe extern "C" fn sp_cmd_char_grid_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, @@ -37,6 +47,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_clone( unsafe { heap_clone(command) } } +/// Deallocates a [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_free( command: NonNull, diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 2ab4e54..f6b8f9d 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -6,6 +6,11 @@ use servicepoint::{ }; use std::ptr::NonNull; +/// Show text on the screen. +/// +/// The text is sent in the form of a 2D grid of [CP-437] encoded characters. +/// +/// The origin is relative to the top-left of the display. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_new( grid: NonNull, @@ -18,6 +23,8 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_new( }) } +/// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], +/// leaving other fields as their default values. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid( grid: NonNull, @@ -32,6 +39,9 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_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, @@ -39,6 +49,7 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_clone( unsafe { heap_clone(command) } } +/// Deallocates a [Cp437GridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_free( command: NonNull, @@ -59,6 +70,12 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_set( } } +/// Show text on the screen. +/// +/// The text is sent in the form of a 2D grid of [CP-437] encoded characters. +/// For sending UTF-8 encoded characters, see [servicepoint::CharGridCommand]. +/// +/// [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 #[no_mangle] pub unsafe extern "C" fn sp_cmd_cp437_grid_get( mut command: NonNull, diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 59eb8f8..bdeab5e 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,6 @@ use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, + heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, + heap_remove, }; use servicepoint::{ BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand, @@ -229,7 +230,7 @@ pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) { unsafe { match command.tag { - CommandTag::Invalid => return, + CommandTag::Invalid => (), CommandTag::Bitmap => heap_drop(command.data.bitmap), CommandTag::BitVec => heap_drop(command.data.bitvec), CommandTag::BrightnessGrid => { @@ -273,20 +274,20 @@ pub unsafe extern "C" fn sp_cmd_generic_into_packet( CommandTag::Cp437Grid => heap_move_ok(unsafe { heap_remove(command.data.cp437_grid).try_into() }), - CommandTag::GlobalBrightness => heap_move_ok(unsafe { - heap_remove(command.data.global_brightness).try_into() + CommandTag::GlobalBrightness => heap_move(unsafe { + heap_remove(command.data.global_brightness).into() }), CommandTag::Clear => { - heap_move_ok(unsafe { heap_remove(command.data.clear).try_into() }) + heap_move(unsafe { heap_remove(command.data.clear).into() }) + } + CommandTag::HardReset => { + heap_move(unsafe { heap_remove(command.data.hard_reset).into() }) + } + CommandTag::FadeOut => { + heap_move(unsafe { heap_remove(command.data.fade_out).into() }) + } + CommandTag::BitmapLegacy => { + heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) } - CommandTag::HardReset => heap_move_ok(unsafe { - heap_remove(command.data.hard_reset).try_into() - }), - CommandTag::FadeOut => heap_move_ok(unsafe { - heap_remove(command.data.fade_out).try_into() - }), - CommandTag::BitmapLegacy => heap_move_ok(unsafe { - heap_remove(command.data.bitmap_legacy).try_into() - }), } } diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 9d1690c..063dea7 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,11 +1,12 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, -}; +use crate::mem::{heap_clone, heap_drop, heap_move_nonnull, heap_remove}; use servicepoint::{ BitmapCommand, Brightness, GlobalBrightnessCommand, Packet, }; use std::ptr::NonNull; +/// Set the brightness of all tiles to the same value. +/// +/// Returns: a new [GlobalBrightnessCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_global_new( brightness: Brightness, @@ -16,10 +17,13 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_new( #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) +) -> NonNull { + 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, diff --git a/src/lib.rs b/src/lib.rs index 14b172d..377b2a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,10 +25,14 @@ //! } //! ``` +/// Functions related to commands. pub mod commands; +/// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends. pub mod containers; -pub mod mem; +pub(crate) mod mem; +/// Functions related to [Packet]. pub mod packet; +/// Functions related to [UdpSocket]. pub mod udp; use std::time::Duration; diff --git a/src/packet.rs b/src/packet.rs index 5fe7572..e9593c9 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -79,6 +79,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, From b8a55d0433afef5848b865078345b4c4f47c649a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 7 May 2025 22:50:53 +0200 Subject: [PATCH 12/76] even more docs --- example/src/brightness_tester.c | 2 +- include/servicepoint.h | 103 +++++++++++++++++++++--- src/commands/bitmap_command.rs | 12 ++- src/commands/bitvec_command.rs | 14 +++- src/commands/brightness_grid_command.rs | 8 +- src/commands/char_grid_command.rs | 10 ++- src/commands/cp437_grid_command.rs | 5 +- 7 files changed, 133 insertions(+), 21 deletions(-) diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 9476422..21d814d 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -7,7 +7,7 @@ void enable_all_pixels(void) { sp_bitmap_fill(all_on, true); BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on); - Packet *packet = sp_cmd_bitmap_into_packet(bitmapCommand); + Packet *packet = sp_cmd_bitmap_try_into_packet(bitmapCommand); if (packet != NULL) sp_udp_send_packet(connection, packet); } diff --git a/include/servicepoint.h b/include/servicepoint.h index a5b3ac7..d5b0199 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1131,6 +1131,9 @@ size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); */ BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ command); +/** + * Deallocates a [BitmapCommand] instance. + */ void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); /** @@ -1151,14 +1154,18 @@ BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap) */ Bitmap */*notnull*/ sp_cmd_bitmap_get(BitmapCommand */*notnull*/ command); +/** + * Reads the compression kind of the [BitmapCommand]. + */ CompressionCode sp_cmd_bitmap_get_compression(BitmapCommand */*notnull*/ command); +/** + * Reads the origin field of the [BitmapCommand]. + */ void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); -Packet *sp_cmd_bitmap_into_packet(BitmapCommand */*notnull*/ command); - /** * Sets a window of pixels to the specified values. * @@ -1172,18 +1179,31 @@ BitmapCommand */*notnull*/ sp_cmd_bitmap_new(Bitmap */*notnull*/ bitmap, CompressionCode compression); /** - * Moves the provided bitmap to be contained in the command. + * Moves the provided [Bitmap] to be contained in the [BitmapCommand]. */ void sp_cmd_bitmap_set(BitmapCommand */*notnull*/ command, Bitmap */*notnull*/ bitmap); +/** + * Overwrites the compression kind of the [BitmapCommand]. + */ void sp_cmd_bitmap_set_compression(BitmapCommand */*notnull*/ command, CompressionCode compression); +/** + * Overwrites the origin field of the [BitmapCommand]. + */ void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + * Tries to turn a [BitmapCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + */ +Packet *sp_cmd_bitmap_try_into_packet(BitmapCommand */*notnull*/ command); + /** * Clones an [BitVecCommand] instance. * @@ -1196,16 +1216,26 @@ BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(BitVecCommand */*notnull*/ comman */ void sp_cmd_bitvec_free(BitVecCommand */*notnull*/ command); +/** + * Returns a pointer to the [BitVec] contained in the [BitVecCommand]. + */ BitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); +/** + * Reads the compression kind of the [BitVecCommand]. + */ CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command); +/** + * Reads the offset field of the [BitVecCommand]. + */ Offset sp_cmd_bitvec_get_offset(BitVecCommand */*notnull*/ command); +/** + * Returns the [BinaryOperation] of the command. + */ BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); -Packet *sp_cmd_bitvec_into_packet(BitVecCommand */*notnull*/ command); - /** * Set pixel data starting at the pixel offset on screen. * @@ -1226,20 +1256,36 @@ BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, CompressionCode compression); /** - * Moves the provided bitmap to be contained in the command. + * Moves the provided [BitVec] to be contained in the [BitVecCommand]. */ void sp_cmd_bitvec_set(BitVecCommand */*notnull*/ command, BitVec */*notnull*/ bitvec); +/** + * Overwrites the compression kind of the [BitVecCommand]. + */ void sp_cmd_bitvec_set_compression(BitVecCommand */*notnull*/ command, CompressionCode compression); +/** + * Overwrites the offset field of the [BitVecCommand]. + */ void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, Offset offset); +/** + * Overwrites the [BinaryOperation] of the command. + */ void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, BinaryOperation operation); +/** + * Tries to turn a [BitVecCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + */ +Packet *sp_cmd_bitvec_try_into_packet(BitVecCommand */*notnull*/ command); + /** * Clones an [GlobalBrightnessCommand] instance. * @@ -1284,12 +1330,23 @@ void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command); */ BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); +/** + * Returns a pointer to the [BrightnessGrid] contained in the [BrightnessGridCommand]. + */ BrightnessGrid *sp_cmd_brightness_grid_get(BrightnessGridCommand */*notnull*/ command); +/** + * Overwrites the origin field of the [BrightnessGridCommand]. + */ void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); +/** + * Tries to turn a [BrightnessGridCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + */ Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command); /** @@ -1304,11 +1361,14 @@ BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */* size_t origin_y); /** - * Moves the provided bitmap to be contained in the command. + * Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. */ void sp_cmd_brightness_grid_set(BrightnessGridCommand */*notnull*/ command, BrightnessGrid */*notnull*/ grid); +/** + * Reads the origin field of the [BrightnessGridCommand]. + */ void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1331,14 +1391,18 @@ void sp_cmd_char_grid_free(BitmapCommand */*notnull*/ command); */ CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); +/** + * Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. + */ CharGrid *sp_cmd_char_grid_get(CharGridCommand */*notnull*/ command); +/** + * Reads the origin field of the [CharGridCommand]. + */ void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); -Packet *sp_cmd_char_grid_into_packet(CharGridCommand */*notnull*/ command); - /** * Show UTF-8 encoded text on the screen. * @@ -1351,15 +1415,25 @@ CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, size_t origin_y); /** - * Moves the provided bitmap to be contained in the command. + * Moves the provided [CharGrid] to be contained in the [CharGridCommand]. */ void sp_cmd_char_grid_set(CharGridCommand */*notnull*/ command, CharGrid */*notnull*/ grid); +/** + * Overwrites the origin field of the [CharGridCommand]. + */ void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + * Tries to turn a [CharGridCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + */ +Packet *sp_cmd_char_grid_try_into_packet(CharGridCommand */*notnull*/ command); + /** * Deallocates a [ClearCommand]. */ @@ -1411,8 +1485,6 @@ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); -Packet *sp_cmd_cp437_grid_into_packet(Cp437GridCommand */*notnull*/ command); - /** * Show text on the screen. * @@ -1441,6 +1513,13 @@ void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + * Tries to turn a [Cp437GridCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + */ +Packet *sp_cmd_cp437_grid_try_into_packet(Cp437GridCommand */*notnull*/ command); + /** * Deallocates a [FadeOutCommand]. */ diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 731a644..286a6f1 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -34,8 +34,11 @@ pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) } +/// Tries to turn a [BitmapCommand] into a [Packet]. +/// +/// Returns: NULL or a [Packet] containing the command. #[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_into_packet( +pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) @@ -51,6 +54,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_clone( unsafe { heap_clone(command) } } +/// Deallocates a [BitmapCommand] instance. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull) { unsafe { heap_drop(command) } @@ -69,7 +73,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_get( unsafe { NonNull::from(&mut (command.as_mut().bitmap)) } } -/// Moves the provided bitmap to be contained in the command. +/// Moves the provided [Bitmap] to be contained in the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set( mut command: NonNull, @@ -80,6 +84,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_set( } } +/// Reads the origin field of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_get_origin( command: NonNull, @@ -93,6 +98,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_get_origin( } } +/// Overwrites the origin field of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( mut command: NonNull, @@ -104,6 +110,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( } } +/// Overwrites the compression kind of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( mut command: NonNull, @@ -114,6 +121,7 @@ pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( } } +/// Reads the compression kind of the [BitmapCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( command: NonNull, diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 7734c05..617e3af 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -34,8 +34,11 @@ pub unsafe extern "C" fn sp_cmd_bitvec_new( }) } +/// Tries to turn a [BitVecCommand] into a [Packet]. +/// +/// Returns: NULL or a [Packet] containing the command. #[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_into_packet( +pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) @@ -57,6 +60,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { unsafe { heap_drop(command) } } +/// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get( mut command: NonNull, @@ -64,7 +68,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_get( &mut unsafe { command.as_mut() }.bitvec } -/// Moves the provided bitmap to be contained in the command. +/// Moves the provided [BitVec] to be contained in the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set( mut command: NonNull, @@ -75,6 +79,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set( } } +/// Reads the offset field of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( command: NonNull, @@ -82,6 +87,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( unsafe { command.as_ref().offset } } +/// Overwrites the offset field of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( mut command: NonNull, @@ -92,6 +98,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( } } +/// Returns the [BinaryOperation] of the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( command: NonNull, @@ -99,6 +106,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( unsafe { command.as_ref().operation.clone() } // TODO remove clone } +/// Overwrites the [BinaryOperation] of the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( mut command: NonNull, @@ -109,6 +117,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( } } +/// Overwrites the compression kind of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( mut command: NonNull, @@ -119,6 +128,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( } } +/// Reads the compression kind of the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get_compression( command: NonNull, diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index aa1caab..80e1b89 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -32,6 +32,9 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid( heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } +/// Tries to turn a [BrightnessGridCommand] into a [Packet]. +/// +/// Returns: NULL or a [Packet] containing the command. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( command: NonNull, @@ -57,7 +60,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_free( unsafe { heap_drop(command) } } -/// Moves the provided bitmap to be contained in the command. +/// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_set( mut command: NonNull, @@ -68,6 +71,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_set( } } +/// Returns a pointer to the [BrightnessGrid] contained in the [BrightnessGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_get( mut command: NonNull, @@ -75,6 +79,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_get( &mut unsafe { command.as_mut() }.grid } +/// Overwrites the origin field of the [BrightnessGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_get_origin( command: NonNull, @@ -88,6 +93,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_get_origin( } } +/// Reads the origin field of the [BrightnessGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_grid_set_origin( mut command: NonNull, diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index c9e5d9b..8a220a5 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -30,8 +30,11 @@ pub unsafe extern "C" fn sp_cmd_char_grid_from_grid( heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } +/// Tries to turn a [CharGridCommand] into a [Packet]. +/// +/// Returns: NULL or a [Packet] containing the command. #[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_into_packet( +pub unsafe extern "C" fn sp_cmd_char_grid_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) @@ -55,7 +58,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_free( unsafe { heap_drop(command) } } -/// Moves the provided bitmap to be contained in the command. +/// Moves the provided [CharGrid] to be contained in the [CharGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_set( mut command: NonNull, @@ -66,6 +69,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_set( } } +/// Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_get( mut command: NonNull, @@ -73,6 +77,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_get( &mut unsafe { command.as_mut() }.grid } +/// Reads the origin field of the [CharGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_get_origin( command: NonNull, @@ -86,6 +91,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_get_origin( } } +/// Overwrites the origin field of the [CharGridCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_set_origin( mut command: NonNull, diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index f6b8f9d..f653ef5 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -32,8 +32,11 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid( heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } +/// Tries to turn a [Cp437GridCommand] into a [Packet]. +/// +/// Returns: NULL or a [Packet] containing the command. #[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_into_packet( +pub unsafe extern "C" fn sp_cmd_cp437_grid_try_into_packet( command: NonNull, ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) From 4ab5305377c23792600bba4dc364fd3753187092 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 10 May 2025 14:58:50 +0200 Subject: [PATCH 13/76] add example helper, translate more examples update to wip servicepoint lib --- Cargo.lock | 3 +-- Cargo.toml | 5 ++-- cbindgen.toml | 4 ++- example/src/announce.c | 16 +++--------- example/src/brightness_tester.c | 21 +++++---------- example/src/helpers.h | 45 +++++++++++++++++++++++++++++++++ example/src/moving_line.c | 34 +++++++++++++++++++++++++ example/src/random_stuff.c | 9 +++---- example/src/wiping_clear.c | 34 +++++++++++++++++++++++++ include/servicepoint.h | 26 ++++++++++++++++--- src/commands/bitvec_command.rs | 2 +- src/containers/byte_slice.rs | 25 ++++++++++++------ src/lib.rs | 4 +-- src/packet.rs | 26 ++++++++++++++----- src/udp.rs | 2 +- 15 files changed, 195 insertions(+), 61 deletions(-) create mode 100644 example/src/helpers.h create mode 100644 example/src/moving_line.c create mode 100644 example/src/wiping_clear.c diff --git a/Cargo.lock b/Cargo.lock index 78e2169..fcd4aef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -415,8 +415,7 @@ dependencies = [ [[package]] name = "servicepoint" version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6bd5cfa49c73aeecb344680ffbf697abf73e0563a441b93b9723ae43867500f" +source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint.git?branch=next#d979d46d3e770222c5a7ca549e601b45eff48299" dependencies = [ "bitvec", "bzip2", diff --git a/Cargo.toml b/Cargo.toml index 0eba069..fcb4eb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,9 +18,10 @@ crate-type = ["staticlib", "cdylib", "rlib"] cbindgen = "0.28.0" [dependencies.servicepoint] -package = "servicepoint" -version = "0.14.1" +# version = "0.14.1" default-features = false +git = "https://git.berlin.ccc.de/servicepoint/servicepoint.git" +branch = "next" [features] all_compressions = ["servicepoint/all_compressions"] diff --git a/cbindgen.toml b/cbindgen.toml index 7841755..9749726 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -22,10 +22,12 @@ usize_is_size_t = true # this is needed because otherwise the order in the C bindings is different on different machines sort_by = "Name" +include_guard = "SERVICEPOINT_BINDINGS_C" + [parse] parse_deps = true include = ["servicepoint", "std"] -extra_bindings = ["servicepoint"] +extra_bindings = ["servicepoint", "servicepoint_binding_c"] [parse.expand] features = ["full"] diff --git a/example/src/announce.c b/example/src/announce.c index d256cff..bfdc48f 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -1,16 +1,9 @@ -#include -#include "servicepoint.h" - +#include "helpers.h" int main(void) { - printf("test\n"); + sock_init(); - UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); - //UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return 1; - - sp_udp_send_header(connection, (Header) {.command_code = COMMAND_CODE_CLEAR}); + sp_udp_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); CharGrid *grid = sp_char_grid_new(5, 2); if (grid == NULL) @@ -30,8 +23,7 @@ int main(void) { Packet *packet = sp_char_grid_into_packet(grid, 0, 0); if (packet == NULL) return 1; - sp_udp_send_packet(connection, packet); + sp_udp_send_packet(sock, packet); - sp_udp_free(connection); return 0; } diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 21d814d..51790cf 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -1,6 +1,5 @@ #include "servicepoint.h" - -static UdpSocket *connection = NULL; +#include "helpers.h" void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); @@ -9,26 +8,18 @@ void enable_all_pixels(void) { BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on); Packet *packet = sp_cmd_bitmap_try_into_packet(bitmapCommand); if (packet != NULL) - sp_udp_send_packet(connection, packet); + sp_udp_send_packet(sock, packet); } void make_brightness_pattern(BrightnessGrid *grid) { ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid); for (size_t index = 0; index < slice.length; index++) { - slice.start[index] = (uint8_t) (index % ((size_t) Brightness_MAX)); + slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX)); } } -void run_at_exit() { - sp_udp_free(connection); -} - int main(void) { - //UdpSocket *connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); - connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return -1; - atexit(run_at_exit); + sock_init(); enable_all_pixels(); @@ -37,8 +28,8 @@ int main(void) { Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid)); if (packet == NULL) - return -2; + return -2; - sp_udp_send_packet(connection, packet); + sp_udp_send_packet(sock, packet); return 0; } diff --git a/example/src/helpers.h b/example/src/helpers.h new file mode 100644 index 0000000..904e86b --- /dev/null +++ b/example/src/helpers.h @@ -0,0 +1,45 @@ +#pragma once +#ifndef SERVICEPOINT_BINDING_C_MSLEEP_H +#define SERVICEPOINT_BINDING_C_MSLEEP_H + +#include +#include +#include +#include "servicepoint.h" + +static UdpSocket *sock = NULL; + +void sock_free() { + sp_udp_free(sock); +} + +void sock_init() { + //sock = sp_udp_open_ipv4(127, 0, 0, 1, 2342); + sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + if (sock == NULL) + exit(-1); + atexit(sock_free); +} + +/// TODO: all of this for sleeping n ms? There should be a better way! +int msleep(long msec) { + int res; + + if (msec < 0) { + errno = EINVAL; + return -1; + } + + struct timespec ts = { + .tv_sec = msec / 1000, + .tv_nsec = (msec % 1000) * 1000000, + }; + + do { + res = nanosleep(&ts, &ts); + } while (res && errno == EINTR); + + return res; +} + +#endif //SERVICEPOINT_BINDING_C_MSLEEP_H diff --git a/example/src/moving_line.c b/example/src/moving_line.c new file mode 100644 index 0000000..688f249 --- /dev/null +++ b/example/src/moving_line.c @@ -0,0 +1,34 @@ +#include "servicepoint.h" +#include "helpers.h" + +int main() { + sock_init(); + + int result = 0; + Bitmap *bitmap = sp_bitmap_new_max_sized(); + for (int x = 0; x < sp_bitmap_width(bitmap); x++) { + sp_bitmap_fill(bitmap, false); + + for (int y = 0; y < sp_bitmap_height(bitmap); y++) { + sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true); + } + + BitmapCommand *command = sp_cmd_bitmap_from_bitmap(sp_bitmap_clone(bitmap)); + Packet *packet = sp_cmd_bitmap_try_into_packet(command); + if (packet == NULL) { + result = -2; + goto exit; + } + + if (!sp_udp_send_packet(sock, packet)) { + result = -3; + goto exit; + } + + msleep(SP_FRAME_PACING_MS); + } + + exit: + sp_bitmap_free(bitmap); + return result; +} diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 2ebd485..2d818e3 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -1,10 +1,9 @@ #include #include "servicepoint.h" +#include "helpers.h" int main(void) { - UdpSocket *connection = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - if (connection == NULL) - return 1; + sock_init(); Bitmap *pixels = sp_bitmap_new(PIXEL_WIDTH, PIXEL_HEIGHT); if (pixels == NULL) @@ -19,8 +18,6 @@ int main(void) { Header *header = sp_packet_get_header(packet); printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d); - sp_udp_send_packet(connection, packet); - - sp_udp_free(connection); + sp_udp_send_packet(sock, packet); return 0; } diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c new file mode 100644 index 0000000..a3f7017 --- /dev/null +++ b/example/src/wiping_clear.c @@ -0,0 +1,34 @@ +#include "servicepoint.h" +#include "helpers.h" + +int main() { + sock_init(); + + Bitmap *enabled_pixels = sp_bitmap_new_max_sized(); + sp_bitmap_fill(enabled_pixels, true); + + int result = 0; + for (int x = 0; x < PIXEL_WIDTH; x++) { + for (int y = 0; y < PIXEL_HEIGHT; y++) { + sp_bitmap_set(enabled_pixels, x, y, false); + } + + BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); + BitVecCommand *command = sp_cmd_bitvec_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); + Packet *packet = sp_cmd_bitvec_try_into_packet(command); + if (packet == NULL) { + result = -2; + goto exit; + } + if (!sp_udp_send_packet(sock, packet)) { + result = -3; + goto exit; + } + + msleep(SP_FRAME_PACING_MS); + } + + exit: + sp_bitmap_free(enabled_pixels); + return result; +} diff --git a/include/servicepoint.h b/include/servicepoint.h index d5b0199..1a4d023 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1,3 +1,6 @@ +#ifndef SERVICEPOINT_BINDINGS_C +#define SERVICEPOINT_BINDINGS_C + /* Generated with cbindgen:0.28.0 */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ @@ -446,12 +449,19 @@ typedef struct ValueGrid_u8 ValueGrid_u8; * - accesses to the memory pointed to with `start` is never accessed outside `length` * - 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}; + * ``` */ typedef struct { /** * The start address of the memory. */ - uint8_t */*notnull*/ start; + uint8_t *start; /** * The amount of memory in bytes. */ @@ -606,6 +616,13 @@ typedef struct { +/** + * Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. + */ +#define SP_FRAME_PACING_MS 30 + + + #ifdef __cplusplus extern "C" { #endif // __cplusplus @@ -1703,8 +1720,7 @@ void sp_packet_free(Packet */*notnull*/ packet); * * returns: new instance. Will never return null. */ -Packet */*notnull*/ sp_packet_from_parts(Header header, - const ByteSlice *payload); +Packet */*notnull*/ sp_packet_from_parts(Header header, ByteSlice payload); /** * Returns a pointer to the header field of the provided packet. @@ -1716,6 +1732,8 @@ Header */*notnull*/ sp_packet_get_header(Packet */*notnull*/ packet); /** * Returns a pointer to the current payload of the provided packet. * + * Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. + * * The returned memory can be changed and will be valid until a new payload is set. */ ByteSlice sp_packet_get_payload(Packet */*notnull*/ packet); @@ -1831,3 +1849,5 @@ bool sp_udp_send_packet(UdpSocket */*notnull*/ connection, #ifdef __cplusplus } // extern "C" #endif // __cplusplus + +#endif /* SERVICEPOINT_BINDINGS_C */ diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 617e3af..f2f667c 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -103,7 +103,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( command: NonNull, ) -> BinaryOperation { - unsafe { command.as_ref().operation.clone() } // TODO remove clone + unsafe { command.as_ref().operation } } /// Overwrites the [BinaryOperation] of the command. diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index de7d0df..0668318 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -1,7 +1,5 @@ //! FFI slice helper -use std::ptr::NonNull; - /// Represents a span of memory (`&mut [u8]` ) as a struct. /// /// # Safety @@ -11,28 +9,39 @@ use std::ptr::NonNull; /// - accesses to the memory pointed to with `start` is never accessed outside `length` /// - 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}; +/// ``` #[repr(C)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct ByteSlice { /// The start address of the memory. - pub start: NonNull, + pub start: *mut u8, /// The amount of memory in bytes. pub length: usize, } impl ByteSlice { + pub(crate) const INVALID: ByteSlice = ByteSlice { + start: std::ptr::null_mut(), + length: 0, + }; + pub(crate) unsafe fn as_slice(&self) -> &[u8] { - unsafe { std::slice::from_raw_parts(self.start.as_ptr(), self.length) } + unsafe { std::slice::from_raw_parts(self.start, self.length) } } pub(crate) unsafe fn as_slice_mut(&mut self) -> &mut [u8] { - unsafe { - std::slice::from_raw_parts_mut(self.start.as_ptr(), self.length) - } + unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } pub(crate) unsafe fn from_slice(slice: &mut [u8]) -> Self { Self { - start: NonNull::new(slice.as_mut_ptr()).unwrap(), + start: slice.as_mut_ptr(), length: slice.len(), } } diff --git a/src/lib.rs b/src/lib.rs index 377b2a0..ea3f916 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,10 +35,8 @@ pub mod packet; /// Functions related to [UdpSocket]. pub mod udp; -use std::time::Duration; - /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. -pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis(); +pub const SP_FRAME_PACING_MS: u128 = 30; /// This is a type only used by cbindgen to have a type for pointers. pub struct UdpSocket; diff --git a/src/packet.rs b/src/packet.rs index e9593c9..2fdbefc 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -20,13 +20,12 @@ pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet { #[no_mangle] pub unsafe extern "C" fn sp_packet_from_parts( header: Header, - payload: *const ByteSlice, + payload: ByteSlice, ) -> NonNull { - let payload = if payload.is_null() { - vec![] + let payload = if payload == ByteSlice::INVALID { + None } else { - let payload = unsafe { (*payload).as_slice() }; - Vec::from(payload) + Some(Vec::from(unsafe { payload.as_slice() })) }; heap_move_nonnull(Packet { header, payload }) @@ -44,12 +43,19 @@ pub unsafe extern "C" fn sp_packet_get_header( /// Returns a pointer to the current payload of the provided packet. /// +/// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. +/// /// The returned memory can be changed and will be valid until a new payload is set. #[no_mangle] pub unsafe extern "C" fn sp_packet_get_payload( packet: NonNull, ) -> ByteSlice { - unsafe { ByteSlice::from_slice(&mut (*packet.as_ptr()).payload) } + unsafe { + match &mut (*packet.as_ptr()).payload { + None => ByteSlice::INVALID, + Some(payload) => ByteSlice::from_slice(payload), + } + } } /// Sets the payload of the provided packet to the provided data. @@ -60,7 +66,13 @@ pub unsafe extern "C" fn sp_packet_set_payload( packet: NonNull, data: ByteSlice, ) { - unsafe { (*packet.as_ptr()).payload = data.as_slice().to_vec() } + unsafe { + (*packet.as_ptr()).payload = if data == ByteSlice::INVALID { + None + } else { + Some(data.as_slice().to_vec()) + } + } } /// Serialize the packet into the provided buffer. diff --git a/src/udp.rs b/src/udp.rs index 7c2ab2f..fe9f876 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -136,7 +136,7 @@ pub unsafe extern "C" fn sp_udp_send_header( ) -> bool { let packet = Packet { header, - payload: vec![], + payload: None, }; unsafe { udp_connection.as_ref() } .send(&Vec::from(packet)) From 0c6efcee561fa4294e310ac2fba7a8e61f469803 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 15 May 2025 23:48:03 +0200 Subject: [PATCH 14/76] add example for reading packages, fix functions return pointers to copy on stack --- example/Makefile | 2 +- example/src/header_logger.c | 76 +++++++++++++++++++++++ example/src/moving_line.c | 6 +- src/commands/bitvec_command.rs | 2 +- src/commands/brightness_grid_command.rs | 2 +- src/commands/char_grid_command.rs | 2 +- src/commands/cp437_grid_command.rs | 2 +- src/commands/global_brightness_command.rs | 2 +- src/packet.rs | 2 +- 9 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 example/src/header_logger.c diff --git a/example/Makefile b/example/Makefile index 32e6091..0b91165 100644 --- a/example/Makefile +++ b/example/Makefile @@ -10,7 +10,7 @@ export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include override CFG_MUSL := $(if $(CFG_MUSL),$(CFG_MUSL),$(if $(MUSL),$(MUSL),0)) override CFG_PROFILE := $(if $(CFG_PROFILE),$(CFG_PROFILE),$(if $(PROFILE),$(PROFILE),release)) -CCFLAGS += -Wall -fwhole-program -fPIE -pie +CCFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag diff --git a/example/src/header_logger.c b/example/src/header_logger.c new file mode 100644 index 0000000..97cd3dd --- /dev/null +++ b/example/src/header_logger.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include "servicepoint.h" + +#define DEFAULT_LISTEN_IP "127.0.0.1" + +void handle_error(const char *msg) { + perror(msg); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) { + init_env_logger(); + + int udp_socket = socket(AF_INET, SOCK_DGRAM, 0); + if (udp_socket == -1) + handle_error("socket could not be created\n"); + + char *listen_addr_arg; + if (argc > 1) { + listen_addr_arg = argv[1]; + } else { + listen_addr_arg = DEFAULT_LISTEN_IP; + } + + struct in_addr addr; + if (inet_aton(listen_addr_arg, &addr) == 0) + handle_error("listen ip could not be parsed\n"); + + struct sockaddr_in sockaddrIn = { + .sin_addr = addr, + .sin_family = AF_INET, + .sin_port = htons(2342), + }; + memset(sockaddrIn.sin_zero, 0, sizeof(sockaddrIn.sin_zero)); + + if (bind(udp_socket, (struct sockaddr *) &sockaddrIn, sizeof(sockaddrIn)) == -1) + handle_error("could not bind socket\n"); + + printf("socket ready to receive on %s\n", listen_addr_arg); + + uint8_t buffer[10000]; + bool done = false; + while (!done) { + memset(buffer, 0, sizeof(buffer)); + + ssize_t num_bytes = recv(udp_socket, (void *) buffer, sizeof(buffer), 0); + if (num_bytes == -1) + handle_error("could not read from client"); + + Packet *packet = sp_packet_try_load((ByteSlice) { + .start = buffer, + .length = num_bytes, + }); + if (packet == NULL) { + printf("received invalid packet\n"); + continue; + } + + struct Header *header = sp_packet_get_header(packet); + ByteSlice payload = sp_packet_get_payload(packet); + printf("Received packet: cc=%d, a=%d, b=%d, c=%d, d=%d, payload=%p (len %zu)\n", + header->command_code, header->a, header->b, header->c, header->d, + payload.start, payload.length); + + done = header->command_code == COMMAND_CODE_HARD_RESET; + sp_packet_free(packet); + } + + exit(EXIT_SUCCESS); +} diff --git a/example/src/moving_line.c b/example/src/moving_line.c index 688f249..35ba20d 100644 --- a/example/src/moving_line.c +++ b/example/src/moving_line.c @@ -1,15 +1,15 @@ #include "servicepoint.h" #include "helpers.h" -int main() { +int main(void) { sock_init(); int result = 0; Bitmap *bitmap = sp_bitmap_new_max_sized(); - for (int x = 0; x < sp_bitmap_width(bitmap); x++) { + for (size_t x = 0; x < sp_bitmap_width(bitmap); x++) { sp_bitmap_fill(bitmap, false); - for (int y = 0; y < sp_bitmap_height(bitmap); y++) { + for (size_t y = 0; y < sp_bitmap_height(bitmap); y++) { sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true); } diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index f2f667c..b49cdc5 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -65,7 +65,7 @@ pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { pub unsafe extern "C" fn sp_cmd_bitvec_get( mut command: NonNull, ) -> *mut DisplayBitVec { - &mut unsafe { command.as_mut() }.bitvec + unsafe { &mut command.as_mut().bitvec } } /// Moves the provided [BitVec] to be contained in the [BitVecCommand]. diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 80e1b89..7410f2d 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -76,7 +76,7 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_set( pub unsafe extern "C" fn sp_cmd_brightness_grid_get( mut command: NonNull, ) -> *mut BrightnessGrid { - &mut unsafe { command.as_mut() }.grid + unsafe { &mut command.as_mut().grid } } /// Overwrites the origin field of the [BrightnessGridCommand]. diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 8a220a5..1fbd66c 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -74,7 +74,7 @@ pub unsafe extern "C" fn sp_cmd_char_grid_set( pub unsafe extern "C" fn sp_cmd_char_grid_get( mut command: NonNull, ) -> *mut CharGrid { - &mut unsafe { command.as_mut() }.grid + unsafe { &mut command.as_mut().grid } } /// Reads the origin field of the [CharGridCommand]. diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index f653ef5..f034c6d 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -83,7 +83,7 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_set( pub unsafe extern "C" fn sp_cmd_cp437_grid_get( mut command: NonNull, ) -> *mut Cp437Grid { - &mut unsafe { command.as_mut() }.grid + unsafe { &mut command.as_mut().grid } } /// Gets the origin field of the [Cp437GridCommand]. diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 063dea7..6cb777f 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -53,5 +53,5 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_set( pub unsafe extern "C" fn sp_cmd_brightness_global_get( mut command: NonNull, ) -> *mut Brightness { - &mut unsafe { command.as_mut() }.brightness + unsafe { &mut command.as_mut().brightness } } diff --git a/src/packet.rs b/src/packet.rs index 2fdbefc..b1269df 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -38,7 +38,7 @@ pub unsafe extern "C" fn sp_packet_from_parts( pub unsafe extern "C" fn sp_packet_get_header( packet: NonNull, ) -> NonNull
{ - NonNull::from(&mut unsafe { (*packet.as_ptr()).header }) + NonNull::from(unsafe { &mut (*packet.as_ptr()).header }) } /// Returns a pointer to the current payload of the provided packet. From 389ced492ccbe1caffc52e445455f525d7bed0d8 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 16 May 2025 00:33:27 +0200 Subject: [PATCH 15/76] change typedef style, add optional logging --- Cargo.lock | 102 ++++++++++++- Cargo.toml | 11 +- cbindgen.toml | 2 +- example/Makefile | 4 +- include/servicepoint.h | 322 +++++++++++++++++++++-------------------- src/lib.rs | 8 + 6 files changed, 286 insertions(+), 163 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fcd4aef..030efe3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.18" @@ -173,6 +182,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -257,6 +289,30 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "jiff" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f02000660d30638906021176af16b17498bd0d12813dbfe7b276d8bc7f3c0806" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde", +] + +[[package]] +name = "jiff-static" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c30758ddd7188629c6713fc45d1188af4f44c90582311d0c8d8c9907f60c48" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "jobserver" version = "0.1.33" @@ -312,6 +368,21 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -342,6 +413,35 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "rust-lzma" version = "0.6.0" @@ -415,7 +515,6 @@ dependencies = [ [[package]] name = "servicepoint" version = "0.14.1" -source = "git+https://git.berlin.ccc.de/servicepoint/servicepoint.git?branch=next#d979d46d3e770222c5a7ca549e601b45eff48299" dependencies = [ "bitvec", "bzip2", @@ -432,6 +531,7 @@ name = "servicepoint_binding_c" version = "0.14.1" dependencies = [ "cbindgen", + "env_logger", "servicepoint", ] diff --git a/Cargo.toml b/Cargo.toml index fcb4eb8..4cbe337 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,14 +18,17 @@ crate-type = ["staticlib", "cdylib", "rlib"] cbindgen = "0.28.0" [dependencies.servicepoint] -# version = "0.14.1" +version = "0.14.1" default-features = false -git = "https://git.berlin.ccc.de/servicepoint/servicepoint.git" -branch = "next" + +[dependencies.env_logger] +version = "0.11.8" +optional = true [features] all_compressions = ["servicepoint/all_compressions"] -default = ["all_compressions", "servicepoint/default"] +default = ["all_compressions", "servicepoint/default", "env_logger"] +env_logger = ["dep:env_logger"] [lints.rust] missing-docs = "warn" diff --git a/cbindgen.toml b/cbindgen.toml index 9749726..6edd33f 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -16,7 +16,7 @@ line_endings = "LF" ############################# Codegen Options ################################## -style = "type" +style = "both" usize_is_size_t = true # this is needed because otherwise the order in the C bindings is different on different machines diff --git a/example/Makefile b/example/Makefile index 0b91165..e041b08 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,7 +1,7 @@ CARGO ?= cargo STRIP ?= strip -FEATURES := "" +FEATURES := "env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) @@ -99,7 +99,7 @@ clean-rust: .PHONY: all clean sizes $(_run_programs) clean-c clean-rust -$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(_sp_artifacts) +$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h src/helpers.h $(_sp_artifacts) mkdir -p out || true $(CC) $< \ -I $(SERVICEPOINT_HEADER_OUT) \ diff --git a/include/servicepoint.h b/include/servicepoint.h index 1a4d023..dec3af2 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -457,7 +457,7 @@ typedef struct ValueGrid_u8 ValueGrid_u8; * ByteSlice empty = {.start: NULL, .length = 0}; * ``` */ -typedef struct { +typedef struct ByteSlice { /** * The start address of the memory. */ @@ -486,7 +486,7 @@ typedef struct { * }).unwrap() * ``` */ -typedef ValueGrid_Brightness BrightnessGrid; +typedef struct ValueGrid_Brightness BrightnessGrid; /** * A display brightness value, checked for correct value range @@ -532,7 +532,7 @@ typedef uint8_t Brightness; * connection.send_command(command).unwrap() * ``` */ -typedef ValueGrid_char CharGrid; +typedef struct ValueGrid_char CharGrid; /** * Type alias for documenting the meaning of the u16 in enum values @@ -544,23 +544,23 @@ typedef size_t Offset; * * The encoding is currently not enforced. */ -typedef ValueGrid_u8 Cp437Grid; +typedef struct ValueGrid_u8 Cp437Grid; /** * Pointer to one of the available command structs. */ -typedef union { +typedef union CommandUnion { uint8_t *null; - BitmapCommand */*notnull*/ bitmap; - BitVecCommand */*notnull*/ bitvec; - BrightnessGridCommand */*notnull*/ brightness_grid; - CharGridCommand */*notnull*/ char_grid; - Cp437GridCommand */*notnull*/ cp437_grid; - GlobalBrightnessCommand */*notnull*/ global_brightness; - ClearCommand */*notnull*/ clear; - BitmapLegacyCommand */*notnull*/ bitmap_legacy; - HardResetCommand */*notnull*/ hard_reset; - FadeOutCommand */*notnull*/ fade_out; + struct BitmapCommand */*notnull*/ bitmap; + struct BitVecCommand */*notnull*/ bitvec; + struct BrightnessGridCommand */*notnull*/ brightness_grid; + struct CharGridCommand */*notnull*/ char_grid; + struct Cp437GridCommand */*notnull*/ cp437_grid; + struct GlobalBrightnessCommand */*notnull*/ global_brightness; + struct ClearCommand */*notnull*/ clear; + struct BitmapLegacyCommand */*notnull*/ bitmap_legacy; + struct HardResetCommand */*notnull*/ hard_reset; + struct FadeOutCommand */*notnull*/ fade_out; } CommandUnion; /** @@ -570,7 +570,7 @@ typedef union { * * Rust equivalent: [TypedCommand]. */ -typedef struct { +typedef struct Command { /** * Specifies which kind of command struct is contained in `data` */ @@ -578,7 +578,7 @@ typedef struct { /** * The pointer to the command struct */ - CommandUnion data; + union CommandUnion data; } Command; /** @@ -591,7 +591,7 @@ typedef struct { * * The contained values are in platform endian-ness and may need to be converted before sending. */ -typedef struct { +typedef struct Header { /** * The first two bytes specify which command this packet represents. */ @@ -627,10 +627,12 @@ typedef struct { extern "C" { #endif // __cplusplus +void init_env_logger(void); + /** * Clones a [Bitmap]. */ -Bitmap */*notnull*/ sp_bitmap_clone(Bitmap */*notnull*/ bitmap); +struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap); /** * Sets the state of all pixels in the [Bitmap]. @@ -640,12 +642,12 @@ Bitmap */*notnull*/ sp_bitmap_clone(Bitmap */*notnull*/ bitmap); * - `bitmap`: instance to write to * - `value`: the value to set all pixels to */ -void sp_bitmap_fill(Bitmap */*notnull*/ bitmap, bool value); +void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value); /** * Deallocates a [Bitmap]. */ -void sp_bitmap_free(Bitmap */*notnull*/ bitmap); +void sp_bitmap_free(struct Bitmap */*notnull*/ bitmap); /** * Tries to convert the BitVec to a Bitmap. @@ -654,7 +656,7 @@ void sp_bitmap_free(Bitmap */*notnull*/ bitmap); * * Returns NULL in case of error. */ -Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); +struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** * Gets the current value at the specified position in the [Bitmap]. @@ -668,7 +670,7 @@ Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * * - when accessing `x` or `y` out of bounds */ -bool sp_bitmap_get(Bitmap */*notnull*/ bitmap, size_t x, size_t y); +bool sp_bitmap_get(struct Bitmap */*notnull*/ bitmap, size_t x, size_t y); /** * Gets the height in pixels of the [Bitmap] instance. @@ -677,12 +679,12 @@ bool sp_bitmap_get(Bitmap */*notnull*/ bitmap, size_t x, size_t y); * * - `bitmap`: instance to read from */ -size_t sp_bitmap_height(Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_height(struct Bitmap */*notnull*/ bitmap); /** * Consumes the Bitmap and returns the contained BitVec */ -BitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); +BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); /** * Creates a [BitmapCommand] and immediately turns that into a [Packet]. @@ -691,10 +693,10 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(Bitmap */*notnull*/ bitmap); * * Returns NULL in case of an error. */ -Packet *sp_bitmap_into_packet(Bitmap */*notnull*/ bitmap, - size_t x, - size_t y, - CompressionCode compression); +struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, + size_t x, + size_t y, + CompressionCode compression); /** * Loads a [Bitmap] with the specified dimensions from the provided data. @@ -706,9 +708,9 @@ Packet *sp_bitmap_into_packet(Bitmap */*notnull*/ bitmap, * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. */ -Bitmap *sp_bitmap_load(size_t width, - size_t height, - ByteSlice data); +struct Bitmap *sp_bitmap_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Bitmap] with the specified dimensions. @@ -735,14 +737,14 @@ Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_free(grid); * ``` */ -Bitmap *sp_bitmap_new(size_t width, size_t height); +struct Bitmap *sp_bitmap_new(size_t width, size_t height); /** * Creates a new [Bitmap] with a size matching the screen. * * returns: [Bitmap] initialized to all pixels off. */ -Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); +struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** * Sets the value of the specified position in the [Bitmap]. @@ -757,14 +759,17 @@ Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds */ -void sp_bitmap_set(Bitmap */*notnull*/ bitmap, size_t x, size_t y, bool value); +void sp_bitmap_set(struct Bitmap */*notnull*/ bitmap, + size_t x, + size_t y, + bool value); /** * Gets an unsafe reference to the data of the [Bitmap] instance. * * The returned memory is valid for the lifetime of the bitmap. */ -ByteSlice sp_bitmap_unsafe_data_ref(Bitmap */*notnull*/ bitmap); +struct ByteSlice sp_bitmap_unsafe_data_ref(struct Bitmap */*notnull*/ bitmap); /** * Gets the width in pixels of the [Bitmap] instance. @@ -783,7 +788,7 @@ ByteSlice sp_bitmap_unsafe_data_ref(Bitmap */*notnull*/ bitmap); * * - `bitmap` points to a valid [Bitmap] */ -size_t sp_bitmap_width(Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); /** * Clones a [DisplayBitVec]. @@ -828,10 +833,10 @@ bool sp_bitvec_get(BitVec */*notnull*/ bit_vec, size_t index); * * Returns NULL in case of an error. */ -Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); +struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); /** * Returns true if length is 0. @@ -856,7 +861,7 @@ size_t sp_bitvec_len(BitVec */*notnull*/ bit_vec); * * returns: [DisplayBitVec] instance containing data. */ -BitVec */*notnull*/ sp_bitvec_load(ByteSlice data); +BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); /** * Creates a new [DisplayBitVec] instance. @@ -897,7 +902,7 @@ void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value); * * - `bit_vec`: instance to write to */ -ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); +struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); /** * Clones a [BrightnessGrid]. @@ -955,9 +960,9 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ brightness_grid); * * Returns NULL in case of an error. */ -Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -968,7 +973,7 @@ Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, */ BrightnessGrid *sp_brightness_grid_load(size_t width, size_t height, - ByteSlice data); + struct ByteSlice data); /** * Creates a new [BrightnessGrid] with the specified dimensions. @@ -1022,7 +1027,7 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ brightness_grid, * * returns: slice of bytes underlying the `brightness_grid`. */ -ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid); +struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid); /** * Gets the width of the [BrightnessGrid] instance. @@ -1085,16 +1090,16 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ char_grid); * * Returns NULL in case of an error. */ -Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [CharGrid] with the specified dimensions from the provided data. * * returns: new CharGrid or NULL in case of an error */ -CharGrid *sp_char_grid_load(size_t width, size_t height, ByteSlice data); +CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); /** * Creates a new [CharGrid] with the specified dimensions. @@ -1146,12 +1151,12 @@ size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); * * returns: a new [BitmapCommand] instance. */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(BitmapCommand */*notnull*/ command); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ command); /** * Deallocates a [BitmapCommand] instance. */ -void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); +void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ command); /** * Move the provided [Bitmap] into a new [BitmapCommand], @@ -1159,7 +1164,7 @@ void sp_cmd_bitmap_free(BitmapCommand */*notnull*/ command); * * Rust equivalent: `BitmapCommand::from(bitmap)` */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** * Returns a pointer to the provided `BitmapCommand`. @@ -1169,17 +1174,17 @@ BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(Bitmap */*notnull*/ bitmap) * - The returned bitmap inherits the lifetime of the command in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. */ -Bitmap */*notnull*/ sp_cmd_bitmap_get(BitmapCommand */*notnull*/ command); +struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ command); /** * Reads the compression kind of the [BitmapCommand]. */ -CompressionCode sp_cmd_bitmap_get_compression(BitmapCommand */*notnull*/ command); +CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ command); /** * Reads the origin field of the [BitmapCommand]. */ -void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, +void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1190,27 +1195,27 @@ void sp_cmd_bitmap_get_origin(BitmapCommand */*notnull*/ command, * * Returns: a new [BitmapCommand] instance. */ -BitmapCommand */*notnull*/ sp_cmd_bitmap_new(Bitmap */*notnull*/ bitmap, - size_t origin_x, - size_t origin_y, - CompressionCode compression); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, + size_t origin_x, + size_t origin_y, + CompressionCode compression); /** * Moves the provided [Bitmap] to be contained in the [BitmapCommand]. */ -void sp_cmd_bitmap_set(BitmapCommand */*notnull*/ command, - Bitmap */*notnull*/ bitmap); +void sp_cmd_bitmap_set(struct BitmapCommand */*notnull*/ command, + struct Bitmap */*notnull*/ bitmap); /** * Overwrites the compression kind of the [BitmapCommand]. */ -void sp_cmd_bitmap_set_compression(BitmapCommand */*notnull*/ command, +void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ command, CompressionCode compression); /** * Overwrites the origin field of the [BitmapCommand]. */ -void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, +void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1219,39 +1224,39 @@ void sp_cmd_bitmap_set_origin(BitmapCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_bitmap_try_into_packet(BitmapCommand */*notnull*/ command); +struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** * Clones an [BitVecCommand] instance. * * returns: a new [BitVecCommand] instance. */ -BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(BitVecCommand */*notnull*/ command); +struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ command); /** * Deallocates a [BitVecCommand]. */ -void sp_cmd_bitvec_free(BitVecCommand */*notnull*/ command); +void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ command); /** * Returns a pointer to the [BitVec] contained in the [BitVecCommand]. */ -BitVec *sp_cmd_bitvec_get(BitVecCommand */*notnull*/ command); +BitVec *sp_cmd_bitvec_get(struct BitVecCommand */*notnull*/ command); /** * Reads the compression kind of the [BitVecCommand]. */ -CompressionCode sp_cmd_bitvec_get_compression(BitVecCommand */*notnull*/ command); +CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ command); /** * Reads the offset field of the [BitVecCommand]. */ -Offset sp_cmd_bitvec_get_offset(BitVecCommand */*notnull*/ command); +Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ command); /** * Returns the [BinaryOperation] of the command. */ -BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); +BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ command); /** * Set pixel data starting at the pixel offset on screen. @@ -1267,33 +1272,33 @@ BinaryOperation sp_cmd_bitvec_get_operation(BitVecCommand */*notnull*/ command); * * The contained [`DisplayBitVec`] is always uncompressed. */ -BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); +struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); /** * Moves the provided [BitVec] to be contained in the [BitVecCommand]. */ -void sp_cmd_bitvec_set(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set(struct BitVecCommand */*notnull*/ command, BitVec */*notnull*/ bitvec); /** * Overwrites the compression kind of the [BitVecCommand]. */ -void sp_cmd_bitvec_set_compression(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ command, CompressionCode compression); /** * Overwrites the offset field of the [BitVecCommand]. */ -void sp_cmd_bitvec_set_offset(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ command, Offset offset); /** * Overwrites the [BinaryOperation] of the command. */ -void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, +void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ command, BinaryOperation operation); /** @@ -1301,32 +1306,32 @@ void sp_cmd_bitvec_set_operation(BitVecCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_bitvec_try_into_packet(BitVecCommand */*notnull*/ command); +struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** * Clones an [GlobalBrightnessCommand] instance. * * returns: a new [GlobalBrightnessCommand] instance. */ -GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(GlobalBrightnessCommand */*notnull*/ command); +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ command); -void sp_cmd_brightness_global_free(BitmapCommand */*notnull*/ command); +void sp_cmd_brightness_global_free(struct BitmapCommand */*notnull*/ command); -Brightness *sp_cmd_brightness_global_get(GlobalBrightnessCommand */*notnull*/ command); +Brightness *sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); -Packet */*notnull*/ sp_cmd_brightness_global_into_packet(GlobalBrightnessCommand */*notnull*/ command); +struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); /** * Set the brightness of all tiles to the same value. * * Returns: a new [GlobalBrightnessCommand] instance. */ -GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** * Moves the provided bitmap to be contained in the command. */ -void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, +void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ command, Brightness brightness); /** @@ -1334,28 +1339,28 @@ void sp_cmd_brightness_global_set(GlobalBrightnessCommand */*notnull*/ command, * * returns: a new [BrightnessGridCommand] instance. */ -BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(BrightnessGridCommand */*notnull*/ command); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ command); /** * Deallocates a [BitmapCommand]. */ -void sp_cmd_brightness_grid_free(BitmapCommand */*notnull*/ command); +void sp_cmd_brightness_grid_free(struct BitmapCommand */*notnull*/ command); /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. */ -BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); /** * Returns a pointer to the [BrightnessGrid] contained in the [BrightnessGridCommand]. */ -BrightnessGrid *sp_cmd_brightness_grid_get(BrightnessGridCommand */*notnull*/ command); +BrightnessGrid *sp_cmd_brightness_grid_get(struct BrightnessGridCommand */*notnull*/ command); /** * Overwrites the origin field of the [BrightnessGridCommand]. */ -void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ command, +void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1364,7 +1369,7 @@ void sp_cmd_brightness_grid_get_origin(BrightnessGridCommand */*notnull*/ comman * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ command); +struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); /** * Set the brightness of individual tiles in a rectangular area of the display. @@ -1373,20 +1378,20 @@ Packet *sp_cmd_brightness_grid_into_packet(BrightnessGridCommand */*notnull*/ co * * Returns: a new [BrightnessGridCommand] instance. */ -BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. */ -void sp_cmd_brightness_grid_set(BrightnessGridCommand */*notnull*/ command, +void sp_cmd_brightness_grid_set(struct BrightnessGridCommand */*notnull*/ command, BrightnessGrid */*notnull*/ grid); /** * Reads the origin field of the [BrightnessGridCommand]. */ -void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ command, +void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1395,28 +1400,28 @@ void sp_cmd_brightness_grid_set_origin(BrightnessGridCommand */*notnull*/ comman * * returns: a new [CharGridCommand] instance. */ -CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(CharGridCommand */*notnull*/ command); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ command); /** * Deallocates a [BitmapCommand]. */ -void sp_cmd_char_grid_free(BitmapCommand */*notnull*/ command); +void sp_cmd_char_grid_free(struct BitmapCommand */*notnull*/ command); /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. */ -CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); /** * Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. */ -CharGrid *sp_cmd_char_grid_get(CharGridCommand */*notnull*/ command); +CharGrid *sp_cmd_char_grid_get(struct CharGridCommand */*notnull*/ command); /** * Reads the origin field of the [CharGridCommand]. */ -void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, +void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1427,20 +1432,20 @@ void sp_cmd_char_grid_get_origin(CharGridCommand */*notnull*/ command, * * Returns: a new [CharGridCommand] instance. */ -CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Moves the provided [CharGrid] to be contained in the [CharGridCommand]. */ -void sp_cmd_char_grid_set(CharGridCommand */*notnull*/ command, +void sp_cmd_char_grid_set(struct CharGridCommand */*notnull*/ command, CharGrid */*notnull*/ grid); /** * Overwrites the origin field of the [CharGridCommand]. */ -void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, +void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1449,12 +1454,12 @@ void sp_cmd_char_grid_set_origin(CharGridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_char_grid_try_into_packet(CharGridCommand */*notnull*/ command); +struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** * Deallocates a [ClearCommand]. */ -void sp_cmd_clear_free(ClearCommand */*notnull*/ command); +void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command); /** * Set all pixels to the off state. @@ -1463,25 +1468,25 @@ void sp_cmd_clear_free(ClearCommand */*notnull*/ command); * * Returns: a new [ClearCommand] instance. */ -ClearCommand */*notnull*/ sp_cmd_clear_new(void); +struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); /** * Clones an [Cp437GridCommand] instance. * * returns: a new [Cp437GridCommand] instance. */ -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(Cp437GridCommand */*notnull*/ command); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ command); /** * Deallocates a [Cp437GridCommand]. */ -void sp_cmd_cp437_grid_free(BitmapCommand */*notnull*/ command); +void sp_cmd_cp437_grid_free(struct BitmapCommand */*notnull*/ command); /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. */ -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); /** * Show text on the screen. @@ -1491,14 +1496,14 @@ Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ * * [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 */ -Cp437Grid *sp_cmd_cp437_grid_get(Cp437GridCommand */*notnull*/ command); +Cp437Grid *sp_cmd_cp437_grid_get(struct Cp437GridCommand */*notnull*/ command); /** * Gets the origin field of the [Cp437GridCommand]. * * Rust equivalent: `cp437_command.origin` */ -void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, +void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); @@ -1509,16 +1514,16 @@ void sp_cmd_cp437_grid_get_origin(Cp437GridCommand */*notnull*/ command, * * The origin is relative to the top-left of the display. */ -Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Moves the provided bitmap into the provided command. * * This drops the previously contained [Cp437Grid]. */ -void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, +void sp_cmd_cp437_grid_set(struct Cp437GridCommand */*notnull*/ command, Cp437Grid */*notnull*/ grid); /** @@ -1526,7 +1531,7 @@ void sp_cmd_cp437_grid_set(Cp437GridCommand */*notnull*/ command, * * Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` */ -void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, +void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); @@ -1535,26 +1540,26 @@ void sp_cmd_cp437_grid_set_origin(Cp437GridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. */ -Packet *sp_cmd_cp437_grid_try_into_packet(Cp437GridCommand */*notnull*/ command); +struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** * Deallocates a [FadeOutCommand]. */ -void sp_cmd_fade_out_free(ClearCommand */*notnull*/ command); +void sp_cmd_fade_out_free(struct ClearCommand */*notnull*/ command); /** * A yet-to-be-tested command. * * Returns: a new [FadeOutCommand] instance. */ -FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); +struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); /** * Clones an [SPCommand] instance. * * returns: a new [SPCommand] instance. */ -Command sp_cmd_generic_clone(Command command); +struct Command sp_cmd_generic_clone(struct Command command); /** * Deallocates an [SPCommand]. @@ -1566,7 +1571,7 @@ Command sp_cmd_generic_clone(Command command); * sp_command_free(c); * ``` */ -void sp_cmd_generic_free(Command command); +void sp_cmd_generic_free(struct Command command); /** * Tries to turn a [SPCommand] into a [Packet]. @@ -1574,7 +1579,7 @@ void sp_cmd_generic_free(Command command); * * Returns tag [CommandTag::Invalid] in case of an error. */ -Packet *sp_cmd_generic_into_packet(Command command); +struct Packet *sp_cmd_generic_into_packet(struct Command command); /** * Tries to turn a [Packet] into a [SPCommand]. @@ -1583,12 +1588,12 @@ Packet *sp_cmd_generic_into_packet(Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. */ -Command *sp_cmd_generic_try_from_packet(Packet */*notnull*/ packet); +struct Command *sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** * Deallocates a [HardResetCommand]. */ -void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); +void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command); /** * Kills the udp daemon on the display, which usually results in a restart. @@ -1597,7 +1602,7 @@ void sp_cmd_hard_reset_free(ClearCommand */*notnull*/ command); * * Returns: a new [HardResetCommand] instance. */ -HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); +struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** * Clones a [Cp437Grid]. @@ -1651,14 +1656,16 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ cp437_grid); * * Returns NULL in case of an error. */ -Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. */ -Cp437Grid *sp_cp437_grid_load(size_t width, size_t height, ByteSlice data); +Cp437Grid *sp_cp437_grid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Cp437Grid] with the specified dimensions. @@ -1692,7 +1699,7 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ cp437_grid, * * The returned memory is valid for the lifetime of the grid. */ -ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid); +struct ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid); /** * Gets the width of the [Cp437Grid] instance. @@ -1708,26 +1715,27 @@ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); * * returns: a new [Packet] instance. */ -Packet */*notnull*/ sp_packet_clone(Packet */*notnull*/ packet); +struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ packet); /** * Deallocates a [Packet]. */ -void sp_packet_free(Packet */*notnull*/ packet); +void sp_packet_free(struct Packet */*notnull*/ packet); /** * Creates a raw [Packet] from parts. * * returns: new instance. Will never return null. */ -Packet */*notnull*/ sp_packet_from_parts(Header header, ByteSlice payload); +struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, + struct ByteSlice payload); /** * Returns a pointer to the header field of the provided packet. * * The returned header can be changed and will be valid for the lifetime of the packet. */ -Header */*notnull*/ sp_packet_get_header(Packet */*notnull*/ packet); +struct Header */*notnull*/ sp_packet_get_header(struct Packet */*notnull*/ packet); /** * Returns a pointer to the current payload of the provided packet. @@ -1736,7 +1744,7 @@ Header */*notnull*/ sp_packet_get_header(Packet */*notnull*/ packet); * * The returned memory can be changed and will be valid until a new payload is set. */ -ByteSlice sp_packet_get_payload(Packet */*notnull*/ packet); +struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); /** * Serialize the packet into the provided buffer. @@ -1745,21 +1753,23 @@ ByteSlice sp_packet_get_payload(Packet */*notnull*/ packet); * * - if the buffer is not big enough to hold header+payload. */ -void sp_packet_serialize_to(Packet */*notnull*/ packet, ByteSlice buffer); +void sp_packet_serialize_to(struct Packet */*notnull*/ packet, + struct ByteSlice buffer); /** * Sets the payload of the provided packet to the provided data. * * This makes previous payload pointers invalid. */ -void sp_packet_set_payload(Packet */*notnull*/ packet, ByteSlice data); +void sp_packet_set_payload(struct Packet */*notnull*/ packet, + struct ByteSlice data); /** * Tries to load a [Packet] from the passed array with the specified length. * * returns: NULL in case of an error, pointer to the allocated packet otherwise */ -Packet *sp_packet_try_load(ByteSlice data); +struct Packet *sp_packet_try_load(struct ByteSlice data); /** * Converts u16 into [CommandCode]. @@ -1772,7 +1782,7 @@ bool sp_u16_to_command_code(uint16_t code, /** * Closes and deallocates a [UdpSocket]. */ -void sp_udp_free(UdpSocket */*notnull*/ connection); +void sp_udp_free(struct UdpSocket */*notnull*/ connection); /** * Creates a new instance of [UdpSocket]. @@ -1787,7 +1797,7 @@ void sp_udp_free(UdpSocket */*notnull*/ connection); * sp_udp_send_command(connection, sp_command_clear()); * ``` */ -UdpSocket *sp_udp_open(char */*notnull*/ host); +struct UdpSocket *sp_udp_open(char */*notnull*/ host); /** * Creates a new instance of [UdpSocket]. @@ -1802,11 +1812,11 @@ UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` */ -UdpSocket *sp_udp_open_ipv4(uint8_t ip1, - uint8_t ip2, - uint8_t ip3, - uint8_t ip4, - uint16_t port); +struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, + uint8_t ip2, + uint8_t ip3, + uint8_t ip4, + uint16_t port); /** * Sends a [SPCommand] to the display using the [UdpSocket]. @@ -1821,7 +1831,8 @@ UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_command(UdpSocket */*notnull*/ connection, Command command); +bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, + struct Command command); /** * Sends a [Header] to the display using the [UdpSocket]. @@ -1834,7 +1845,8 @@ bool sp_udp_send_command(UdpSocket */*notnull*/ connection, Command command); * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` */ -bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header); +bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, + struct Header header); /** * Sends a [Packet] to the display using the [UdpSocket]. @@ -1843,8 +1855,8 @@ bool sp_udp_send_header(UdpSocket */*notnull*/ udp_connection, Header header); * * returns: true in case of success */ -bool sp_udp_send_packet(UdpSocket */*notnull*/ connection, - Packet */*notnull*/ packet); +bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, + struct Packet */*notnull*/ packet); #ifdef __cplusplus } // extern "C" diff --git a/src/lib.rs b/src/lib.rs index ea3f916..17c4b3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,3 +43,11 @@ pub struct UdpSocket; /// This is a type only used by cbindgen to have a type for pointers. pub struct DisplayBitVec; + +#[cfg(feature = "env_logger")] +pub mod feature_env_logger { + #[no_mangle] + pub unsafe extern "C" fn init_env_logger() { + env_logger::init(); + } +} From c9d2479f5e04a35c4df4b15725541b29316a7f52 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 18 May 2025 11:20:57 +0200 Subject: [PATCH 16/76] sp_cmd_generic_try_from_packet return struct directly --- example/src/header_logger.c | 13 +++++++++++-- include/servicepoint.h | 4 +++- src/commands/generic_command.rs | 15 ++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 97cd3dd..8f53be1 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "servicepoint.h" #define DEFAULT_LISTEN_IP "127.0.0.1" @@ -63,14 +64,22 @@ int main(int argc, char **argv) { } struct Header *header = sp_packet_get_header(packet); + done = header->command_code == COMMAND_CODE_HARD_RESET; + ByteSlice payload = sp_packet_get_payload(packet); printf("Received packet: cc=%d, a=%d, b=%d, c=%d, d=%d, payload=%p (len %zu)\n", header->command_code, header->a, header->b, header->c, header->d, payload.start, payload.length); - done = header->command_code == COMMAND_CODE_HARD_RESET; - sp_packet_free(packet); + struct Command command = sp_cmd_generic_try_from_packet(packet); + if (command.tag == COMMAND_TAG_INVALID) { + printf("received invalid command\n"); + continue; + } + + sp_cmd_generic_free(command); } + close(udp_socket); exit(EXIT_SUCCESS); } diff --git a/include/servicepoint.h b/include/servicepoint.h index dec3af2..0fc72d5 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1564,6 +1564,8 @@ struct Command sp_cmd_generic_clone(struct Command command); /** * Deallocates an [SPCommand]. * + * Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. + * * # Examples * * ```C @@ -1588,7 +1590,7 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. */ -struct Command *sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); +struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** * Deallocates a [HardResetCommand]. diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index bdeab5e..851e897 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -74,10 +74,10 @@ impl SPCommand { #[no_mangle] pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( packet: NonNull, -) -> *mut SPCommand { +) -> SPCommand { let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; - heap_move_ok(servicepoint::TypedCommand::try_from(packet).map(|value| { - match value { + servicepoint::TypedCommand::try_from(packet) + .map(|value| match value { TypedCommand::Clear(clear) => SPCommand { tag: CommandTag::Clear, data: CommandUnion { @@ -139,8 +139,11 @@ pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( bitmap_legacy: heap_move_nonnull(bitmap_legacy), }, }, - } - })) + }) + .unwrap_or_else(move |_| SPCommand { + tag: CommandTag::Invalid, + data: CommandUnion { null: null_mut() }, + }) } /// Clones an [SPCommand] instance. @@ -219,6 +222,8 @@ 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 /// From 01b31690204f91828c716a3d41f10fa8a1033a03 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 24 May 2025 13:50:01 +0200 Subject: [PATCH 17/76] sp_cmd_brightness_global_get returns value --- example/Makefile | 48 ++++---- example/src/header_logger.c | 127 +++++++++++++++++++++- include/servicepoint.h | 7 +- src/commands/global_brightness_command.rs | 4 +- src/lib.rs | 4 +- 5 files changed, 161 insertions(+), 29 deletions(-) diff --git a/example/Makefile b/example/Makefile index e041b08..0a865ac 100644 --- a/example/Makefile +++ b/example/Makefile @@ -14,23 +14,11 @@ CCFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag -ifeq ($(CFG_MUSL), 1) - TARGET ?= x86_64-unknown-linux-musl - CC ?= musl-gcc - CCFLAGS += -static -lservicepoint_binding_c - RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static -else - TARGET ?= x86_64-unknown-linux-gnu - CC ?= gcc - #CCFLAGS += -shared - CCFLAGS += -Wl,-Bstatic -lservicepoint_binding_c -Wl,-Bdynamic -endif - #ifeq ($(CFG_PROFILE), size-optimized) # CCFLAGS += -nodefaultlibs -lc #endif -RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE) +STATIC_LINK_LIBS := -lservicepoint_binding_c ifeq ($(CFG_PROFILE), size-optimized) CARGO_PROFILE := size-optimized @@ -53,16 +41,34 @@ ifeq ($(CFG_PROFILE), size-optimized) -C link-arg=-z,norelro \ -C panic=abort #-C link-arg=--hash-style=gnu -else ifeq ($(CFG_PROFILE), release) - CARGO_PROFILE := release - CCFLAGS += -O2 -else ifeq ($(CFG_PROFILE), debug) - CCFLAGS += -Og - CARGO_PROFILE := dev else - CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized") + FEATURES := $(FEATURES),all_compressions + STATIC_LINK_LIBS += -llzma + ifeq ($(CFG_PROFILE), release) + CARGO_PROFILE := release + CCFLAGS += -O2 + else ifeq ($(CFG_PROFILE), debug) + CCFLAGS += -Og + CARGO_PROFILE := dev + else + CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized") + endif endif + +ifeq ($(CFG_MUSL), 1) + TARGET ?= x86_64-unknown-linux-musl + CC ?= musl-gcc + CCFLAGS += -static $(STATIC_LINK_LIBS) + RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static +else + TARGET ?= x86_64-unknown-linux-gnu + CC ?= gcc + #CCFLAGS += -shared + CCFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic +endif + + CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --profile=$(CARGO_PROFILE) \ --no-default-features \ @@ -78,6 +84,8 @@ ifeq ($(LTO), 1) CCFLAGS += -flto endif +RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE) + _c_src := $(wildcard ./src/*.c) _programs := $(basename $(notdir $(_c_src))) _bins := $(addprefix out/, $(_programs)) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 8f53be1..a4ea051 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -49,6 +49,7 @@ int main(int argc, char **argv) { bool done = false; while (!done) { memset(buffer, 0, sizeof(buffer)); + printf("\n"); ssize_t num_bytes = recv(udp_socket, (void *) buffer, sizeof(buffer), 0); if (num_bytes == -1) @@ -64,7 +65,6 @@ int main(int argc, char **argv) { } struct Header *header = sp_packet_get_header(packet); - done = header->command_code == COMMAND_CODE_HARD_RESET; ByteSlice payload = sp_packet_get_payload(packet); printf("Received packet: cc=%d, a=%d, b=%d, c=%d, d=%d, payload=%p (len %zu)\n", @@ -72,9 +72,128 @@ int main(int argc, char **argv) { payload.start, payload.length); struct Command command = sp_cmd_generic_try_from_packet(packet); - if (command.tag == COMMAND_TAG_INVALID) { - printf("received invalid command\n"); - continue; + switch (command.tag) { + case COMMAND_TAG_INVALID: { + printf("-> this is an invalid command\n"); + break; + } + case COMMAND_TAG_HARD_RESET: { + printf("-> HardReset command - exiting\n"); + done = true; + break; + } + case COMMAND_TAG_BITMAP: { + BitmapCommand *bitmapCommand = command.data.bitmap; + + CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); + + size_t x, y; + sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); + + Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); + size_t w = sp_bitmap_width(bitmap); + size_t h = sp_bitmap_height(bitmap); + + printf("-> BitmapCommand with params: x=%zu, y=%zu, w=%zu, h=%zu, compression=%hu\n", + x, y, w, h, compression); + break; + } + case COMMAND_TAG_BRIGHTNESS_GRID: { + BrightnessGridCommand *gridCommand = command.data.brightness_grid; + + size_t x, y; + sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); + + BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); + size_t w = sp_brightness_grid_width(grid); + size_t h = sp_brightness_grid_height(grid); + + printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CHAR_GRID: { + CharGridCommand *gridCommand = command.data.char_grid; + + size_t x, y; + sp_cmd_char_grid_get_origin(gridCommand, &x, &y); + + CharGrid *grid = sp_cmd_char_grid_get(gridCommand); + size_t w = sp_char_grid_width(grid); + size_t h = sp_char_grid_height(grid); + + printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CP437_GRID: { + Cp437GridCommand *gridCommand = command.data.cp437_grid; + + size_t x, y; + sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); + + Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); + size_t w = sp_cp437_grid_width(grid); + size_t h = sp_cp437_grid_height(grid); + + printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_BIT_VEC: { + BitVecCommand *bitvecCommand = command.data.bitvec; + + size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); + CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); + + BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); + char *operationText; + switch (operation) { + case BINARY_OPERATION_AND: + operationText = "and"; + break; + case BINARY_OPERATION_OR: + operationText = "or"; + break; + case BINARY_OPERATION_XOR: + operationText = "xor"; + break; + case BINARY_OPERATION_OVERWRITE: + operationText ="overwrite"; + break; + default: + operationText ="unknown"; + break; + } + + BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); + size_t len = sp_bitvec_len(bitvec); + + printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", + offset, len, compression, operationText); + break; + } + case COMMAND_TAG_CLEAR: { + printf("-> ClearCommand\n"); + break; + } + case COMMAND_TAG_BITMAP_LEGACY: { + printf("-> BitmapLinearLegacy\n"); + break; + } + case COMMAND_TAG_FADE_OUT:{ + printf("-> FadeOutCommand\n"); + break; + } + case COMMAND_TAG_GLOBAL_BRIGHTNESS: { + Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); + printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); + break; + } + default: { + printf("-> unknown command tag %d\n", command.tag); + break; + } } sp_cmd_generic_free(command); diff --git a/include/servicepoint.h b/include/servicepoint.h index 0fc72d5..0f269ea 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -279,7 +279,6 @@ typedef struct BitmapCommand BitmapCommand; * # use servicepoint::*; * # let connection = FakeConnection; * // this sends a packet that does nothing - * # #[allow(deprecated)] * connection.send_command(BitmapLegacyCommand).unwrap(); * ``` */ @@ -627,6 +626,10 @@ typedef struct Header { extern "C" { #endif // __cplusplus +/** + * Call this function at the beginning of main to enable rust logging controlled by the + * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). + */ void init_env_logger(void); /** @@ -1317,7 +1320,7 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struc void sp_cmd_brightness_global_free(struct BitmapCommand */*notnull*/ command); -Brightness *sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); +Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6cb777f..6b2643e 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -52,6 +52,6 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_set( #[no_mangle] pub unsafe extern "C" fn sp_cmd_brightness_global_get( mut command: NonNull, -) -> *mut Brightness { - unsafe { &mut command.as_mut().brightness } +) -> Brightness { + unsafe { command.as_mut().brightness } } diff --git a/src/lib.rs b/src/lib.rs index 17c4b3a..5c64328 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,9 @@ pub struct UdpSocket; pub struct DisplayBitVec; #[cfg(feature = "env_logger")] -pub mod feature_env_logger { +mod feature_env_logger { + /// Call this function at the beginning of main to enable rust logging controlled by the + /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). #[no_mangle] pub unsafe extern "C" fn init_env_logger() { env_logger::init(); From e5825819e69fc75f17f6ab15362304dc11aceda8 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 24 May 2025 13:50:21 +0200 Subject: [PATCH 18/76] update to servicepoint v0.15.0 --- Cargo.lock | 4 +++- Cargo.toml | 2 +- include/servicepoint.h | 11 +++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 030efe3..36e4b2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -514,7 +514,9 @@ dependencies = [ [[package]] name = "servicepoint" -version = "0.14.1" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91a33bff7f9db5008748b23ca0c906c276fe00694390b681f004a55968a42cfe" dependencies = [ "bitvec", "bzip2", diff --git a/Cargo.toml b/Cargo.toml index 4cbe337..dd3d637 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ crate-type = ["staticlib", "cdylib", "rlib"] cbindgen = "0.28.0" [dependencies.servicepoint] -version = "0.14.1" +version = "0.15.0" default-features = false [dependencies.env_logger] diff --git a/include/servicepoint.h b/include/servicepoint.h index 0f269ea..3721527 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -159,23 +159,28 @@ typedef uint8_t CommandTag; * * # Examples * + * create command without payload compression * ```rust * # use servicepoint::*; - * // create command without payload compression * # let pixels = Bitmap::max_sized(); * _ = BitmapCommand { * origin: Origin::ZERO, * bitmap: pixels, * compression: CompressionCode::Uncompressed * }; + * ``` * - * // create command with payload compressed with lzma and appropriate header flags + * create command with payload compressed with lzma and appropriate header flags + * ```rust + * # use servicepoint::*; * # let pixels = Bitmap::max_sized(); + * # #[cfg(feature = "compression_lzma")] { * _ = BitmapCommand { * origin: Origin::ZERO, * bitmap: pixels, * compression: CompressionCode::Lzma * }; + * # } * ``` */ enum CompressionCode @@ -331,8 +336,10 @@ typedef struct ClearCommand ClearCommand; * # use servicepoint::*; * # let connection = FakeConnection; * let grid = CharGrid::from("Hello,\nWorld!"); + * # #[cfg(feature = "cp437")] { * let grid = Cp437Grid::from(&grid); * connection.send_command(Cp437GridCommand{ origin: Origin::ZERO, grid }).expect("send failed"); + * # } * ``` * * ```rust From e7426bdabedacb8f6538c00105beb251d32828e0 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 24 May 2025 13:52:04 +0200 Subject: [PATCH 19/76] update cargo packages and flake --- Cargo.lock | 52 +++++++++++++++++++++++++++++----------------------- flake.lock | 6 +++--- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 36e4b2a..f7deee5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,20 +58,20 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "3.0.7" +version = "3.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +checksum = "6680de5231bd6ee4c6191b8a1325daa282b415391ec9d3a37bd34f2060dc73fa" dependencies = [ "anstyle", - "once_cell", + "once_cell_polyfill", "windows-sys", ] [[package]] name = "bitflags" -version = "2.9.0" +version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" [[package]] name = "bitvec" @@ -125,9 +125,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.21" +version = "1.2.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8691782945451c1c383942c4874dbe63814f61cb57ef773cda2972682b7bb3c0" +checksum = "16595d3be041c03b09d08d0858631facccee9221e579704070e6e9e4915d3bc7" dependencies = [ "jobserver", "libc", @@ -142,18 +142,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.37" +version = "4.5.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eccb054f56cbd38340b380d4a8e69ef1f02f1af43db2f0cc817a4774d80ae071" +checksum = "ed93b9805f8ba930df42c2590f05453d5ec36cbb85d018868a5b24d31f6ac000" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" -version = "4.5.37" +version = "4.5.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd9466fac8543255d3b1fcad4762c5e116ffe808c8a3043d4263cd4fd4862a2" +checksum = "379026ff283facf611b0ea629334361c4211d1b12ee01024eec1591133b04120" dependencies = [ "anstream", "anstyle", @@ -213,9 +213,9 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "errno" -version = "0.3.11" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" +checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18" dependencies = [ "libc", "windows-sys", @@ -245,9 +245,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "getrandom" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" dependencies = [ "cfg-if", "libc", @@ -291,9 +291,9 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "jiff" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f02000660d30638906021176af16b17498bd0d12813dbfe7b276d8bc7f3c0806" +checksum = "a194df1107f33c79f4f93d02c80798520551949d59dfad22b6157048a88cca93" dependencies = [ "jiff-static", "log", @@ -304,9 +304,9 @@ dependencies = [ [[package]] name = "jiff-static" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c30758ddd7188629c6713fc45d1188af4f44c90582311d0c8d8c9907f60c48" +checksum = "6c6e1db7ed32c6c71b759497fae34bf7933636f75a251b9e736555da426f6442" dependencies = [ "proc-macro2", "quote", @@ -362,6 +362,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + [[package]] name = "pkg-config" version = "0.3.32" @@ -568,9 +574,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "tempfile" -version = "3.19.1" +version = "3.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" +checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" dependencies = [ "fastrand", "getrandom", @@ -742,9 +748,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.7.9" +version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9fb597c990f03753e08d3c29efbfcf2019a003b4bf4ba19225c158e1549f0f3" +checksum = "c06928c8748d81b05c9be96aad92e1b6ff01833332f281e8cfca3be4b35fc9ec" dependencies = [ "memchr", ] diff --git a/flake.lock b/flake.lock index 9e70e85..ad77a01 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1739357830, - "narHash": "sha256-9xim3nJJUFbVbJCz48UP4fGRStVW5nv4VdbimbKxJ3I=", + "lastModified": 1747862697, + "narHash": "sha256-U4HaNZ1W26cbOVm0Eb5OdGSnfQVWQKbLSPrSSa78KC0=", "owner": "nixos", "repo": "nixpkgs", - "rev": "0ff09db9d034a04acd4e8908820ba0b410d7a33a", + "rev": "2baa12ff69913392faf0ace833bc54bba297ea95", "type": "github" }, "original": { From 2d937b9c3c1ca7a82ec5a00c7842a8a8254d24fa Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 11:49:44 +0200 Subject: [PATCH 20/76] simplify conditional logic with constructed macro names based on https://make.mad-scientist.net/constructed-macro-names/ --- example/Makefile | 151 +++++++++++++++++------------------- example/src/header_logger.c | 2 +- flake.lock | 6 +- 3 files changed, 75 insertions(+), 84 deletions(-) diff --git a/example/Makefile b/example/Makefile index 0a865ac..c2e75bb 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,98 +1,89 @@ CARGO ?= cargo STRIP ?= strip +CC ?= gcc -FEATURES := "env_logger" +RUST_ARCH ?= x86_64 + +FEATURES := ""#"env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include -override CFG_MUSL := $(if $(CFG_MUSL),$(CFG_MUSL),$(if $(MUSL),$(MUSL),0)) -override CFG_PROFILE := $(if $(CFG_PROFILE),$(CFG_PROFILE),$(if $(PROFILE),$(PROFILE),release)) +# TODO: check if override is needed +override _libc := $(if $(MUSL),musl,gnu) +override _profile := $(if $(PROFILE),$(PROFILE),release) +override _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) +override _link_type := $(if $(LINK),$(LINK),dynamic) -CCFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie +# TODO: make LINK=static fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a -STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag - -#ifeq ($(CFG_PROFILE), size-optimized) -# CCFLAGS += -nodefaultlibs -lc -#endif - -STATIC_LINK_LIBS := -lservicepoint_binding_c - -ifeq ($(CFG_PROFILE), size-optimized) - CARGO_PROFILE := size-optimized - CCFLAGS += -Oz \ - -fwrapv -fomit-frame-pointer -fno-stack-protector\ - -fno-unroll-loops \ - -fno-unwind-tables -fno-asynchronous-unwind-tables \ - -fmerge-all-constants \ - -Wl,-z,norelro \ - -Wl,--hash-style=gnu \ - -fvisibility=hidden \ - -Bsymbolic \ - -Wl,--exclude-libs,ALL \ - -fno-ident \ - -fno-exceptions - CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ - -Zbuild-std-features="panic_immediate_abort" - RUSTFLAGS += -Zlocation-detail=none \ - -Zfmt-debug=none \ - -C link-arg=-z,norelro \ - -C panic=abort - #-C link-arg=--hash-style=gnu -else - FEATURES := $(FEATURES),all_compressions - STATIC_LINK_LIBS += -llzma - ifeq ($(CFG_PROFILE), release) - CARGO_PROFILE := release - CCFLAGS += -O2 - else ifeq ($(CFG_PROFILE), debug) - CCFLAGS += -Og - CARGO_PROFILE := dev - else - CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized") - endif +_all_profiles := release debug size-optimized +ifeq (,$(filter $(_all_profiles),$(_profile))) + _profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized") endif +STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag +STATIC_LINK_LIBS += -lservicepoint_binding_c -ifeq ($(CFG_MUSL), 1) - TARGET ?= x86_64-unknown-linux-musl - CC ?= musl-gcc - CCFLAGS += -static $(STATIC_LINK_LIBS) - RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static -else - TARGET ?= x86_64-unknown-linux-gnu - CC ?= gcc - #CCFLAGS += -shared - CCFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic -endif - +RUST_TARGET := $(RUST_ARCH)-unknown-linux-$(_libc) +size-optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ + -Zbuild-std-features="panic_immediate_abort" CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ - --profile=$(CARGO_PROFILE) \ + --profile=$(_rust_cli_profile) \ --no-default-features \ --features=$(FEATURES) \ - --target=$(TARGET) + --target=$(RUST_TARGET) \ + $($(_profile)_CARGOFLAGS) -ifneq ($(CFG_PROFILE), debug) - CCFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections +CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie +_no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections +size-optimized_CFLAGS += -Oz \ + -fwrapv -fomit-frame-pointer -fno-stack-protector\ + -fno-unroll-loops \ + -fno-unwind-tables -fno-asynchronous-unwind-tables \ + -fmerge-all-constants \ + -Wl,-z,norelro \ + -Wl,--hash-style=gnu \ + -fvisibility=hidden \ + -Bsymbolic \ + -Wl,--exclude-libs,ALL \ + -fno-ident \ + -fno-exceptions \ + $(_no_debug_cflags) +release_CFLAGS += -O2 \ + $(_no_debug_cflags) +debug_CFLAGS += -Og +static_CFLAGS += -static $(STATIC_LINK_LIBS) +dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic + +size-optimized_RUSTFLAGS += -Zlocation-detail=none \ + -Zfmt-debug=none \ + -C link-arg=-z,norelro \ + -C panic=abort + #-C link-arg=--hash-style=gnu +musl_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static + +RUSTFLAGS += $($(_libc)_RUSTFLAGS) $($(_profile)_RUSTFLAGS) $($(_link_type)_RUSTFLAGS) +ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif +CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) ifeq ($(LTO), 1) - CCFLAGS += -flto + CFLAGS += -flto endif -RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE) +RUST_TARGET_DIR := $(REPO_ROOT)/target/$(RUST_TARGET)/$(_profile) _c_src := $(wildcard ./src/*.c) _programs := $(basename $(notdir $(_c_src))) _bins := $(addprefix out/, $(_programs)) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) _run_programs := $(addprefix run_, $(_programs)) -_rs_src := $(wildcard ../src/**.rs) ../Cargo.lock ../Cargo.toml ../cbindgen.toml -_sp_artifacts := $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(RUST_TARGET_DIR)/libservicepoint_binding_c.a $(RUST_TARGET_DIR)/libservicepoint_binding_c.so + +# TODO: use cargo --target-dir to put rust binaries into same dir all: $(_bins) @@ -105,33 +96,33 @@ clean-rust: rm $(SERVICEPOINT_HEADER_OUT)/servicepoint.h || true cargo clean -.PHONY: all clean sizes $(_run_programs) clean-c clean-rust +.PHONY: all clean sizes $(_run_programs) clean-c clean-rust build-rust help FORCE -$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h src/helpers.h $(_sp_artifacts) +$(_unstripped_bins): out/%_unstripped: src/%.c src/helpers.h build-rust mkdir -p out || true $(CC) $< \ -I $(SERVICEPOINT_HEADER_OUT) \ -L $(RUST_TARGET_DIR) \ - $(CCFLAGS) \ + $(CFLAGS) \ -o $@ $(_bins): out/%: out/%_unstripped $(STRIP) $(STRIPFLAGS) $^ -o $@ -$(_sp_artifacts): $(_rs_src) +build-rust: FORCE mkdir -p $(SERVICEPOINT_HEADER_OUT) || true # generate servicepoint header and library to link against $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) -$(_run_programs): run_%: out/% FORCE - ./$< - -sizes: $(_bins) - ls -lB out - -analyze-size: out/$(BIN)_unstripped - nm --print-size --size-sort --reverse-sort --radix=d --demangle out/$(BIN)_unstripped \ - | awk '{size=$$2+0; print size "\t" $$4}' \ - | less - FORCE: ; + +help: + @echo "You have the choice of the following parameters:" + @echo "" + @echo "Variable | Description | Default | Values" + @echo "---------+-----------------------------------+-----------+---------------------------" + @echo "CARGO | which cargo binary to use | 'cargo' | 'rustup run nightly cargo'" + @echo "CC | which C compiler to use | 'gcc' | 'musl-gcc'" + @echo "STRIP | which strip command to use | 'strip' | -" + @echo "MUSL | if set, use musl instead of glibc | unset | '1'" + @echo "PROFILE | Compilation profile | 'release' | 'debug' or 'size-optimized'" diff --git a/example/src/header_logger.c b/example/src/header_logger.c index a4ea051..93c17dd 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -16,7 +16,7 @@ void handle_error(const char *msg) { } int main(int argc, char **argv) { - init_env_logger(); + //init_env_logger(); int udp_socket = socket(AF_INET, SOCK_DGRAM, 0); if (udp_socket == -1) diff --git a/flake.lock b/flake.lock index ad77a01..c5f76d2 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1747862697, - "narHash": "sha256-U4HaNZ1W26cbOVm0Eb5OdGSnfQVWQKbLSPrSSa78KC0=", + "lastModified": 1748037224, + "narHash": "sha256-92vihpZr6dwEMV6g98M5kHZIttrWahb9iRPBm1atcPk=", "owner": "nixos", "repo": "nixpkgs", - "rev": "2baa12ff69913392faf0ace833bc54bba297ea95", + "rev": "f09dede81861f3a83f7f06641ead34f02f37597f", "type": "github" }, "original": { From 6c3792330d127a3d681e849b6557ea55605a8c20 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 12:25:30 +0200 Subject: [PATCH 21/76] do not overwrite header if content did not change --- build.rs | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/build.rs b/build.rs index 83e9641..615d00f 100644 --- a/build.rs +++ b/build.rs @@ -4,32 +4,55 @@ //! the out directory. This can be used to use the build script as a command line tool from other //! build tools. -use std::{env, fs::copy}; +use std::{env, fs}; fn main() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - println!("cargo::rerun-if-changed={crate_dir}"); + println!("cargo::rerun-if-changed={crate_dir}/src"); + println!("cargo::rerun-if-changed={crate_dir}/build.rs"); + println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); + println!("cargo::rerun-if-changed={crate_dir}/Cargo.lock"); + println!("cargo::rerun-if-changed={crate_dir}/cbindgen.toml"); + println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); let config = cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") .unwrap(); - let output_dir = env::var("OUT_DIR").unwrap(); let header_file = output_dir.clone() + "/servicepoint.h"; - if let Ok(bindings) = cbindgen::generate_with_config(crate_dir, config) { - bindings.write_to_file(&header_file); - - println!("cargo:include={output_dir}"); - - println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); - if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { - let header_copy = header_out + "/servicepoint.h"; - println!("cargo:warning=Copying header to {header_copy}"); - copy(header_file, &header_copy).unwrap(); - println!("cargo::rerun-if-changed={header_copy}"); + let bindings = match cbindgen::generate_with_config(crate_dir, config) { + Ok(bindings) => bindings, + Err(e) => { + eprintln!("cargo:warning=Servicepoint header could not be generated: {e:?}"); + return; } - } else { - eprintln!("cargo:warning=Servicepoint header could not be generated"); + }; + + bindings.write_to_file(&header_file); + println!("cargo:include={output_dir}"); + + if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { + let header_copy = header_out + "/servicepoint.h"; + + if fs::exists(&header_copy).ok().unwrap_or(false) { + // check if content changed to prevent rebuild of dependents if not + + let mut bindings_text = Vec::new(); + bindings.write(&mut bindings_text); + + match fs::read(&header_copy) { + Ok(old_content) if old_content == bindings_text => { + println!("cargo:warning=Header did not change, not updating timestamp"); + return; + } + _ => {} + }; + } + + // file does not exist or is different + println!("cargo:warning=Copying header to {header_copy}"); + fs::copy(header_file, &header_copy).unwrap(); + println!("cargo::rerun-if-changed={header_copy}"); } } From c7d40b828b2c861ea590f3f6271264215de64b17 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 14:45:59 +0200 Subject: [PATCH 22/76] per-config build output using VPATH based on https://make.mad-scientist.net/papers/multi-architecture-builds/ --- .github/workflows/rust.yml | 7 +-- .gitignore | 3 +- example/Makefile | 96 ++++++++++++++++++-------------------- example/target.mk | 52 +++++++++++++++++++++ 4 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 example/target.mk diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e9947d5..f4af981 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -36,9 +36,9 @@ jobs: run: output=$(git status --porcelain) && [ -z "$output" ] - name: build example -- glibc release - run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=release + run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=release - name: build example -- glibc debug - run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=debug + run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=debug build-size-gnu-unstable: runs-on: ubuntu-latest @@ -53,4 +53,5 @@ jobs: run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update - name: build example -- glibc size-optimized - run: cd example && make clean && make TARGET=aarch64-unknown-linux-gnu PROFILE=size-optimized CARGO="rustup run nightly cargo" LTO=1 + run: cd example && make clean + && make LIBC=gnu LINK=dynamic PROFILE=size-optimized CARGO="rustup run nightly cargo" LTO=1 diff --git a/.gitignore b/.gitignore index f48287d..6f84fcd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ out .direnv .envrc result -mutants.* \ No newline at end of file +mutants.* +_out_* \ No newline at end of file diff --git a/example/Makefile b/example/Makefile index c2e75bb..b238200 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,33 +1,48 @@ +# based on https://make.mad-scientist.net/papers/multi-architecture-builds/ + +_libc := $(if $(LIBC),$(LIBC),gnu) +ifeq (,$(filter gnu musl,$(_libc))) + _link_type := $(error "LIBC has to be set to one of: gnu, musl") +endif + +_link_type := $(if $(LINK),$(LINK),dynamic) +ifeq (,$(filter dynamic static,$(_link_type))) + _link_type := $(error "LINK has to be set to one of: dynamic, static") +endif + +_profile := $(if $(PROFILE),$(PROFILE),release) +ifeq (,$(filter release debug size-optimized,$(_profile))) + _profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized") +endif + +ARCH ?= $(shell uname -m) +RUST_TARGET := $(ARCH)-unknown-linux-$(_libc) + +#----- Begin Boilerplate +ifeq (,$(filter _out_%,$(notdir $(CURDIR)))) + include target.mk +else +#----- End Boilerplate + +VPATH = $(SRCDIR) + CARGO ?= cargo STRIP ?= strip CC ?= gcc -RUST_ARCH ?= x86_64 - FEATURES := ""#"env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) -export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include # TODO: check if override is needed -override _libc := $(if $(MUSL),musl,gnu) -override _profile := $(if $(PROFILE),$(PROFILE),release) -override _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) -override _link_type := $(if $(LINK),$(LINK),dynamic) +_rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) -# TODO: make LINK=static fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a - -_all_profiles := release debug size-optimized -ifeq (,$(filter $(_all_profiles),$(_profile))) - _profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized") -endif +# TODO: `make LINK=static` fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag STATIC_LINK_LIBS += -lservicepoint_binding_c -RUST_TARGET := $(RUST_ARCH)-unknown-linux-$(_libc) - size-optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ -Zbuild-std-features="panic_immediate_abort" CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ @@ -35,7 +50,8 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --no-default-features \ --features=$(FEATURES) \ --target=$(RUST_TARGET) \ - $($(_profile)_CARGOFLAGS) + $($(_profile)_CARGOFLAGS) \ + --target-dir=cargo CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections @@ -53,7 +69,7 @@ size-optimized_CFLAGS += -Oz \ -fno-exceptions \ $(_no_debug_cflags) release_CFLAGS += -O2 \ - $(_no_debug_cflags) + $(_no_debug_cflags) debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic @@ -70,16 +86,19 @@ ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) +CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) +_servicepoint_cflags := -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR) + +CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) ifeq ($(LTO), 1) CFLAGS += -flto endif -RUST_TARGET_DIR := $(REPO_ROOT)/target/$(RUST_TARGET)/$(_profile) - -_c_src := $(wildcard ./src/*.c) +# TODO: wildcard does not work with VPATH +_c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ + src/moving_line.c src/random_stuff.c src/wiping_clear.c _programs := $(basename $(notdir $(_c_src))) -_bins := $(addprefix out/, $(_programs)) +_bins := $(_programs) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) _run_programs := $(addprefix run_, $(_programs)) @@ -87,42 +106,19 @@ _run_programs := $(addprefix run_, $(_programs)) all: $(_bins) -clean: clean-c clean-rust - -clean-c: - rm -r out || true - -clean-rust: - rm $(SERVICEPOINT_HEADER_OUT)/servicepoint.h || true - cargo clean - .PHONY: all clean sizes $(_run_programs) clean-c clean-rust build-rust help FORCE -$(_unstripped_bins): out/%_unstripped: src/%.c src/helpers.h build-rust - mkdir -p out || true - $(CC) $< \ - -I $(SERVICEPOINT_HEADER_OUT) \ - -L $(RUST_TARGET_DIR) \ - $(CFLAGS) \ - -o $@ +$(_unstripped_bins): %_unstripped: src/%.c src/helpers.h build-rust + $(CC) $< $(CFLAGS) -o $@ -$(_bins): out/%: out/%_unstripped +$(_bins): %: %_unstripped $(STRIP) $(STRIPFLAGS) $^ -o $@ build-rust: FORCE - mkdir -p $(SERVICEPOINT_HEADER_OUT) || true # generate servicepoint header and library to link against $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) FORCE: ; -help: - @echo "You have the choice of the following parameters:" - @echo "" - @echo "Variable | Description | Default | Values" - @echo "---------+-----------------------------------+-----------+---------------------------" - @echo "CARGO | which cargo binary to use | 'cargo' | 'rustup run nightly cargo'" - @echo "CC | which C compiler to use | 'gcc' | 'musl-gcc'" - @echo "STRIP | which strip command to use | 'strip' | -" - @echo "MUSL | if set, use musl instead of glibc | unset | '1'" - @echo "PROFILE | Compilation profile | 'release' | 'debug' or 'size-optimized'" +#----- Begin Boilerplate +endif \ No newline at end of file diff --git a/example/target.mk b/example/target.mk new file mode 100644 index 0000000..12d98b1 --- /dev/null +++ b/example/target.mk @@ -0,0 +1,52 @@ +# Based on https://make.mad-scientist.net/papers/multi-architecture-builds/ + +.SUFFIXES: + +OBJDIR := _out_$(RUST_TARGET)-$(_link_type)-$(_profile) + +# Define the rules to build in the target subdirectories. +# +MAKETARGET = $(MAKE) --no-print-directory -C $@ -f $(CURDIR)/Makefile \ + SRCDIR=$(CURDIR) $(MAKECMDGOALS) + +.PHONY: $(OBJDIR) +$(OBJDIR): + +@[ -d "$@" ] || mkdir -p "$@" + +@$(MAKETARGET) + +# These rules keep make from trying to use the match-anything rule below to +# rebuild the makefiles--ouch! Obviously, if you don't follow my convention +# of using a `.mk' suffix on all non-standard makefiles you'll need to change +# the pattern rule. +# +Makefile : ; +%.mk :: ; + +# Anything we don't know how to build will use this rule. The command is a +# do-nothing command, but the prerequisites ensure that the appropriate +# recursive invocations of make will occur. +# +% :: $(OBJDIR) ; + +# The clean rule is best handled from the source directory: since we're +# rigorous about keeping the target directories containing only target files +# and the source directory containing only source files, `clean' is as trivial +# as removing the target directories! +# +.PHONY: clean clean-all +clean: + rm -rf $(OBJDIR) +clean-all: + rm -rf _out_* + +help: + @echo "You have the choice of the following parameters:" + @echo "" + @echo "Variable | Description | Default | Values" + @echo "---------+----------------------+-----------+---------------------------" + @echo "LIBC | libc to link against | 'gnu' | 'gnu' or 'musl'" + @echo "PROFILE | Optimization profile | 'release' | 'debug' or 'size-optimized'" + @echo "LINK | | 'dynamic' | 'dynamic' or 'static'" + @echo "CARGO | cargo binary to use | 'cargo' | 'rustup run nightly cargo'" + @echo "CC | C compiler to use | 'gcc' | 'musl-gcc'" + @echo "STRIP | strip command to use | 'strip' | -" From a4699b9bc8c5d6d91b51d4232259e7ea549cbd2e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 16:48:19 +0200 Subject: [PATCH 23/76] rename size-optimized to size_optimized --- .github/workflows/rust.yml | 4 ++-- Cargo.toml | 2 +- example/Makefile | 10 +++++----- example/target.mk | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f4af981..c025406 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -52,6 +52,6 @@ jobs: - name: install rust targets run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update - - name: build example -- glibc size-optimized + - name: build example -- glibc size_optimized run: cd example && make clean - && make LIBC=gnu LINK=dynamic PROFILE=size-optimized CARGO="rustup run nightly cargo" LTO=1 + && make LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 diff --git a/Cargo.toml b/Cargo.toml index dd3d637..5e29900 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ missing_safety_doc = "allow" [package.metadata.docs.rs] all-features = true -[profile.size-optimized] +[profile.size_optimized] inherits = "release" opt-level = 'z' # Optimize for size lto = true # Enable link-time optimization diff --git a/example/Makefile b/example/Makefile index b238200..c69b9a7 100644 --- a/example/Makefile +++ b/example/Makefile @@ -11,8 +11,8 @@ ifeq (,$(filter dynamic static,$(_link_type))) endif _profile := $(if $(PROFILE),$(PROFILE),release) -ifeq (,$(filter release debug size-optimized,$(_profile))) - _profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized") +ifeq (,$(filter release debug size_optimized,$(_profile))) + _profile := $(error "PROFILE has to be set to one of: debug, release, size_optimized") endif ARCH ?= $(shell uname -m) @@ -43,7 +43,7 @@ _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag STATIC_LINK_LIBS += -lservicepoint_binding_c -size-optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ +size_optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ -Zbuild-std-features="panic_immediate_abort" CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --profile=$(_rust_cli_profile) \ @@ -55,7 +55,7 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections -size-optimized_CFLAGS += -Oz \ +size_optimized_CFLAGS += -Oz \ -fwrapv -fomit-frame-pointer -fno-stack-protector\ -fno-unroll-loops \ -fno-unwind-tables -fno-asynchronous-unwind-tables \ @@ -74,7 +74,7 @@ debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic -size-optimized_RUSTFLAGS += -Zlocation-detail=none \ +size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ -C panic=abort diff --git a/example/target.mk b/example/target.mk index 12d98b1..2b74f03 100644 --- a/example/target.mk +++ b/example/target.mk @@ -45,7 +45,7 @@ help: @echo "Variable | Description | Default | Values" @echo "---------+----------------------+-----------+---------------------------" @echo "LIBC | libc to link against | 'gnu' | 'gnu' or 'musl'" - @echo "PROFILE | Optimization profile | 'release' | 'debug' or 'size-optimized'" + @echo "PROFILE | Optimization profile | 'release' | 'debug' or 'size_optimized'" @echo "LINK | | 'dynamic' | 'dynamic' or 'static'" @echo "CARGO | cargo binary to use | 'cargo' | 'rustup run nightly cargo'" @echo "CC | C compiler to use | 'gcc' | 'musl-gcc'" From 4d9c9e2ac4e08e1c86a63e5753cbb0b928410d35 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 28 May 2025 17:21:08 +0200 Subject: [PATCH 24/76] clean up .PHONY and all --- .github/workflows/rust.yml | 8 ++++---- example/Makefile | 27 ++++++++++++--------------- example/target.mk | 6 +++++- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c025406..d0f1da3 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -36,9 +36,9 @@ jobs: run: output=$(git status --porcelain) && [ -z "$output" ] - name: build example -- glibc release - run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=release + run: cd example && make -r clean-all && make -r LIBC=gnu LINK=dynamic PROFILE=release - name: build example -- glibc debug - run: cd example && make clean && make LIBC=gnu LINK=dynamic PROFILE=debug + run: cd example && make -r clean-all && make -r LIBC=gnu LINK=dynamic PROFILE=debug build-size-gnu-unstable: runs-on: ubuntu-latest @@ -53,5 +53,5 @@ jobs: run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update - name: build example -- glibc size_optimized - run: cd example && make clean - && make LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 + run: cd example && make clean-all -r + && make -r LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 diff --git a/example/Makefile b/example/Makefile index c69b9a7..e2ac12e 100644 --- a/example/Makefile +++ b/example/Makefile @@ -34,6 +34,7 @@ FEATURES := ""#"env_logger" THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) +CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) # TODO: check if override is needed _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) @@ -68,12 +69,19 @@ size_optimized_CFLAGS += -Oz \ -fno-ident \ -fno-exceptions \ $(_no_debug_cflags) + release_CFLAGS += -O2 \ $(_no_debug_cflags) debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic +_servicepoint_cflags := -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR) +CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) +ifeq ($(LTO), 1) + CFLAGS += -flto +endif + size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ @@ -86,39 +94,28 @@ ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) -_servicepoint_cflags := -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR) - -CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) -ifeq ($(LTO), 1) - CFLAGS += -flto -endif - # TODO: wildcard does not work with VPATH _c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ src/moving_line.c src/random_stuff.c src/wiping_clear.c _programs := $(basename $(notdir $(_c_src))) _bins := $(_programs) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) -_run_programs := $(addprefix run_, $(_programs)) -# TODO: use cargo --target-dir to put rust binaries into same dir +.SUFFIXES: + +.PHONY: all build-rust all: $(_bins) -.PHONY: all clean sizes $(_run_programs) clean-c clean-rust build-rust help FORCE - $(_unstripped_bins): %_unstripped: src/%.c src/helpers.h build-rust $(CC) $< $(CFLAGS) -o $@ $(_bins): %: %_unstripped $(STRIP) $(STRIPFLAGS) $^ -o $@ -build-rust: FORCE +build-rust: # generate servicepoint header and library to link against $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) -FORCE: ; - #----- Begin Boilerplate endif \ No newline at end of file diff --git a/example/target.mk b/example/target.mk index 2b74f03..b7e743c 100644 --- a/example/target.mk +++ b/example/target.mk @@ -33,7 +33,7 @@ Makefile : ; # and the source directory containing only source files, `clean' is as trivial # as removing the target directories! # -.PHONY: clean clean-all +.PHONY: clean clean-all help clean: rm -rf $(OBJDIR) clean-all: @@ -50,3 +50,7 @@ help: @echo "CARGO | cargo binary to use | 'cargo' | 'rustup run nightly cargo'" @echo "CC | C compiler to use | 'gcc' | 'musl-gcc'" @echo "STRIP | strip command to use | 'strip' | -" + @echo "" + @echo "When building this project, each configuration will result in a separate output directory." + @echo "The target clean only removes the output directory of the specified configuration." + @echo "The target clean-all can be used to remove the builds of all configurations." From e824baa4bad8a85e1d37b857cc0237e45e1ea0f7 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 30 May 2025 00:09:41 +0200 Subject: [PATCH 25/76] more configs work now ``` make LIBC=gnu LINK=dynamic make LIBC=musl LINK=dynamic CC=musl-gcc make LIBC=musl LINK=static CC=musl-gcc ``` --- example/Makefile | 18 +++++++++-------- flake.lock | 8 ++++---- flake.nix | 50 ++++++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/example/Makefile b/example/Makefile index e2ac12e..1617552 100644 --- a/example/Makefile +++ b/example/Makefile @@ -36,11 +36,8 @@ THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) -# TODO: check if override is needed _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) -# TODO: `make LINK=static` fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a - STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag STATIC_LINK_LIBS += -lservicepoint_binding_c @@ -57,7 +54,7 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections size_optimized_CFLAGS += -Oz \ - -fwrapv -fomit-frame-pointer -fno-stack-protector\ + -fwrapv -fomit-frame-pointer -fno-stack-protector \ -fno-unroll-loops \ -fno-unwind-tables -fno-asynchronous-unwind-tables \ -fmerge-all-constants \ @@ -81,20 +78,25 @@ CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_se ifeq ($(LTO), 1) CFLAGS += -flto endif +ifeq ($(_libc),gnu) + ifeq ($(_link_type),static) + CFLAGS += $(error "statically linking glibc is known to be broken") + endif +endif size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ - -C panic=abort - #-C link-arg=--hash-style=gnu -musl_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static + -C panic=abort \ + -C link-arg=-Wl,--hash-style=gnu +static_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=+crt-static RUSTFLAGS += $($(_libc)_RUSTFLAGS) $($(_profile)_RUSTFLAGS) $($(_link_type)_RUSTFLAGS) ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -# TODO: wildcard does not work with VPATH +# ADD NEW EXAMPLES HERE _c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ src/moving_line.c src/random_stuff.c src/wiping_clear.c _programs := $(basename $(notdir $(_c_src))) diff --git a/flake.lock b/flake.lock index c5f76d2..9091d9f 100644 --- a/flake.lock +++ b/flake.lock @@ -2,16 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1748037224, - "narHash": "sha256-92vihpZr6dwEMV6g98M5kHZIttrWahb9iRPBm1atcPk=", + "lastModified": 1748302896, + "narHash": "sha256-ixMT0a8mM091vSswlTORZj93WQAJsRNmEvqLL+qwTFM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "f09dede81861f3a83f7f06641ead34f02f37597f", + "rev": "7848cd8c982f7740edf76ddb3b43d234cb80fc4d", "type": "github" }, "original": { "owner": "nixos", - "ref": "nixos-24.11", + "ref": "nixos-25.05", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 82983f7..2adaa88 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,7 @@ description = "Flake for the servicepoint library."; inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; }; outputs = @@ -31,37 +31,41 @@ { devShells = forAllSystems ( { pkgs, system }: + let + toolchain = pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }; + in { default = pkgs.mkShell rec { - buildInputs = with pkgs; [ + packages = with pkgs; [ + toolchain + gcc + gdb + gnumake + pkg-config xe xz libgcc libunwind + pkgsStatic.gcc + pkgsStatic.libgcc pkgsStatic.musl ]; - nativeBuildInputs = with pkgs; [ - (pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }) - gcc - gdb - pkgsStatic.gcc - gnumake - pkg-config - ]; - - RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; + RUST_BACKTRACE = 1; + RUST_LOG = "all"; }; } ); From d205ed44b7829f00ffb9efad5b3914912cb0fdaa Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 1 Jun 2025 13:00:22 +0200 Subject: [PATCH 26/76] version 0.15.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- build.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7deee5..121e01f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -536,7 +536,7 @@ dependencies = [ [[package]] name = "servicepoint_binding_c" -version = "0.14.1" +version = "0.15.0" dependencies = [ "cbindgen", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 5e29900..f6bd53b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "servicepoint_binding_c" -version = "0.14.1" +version = "0.15.0" publish = true edition = "2021" license = "GPL-3.0-or-later" diff --git a/build.rs b/build.rs index 615d00f..397341d 100644 --- a/build.rs +++ b/build.rs @@ -35,7 +35,7 @@ fn main() { if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { let header_copy = header_out + "/servicepoint.h"; - if fs::exists(&header_copy).ok().unwrap_or(false) { + if fs::exists(&header_copy).unwrap_or(false) { // check if content changed to prevent rebuild of dependents if not let mut bindings_text = Vec::new(); From 7b6b4f7e5b53151281e51a69f030502ba759c5b8 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 1 Jun 2025 16:51:11 +0200 Subject: [PATCH 27/76] split flake.nix, shell per package, generate pkg-config --- devShells.nix | 51 +++++++++++++++++++++++++++++++++++++++ flake.lock | 39 ++++++++++++++++++++++++++++++ flake.nix | 56 ++++++++++--------------------------------- packages.nix | 59 ++++++++++++++++++++++++++++++++++++++++++++++ servicepoint.pc.in | 6 +++++ 5 files changed, 167 insertions(+), 44 deletions(-) create mode 100644 devShells.nix create mode 100644 packages.nix create mode 100644 servicepoint.pc.in diff --git a/devShells.nix b/devShells.nix new file mode 100644 index 0000000..367faf4 --- /dev/null +++ b/devShells.nix @@ -0,0 +1,51 @@ +{ + pkgs, + fenix, + selfPkgs, + ... +}: +(builtins.mapAttrs ( + packageName: package: + pkgs.mkShell { + inputsFrom = [ package ]; + packages = with pkgs; [ + rustfmt + clippy + cargo-expand + cargo-tarpaulin + gdb + ]; + + RUST_BACKTRACE = 1; + RUST_LOG = "all"; + } +) selfPkgs) +// { + test = pkgs.mkShell { + packages = with pkgs; [ + (pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }) + gcc + gnumake + xe + libgcc + libunwind + pkgsStatic.gcc + pkgsStatic.libgcc + pkgsStatic.musl + ]; + + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; + }; +} diff --git a/flake.lock b/flake.lock index 9091d9f..07ccbb6 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,26 @@ { "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1748759782, + "narHash": "sha256-MJNhEBsAbxRp/53qsXv6/eaWkGS8zMGX9LuCz1BLeck=", + "owner": "nix-community", + "repo": "fenix", + "rev": "9be40ad995bac282160ff374a47eed67c74f9c2a", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1748302896, @@ -18,8 +39,26 @@ }, "root": { "inputs": { + "fenix": "fenix", "nixpkgs": "nixpkgs" } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1748695646, + "narHash": "sha256-VwSuuRF4NvAoeHZJRRlX8zAFZ+nZyuiIvmVqBAX0Bcg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "2a388d1103450d814a84eda98efe89c01b158343", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 2adaa88..d35dbd2 100644 --- a/flake.nix +++ b/flake.nix @@ -3,15 +3,19 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = inputs@{ self, nixpkgs, + fenix, }: let - lib = nixpkgs.lib; supported-systems = [ "x86_64-linux" "aarch64-linux" @@ -20,56 +24,20 @@ ]; forAllSystems = f: - lib.genAttrs supported-systems ( + nixpkgs.lib.genAttrs supported-systems ( system: f rec { + inherit system self; pkgs = nixpkgs.legacyPackages.${system}; - inherit system; + lib = pkgs.lib; + fenix = inputs.fenix.packages.${system}; + selfPkgs = self.packages.${system}; } ); in { - devShells = forAllSystems ( - { pkgs, system }: - let - toolchain = pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustPlatform.rustLibSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }; - in - { - default = pkgs.mkShell rec { - packages = with pkgs; [ - toolchain - gcc - gdb - gnumake - pkg-config - xe - xz - libgcc - libunwind - pkgsStatic.gcc - pkgsStatic.libgcc - pkgsStatic.musl - ]; - - RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; - RUST_BACKTRACE = 1; - RUST_LOG = "all"; - }; - } - ); - + packages = forAllSystems (import ./packages.nix); + devShells = forAllSystems (import ./devShells.nix); formatter = forAllSystems ({ pkgs, ... }: pkgs.nixfmt-rfc-style); }; } diff --git a/packages.nix b/packages.nix new file mode 100644 index 0000000..02eb540 --- /dev/null +++ b/packages.nix @@ -0,0 +1,59 @@ +{ + pkgs, + lib, + fenix, + ... +}: +let + mkServicepointBindingC = + { + rustPlatform, + buildType, + }: + rustPlatform.buildRustPackage (finalAttrs: { + inherit buildType; + + pname = "servicepoint-binding-c"; + version = "0.15.0"; + src = ./.; + cargoLock.lockFile = ./Cargo.lock; + meta = { + description = "C bindings for the servicepoint crate."; + homepage = "https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c"; + license = lib.licenses.gpl3Plus; + pkgConfigModules = [ "servicepoint" ]; + }; + nativeBuildInputs = with pkgs; [ pkg-config ]; + buildInputs = with pkgs; [ xz ]; + + preBuild = '' + mkdir -p include + export SERVICEPOINT_HEADER_OUT=$(realpath "include") + + echo "Rust version: $(rustc --version)" + echo "preBuild hook: set SERVICEPOINT_HEADER_OUT to $SERVICEPOINT_HEADER_OUT" + ''; + postInstall = '' + cp -r include $out + + mkdir -p $out/lib/pkgconfig + sed "s:\$out:$out:g" ${./servicepoint.pc.in} | sed "s:\$version:$version:g" > $out/lib/pkgconfig/servicepoint.pc + ''; + }); + rustPlatform-stable = pkgs.rustPlatform; + rustPlatform-unstable = pkgs.makeRustPlatform { + cargo = fenix.minimal.cargo; + rustc = fenix.minimal.rustc; + }; +in +rec { + servicepoint-binding-c-release = mkServicepointBindingC { + buildType = "release"; + rustPlatform = rustPlatform-stable; + }; + servicepoint-binding-c-size-optimized = mkServicepointBindingC { + buildType = "size_optimized"; + rustPlatform = rustPlatform-unstable; + }; + servicepoint-binding-c = servicepoint-binding-c-release; +} diff --git a/servicepoint.pc.in b/servicepoint.pc.in new file mode 100644 index 0000000..5f474e7 --- /dev/null +++ b/servicepoint.pc.in @@ -0,0 +1,6 @@ +Name: servicepoint +Description: C bindings for the servicepoint library +Version: $version +URL: https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c +Libs: -L$out/lib -lservicepoint_binding_c +Cflags: -I$out/include From d98aec63b0b7be95623f4d914fc9870614647ad4 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 2 Jun 2025 17:22:38 +0200 Subject: [PATCH 28/76] examples work as packages in flake --- devShells.nix | 70 ++++++++++++++++++++++++++++----------------------- flake.lock | 16 ++++++++++++ flake.nix | 4 ++- packages.nix | 60 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 112 insertions(+), 38 deletions(-) diff --git a/devShells.nix b/devShells.nix index 367faf4..498b51b 100644 --- a/devShells.nix +++ b/devShells.nix @@ -4,47 +4,55 @@ selfPkgs, ... }: +let + defaultAdditionalPkgs = with pkgs; [ + rustfmt + clippy + cargo-expand + cargo-tarpaulin + gdb + ]; +in (builtins.mapAttrs ( packageName: package: pkgs.mkShell { inputsFrom = [ package ]; - packages = with pkgs; [ - rustfmt - clippy - cargo-expand - cargo-tarpaulin - gdb - ]; - + packages = defaultAdditionalPkgs; RUST_BACKTRACE = 1; RUST_LOG = "all"; } ) selfPkgs) // { - test = pkgs.mkShell { - packages = with pkgs; [ - (pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustPlatform.rustLibSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }) - gcc - gnumake - xe - libgcc - libunwind - pkgsStatic.gcc - pkgsStatic.libgcc - pkgsStatic.musl + default = pkgs.mkShell { + inputsFrom = [ + selfPkgs.servicepoint-binding-c + selfPkgs.announce ]; + packages = + defaultAdditionalPkgs + ++ (with pkgs; [ + (pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }) + gcc + gnumake + xe + libgcc + libunwind + pkgsStatic.gcc + pkgsStatic.libgcc + pkgsStatic.musl + ]); RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; }; diff --git a/flake.lock b/flake.lock index 07ccbb6..873e7c3 100644 --- a/flake.lock +++ b/flake.lock @@ -21,6 +21,21 @@ "type": "github" } }, + "nix-filter": { + "locked": { + "lastModified": 1731533336, + "narHash": "sha256-oRam5PS1vcrr5UPgALW0eo1m/5/pls27Z/pabHNy2Ms=", + "owner": "numtide", + "repo": "nix-filter", + "rev": "f7653272fd234696ae94229839a99b73c9ab7de0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "nix-filter", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1748302896, @@ -40,6 +55,7 @@ "root": { "inputs": { "fenix": "fenix", + "nix-filter": "nix-filter", "nixpkgs": "nixpkgs" } }, diff --git a/flake.nix b/flake.nix index d35dbd2..1501caa 100644 --- a/flake.nix +++ b/flake.nix @@ -3,6 +3,7 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + nix-filter.url = "github:numtide/nix-filter"; fenix = { url = "github:nix-community/fenix"; inputs.nixpkgs.follows = "nixpkgs"; @@ -14,6 +15,7 @@ self, nixpkgs, fenix, + nix-filter }: let supported-systems = [ @@ -29,7 +31,7 @@ f rec { inherit system self; pkgs = nixpkgs.legacyPackages.${system}; - lib = pkgs.lib; + lib = pkgs.lib // nix-filter.lib; fenix = inputs.fenix.packages.${system}; selfPkgs = self.packages.${system}; } diff --git a/packages.nix b/packages.nix index 02eb540..1716250 100644 --- a/packages.nix +++ b/packages.nix @@ -2,20 +2,31 @@ pkgs, lib, fenix, + selfPkgs, ... }: let - mkServicepointBindingC = + version = "0.15.0"; + mkServicepoint = { rustPlatform, buildType, }: rustPlatform.buildRustPackage (finalAttrs: { - inherit buildType; + inherit buildType version; pname = "servicepoint-binding-c"; - version = "0.15.0"; - src = ./.; + src = lib.filter { + root = ./.; + include = [ + ./Cargo.lock + ./Cargo.toml + ./src + ./build.rs + ./LICENSE + ./cbindgen.toml + ]; + }; cargoLock.lockFile = ./Cargo.lock; meta = { description = "C bindings for the servicepoint crate."; @@ -40,20 +51,57 @@ let sed "s:\$out:$out:g" ${./servicepoint.pc.in} | sed "s:\$version:$version:g" > $out/lib/pkgconfig/servicepoint.pc ''; }); + mkExample = + { name, servicepointBinding }: + pkgs.stdenv.mkDerivation { + inherit version; + pname = "servicepoint-c-example-${name}"; + + nativeBuildInputs = with pkgs; [ pkg-config ]; + buildInputs = [ servicepointBinding ]; + src = ./example/src; + + #dontUnpack = true; + buildPhase = '' + mkdir -p $out/bin + + gcc ${name}.c $(pkg-config --libs servicepoint --cflags) -o $out/bin/${name} + ''; + }; rustPlatform-stable = pkgs.rustPlatform; rustPlatform-unstable = pkgs.makeRustPlatform { cargo = fenix.minimal.cargo; rustc = fenix.minimal.rustc; }; + examples = [ + "announce" + "brightness_tester" + "header_logger" + "moving_line" + "random_stuff" + "wiping_clear" + ]; in rec { - servicepoint-binding-c-release = mkServicepointBindingC { + servicepoint-binding-c-release = mkServicepoint { buildType = "release"; rustPlatform = rustPlatform-stable; }; - servicepoint-binding-c-size-optimized = mkServicepointBindingC { + servicepoint-binding-c-size-optimized = mkServicepoint { buildType = "size_optimized"; rustPlatform = rustPlatform-unstable; }; servicepoint-binding-c = servicepoint-binding-c-release; + + all-examples = pkgs.symlinkJoin { + name = "servicepoint-all-examples"; + paths = builtins.map (e: selfPkgs.${e}) examples; + }; } +// (lib.genAttrs examples ( + name: + mkExample { + inherit name; + servicepointBinding = selfPkgs.servicepoint-binding-c; + } +)) From 579a68c77ad38b1fd989021878c2e9cb264adb9a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 2 Jun 2025 23:10:03 +0200 Subject: [PATCH 29/76] size optimized examples build in flake, remove unstable compiler flags from makefile dynamically linked bin is 16k --- example/Makefile | 8 ++--- nix-build-all.sh | 13 +++++++ packages.nix | 91 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 84 insertions(+), 28 deletions(-) create mode 100755 nix-build-all.sh diff --git a/example/Makefile b/example/Makefile index 1617552..5659216 100644 --- a/example/Makefile +++ b/example/Makefile @@ -39,16 +39,12 @@ CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag -STATIC_LINK_LIBS += -lservicepoint_binding_c -size_optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ - -Zbuild-std-features="panic_immediate_abort" CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --profile=$(_rust_cli_profile) \ --no-default-features \ --features=$(FEATURES) \ --target=$(RUST_TARGET) \ - $($(_profile)_CARGOFLAGS) \ --target-dir=cargo CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie @@ -73,7 +69,7 @@ debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic -_servicepoint_cflags := -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR) +_servicepoint_cflags := $(pkg-config --libs servicepoint --cflags) CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) ifeq ($(LTO), 1) CFLAGS += -flto @@ -120,4 +116,4 @@ build-rust: $(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS) #----- Begin Boilerplate -endif \ No newline at end of file +endif diff --git a/nix-build-all.sh b/nix-build-all.sh new file mode 100755 index 0000000..059297b --- /dev/null +++ b/nix-build-all.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e +set -x + +nix build .#servicepoint-binding-c + +nix build .#servicepoint-binding-c-stable-release +nix build .#servicepoint-binding-c-nightly-release +nix build .#servicepoint-binding-c-nightly-size + +nix build .#all-examples +nix build .#all-examples-size diff --git a/packages.nix b/packages.nix index 1716250..f973020 100644 --- a/packages.nix +++ b/packages.nix @@ -8,12 +8,9 @@ let version = "0.15.0"; mkServicepoint = - { - rustPlatform, - buildType, - }: + rustPlatform: rustPlatform.buildRustPackage (finalAttrs: { - inherit buildType version; + inherit version; pname = "servicepoint-binding-c"; src = lib.filter { @@ -28,14 +25,15 @@ let ]; }; cargoLock.lockFile = ./Cargo.lock; + useFetchCargoVendor = true; meta = { description = "C bindings for the servicepoint crate."; homepage = "https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c"; license = lib.licenses.gpl3Plus; pkgConfigModules = [ "servicepoint" ]; }; - nativeBuildInputs = with pkgs; [ pkg-config ]; - buildInputs = with pkgs; [ xz ]; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ pkgs.xz ]; preBuild = '' mkdir -p include @@ -56,16 +54,17 @@ let pkgs.stdenv.mkDerivation { inherit version; pname = "servicepoint-c-example-${name}"; - - nativeBuildInputs = with pkgs; [ pkg-config ]; - buildInputs = [ servicepointBinding ]; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ + servicepointBinding + pkgs.xz + ]; src = ./example/src; - - #dontUnpack = true; buildPhase = '' + set -e + set -x mkdir -p $out/bin - - gcc ${name}.c $(pkg-config --libs servicepoint --cflags) -o $out/bin/${name} + gcc ${name}.c $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) $EXTRA_CFLAGS -o $out/bin/${name} ''; }; rustPlatform-stable = pkgs.rustPlatform; @@ -83,21 +82,29 @@ let ]; in rec { - servicepoint-binding-c-release = mkServicepoint { - buildType = "release"; - rustPlatform = rustPlatform-stable; - }; - servicepoint-binding-c-size-optimized = mkServicepoint { + servicepoint-binding-c-stable-release = mkServicepoint rustPlatform-stable; + servicepoint-binding-c-nightly-release = mkServicepoint rustPlatform-unstable; + servicepoint-binding-c-nightly-size = servicepoint-binding-c-nightly-release // { buildType = "size_optimized"; - rustPlatform = rustPlatform-unstable; + buildNoDefaultFeatures = true; + # TODO: do these override the nix flags? + #CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; + # TODO: those override the nix flags + # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; + # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; }; - servicepoint-binding-c = servicepoint-binding-c-release; + servicepoint-binding-c = servicepoint-binding-c-stable-release; all-examples = pkgs.symlinkJoin { name = "servicepoint-all-examples"; - paths = builtins.map (e: selfPkgs.${e}) examples; + paths = builtins.map (e: selfPkgs."${e}") examples; + }; + all-examples-size = pkgs.symlinkJoin { + name = "servicepoint-all-examples-size"; + paths = builtins.map (e: selfPkgs."${e}-size") examples; }; } +# construct one package per example // (lib.genAttrs examples ( name: mkExample { @@ -105,3 +112,43 @@ rec { servicepointBinding = selfPkgs.servicepoint-binding-c; } )) +# construct another pakage per example, but with a different name and compiler options +// (lib.mapAttrs' + ( + name: value: + lib.nameValuePair ("${name}-size") ( + value + // { + EXTRA_CFLAGS = builtins.toString [ + "-Oz" + "-fwrapv" + "-fomit-frame-pointer" + "-fno-stack-protector" + "-fno-unroll-loops" + "-fno-unwind-tables" + "-fno-asynchronous-unwind-tables" + "-fmerge-all-constants" + "-fvisibility=hidden" + "-Bsymbolic" + "-fno-ident" + "-fno-exceptions" + "-ffunction-sections" + "-fdata-sections" + "-Wl,-z,norelro" + "-Wl,--hash-style=gnu" + "-Wl,--gc-sections" + "-Wl,--exclude-libs,ALL" + ]; + } + ) + ) + ( + lib.genAttrs examples ( + name: + mkExample { + inherit name; + servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; + } + ) + ) +) From 19a9af48ee1baccc33ee2124d659fa6ab0bba665 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 2 Jun 2025 23:28:40 +0200 Subject: [PATCH 30/76] make header_logger slightly less ugly --- example/src/header_logger.c | 251 ++++++++++++++++++------------------ 1 file changed, 128 insertions(+), 123 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 93c17dd..8fe1455 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -15,6 +15,133 @@ void handle_error(const char *msg) { exit(EXIT_FAILURE); } +bool log_command(struct Command command) { + switch (command.tag) { + case COMMAND_TAG_INVALID: { + printf("-> this is an invalid command\n"); + break; + } + case COMMAND_TAG_HARD_RESET: { + printf("-> HardReset command - exiting\n"); + return true; + } + case COMMAND_TAG_BITMAP: { + BitmapCommand *bitmapCommand = command.data.bitmap; + + CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); + + size_t x, y; + sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); + + Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); + size_t w = sp_bitmap_width(bitmap); + size_t h = sp_bitmap_height(bitmap); + + printf("-> BitmapCommand with params: x=%zu, y=%zu, w=%zu, h=%zu, compression=%hu\n", + x, y, w, h, compression); + break; + } + case COMMAND_TAG_BRIGHTNESS_GRID: { + BrightnessGridCommand *gridCommand = command.data.brightness_grid; + + size_t x, y; + sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); + + BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); + size_t w = sp_brightness_grid_width(grid); + size_t h = sp_brightness_grid_height(grid); + + printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CHAR_GRID: { + CharGridCommand *gridCommand = command.data.char_grid; + + size_t x, y; + sp_cmd_char_grid_get_origin(gridCommand, &x, &y); + + CharGrid *grid = sp_cmd_char_grid_get(gridCommand); + size_t w = sp_char_grid_width(grid); + size_t h = sp_char_grid_height(grid); + + printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_CP437_GRID: { + Cp437GridCommand *gridCommand = command.data.cp437_grid; + + size_t x, y; + sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); + + Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); + size_t w = sp_cp437_grid_width(grid); + size_t h = sp_cp437_grid_height(grid); + + printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", + x, y, w, h); + break; + } + case COMMAND_TAG_BIT_VEC: { + BitVecCommand *bitvecCommand = command.data.bitvec; + + size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); + CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); + + BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); + char *operationText; + switch (operation) { + case BINARY_OPERATION_AND: + operationText = "and"; + break; + case BINARY_OPERATION_OR: + operationText = "or"; + break; + case BINARY_OPERATION_XOR: + operationText = "xor"; + break; + case BINARY_OPERATION_OVERWRITE: + operationText ="overwrite"; + break; + default: + operationText ="unknown"; + break; + } + + BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); + size_t len = sp_bitvec_len(bitvec); + + printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", + offset, len, compression, operationText); + break; + } + case COMMAND_TAG_CLEAR: { + printf("-> ClearCommand\n"); + break; + } + case COMMAND_TAG_BITMAP_LEGACY: { + printf("-> BitmapLinearLegacy\n"); + break; + } + case COMMAND_TAG_FADE_OUT:{ + printf("-> FadeOutCommand\n"); + break; + } + case COMMAND_TAG_GLOBAL_BRIGHTNESS: { + Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); + printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); + break; + } + default: { + printf("-> unknown command tag %d\n", command.tag); + break; + } + } + + return false; +} + int main(int argc, char **argv) { //init_env_logger(); @@ -72,129 +199,7 @@ int main(int argc, char **argv) { payload.start, payload.length); struct Command command = sp_cmd_generic_try_from_packet(packet); - switch (command.tag) { - case COMMAND_TAG_INVALID: { - printf("-> this is an invalid command\n"); - break; - } - case COMMAND_TAG_HARD_RESET: { - printf("-> HardReset command - exiting\n"); - done = true; - break; - } - case COMMAND_TAG_BITMAP: { - BitmapCommand *bitmapCommand = command.data.bitmap; - - CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); - - size_t x, y; - sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); - - Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); - size_t w = sp_bitmap_width(bitmap); - size_t h = sp_bitmap_height(bitmap); - - printf("-> BitmapCommand with params: x=%zu, y=%zu, w=%zu, h=%zu, compression=%hu\n", - x, y, w, h, compression); - break; - } - case COMMAND_TAG_BRIGHTNESS_GRID: { - BrightnessGridCommand *gridCommand = command.data.brightness_grid; - - size_t x, y; - sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); - - BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); - size_t w = sp_brightness_grid_width(grid); - size_t h = sp_brightness_grid_height(grid); - - printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", - x, y, w, h); - break; - } - case COMMAND_TAG_CHAR_GRID: { - CharGridCommand *gridCommand = command.data.char_grid; - - size_t x, y; - sp_cmd_char_grid_get_origin(gridCommand, &x, &y); - - CharGrid *grid = sp_cmd_char_grid_get(gridCommand); - size_t w = sp_char_grid_width(grid); - size_t h = sp_char_grid_height(grid); - - printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", - x, y, w, h); - break; - } - case COMMAND_TAG_CP437_GRID: { - Cp437GridCommand *gridCommand = command.data.cp437_grid; - - size_t x, y; - sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); - - Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); - size_t w = sp_cp437_grid_width(grid); - size_t h = sp_cp437_grid_height(grid); - - printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", - x, y, w, h); - break; - } - case COMMAND_TAG_BIT_VEC: { - BitVecCommand *bitvecCommand = command.data.bitvec; - - size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); - CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); - - BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); - char *operationText; - switch (operation) { - case BINARY_OPERATION_AND: - operationText = "and"; - break; - case BINARY_OPERATION_OR: - operationText = "or"; - break; - case BINARY_OPERATION_XOR: - operationText = "xor"; - break; - case BINARY_OPERATION_OVERWRITE: - operationText ="overwrite"; - break; - default: - operationText ="unknown"; - break; - } - - BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); - size_t len = sp_bitvec_len(bitvec); - - printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", - offset, len, compression, operationText); - break; - } - case COMMAND_TAG_CLEAR: { - printf("-> ClearCommand\n"); - break; - } - case COMMAND_TAG_BITMAP_LEGACY: { - printf("-> BitmapLinearLegacy\n"); - break; - } - case COMMAND_TAG_FADE_OUT:{ - printf("-> FadeOutCommand\n"); - break; - } - case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); - printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); - break; - } - default: { - printf("-> unknown command tag %d\n", command.tag); - break; - } - } + done = log_command(command); sp_cmd_generic_free(command); } From 42defc7732dc5fe6cf0b979afef3885cb7cbc1cd Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 3 Jun 2025 17:22:12 +0200 Subject: [PATCH 31/76] force use of gcc --- packages.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages.nix b/packages.nix index f973020..74bbac9 100644 --- a/packages.nix +++ b/packages.nix @@ -51,7 +51,7 @@ let }); mkExample = { name, servicepointBinding }: - pkgs.stdenv.mkDerivation { + pkgs.gccStdenv.mkDerivation { inherit version; pname = "servicepoint-c-example-${name}"; nativeBuildInputs = [ pkgs.pkg-config ]; From 471717a36f0a95879d92028a16b98ea497041880 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 3 Jun 2025 21:44:08 +0200 Subject: [PATCH 32/76] flake musl build --- devShells.nix | 1 + nix-build-all.sh | 23 +++++-- packages.nix | 170 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 147 insertions(+), 47 deletions(-) diff --git a/devShells.nix b/devShells.nix index 498b51b..7942618 100644 --- a/devShells.nix +++ b/devShells.nix @@ -11,6 +11,7 @@ let cargo-expand cargo-tarpaulin gdb + nix-output-monitor ]; in (builtins.mapAttrs ( diff --git a/nix-build-all.sh b/nix-build-all.sh index 059297b..2973cbc 100755 --- a/nix-build-all.sh +++ b/nix-build-all.sh @@ -3,11 +3,22 @@ set -e set -x -nix build .#servicepoint-binding-c +BUILD="nix build" -nix build .#servicepoint-binding-c-stable-release -nix build .#servicepoint-binding-c-nightly-release -nix build .#servicepoint-binding-c-nightly-size +$BUILD .#servicepoint-binding-c -nix build .#all-examples -nix build .#all-examples-size +$BUILD .#servicepoint-binding-c-stable-release +$BUILD .#servicepoint-binding-c-stable-size +$BUILD .#servicepoint-binding-c-nightly-release +$BUILD .#servicepoint-binding-c-nightly-size + +$BUILD .#servicepoint-binding-c-musl-stable-release +$BUILD .#servicepoint-binding-c-musl-stable-size +# $BUILD .#servicepoint-binding-c-musl-nightly-release +# $BUILD .#servicepoint-binding-c-musl-nightly-size + +$BUILD .#all-examples +$BUILD .#all-examples-size +$BUILD .#all-examples-musl +$BUILD .#all-examples-musl-static +$BUILD .#all-examples-musl-static-size diff --git a/packages.nix b/packages.nix index 74bbac9..24cf51f 100644 --- a/packages.nix +++ b/packages.nix @@ -8,7 +8,10 @@ let version = "0.15.0"; mkServicepoint = - rustPlatform: + { + rustPlatform, + pkgs, + }: rustPlatform.buildRustPackage (finalAttrs: { inherit version; @@ -50,7 +53,11 @@ let ''; }); mkExample = - { name, servicepointBinding }: + { + name, + servicepointBinding, + pkgs, + }: pkgs.gccStdenv.mkDerivation { inherit version; pname = "servicepoint-c-example-${name}"; @@ -64,7 +71,7 @@ let set -e set -x mkdir -p $out/bin - gcc ${name}.c $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) $EXTRA_CFLAGS -o $out/bin/${name} + $CC ${name}.c $CFLAGS $EXTRA_CFLAGS $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) -o $out/bin/${name} ''; }; rustPlatform-stable = pkgs.rustPlatform; @@ -72,6 +79,11 @@ let cargo = fenix.minimal.cargo; rustc = fenix.minimal.rustc; }; + rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; + rustPlatform-musl-unstable = pkgs.pkgsMusl.makeRustPlatform { + cargo = fenix.minimal.cargo; + rustc = fenix.minimal.rustc; + }; examples = [ "announce" "brightness_tester" @@ -80,65 +92,102 @@ let "random_stuff" "wiping_clear" ]; -in -rec { - servicepoint-binding-c-stable-release = mkServicepoint rustPlatform-stable; - servicepoint-binding-c-nightly-release = mkServicepoint rustPlatform-unstable; - servicepoint-binding-c-nightly-size = servicepoint-binding-c-nightly-release // { + size-cflags = [ + "-Oz" + "-fwrapv" + "-fomit-frame-pointer" + "-fno-stack-protector" + "-fno-unroll-loops" + "-fno-unwind-tables" + "-fno-asynchronous-unwind-tables" + "-fmerge-all-constants" + "-fvisibility=hidden" + "-Bsymbolic" + "-fno-ident" + "-fno-exceptions" + "-ffunction-sections" + "-fdata-sections" + "-Wl,-z,norelro" + "-Wl,--hash-style=gnu" + "-Wl,--gc-sections" + "-Wl,--exclude-libs,ALL" + ]; + servicepoint-stable-size-args = { buildType = "size_optimized"; buildNoDefaultFeatures = true; + }; + servicepoint-unstable-size-args = { # TODO: do these override the nix flags? - #CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; + CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; # TODO: those override the nix flags # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; }; - servicepoint-binding-c = servicepoint-binding-c-stable-release; + mkAllExamples = + suffix: + pkgs.symlinkJoin { + name = "servicepoint-all-examples"; + paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; + }; +in +rec { + servicepoint-binding-c-stable-release = mkServicepoint { + inherit pkgs; + rustPlatform = rustPlatform-stable; + }; + servicepoint-binding-c-nightly-release = mkServicepoint { + inherit pkgs; + rustPlatform = rustPlatform-unstable; + }; + servicepoint-binding-c-musl-stable-release = mkServicepoint { + pkgs = pkgs.pkgsMusl; + rustPlatform = rustPlatform-musl-stable; + }; + servicepoint-binding-c-musl-nightly-release = mkServicepoint { + pkgs = pkgs.pkgsMusl; + rustPlatform = rustPlatform-musl-unstable; + }; - all-examples = pkgs.symlinkJoin { - name = "servicepoint-all-examples"; - paths = builtins.map (e: selfPkgs."${e}") examples; - }; - all-examples-size = pkgs.symlinkJoin { - name = "servicepoint-all-examples-size"; - paths = builtins.map (e: selfPkgs."${e}-size") examples; - }; + servicepoint-binding-c-stable-size = + servicepoint-binding-c-stable-release // servicepoint-stable-size-args; + servicepoint-binding-c-nightly-size = + servicepoint-binding-c-nightly-release + // servicepoint-stable-size-args + // servicepoint-unstable-size-args; + servicepoint-binding-c-musl-stable-size = + servicepoint-binding-c-musl-stable-release // servicepoint-stable-size-args; + servicepoint-binding-c-musl-nightly-size = + servicepoint-binding-c-musl-nightly-release + // servicepoint-stable-size-args + // servicepoint-unstable-size-args; + + # default variants + servicepoint-binding-c = servicepoint-binding-c-stable-release; + servicepoint-binding-c-musl = servicepoint-binding-c-musl-stable-release; + + all-examples = mkAllExamples ""; + all-examples-size = mkAllExamples "-size"; + # TODO: musl targets do not work on darwin + all-examples-musl = mkAllExamples "-musl"; + all-examples-musl-static = mkAllExamples "-musl-static"; + all-examples-musl-static-size = mkAllExamples "-musl-static-size"; } # construct one package per example // (lib.genAttrs examples ( name: mkExample { - inherit name; + inherit name pkgs; servicepointBinding = selfPkgs.servicepoint-binding-c; } )) -# construct another pakage per example, but with a different name and compiler options +# construct another pakage per example, but optimized for size with unstable rust // (lib.mapAttrs' ( name: value: lib.nameValuePair ("${name}-size") ( value // { - EXTRA_CFLAGS = builtins.toString [ - "-Oz" - "-fwrapv" - "-fomit-frame-pointer" - "-fno-stack-protector" - "-fno-unroll-loops" - "-fno-unwind-tables" - "-fno-asynchronous-unwind-tables" - "-fmerge-all-constants" - "-fvisibility=hidden" - "-Bsymbolic" - "-fno-ident" - "-fno-exceptions" - "-ffunction-sections" - "-fdata-sections" - "-Wl,-z,norelro" - "-Wl,--hash-style=gnu" - "-Wl,--gc-sections" - "-Wl,--exclude-libs,ALL" - ]; + EXTRA_CFLAGS = builtins.toString size-cflags; } ) ) @@ -146,9 +195,48 @@ rec { lib.genAttrs examples ( name: mkExample { - inherit name; + inherit name pkgs; servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; } ) ) ) +# construct another pakage per example, but with musl +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name; + pkgs = pkgs.pkgsMusl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + } + ) +)) +# construct another pakage per example, but statically linked with musl +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl-static") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name; + pkgs = pkgs.pkgsMusl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + } + // { + EXTRA_CFLAGS = "-static"; + } + ) +)) +# construct another pakage per example, but statically linked with musl and size optimized +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl-static-size") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name; + pkgs = pkgs.pkgsMusl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + } + // { + EXTRA_CFLAGS = "-static" + builtins.toString size-cflags; + } + ) +)) From 53493a5fb1401130c879899ce7003e42a9d9e9af Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 4 Jun 2025 22:14:19 +0200 Subject: [PATCH 33/76] actually change build flags --- packages.nix | 133 +++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 68 deletions(-) diff --git a/packages.nix b/packages.nix index 24cf51f..348d98c 100644 --- a/packages.nix +++ b/packages.nix @@ -11,9 +11,12 @@ let { rustPlatform, pkgs, + buildType ? "release", + buildNoDefaultFeatures ? false, + cargoBuildFlags ? [ ], }: rustPlatform.buildRustPackage (finalAttrs: { - inherit version; + inherit version buildType cargoBuildFlags; pname = "servicepoint-binding-c"; src = lib.filter { @@ -57,9 +60,14 @@ let name, servicepointBinding, pkgs, + EXTRA_CFLAGS ? "", + static ? false, }: + let + staticPkgConfigParam = if static then "--static" else ""; + in pkgs.gccStdenv.mkDerivation { - inherit version; + inherit version EXTRA_CFLAGS; pname = "servicepoint-c-example-${name}"; nativeBuildInputs = [ pkgs.pkg-config ]; buildInputs = [ @@ -71,19 +79,12 @@ let set -e set -x mkdir -p $out/bin - $CC ${name}.c $CFLAGS $EXTRA_CFLAGS $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) -o $out/bin/${name} + $CC ${name}.c ${if static then "-static" else ""} $CFLAGS $EXTRA_CFLAGS \ + $(pkg-config --libs --cflags ${staticPkgConfigParam} servicepoint) \ + $(pkg-config --libs --cflags ${staticPkgConfigParam} liblzma) \ + -o $out/bin/${name} ''; }; - rustPlatform-stable = pkgs.rustPlatform; - rustPlatform-unstable = pkgs.makeRustPlatform { - cargo = fenix.minimal.cargo; - rustc = fenix.minimal.rustc; - }; - rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; - rustPlatform-musl-unstable = pkgs.pkgsMusl.makeRustPlatform { - cargo = fenix.minimal.cargo; - rustc = fenix.minimal.rustc; - }; examples = [ "announce" "brightness_tester" @@ -92,6 +93,12 @@ let "random_stuff" "wiping_clear" ]; + mkAllExamples = + suffix: + pkgs.symlinkJoin { + name = "servicepoint-all-examples"; + paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; + }; size-cflags = [ "-Oz" "-fwrapv" @@ -112,54 +119,57 @@ let "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL" ]; - servicepoint-stable-size-args = { + stable-size-args = { buildType = "size_optimized"; buildNoDefaultFeatures = true; }; - servicepoint-unstable-size-args = { - # TODO: do these override the nix flags? - CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; + unstable-size-args = { + cargoBuildFlags = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; # TODO: those override the nix flags # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; }; - mkAllExamples = - suffix: - pkgs.symlinkJoin { - name = "servicepoint-all-examples"; - paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; - }; -in -rec { - servicepoint-binding-c-stable-release = mkServicepoint { + rustPlatform-stable = pkgs.rustPlatform; + rustPlatform-nightly = pkgs.makeRustPlatform { + inherit (fenix.minimal) cargo rustc; + }; + rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; + rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform { + inherit (fenix.minimal) cargo rustc; + }; + stable-release-args = { inherit pkgs; rustPlatform = rustPlatform-stable; }; - servicepoint-binding-c-nightly-release = mkServicepoint { + nightly-release-args = { inherit pkgs; - rustPlatform = rustPlatform-unstable; + rustPlatform = rustPlatform-nightly; }; - servicepoint-binding-c-musl-stable-release = mkServicepoint { + musl-stable-release-args = { pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-stable; }; - servicepoint-binding-c-musl-nightly-release = mkServicepoint { + musl-nightly-release-args = { pkgs = pkgs.pkgsMusl; - rustPlatform = rustPlatform-musl-unstable; + rustPlatform = rustPlatform-musl-nightly; }; +in +rec { + servicepoint-binding-c-stable-release = mkServicepoint stable-release-args; + servicepoint-binding-c-nightly-release = mkServicepoint nightly-release-args; + servicepoint-binding-c-musl-stable-release = mkServicepoint musl-stable-release-args; + servicepoint-binding-c-musl-nightly-release = mkServicepoint musl-nightly-release-args; - servicepoint-binding-c-stable-size = - servicepoint-binding-c-stable-release // servicepoint-stable-size-args; - servicepoint-binding-c-nightly-size = - servicepoint-binding-c-nightly-release - // servicepoint-stable-size-args - // servicepoint-unstable-size-args; - servicepoint-binding-c-musl-stable-size = - servicepoint-binding-c-musl-stable-release // servicepoint-stable-size-args; - servicepoint-binding-c-musl-nightly-size = - servicepoint-binding-c-musl-nightly-release - // servicepoint-stable-size-args - // servicepoint-unstable-size-args; + servicepoint-binding-c-stable-size = mkServicepoint (stable-release-args // stable-size-args); + servicepoint-binding-c-nightly-size = mkServicepoint ( + nightly-release-args // stable-size-args // unstable-size-args + ); + servicepoint-binding-c-musl-stable-size = mkServicepoint ( + musl-stable-release-args // stable-size-args + ); + servicepoint-binding-c-musl-nightly-size = mkServicepoint ( + musl-nightly-release-args // stable-size-args // unstable-size-args + ); # default variants servicepoint-binding-c = servicepoint-binding-c-stable-release; @@ -181,26 +191,16 @@ rec { } )) # construct another pakage per example, but optimized for size with unstable rust -// (lib.mapAttrs' - ( - name: value: - lib.nameValuePair ("${name}-size") ( - value - // { - EXTRA_CFLAGS = builtins.toString size-cflags; - } - ) +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-size") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name pkgs; + servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; + EXTRA_CFLAGS = builtins.toString size-cflags; + } ) - ( - lib.genAttrs examples ( - name: - mkExample { - inherit name pkgs; - servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; - } - ) - ) -) +)) # construct another pakage per example, but with musl // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl") value) ( lib.genAttrs examples ( @@ -220,9 +220,7 @@ rec { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; - } - // { - EXTRA_CFLAGS = "-static"; + static = true; } ) )) @@ -234,9 +232,8 @@ rec { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; - } - // { - EXTRA_CFLAGS = "-static" + builtins.toString size-cflags; + static = true; + EXTRA_CFLAGS = builtins.toString size-cflags; } ) )) From db94fecbb333242f80ed69f013f421be2a17a259 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 5 Jun 2025 17:38:54 +0200 Subject: [PATCH 34/76] more fixes --- nix-build-all.sh | 28 +++++++++++----------- packages.nix | 60 +++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/nix-build-all.sh b/nix-build-all.sh index 2973cbc..8adce87 100755 --- a/nix-build-all.sh +++ b/nix-build-all.sh @@ -3,22 +3,24 @@ set -e set -x -BUILD="nix build" +BUILD="nix build -L" -$BUILD .#servicepoint-binding-c +$BUILD .#servicepoint-binding-c -o result -$BUILD .#servicepoint-binding-c-stable-release -$BUILD .#servicepoint-binding-c-stable-size -$BUILD .#servicepoint-binding-c-nightly-release -$BUILD .#servicepoint-binding-c-nightly-size +$BUILD .#servicepoint-binding-c-stable-release -o result-stable-release +$BUILD .#servicepoint-binding-c-stable-size -o result-stable-size +$BUILD .#servicepoint-binding-c-nightly-release -o result-nightly-release +$BUILD .#servicepoint-binding-c-nightly-size -o result-nightly-size +$BUILD .#servicepoint-binding-c-musl-stable-release -o result-musl-release +$BUILD .#servicepoint-binding-c-musl-stable-size -o result-musl-size -$BUILD .#servicepoint-binding-c-musl-stable-release -$BUILD .#servicepoint-binding-c-musl-stable-size +# do not work yet: # $BUILD .#servicepoint-binding-c-musl-nightly-release # $BUILD .#servicepoint-binding-c-musl-nightly-size -$BUILD .#all-examples -$BUILD .#all-examples-size -$BUILD .#all-examples-musl -$BUILD .#all-examples-musl-static -$BUILD .#all-examples-musl-static-size +$BUILD .#all-examples -o result-examples +$BUILD .#all-examples-size -o result-examples-size +$BUILD .#all-examples-nightly-size -o result-nightly-size +$BUILD .#all-examples-musl -o result-examples-musl +$BUILD .#all-examples-musl-static -o result-examples-musl-static +$BUILD .#all-examples-musl-static-size -o result-examples-musl-static-size diff --git a/packages.nix b/packages.nix index 348d98c..8096f54 100644 --- a/packages.nix +++ b/packages.nix @@ -14,6 +14,7 @@ let buildType ? "release", buildNoDefaultFeatures ? false, cargoBuildFlags ? [ ], + nativeBuildInputs ? [] }: rustPlatform.buildRustPackage (finalAttrs: { inherit version buildType cargoBuildFlags; @@ -38,7 +39,7 @@ let license = lib.licenses.gpl3Plus; pkgConfigModules = [ "servicepoint" ]; }; - nativeBuildInputs = [ pkgs.pkg-config ]; + nativeBuildInputs = [ pkgs.pkg-config ] ++ nativeBuildInputs; buildInputs = [ pkgs.xz ]; preBuild = '' @@ -119,24 +120,10 @@ let "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL" ]; - stable-size-args = { - buildType = "size_optimized"; - buildNoDefaultFeatures = true; - }; - unstable-size-args = { - cargoBuildFlags = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; - # TODO: those override the nix flags - # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; - # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; - }; rustPlatform-stable = pkgs.rustPlatform; - rustPlatform-nightly = pkgs.makeRustPlatform { - inherit (fenix.minimal) cargo rustc; - }; + rustPlatform-nightly = pkgs.makeRustPlatform fenix.complete; rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; - rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform { - inherit (fenix.minimal) cargo rustc; - }; + rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform fenix.complete; stable-release-args = { inherit pkgs; rustPlatform = rustPlatform-stable; @@ -153,6 +140,21 @@ let pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-nightly; }; + stable-size-args = { + buildType = "size_optimized"; + buildNoDefaultFeatures = true; + }; + nightly-size-args = { + cargoBuildFlags = [ + "-Zbuild-std=core,std,alloc,proc_macro,panic_abort" + "-Zbuild-std-features=panic_immediate_abort" + ]; + # TODO: remove hard-coded target + nativeBuildInputs = [fenix.targets."x86_64-unknown-linux-gnu".latest.rust-std]; + # TODO: those override the nix flags + # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; + # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; + }; in rec { servicepoint-binding-c-stable-release = mkServicepoint stable-release-args; @@ -162,13 +164,13 @@ rec { servicepoint-binding-c-stable-size = mkServicepoint (stable-release-args // stable-size-args); servicepoint-binding-c-nightly-size = mkServicepoint ( - nightly-release-args // stable-size-args // unstable-size-args + nightly-release-args // stable-size-args // nightly-size-args ); servicepoint-binding-c-musl-stable-size = mkServicepoint ( musl-stable-release-args // stable-size-args ); servicepoint-binding-c-musl-nightly-size = mkServicepoint ( - musl-nightly-release-args // stable-size-args // unstable-size-args + musl-nightly-release-args // stable-size-args // nightly-size-args ); # default variants @@ -177,6 +179,7 @@ rec { all-examples = mkAllExamples ""; all-examples-size = mkAllExamples "-size"; + all-examples-nightly-size = mkAllExamples "-nightly-size"; # TODO: musl targets do not work on darwin all-examples-musl = mkAllExamples "-musl"; all-examples-musl-static = mkAllExamples "-musl-static"; @@ -190,8 +193,19 @@ rec { servicepointBinding = selfPkgs.servicepoint-binding-c; } )) -# construct another pakage per example, but optimized for size with unstable rust +# construct another pakage per example, but optimized for size with stable rust // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-size") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name pkgs; + servicepointBinding = selfPkgs.servicepoint-binding-c-stable-size; + EXTRA_CFLAGS = builtins.toString size-cflags; + } + ) +)) +# construct another pakage per example, but optimized for size with unstable rust +// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-unstable-size") value) ( lib.genAttrs examples ( name: mkExample { @@ -208,7 +222,7 @@ rec { mkExample { inherit name; pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-release; } ) )) @@ -219,7 +233,7 @@ rec { mkExample { inherit name; pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-release; static = true; } ) @@ -231,7 +245,7 @@ rec { mkExample { inherit name; pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl; + servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-size; static = true; EXTRA_CFLAGS = builtins.toString size-cflags; } From 02f629c68b93ecb97f61f0d23f77b7548bd7b425 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 16 Jun 2025 21:26:58 +0200 Subject: [PATCH 35/76] generate some functions with macros, cbindgen 0.29 --- Cargo.lock | 15 ++- Cargo.toml | 5 +- build.rs | 50 ++++----- cbindgen.toml | 3 +- include/servicepoint.h | 129 ++++++++++------------ src/commands/bitmap_command.rs | 22 +--- src/commands/bitvec_command.rs | 22 +--- src/commands/brightness_grid_command.rs | 28 +---- src/commands/cc_only_commands.rs | 22 +--- src/commands/char_grid_command.rs | 26 +---- src/commands/cp437_grid_command.rs | 28 +---- src/commands/generic_command.rs | 2 +- src/commands/global_brightness_command.rs | 25 +---- src/containers/bitmap.rs | 21 +--- src/containers/bitvec.rs | 20 +--- src/containers/brightness_grid.rs | 26 +---- src/containers/byte_slice.rs | 4 +- src/containers/char_grid.rs | 20 +--- src/containers/cp437_grid.rs | 21 +--- src/lib.rs | 1 + src/macros.rs | 25 +++++ src/packet.rs | 20 +--- src/udp.rs | 9 +- 23 files changed, 195 insertions(+), 349 deletions(-) create mode 100644 src/macros.rs diff --git a/Cargo.lock b/Cargo.lock index 121e01f..a4f755c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "cbindgen" -version = "0.28.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eadd868a2ce9ca38de7eeafdcec9c7065ef89b42b32f0839278d55f35c54d1ff" +checksum = "975982cdb7ad6a142be15bdf84aea7ec6a9e5d4d797c004d43185b24cfe4e684" dependencies = [ "clap", "heck", @@ -263,9 +263,9 @@ checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "indexmap" @@ -368,6 +368,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pkg-config" version = "0.3.32" @@ -540,6 +546,7 @@ version = "0.15.0" dependencies = [ "cbindgen", "env_logger", + "paste", "servicepoint", ] diff --git a/Cargo.toml b/Cargo.toml index f6bd53b..668edcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["cccb", "cccb-servicepoint", "cbindgen"] crate-type = ["staticlib", "cdylib", "rlib"] [build-dependencies] -cbindgen = "0.28.0" +cbindgen = "0.29.0" [dependencies.servicepoint] version = "0.15.0" @@ -25,6 +25,9 @@ default-features = false version = "0.11.8" optional = true +[dependencies] +paste = "1.0.15" + [features] all_compressions = ["servicepoint/all_compressions"] default = ["all_compressions", "servicepoint/default", "env_logger"] diff --git a/build.rs b/build.rs index 397341d..a353ecd 100644 --- a/build.rs +++ b/build.rs @@ -8,6 +8,13 @@ use std::{env, fs}; fn main() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let is_recursive = env::var("SERVICEPOINT_IS_BUILDING") + .unwrap_or("".to_string()) + == "true"; + unsafe { + env::set_var("SERVICEPOINT_IS_BUILDING", "true"); + } + println!("cargo::rerun-if-changed={crate_dir}/src"); println!("cargo::rerun-if-changed={crate_dir}/build.rs"); println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); @@ -18,41 +25,32 @@ fn main() { let config = cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") .unwrap(); - let output_dir = env::var("OUT_DIR").unwrap(); + + let output_dir = &env::var("OUT_DIR").unwrap(); let header_file = output_dir.clone() + "/servicepoint.h"; - let bindings = match cbindgen::generate_with_config(crate_dir, config) { - Ok(bindings) => bindings, - Err(e) => { - eprintln!("cargo:warning=Servicepoint header could not be generated: {e:?}"); - return; - } - }; + let bindings = cbindgen::generate_with_config(crate_dir, config) + .expect("Servicepoint header could not be generated"); bindings.write_to_file(&header_file); println!("cargo:include={output_dir}"); - if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { - let header_copy = header_out + "/servicepoint.h"; - - if fs::exists(&header_copy).unwrap_or(false) { - // check if content changed to prevent rebuild of dependents if not + if is_recursive { + return; + } - let mut bindings_text = Vec::new(); - bindings.write(&mut bindings_text); - - match fs::read(&header_copy) { - Ok(old_content) if old_content == bindings_text => { - println!("cargo:warning=Header did not change, not updating timestamp"); - return; - } - _ => {} - }; + if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { + if !fs::exists(&header_out).unwrap() { + panic!( + "SERVICEPOINT_HEADER_OUT is not set to an existing directory" + ); } - - // file does not exist or is different + + let header_copy = header_out + "/servicepoint.h"; + println!("cargo:warning=Copying header to {header_copy}"); - fs::copy(header_file, &header_copy).unwrap(); + fs::copy(header_file, &header_copy) + .expect("header could not be copied to SERVICEPOINT_HEADER_OUT"); println!("cargo::rerun-if-changed={header_copy}"); } } diff --git a/cbindgen.toml b/cbindgen.toml index 6edd33f..2341192 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -30,7 +30,8 @@ include = ["servicepoint", "std"] extra_bindings = ["servicepoint", "servicepoint_binding_c"] [parse.expand] -features = ["full"] +crates = ["servicepoint_binding_c", "paste"] +features = [] [export] include = [] diff --git a/include/servicepoint.h b/include/servicepoint.h index 3721527..4caa011 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1,7 +1,7 @@ #ifndef SERVICEPOINT_BINDINGS_C #define SERVICEPOINT_BINDINGS_C -/* Generated with cbindgen:0.28.0 */ +/* Generated with cbindgen:0.29.0 */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ @@ -640,9 +640,9 @@ extern "C" { void init_env_logger(void); /** - * Clones a [Bitmap]. + *Clones a [Bitmap] instance. */ -struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap); +struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); /** * Sets the state of all pixels in the [Bitmap]. @@ -655,9 +655,9 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ bitmap); void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value); /** - * Deallocates a [Bitmap]. + *Deallocates a [Bitmap] instance. */ -void sp_bitmap_free(struct Bitmap */*notnull*/ bitmap); +void sp_bitmap_free(struct Bitmap */*notnull*/ instance); /** * Tries to convert the BitVec to a Bitmap. @@ -801,9 +801,9 @@ struct ByteSlice sp_bitmap_unsafe_data_ref(struct Bitmap */*notnull*/ bitmap); size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); /** - * Clones a [DisplayBitVec]. + *Clones a [DisplayBitVec] instance. */ -BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ bit_vec); +BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); /** * Sets the value of all bits in the [DisplayBitVec]. @@ -816,9 +816,9 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ bit_vec); void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); /** - * Deallocates a [DisplayBitVec]. + *Deallocates a [DisplayBitVec] instance. */ -void sp_bitvec_free(BitVec */*notnull*/ bit_vec); +void sp_bitvec_free(BitVec */*notnull*/ instance); /** * Gets the value of a bit from the [DisplayBitVec]. @@ -915,9 +915,9 @@ void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value); struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); /** - * Clones a [BrightnessGrid]. + *Clones a [BrightnessGrid] instance. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ grid); +BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); /** * Sets the value of all cells in the [BrightnessGrid]. @@ -931,9 +931,9 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid, Brightness value); /** - * Deallocates a [BrightnessGrid]. + *Deallocates a [BrightnessGrid] instance. */ -void sp_brightness_grid_free(BrightnessGrid */*notnull*/ brightness_grid); +void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); /** * Gets the current value at the specified position. @@ -1051,9 +1051,9 @@ struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); /** - * Clones a [CharGrid]. + *Clones a [CharGrid] instance. */ -CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid); +CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); /** * Sets the value of all cells in the [CharGrid]. @@ -1066,9 +1066,9 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ grid); void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value); /** - * Deallocates a [CharGrid]. + *Deallocates a [CharGrid] instance. */ -void sp_char_grid_free(CharGrid */*notnull*/ char_grid); +void sp_char_grid_free(CharGrid */*notnull*/ instance); /** * Returns the current value at the specified position. @@ -1157,16 +1157,14 @@ void sp_char_grid_set(CharGrid */*notnull*/ char_grid, size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); /** - * Clones an [BitmapCommand] instance. - * - * returns: a new [BitmapCommand] instance. + *Clones a [BitmapCommand] instance. */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ command); +struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); /** - * Deallocates a [BitmapCommand] instance. + *Deallocates a [BitmapCommand] instance. */ -void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ command); +void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); /** * Move the provided [Bitmap] into a new [BitmapCommand], @@ -1237,16 +1235,14 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** - * Clones an [BitVecCommand] instance. - * - * returns: a new [BitVecCommand] instance. + *Clones a [BitVecCommand] instance. */ -struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ command); +struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); /** - * Deallocates a [BitVecCommand]. + *Deallocates a [BitVecCommand] instance. */ -void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ command); +void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); /** * Returns a pointer to the [BitVec] contained in the [BitVecCommand]. @@ -1319,13 +1315,14 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ command, struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** - * Clones an [GlobalBrightnessCommand] instance. - * - * returns: a new [GlobalBrightnessCommand] instance. + *Clones a [GlobalBrightnessCommand] instance. */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ command); +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ instance); -void sp_cmd_brightness_global_free(struct BitmapCommand */*notnull*/ command); +/** + *Deallocates a [GlobalBrightnessCommand] instance. + */ +void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance); Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); @@ -1345,16 +1342,14 @@ void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ co Brightness brightness); /** - * Clones an [BrightnessGridCommand] instance. - * - * returns: a new [BrightnessGridCommand] instance. + *Clones a [BrightnessGridCommand] instance. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ command); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - * Deallocates a [BitmapCommand]. + *Deallocates a [BrightnessGridCommand] instance. */ -void sp_cmd_brightness_grid_free(struct BitmapCommand */*notnull*/ command); +void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ instance); /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], @@ -1406,16 +1401,14 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ size_t origin_y); /** - * Clones an [CharGridCommand] instance. - * - * returns: a new [CharGridCommand] instance. + *Clones a [CharGridCommand] instance. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ command); +struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ instance); /** - * Deallocates a [BitmapCommand]. + *Deallocates a [CharGridCommand] instance. */ -void sp_cmd_char_grid_free(struct BitmapCommand */*notnull*/ command); +void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1467,9 +1460,9 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** - * Deallocates a [ClearCommand]. + *Deallocates a [ClearCommand] instance. */ -void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command); +void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); /** * Set all pixels to the off state. @@ -1481,16 +1474,14 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ command); struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); /** - * Clones an [Cp437GridCommand] instance. - * - * returns: a new [Cp437GridCommand] instance. + *Clones a [Cp437GridCommand] instance. */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ command); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ instance); /** - * Deallocates a [Cp437GridCommand]. + *Deallocates a [Cp437GridCommand] instance. */ -void sp_cmd_cp437_grid_free(struct BitmapCommand */*notnull*/ command); +void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance); /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], @@ -1553,9 +1544,9 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** - * Deallocates a [FadeOutCommand]. + *Deallocates a [FadeOutCommand] instance. */ -void sp_cmd_fade_out_free(struct ClearCommand */*notnull*/ command); +void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance); /** * A yet-to-be-tested command. @@ -1603,9 +1594,9 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** - * Deallocates a [HardResetCommand]. + *Deallocates a [HardResetCommand] instance. */ -void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command); +void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); /** * Kills the udp daemon on the display, which usually results in a restart. @@ -1617,9 +1608,9 @@ void sp_cmd_hard_reset_free(struct ClearCommand */*notnull*/ command); struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** - * Clones a [Cp437Grid]. + *Clones a [Cp437Grid] instance. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid); +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); /** * Sets the value of all cells in the [Cp437Grid]. @@ -1632,9 +1623,9 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ grid); void sp_cp437_grid_fill(Cp437Grid */*notnull*/ cp437_grid, uint8_t value); /** - * Deallocates a [Cp437Grid]. + *Deallocates a [Cp437Grid] instance. */ -void sp_cp437_grid_free(Cp437Grid */*notnull*/ cp437_grid); +void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** * Gets the current value at the specified position. @@ -1723,16 +1714,14 @@ struct ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); /** - * Clones a [Packet]. - * - * returns: a new [Packet] instance. + *Clones a [Packet] instance. */ -struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ packet); +struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** - * Deallocates a [Packet]. + *Deallocates a [Packet] instance. */ -void sp_packet_free(struct Packet */*notnull*/ packet); +void sp_packet_free(struct Packet */*notnull*/ instance); /** * Creates a raw [Packet] from parts. @@ -1792,9 +1781,9 @@ bool sp_u16_to_command_code(uint16_t code, CommandCode *result); /** - * Closes and deallocates a [UdpSocket]. + *Deallocates a [UdpSocket] instance. */ -void sp_udp_free(struct UdpSocket */*notnull*/ connection); +void sp_udp_free(struct UdpSocket */*notnull*/ instance); /** * Creates a new instance of [UdpSocket]. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 286a6f1..6018b33 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitmapCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_free(command: NonNull) { - unsafe { heap_drop(command) } -} +wrap_clone!(BitmapCommand, sp_cmd_bitmap); +wrap_free!(BitmapCommand, sp_cmd_bitmap); /// Returns a pointer to the provided `BitmapCommand`. /// diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index b49cdc5..b3eb843 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_free(command: NonNull) { - 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] diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7410f2d..7803486 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_free( - command: NonNull, -) { - 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] diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 2a48c12..0acfa68 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -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 { heap_move_nonnull(ClearCommand) } -/// Deallocates a [ClearCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_clear_free(command: NonNull) { - 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 { heap_move_nonnull(HardResetCommand) } -/// Deallocates a [HardResetCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_hard_reset_free( - command: NonNull, -) { - 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 { heap_move_nonnull(FadeOutCommand) } -/// Deallocates a [FadeOutCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_fade_out_free(command: NonNull) { - unsafe { heap_drop(command) } -} +wrap_free!(FadeOutCommand, sp_cmd_fade_out); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 1fbd66c..8a6c07c 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_free( - command: NonNull, -) { - 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] diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index f034c6d..fc32f05 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -/// Deallocates a [Cp437GridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_free( - command: NonNull, -) { - 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. /// diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 851e897..840900f 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -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 diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6b2643e..48d21d7 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(command) } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_free( - command: NonNull, -) { - 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] diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 7a1b633..63cac85 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(bitmap) } -} - -/// Deallocates a [Bitmap]. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull) { - 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]. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index e9eeb68..5f2d7b0 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(bit_vec) } -} - -/// Deallocates a [DisplayBitVec]. -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull) { - unsafe { heap_drop(bit_vec) } -} +wrap_clone!(DisplayBitVec, sp_bitvec); +wrap_free!(DisplayBitVec, sp_bitvec); /// Gets the value of a bit from the [DisplayBitVec]. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 3a7ae5f..21c3636 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(grid) } -} - -/// Deallocates a [BrightnessGrid]. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_free( - brightness_grid: NonNull, -) { - 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. /// diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 0668318..0642cfc 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -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}; /// ``` diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 3363d43..5c788f4 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(grid) } -} - -/// Deallocates a [CharGrid]. -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull) { - 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. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a636295..bd58981 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(grid) } -} - -/// Deallocates a [Cp437Grid]. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull) { - 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. /// diff --git a/src/lib.rs b/src/lib.rs index 5c64328..e08fbd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..80e19ef --- /dev/null +++ b/src/macros.rs @@ -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}; diff --git a/src/packet.rs b/src/packet.rs index b1269df..658265c 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -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, -) -> NonNull { - unsafe { heap_clone(packet) } -} - -/// Deallocates a [Packet]. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_free(packet: NonNull) { - unsafe { heap_drop(packet) } -} +wrap_clone!(Packet, sp_packet); +wrap_free!(Packet, sp_packet); /// Converts u16 into [CommandCode]. /// diff --git a/src/udp.rs b/src/udp.rs index fe9f876..eef82a8 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -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) { - unsafe { heap_drop(connection) } -} +wrap_free!(UdpSocket, sp_udp); From f5240386252f24ecbf5017a8d2da49bb700ecdfe Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 16 Jun 2025 22:03:57 +0200 Subject: [PATCH 36/76] generate header by running cbindgen directly --- Cargo.toml | 1 - build.rs | 56 --------------------------------------------- generate-binding.sh | 2 +- 3 files changed, 1 insertion(+), 58 deletions(-) delete mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 668edcc..d9a1a3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ description = "C bindings for the servicepoint crate." homepage = "https://docs.rs/crate/servicepoint_binding_c" repository = "https://git.berlin.ccc.de/servicepoint/servicepoint" readme = "README.md" -links = "servicepoint" keywords = ["cccb", "cccb-servicepoint", "cbindgen"] [lib] diff --git a/build.rs b/build.rs deleted file mode 100644 index a353ecd..0000000 --- a/build.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! Build script generating the header for the `servicepoint` C library. -//! -//! When the environment variable `SERVICEPOINT_HEADER_OUT` is set, the header is copied there from -//! the out directory. This can be used to use the build script as a command line tool from other -//! build tools. - -use std::{env, fs}; - -fn main() { - let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); - let is_recursive = env::var("SERVICEPOINT_IS_BUILDING") - .unwrap_or("".to_string()) - == "true"; - unsafe { - env::set_var("SERVICEPOINT_IS_BUILDING", "true"); - } - - println!("cargo::rerun-if-changed={crate_dir}/src"); - println!("cargo::rerun-if-changed={crate_dir}/build.rs"); - println!("cargo::rerun-if-changed={crate_dir}/Cargo.toml"); - println!("cargo::rerun-if-changed={crate_dir}/Cargo.lock"); - println!("cargo::rerun-if-changed={crate_dir}/cbindgen.toml"); - println!("cargo::rerun-if-env-changed=SERVICEPOINT_HEADER_OUT"); - - let config = - cbindgen::Config::from_file(crate_dir.clone() + "/cbindgen.toml") - .unwrap(); - - let output_dir = &env::var("OUT_DIR").unwrap(); - let header_file = output_dir.clone() + "/servicepoint.h"; - - let bindings = cbindgen::generate_with_config(crate_dir, config) - .expect("Servicepoint header could not be generated"); - - bindings.write_to_file(&header_file); - println!("cargo:include={output_dir}"); - - if is_recursive { - return; - } - - if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { - if !fs::exists(&header_out).unwrap() { - panic!( - "SERVICEPOINT_HEADER_OUT is not set to an existing directory" - ); - } - - let header_copy = header_out + "/servicepoint.h"; - - println!("cargo:warning=Copying header to {header_copy}"); - fs::copy(header_file, &header_copy) - .expect("header could not be copied to SERVICEPOINT_HEADER_OUT"); - println!("cargo::rerun-if-changed={header_copy}"); - } -} diff --git a/generate-binding.sh b/generate-binding.sh index 45301bd..744ad29 100755 --- a/generate-binding.sh +++ b/generate-binding.sh @@ -3,4 +3,4 @@ set -e SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" -SERVICEPOINT_HEADER_OUT="$SCRIPT_PATH/include" cargo build --release +cbindgen --config $SCRIPT_PATH/cbindgen.toml --crate servicepoint_binding_c --output $SCRIPT_PATH/include/servicepoint.h From 9756ef39b757ae1588dc5834afc42c91c49f6d63 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 17 Jun 2025 21:12:24 +0200 Subject: [PATCH 37/76] wrap normal methods with macro --- include/servicepoint.h | 281 ++++++++++++++---------------- src/containers/bitmap.rs | 143 ++++++--------- src/containers/bitvec.rs | 146 +++++++--------- src/containers/brightness_grid.rs | 163 +++++++---------- src/containers/char_grid.rs | 130 ++++++-------- src/containers/cp437_grid.rs | 143 ++++++--------- src/macros.rs | 62 ++++++- 7 files changed, 477 insertions(+), 591 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 4caa011..6d3b891 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -645,14 +645,24 @@ void init_env_logger(void); struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); /** + * Calls [`servicepoint::Bitmap::data_ref_mut`]. + * + * Gets an unsafe reference to the data of the [Bitmap] instance. + * + * The returned memory is valid for the lifetime of the bitmap. + */ +struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); + +/** + * Calls [`servicepoint::Bitmap::fill`]. + * * Sets the state of all pixels in the [Bitmap]. * * # Arguments * - * - `bitmap`: instance to write to * - `value`: the value to set all pixels to */ -void sp_bitmap_fill(struct Bitmap */*notnull*/ bitmap, bool value); +void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** *Deallocates a [Bitmap] instance. @@ -669,30 +679,29 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** - * Gets the current value at the specified position in the [Bitmap]. + * Calls [`servicepoint::Bitmap::get`]. + * + * Gets the current value at the specified position. * * # Arguments * - * - `bitmap`: instance to read from * - `x` and `y`: position of the cell to read * * # Panics * * - when accessing `x` or `y` out of bounds */ -bool sp_bitmap_get(struct Bitmap */*notnull*/ bitmap, size_t x, size_t y); +bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height in pixels of the [Bitmap] instance. + * Calls [`servicepoint::Bitmap::height`]. * - * # Arguments - * - * - `bitmap`: instance to read from + * Gets the height in pixels. */ -size_t sp_bitmap_height(struct Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** - * Consumes the Bitmap and returns the contained BitVec + * Consumes the Bitmap and returns the contained BitVec. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -757,11 +766,12 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** - * Sets the value of the specified position in the [Bitmap]. + * Calls [`servicepoint::Bitmap::set`]. + * + * Sets the value of the specified position. * * # Arguments * - * - `bitmap`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -769,36 +779,26 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds */ -void sp_bitmap_set(struct Bitmap */*notnull*/ bitmap, +void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, size_t y, bool value); /** - * Gets an unsafe reference to the data of the [Bitmap] instance. + * Calls [`servicepoint::Bitmap::width`]. * - * The returned memory is valid for the lifetime of the bitmap. + * Gets the width in pixels. */ -struct ByteSlice sp_bitmap_unsafe_data_ref(struct Bitmap */*notnull*/ bitmap); +size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Gets the width in pixels of the [Bitmap] instance. + * Calls [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. * - * # Arguments + * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * - * - `bitmap`: instance to read from - * - * # Panics - * - * - when `bitmap` is NULL - * - * # Safety - * - * The caller has to make sure that: - * - * - `bitmap` points to a valid [Bitmap] + * The returned memory is valid for the lifetime of the bitvec. */ -size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); +struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** *Clones a [DisplayBitVec] instance. @@ -806,14 +806,15 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ bitmap); BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); /** - * Sets the value of all bits in the [DisplayBitVec]. + * Calls [`servicepoint::DisplayBitVec::fill`]. + * + * Sets the value of all bits. * * # Arguments * - * - `bit_vec`: instance to write to * - `value`: the value to set all bits to */ -void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); +void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** *Deallocates a [DisplayBitVec] instance. @@ -821,7 +822,9 @@ void sp_bitvec_fill(BitVec */*notnull*/ bit_vec, bool value); void sp_bitvec_free(BitVec */*notnull*/ instance); /** - * Gets the value of a bit from the [DisplayBitVec]. + * Calls [`servicepoint::DisplayBitVec::get`]. + * + * Gets the value of a bit. * * # Arguments * @@ -834,7 +837,7 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * * - when accessing `index` out of bounds */ -bool sp_bitvec_get(BitVec */*notnull*/ bit_vec, size_t index); +bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -849,22 +852,18 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, CompressionCode compression); /** + * Calls [`servicepoint::DisplayBitVec::is_empty`]. + * * Returns true if length is 0. - * - * # Arguments - * - * - `bit_vec`: instance to write to */ -bool sp_bitvec_is_empty(BitVec */*notnull*/ bit_vec); +bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); /** - * Gets the length of the [DisplayBitVec] in bits. + * Calls [`servicepoint::DisplayBitVec::len`]. * - * # Arguments - * - * - `bit_vec`: instance to write to + * Gets the length in bits. */ -size_t sp_bitvec_len(BitVec */*notnull*/ bit_vec); +size_t sp_bitvec_len(BitVec */*notnull*/ instance); /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. @@ -889,11 +888,12 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); BitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Sets the value of a bit in the [DisplayBitVec]. + * Calls [`servicepoint::DisplayBitVec::set`]. + * + * Sets the value of a bit. * * # Arguments * - * - `bit_vec`: instance to write to * - `index`: the bit index to edit * - `value`: the value to set the bit to * @@ -901,18 +901,7 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds */ -void sp_bitvec_set(BitVec */*notnull*/ bit_vec, size_t index, bool value); - -/** - * Gets an unsafe reference to the data of the [DisplayBitVec] instance. - * - * The returned memory is valid for the lifetime of the bitvec. - * - * # Arguments - * - * - `bit_vec`: instance to write to - */ -struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); +void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** *Clones a [BrightnessGrid] instance. @@ -920,14 +909,24 @@ struct ByteSlice sp_bitvec_unsafe_data_ref(BitVec */*notnull*/ bit_vec); BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); /** - * Sets the value of all cells in the [BrightnessGrid]. + * Calls [`servicepoint::BrightnessGrid::data_ref_mut`]. + * + * Gets an unsafe reference to the data of the instance. + * + * The returned memory is valid for the lifetime of the grid. + */ +struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); + +/** + * Calls [`servicepoint::BrightnessGrid::fill`]. + * + * Sets the value of all cells. * * # Arguments * - * - `brightness_grid`: instance to write to * - `value`: the value to set all cells to */ -void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid, +void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); /** @@ -936,11 +935,12 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ brightness_grid, void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); /** + * Calls [`servicepoint::BrightnessGrid::get`]. + * * Gets the current value at the specified position. * * # Arguments * - * - `brightness_grid`: instance to read from * - `x` and `y`: position of the cell to read * * returns: value at position @@ -948,20 +948,16 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * # Panics * - When accessing `x` or `y` out of bounds. */ -Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ brightness_grid, +Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height of the [BrightnessGrid] instance. + * Calls [`servicepoint::BrightnessGrid::height`]. * - * # Arguments - * - * - `brightness_grid`: instance to read from - * - * returns: height + * Gets the height of the grid. */ -size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ brightness_grid); +size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); /** * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. @@ -1007,11 +1003,12 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); /** - * Sets the value of the specified position in the [BrightnessGrid]. + * Calls [`servicepoint::BrightnessGrid::set`]. + * + * Sets the value of the specified position. * * # Arguments * - * - `brightness_grid`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -1021,34 +1018,17 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * * - When accessing `x` or `y` out of bounds. */ -void sp_brightness_grid_set(BrightnessGrid */*notnull*/ brightness_grid, +void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, size_t x, size_t y, Brightness value); /** - * Gets an unsafe reference to the data of the [BrightnessGrid] instance. + * Calls [`servicepoint::BrightnessGrid::width`]. * - * The returned memory is valid for the lifetime of the brightness grid. - * - * # Arguments - * - * - `brightness_grid`: instance to read from - * - * returns: slice of bytes underlying the `brightness_grid`. + * Gets the width of the grid. */ -struct ByteSlice sp_brightness_grid_unsafe_data_ref(BrightnessGrid */*notnull*/ brightness_grid); - -/** - * Gets the width of the [BrightnessGrid] instance. - * - * # Arguments - * - * - `brightness_grid`: instance to read from - * - * returns: width - */ -size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); +size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); /** *Clones a [CharGrid] instance. @@ -1056,14 +1036,16 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ brightness_grid); CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); /** - * Sets the value of all cells in the [CharGrid]. + * Calls [`servicepoint::CharGrid::fill`]. + * + * Sets the value of all cells in the grid. * * # Arguments * - * - `char_grid`: instance to write to * - `value`: the value to set all cells to + * - when providing values that cannot be converted to Rust's `char`. */ -void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value); +void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [CharGrid] instance. @@ -1071,27 +1053,26 @@ void sp_char_grid_fill(CharGrid */*notnull*/ char_grid, uint32_t value); void sp_char_grid_free(CharGrid */*notnull*/ instance); /** + * Calls [`servicepoint::CharGrid::get`]. + * * Returns the current value at the specified position. * * # Arguments * - * - `char_grid`: instance to read from * - `x` and `y`: position of the cell to read * * # Panics * * - when accessing `x` or `y` out of bounds */ -uint32_t sp_char_grid_get(CharGrid */*notnull*/ char_grid, size_t x, size_t y); +uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height of the [CharGrid] instance. + * Calls [`servicepoint::CharGrid::height`]. * - * # Arguments - * - * - `char_grid`: instance to read from + * Gets the height of the grid. */ -size_t sp_char_grid_height(CharGrid */*notnull*/ char_grid); +size_t sp_char_grid_height(CharGrid */*notnull*/ instance); /** * Creates a [CharGridCommand] and immediately turns that into a [Packet]. @@ -1128,11 +1109,12 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); /** - * Sets the value of the specified position in the [CharGrid]. + * Calls [`servicepoint::CharGrid::set`]. + * + * Sets the value of the specified position in the grid. * * # Arguments * - * - `char_grid`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -1141,20 +1123,19 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * # Panics * * - when accessing `x` or `y` out of bounds + * - when providing values that cannot be converted to Rust's `char`. */ -void sp_char_grid_set(CharGrid */*notnull*/ char_grid, +void sp_char_grid_set(CharGrid */*notnull*/ instance, size_t x, size_t y, uint32_t value); /** - * Gets the width of the [CharGrid] instance. + * Calls [`servicepoint::CharGrid::width`]. * - * # Arguments - * - * - `char_grid`: instance to read from + * Gets the width of the grid. */ -size_t sp_char_grid_width(CharGrid */*notnull*/ char_grid); +size_t sp_char_grid_width(CharGrid */*notnull*/ instance); /** *Clones a [BitmapCommand] instance. @@ -1608,49 +1589,50 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** - *Clones a [Cp437Grid] instance. + * Calls [`servicepoint::Cp437Grid::data_ref_mut`]. + * + * Gets an unsafe reference to the data of the grid. + * + * The returned memory is valid for the lifetime of the instance. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); +struct ByteSlice sp_cp437_data_ref_mut(Cp437Grid */*notnull*/ instance); /** - * Sets the value of all cells in the [Cp437Grid]. + * Calls [`servicepoint::Cp437Grid::fill`]. + * + * Sets the value of all cells in the grid. * * # Arguments * * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to */ -void sp_cp437_grid_fill(Cp437Grid */*notnull*/ cp437_grid, uint8_t value); - -/** - *Deallocates a [Cp437Grid] instance. - */ -void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); +void sp_cp437_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** + * Calls [`servicepoint::Cp437Grid::get`]. + * * Gets the current value at the specified position. * * # Arguments * - * - `cp437_grid`: instance to read from * - `x` and `y`: position of the cell to read * * # Panics * * - when accessing `x` or `y` out of bounds */ -uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ cp437_grid, - size_t x, - size_t y); +uint8_t sp_cp437_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** - * Gets the height of the [Cp437Grid] instance. - * - * # Arguments - * - * - `cp437_grid`: instance to read from + *Clones a [Cp437Grid] instance. */ -size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ cp437_grid); +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); + +/** + *Deallocates a [Cp437Grid] instance. + */ +void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. @@ -1678,11 +1660,19 @@ Cp437Grid *sp_cp437_grid_load(size_t width, Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); /** - * Sets the value of the specified position in the [Cp437Grid]. + * Calls [`servicepoint::Cp437Grid::height`]. + * + * Gets the height of the grid. + */ +size_t sp_cp437_height(Cp437Grid */*notnull*/ instance); + +/** + * Calls [`servicepoint::Cp437Grid::set`]. + * + * Sets the value at the specified position. * * # Arguments * - * - `cp437_grid`: instance to write to * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * @@ -1692,26 +1682,17 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds */ -void sp_cp437_grid_set(Cp437Grid */*notnull*/ cp437_grid, - size_t x, - size_t y, - uint8_t value); +void sp_cp437_set(Cp437Grid */*notnull*/ instance, + size_t x, + size_t y, + uint8_t value); /** - * Gets an unsafe reference to the data of the [Cp437Grid] instance. + * Calls [`servicepoint::Cp437Grid::width`]. * - * The returned memory is valid for the lifetime of the grid. + * Gets the width of the grid. */ -struct ByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid */*notnull*/ cp437_grid); - -/** - * Gets the width of the [Cp437Grid] instance. - * - * # Arguments - * - * - `cp437_grid`: instance to read from - */ -size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ cp437_grid); +size_t sp_cp437_width(Cp437Grid */*notnull*/ instance); /** *Clones a [Packet] instance. diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 63cac85..ae97597 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -83,98 +83,67 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( wrap_clone!(Bitmap, sp_bitmap); wrap_free!(Bitmap, sp_bitmap); -/// Gets the current value at the specified position in the [Bitmap]. -/// -/// # Arguments -/// -/// - `bitmap`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_get( - bitmap: NonNull, - x: usize, - y: usize, -) -> bool { - unsafe { bitmap.as_ref().get(x, y) } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + ref fn get(x: usize, y: usize) -> bool; +); -/// Sets the value of the specified position in the [Bitmap]. -/// -/// # Arguments -/// -/// - `bitmap`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_set( - bitmap: NonNull, - x: usize, - y: usize, - value: bool, -) { - unsafe { (*bitmap.as_ptr()).set(x, y, value) }; -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Sets the value of the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + mut fn set(x: usize, y: usize, value: bool); +); -/// Sets the state of all pixels in the [Bitmap]. -/// -/// # Arguments -/// -/// - `bitmap`: instance to write to -/// - `value`: the value to set all pixels to -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_fill(bitmap: NonNull, value: bool) { - unsafe { (*bitmap.as_ptr()).fill(value) }; -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Sets the state of all pixels in the [Bitmap]. + /// + /// # Arguments + /// + /// - `value`: the value to set all pixels to + mut fn fill(value: bool); +); -/// Gets the width in pixels of the [Bitmap] instance. -/// -/// # Arguments -/// -/// - `bitmap`: instance to read from -/// -/// # Panics -/// -/// - when `bitmap` is NULL -/// -/// # Safety -/// -/// The caller has to make sure that: -/// -/// - `bitmap` points to a valid [Bitmap] -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_width(bitmap: NonNull) -> usize { - unsafe { bitmap.as_ref().width() } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets the width in pixels. + ref fn width() -> usize; +); -/// Gets the height in pixels of the [Bitmap] instance. -/// -/// # Arguments -/// -/// - `bitmap`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_height(bitmap: NonNull) -> usize { - unsafe { bitmap.as_ref().height() } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets the height in pixels. + ref fn height() -> usize; +); -/// Gets an unsafe reference to the data of the [Bitmap] instance. -/// -/// The returned memory is valid for the lifetime of the bitmap. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref( - mut bitmap: NonNull, -) -> ByteSlice { - unsafe { ByteSlice::from_slice(bitmap.as_mut().data_ref_mut()) } -} +wrap_method!( + sp_bitmap :: Bitmap; + /// Gets an unsafe reference to the data of the [Bitmap] instance. + /// + /// The returned memory is valid for the lifetime of the bitmap. + mut fn data_ref_mut() -> ByteSlice; + |slice| unsafe { ByteSlice::from_slice(slice) }; +); -/// Consumes the Bitmap and returns the contained BitVec +/// Consumes the Bitmap and returns the contained BitVec. #[no_mangle] pub unsafe extern "C" fn sp_bitmap_into_bitvec( bitmap: NonNull, diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 5f2d7b0..ea0ca4b 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -38,97 +38,69 @@ pub unsafe extern "C" fn sp_bitvec_load( wrap_clone!(DisplayBitVec, sp_bitvec); wrap_free!(DisplayBitVec, sp_bitvec); -/// Gets the value of a bit from the [DisplayBitVec]. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to read from -/// - `index`: the bit index to read -/// -/// returns: value of the bit -/// -/// # Panics -/// -/// - when accessing `index` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_get( - bit_vec: NonNull, - index: usize, -) -> bool { - unsafe { *bit_vec.as_ref().get(index).unwrap() } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Gets the value of a bit. + /// + /// # Arguments + /// + /// - `bit_vec`: instance to read from + /// - `index`: the bit index to read + /// + /// returns: value of the bit + /// + /// # Panics + /// + /// - when accessing `index` out of bounds + ref fn get(index: usize) -> bool; + |result| result.map(|x| *x).unwrap_or(false); +); -/// Sets the value of a bit in the [DisplayBitVec]. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -/// - `index`: the bit index to edit -/// - `value`: the value to set the bit to -/// -/// # Panics -/// -/// - when accessing `index` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_set( - bit_vec: NonNull, - index: usize, - value: bool, -) { - unsafe { (*bit_vec.as_ptr()).set(index, value) } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Sets the value of a bit. + /// + /// # Arguments + /// + /// - `index`: the bit index to edit + /// - `value`: the value to set the bit to + /// + /// # Panics + /// + /// - when accessing `index` out of bounds + mut fn set(index: usize, value: bool); +); -/// Sets the value of all bits in the [DisplayBitVec]. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -/// - `value`: the value to set all bits to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_fill( - bit_vec: NonNull, - value: bool, -) { - unsafe { (*bit_vec.as_ptr()).fill(value) } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Sets the value of all bits. + /// + /// # Arguments + /// + /// - `value`: the value to set all bits to + mut fn fill(value: bool); +); -/// Gets the length of the [DisplayBitVec] in bits. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_len( - bit_vec: NonNull, -) -> usize { - unsafe { bit_vec.as_ref().len() } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Gets the length in bits. + ref fn len() -> usize; +); -/// Returns true if length is 0. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_is_empty( - bit_vec: NonNull, -) -> bool { - unsafe { bit_vec.as_ref().is_empty() } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Returns true if length is 0. + ref fn is_empty() -> bool; +); -/// Gets an unsafe reference to the data of the [DisplayBitVec] instance. -/// -/// The returned memory is valid for the lifetime of the bitvec. -/// -/// # Arguments -/// -/// - `bit_vec`: instance to write to -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_unsafe_data_ref( - bit_vec: NonNull, -) -> ByteSlice { - unsafe { ByteSlice::from_slice((*bit_vec.as_ptr()).as_raw_mut_slice()) } -} +wrap_method!( + sp_bitvec :: DisplayBitVec; + /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. + /// + /// The returned memory is valid for the lifetime of the bitvec. + mut fn as_raw_mut_slice() -> ByteSlice; + |slice| unsafe { ByteSlice::from_slice(slice) }; +); /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 21c3636..a1a5e4e 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -55,112 +55,73 @@ pub unsafe extern "C" fn sp_brightness_grid_load( wrap_clone!(BrightnessGrid, sp_brightness_grid); wrap_free!(BrightnessGrid, sp_brightness_grid); -/// Gets the current value at the specified position. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// returns: value at position -/// -/// # Panics -/// - When accessing `x` or `y` out of bounds. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_get( - brightness_grid: NonNull, - x: usize, - y: usize, -) -> Brightness { - unsafe { brightness_grid.as_ref().get(x, y) } -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// returns: value at position + /// + /// # Panics + /// - When accessing `x` or `y` out of bounds. + ref fn get(x: usize, y: usize) -> Brightness; +); -/// Sets the value of the specified position in the [BrightnessGrid]. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// returns: old value of the cell -/// -/// # Panics -/// -/// - When accessing `x` or `y` out of bounds. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_set( - brightness_grid: NonNull, - x: usize, - y: usize, - value: Brightness, -) { - unsafe { (*brightness_grid.as_ptr()).set(x, y, value) }; -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Sets the value of the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// returns: old value of the cell + /// + /// # Panics + /// + /// - When accessing `x` or `y` out of bounds. + mut fn set(x: usize, y: usize, value: Brightness); +); -/// Sets the value of all cells in the [BrightnessGrid]. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to write to -/// - `value`: the value to set all cells to -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_fill( - brightness_grid: NonNull, - value: Brightness, -) { - unsafe { (*brightness_grid.as_ptr()).fill(value) }; -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Sets the value of all cells. + /// + /// # Arguments + /// + /// - `value`: the value to set all cells to + mut fn fill(value: Brightness); +); -/// Gets the width of the [BrightnessGrid] instance. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// -/// returns: width -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_width( - brightness_grid: NonNull, -) -> usize { - unsafe { brightness_grid.as_ref().width() } -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets the width of the grid. + ref fn width() -> usize; +); -/// Gets the height of the [BrightnessGrid] instance. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// -/// returns: height -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_height( - brightness_grid: NonNull, -) -> usize { - unsafe { brightness_grid.as_ref().height() } -} +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets the height of the grid. + ref fn height() -> usize; +); -/// Gets an unsafe reference to the data of the [BrightnessGrid] instance. -/// -/// The returned memory is valid for the lifetime of the brightness grid. -/// -/// # Arguments -/// -/// - `brightness_grid`: instance to read from -/// -/// returns: slice of bytes underlying the `brightness_grid`. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_unsafe_data_ref( - brightness_grid: NonNull, -) -> ByteSlice { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); +wrap_method!( + sp_brightness_grid :: BrightnessGrid; + /// Gets an unsafe reference to the data of the instance. + /// + /// The returned memory is valid for the lifetime of the grid. + mut fn data_ref_mut() -> ByteSlice; + |br_slice| unsafe { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - let data = unsafe { (*brightness_grid.as_ptr()).data_ref_mut() }; - unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(data)) - } -} + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + }; +); /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 5c788f4..bc421b5 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; @@ -42,85 +42,63 @@ pub unsafe extern "C" fn sp_char_grid_load( wrap_clone!(CharGrid, sp_char_grid); wrap_free!(CharGrid, sp_char_grid); -/// Returns the current value at the specified position. -/// -/// # Arguments -/// -/// - `char_grid`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_get( - char_grid: NonNull, - x: usize, - y: usize, -) -> u32 { - unsafe { char_grid.as_ref().get(x, y) as u32 } -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Returns the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + ref fn get(x: usize, y: usize) -> u32; + | char | char as u32 +); -/// Sets the value of the specified position in the [CharGrid]. -/// -/// # Arguments -/// -/// - `char_grid`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// returns: old value of the cell -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_set( - char_grid: NonNull, - x: usize, - y: usize, - value: u32, -) { - unsafe { (*char_grid.as_ptr()).set(x, y, char::from_u32(value).unwrap()) }; -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Sets the value of the specified position in the grid. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// returns: old value of the cell + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + /// - when providing values that cannot be converted to Rust's `char`. + mut fn set(x: usize, y: usize, value: u32); + let value = char::from_u32(value).unwrap(); +); -/// Sets the value of all cells in the [CharGrid]. -/// -/// # Arguments -/// -/// - `char_grid`: instance to write to -/// - `value`: the value to set all cells to -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_fill( - char_grid: NonNull, - value: u32, -) { - unsafe { (*char_grid.as_ptr()).fill(char::from_u32(value).unwrap()) }; -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Sets the value of all cells in the grid. + /// + /// # Arguments + /// + /// - `value`: the value to set all cells to + /// - when providing values that cannot be converted to Rust's `char`. + mut fn fill(value: u32); + let value = char::from_u32(value).unwrap(); +); -/// Gets the width of the [CharGrid] instance. -/// -/// # Arguments -/// -/// - `char_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_width( - char_grid: NonNull, -) -> usize { - unsafe { char_grid.as_ref().width() } -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Gets the width of the grid. + ref fn width() -> usize; +); -/// Gets the height of the [CharGrid] instance. -/// -/// # Arguments -/// -/// - `char_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_height( - char_grid: NonNull, -) -> usize { - unsafe { char_grid.as_ref().height() } -} +wrap_method!( + sp_char_grid :: CharGrid; + /// Gets the height of the grid. + ref fn height() -> usize; +); /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index bd58981..88fb2b0 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_method}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -33,95 +33,68 @@ pub unsafe extern "C" fn sp_cp437_grid_load( wrap_clone!(Cp437Grid, sp_cp437_grid); wrap_free!(Cp437Grid, sp_cp437_grid); -/// Gets the current value at the specified position. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to read from -/// - `x` and `y`: position of the cell to read -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_get( - cp437_grid: NonNull, - x: usize, - y: usize, -) -> u8 { - unsafe { cp437_grid.as_ref().get(x, y) } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + ref fn get(x: usize, y: usize) -> u8; +); -/// Sets the value of the specified position in the [Cp437Grid]. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to write to -/// - `x` and `y`: position of the cell -/// - `value`: the value to write to the cell -/// -/// returns: old value of the cell -/// -/// # Panics -/// -/// - when accessing `x` or `y` out of bounds -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_set( - cp437_grid: NonNull, - x: usize, - y: usize, - value: u8, -) { - unsafe { (*cp437_grid.as_ptr()).set(x, y, value) }; -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Sets the value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// returns: old value of the cell + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + mut fn set(x: usize, y: usize, value: u8); +); -/// Sets the value of all cells in the [Cp437Grid]. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to write to -/// - `value`: the value to set all cells to -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_fill( - cp437_grid: NonNull, - value: u8, -) { - unsafe { (*cp437_grid.as_ptr()).fill(value) }; -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Sets the value of all cells in the grid. + /// + /// # Arguments + /// + /// - `cp437_grid`: instance to write to + /// - `value`: the value to set all cells to + mut fn fill(value: u8); +); -/// Gets the width of the [Cp437Grid] instance. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_width( - cp437_grid: NonNull, -) -> usize { - unsafe { cp437_grid.as_ref().width() } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets the width of the grid. + ref fn width() -> usize; +); -/// Gets the height of the [Cp437Grid] instance. -/// -/// # Arguments -/// -/// - `cp437_grid`: instance to read from -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_height( - cp437_grid: NonNull, -) -> usize { - unsafe { cp437_grid.as_ref().height() } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets the height of the grid. + ref fn height() -> usize; +); -/// Gets an unsafe reference to the data of the [Cp437Grid] instance. -/// -/// The returned memory is valid for the lifetime of the grid. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( - cp437_grid: NonNull, -) -> ByteSlice { - unsafe { ByteSlice::from_slice((*cp437_grid.as_ptr()).data_ref_mut()) } -} +wrap_method!( + sp_cp437 :: Cp437Grid; + /// Gets an unsafe reference to the data of the grid. + /// + /// The returned memory is valid for the lifetime of the instance. + mut fn data_ref_mut() -> ByteSlice; + | slice | unsafe { ByteSlice::from_slice(slice) }; +); /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. /// diff --git a/src/macros.rs b/src/macros.rs index 80e19ef..c91248e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,25 +1,77 @@ macro_rules! wrap_free { - ($typ:ident, $prefix:ident) => { + ($typ:ty, $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) } + unsafe { $crate::mem::heap_drop(instance) } } } }; } macro_rules! wrap_clone { - ($typ:ident, $prefix:ident) => { + ($typ:ty, $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) } + unsafe { $crate::mem::heap_clone(instance) } } } }; } -pub(crate) use {wrap_clone, wrap_free}; +macro_rules! nonnull_as_ref { + ($ident:ident) => { + $ident.as_ref() + }; +} + +macro_rules! nonnull_as_mut { + ($ident:ident) => { + &mut *$ident.as_ptr() + }; +} + +// meta required on purpose, because otherwise the added documentation would suppress warnings +macro_rules! wrap_method { + ( + $prefix:ident :: $object_type:ty; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) + $(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)? + $(; let $param_let_name:ident = $param_let_expr:expr)* + $(;)? + ) => { + paste::paste! { + #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), + "::", stringify!($function), "`].")] + #[doc = ""] + $(#[$meta])* + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ $function>]( + instance: NonNull<$object_type>, + $($param_name: $param_type),* + ) $(-> $return_type)? { + let instance = unsafe {$crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance)}; + $(let $param_let_name = $param_let_expr;)* + #[allow( + unused_variables, + reason = "This variable may not be used depending on macro variables" )] + let result = instance.$function($($param_name),*); + $( + $( + let $it = result; + let result = $return_expr; + )? + return result; + )? + } + } + }; +} + +pub(crate) use { + nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_free, wrap_method, +}; From 514c0304b8df80d5134765727d9b9e444f4bddcf Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 17 Jun 2025 22:11:10 +0200 Subject: [PATCH 38/76] clone and free with :: syntax --- src/commands/bitmap_command.rs | 4 ++-- src/commands/bitvec_command.rs | 4 ++-- src/commands/brightness_grid_command.rs | 4 ++-- src/commands/cc_only_commands.rs | 6 +++--- src/commands/char_grid_command.rs | 4 ++-- src/commands/cp437_grid_command.rs | 4 ++-- src/commands/global_brightness_command.rs | 4 ++-- src/containers/bitmap.rs | 16 ++++++++-------- src/containers/bitvec.rs | 16 ++++++++-------- src/containers/brightness_grid.rs | 16 ++++++++-------- src/containers/char_grid.rs | 14 +++++++------- src/containers/cp437_grid.rs | 16 ++++++++-------- src/macros.rs | 4 ++-- src/packet.rs | 4 ++-- src/udp.rs | 2 +- 15 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 6018b33..24400c7 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -45,8 +45,8 @@ pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } -wrap_clone!(BitmapCommand, sp_cmd_bitmap); -wrap_free!(BitmapCommand, sp_cmd_bitmap); +wrap_clone!(sp_cmd_bitmap::BitmapCommand); +wrap_free!(sp_cmd_bitmap::BitmapCommand); /// Returns a pointer to the provided `BitmapCommand`. /// diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index b3eb843..aeff1cd 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -45,8 +45,8 @@ pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } -wrap_clone!(BitVecCommand, sp_cmd_bitvec); -wrap_free!(BitVecCommand, sp_cmd_bitvec); +wrap_clone!(sp_cmd_bitvec::BitVecCommand); +wrap_free!(sp_cmd_bitvec::BitVecCommand); /// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. #[no_mangle] diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7803486..9c5069e 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -41,8 +41,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } -wrap_clone!(BrightnessGridCommand, sp_cmd_brightness_grid); -wrap_free!(BrightnessGridCommand, sp_cmd_brightness_grid); +wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand); +wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand); /// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. #[no_mangle] diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 0acfa68..fc73303 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -12,7 +12,7 @@ pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull { heap_move_nonnull(ClearCommand) } -wrap_free!(ClearCommand, sp_cmd_clear); +wrap_free!(sp_cmd_clear::ClearCommand); /// Kills the udp daemon on the display, which usually results in a restart. /// @@ -24,7 +24,7 @@ pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull { heap_move_nonnull(HardResetCommand) } -wrap_free!(HardResetCommand, sp_cmd_hard_reset); +wrap_free!(sp_cmd_hard_reset::HardResetCommand); /// A yet-to-be-tested command. /// @@ -34,4 +34,4 @@ pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull { heap_move_nonnull(FadeOutCommand) } -wrap_free!(FadeOutCommand, sp_cmd_fade_out); +wrap_free!(sp_cmd_fade_out::FadeOutCommand); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 8a6c07c..b895718 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -41,8 +41,8 @@ pub unsafe extern "C" fn sp_cmd_char_grid_try_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } -wrap_clone!(CharGridCommand, sp_cmd_char_grid); -wrap_free!(CharGridCommand, sp_cmd_char_grid); +wrap_clone!(sp_cmd_char_grid::CharGridCommand); +wrap_free!(sp_cmd_char_grid::CharGridCommand); /// Moves the provided [CharGrid] to be contained in the [CharGridCommand]. #[no_mangle] diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index fc32f05..3f1fa9f 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -41,8 +41,8 @@ pub unsafe extern "C" fn sp_cmd_cp437_grid_try_into_packet( heap_move_ok(unsafe { heap_remove(command) }.try_into()) } -wrap_clone!(Cp437GridCommand, sp_cmd_cp437_grid); -wrap_free!(Cp437GridCommand, sp_cmd_cp437_grid); +wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); +wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); /// Moves the provided bitmap into the provided command. /// diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 48d21d7..20a350a 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -22,8 +22,8 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( heap_move_nonnull(unsafe { heap_remove(command) }.into()) } -wrap_clone!(GlobalBrightnessCommand, sp_cmd_brightness_global); -wrap_free!(GlobalBrightnessCommand, sp_cmd_brightness_global); +wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand); +wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand); /// Moves the provided bitmap to be contained in the command. #[no_mangle] diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index ae97597..9b96d10 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -80,11 +80,11 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( heap_move_ok(Bitmap::from_bitvec(width, bitvec)) } -wrap_clone!(Bitmap, sp_bitmap); -wrap_free!(Bitmap, sp_bitmap); +wrap_clone!(sp_bitmap::Bitmap); +wrap_free!(sp_bitmap::Bitmap); wrap_method!( - sp_bitmap :: Bitmap; + sp_bitmap::Bitmap; /// Gets the current value at the specified position. /// /// # Arguments @@ -98,7 +98,7 @@ wrap_method!( ); wrap_method!( - sp_bitmap :: Bitmap; + sp_bitmap::Bitmap; /// Sets the value of the specified position. /// /// # Arguments @@ -113,7 +113,7 @@ wrap_method!( ); wrap_method!( - sp_bitmap :: Bitmap; + sp_bitmap::Bitmap; /// Sets the state of all pixels in the [Bitmap]. /// /// # Arguments @@ -123,19 +123,19 @@ wrap_method!( ); wrap_method!( - sp_bitmap :: Bitmap; + sp_bitmap::Bitmap; /// Gets the width in pixels. ref fn width() -> usize; ); wrap_method!( - sp_bitmap :: Bitmap; + sp_bitmap::Bitmap; /// Gets the height in pixels. ref fn height() -> usize; ); wrap_method!( - sp_bitmap :: Bitmap; + sp_bitmap::Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index ea0ca4b..aad2a88 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -35,11 +35,11 @@ pub unsafe extern "C" fn sp_bitvec_load( heap_move_nonnull(DisplayBitVec::from_slice(data)) } -wrap_clone!(DisplayBitVec, sp_bitvec); -wrap_free!(DisplayBitVec, sp_bitvec); +wrap_clone!(sp_bitvec::DisplayBitVec); +wrap_free!(sp_bitvec::DisplayBitVec); wrap_method!( - sp_bitvec :: DisplayBitVec; + sp_bitvec::DisplayBitVec; /// Gets the value of a bit. /// /// # Arguments @@ -57,7 +57,7 @@ wrap_method!( ); wrap_method!( - sp_bitvec :: DisplayBitVec; + sp_bitvec::DisplayBitVec; /// Sets the value of a bit. /// /// # Arguments @@ -72,7 +72,7 @@ wrap_method!( ); wrap_method!( - sp_bitvec :: DisplayBitVec; + sp_bitvec::DisplayBitVec; /// Sets the value of all bits. /// /// # Arguments @@ -82,19 +82,19 @@ wrap_method!( ); wrap_method!( - sp_bitvec :: DisplayBitVec; + sp_bitvec::DisplayBitVec; /// Gets the length in bits. ref fn len() -> usize; ); wrap_method!( - sp_bitvec :: DisplayBitVec; + sp_bitvec::DisplayBitVec; /// Returns true if length is 0. ref fn is_empty() -> bool; ); wrap_method!( - sp_bitvec :: DisplayBitVec; + sp_bitvec::DisplayBitVec; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a1a5e4e..54491c2 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -52,11 +52,11 @@ pub unsafe extern "C" fn sp_brightness_grid_load( ) } -wrap_clone!(BrightnessGrid, sp_brightness_grid); -wrap_free!(BrightnessGrid, sp_brightness_grid); +wrap_clone!(sp_brightness_grid::BrightnessGrid); +wrap_free!(sp_brightness_grid::BrightnessGrid); wrap_method!( - sp_brightness_grid :: BrightnessGrid; + sp_brightness_grid::BrightnessGrid; /// Gets the current value at the specified position. /// /// # Arguments @@ -71,7 +71,7 @@ wrap_method!( ); wrap_method!( - sp_brightness_grid :: BrightnessGrid; + sp_brightness_grid::BrightnessGrid; /// Sets the value of the specified position. /// /// # Arguments @@ -88,7 +88,7 @@ wrap_method!( ); wrap_method!( - sp_brightness_grid :: BrightnessGrid; + sp_brightness_grid::BrightnessGrid; /// Sets the value of all cells. /// /// # Arguments @@ -98,19 +98,19 @@ wrap_method!( ); wrap_method!( - sp_brightness_grid :: BrightnessGrid; + sp_brightness_grid::BrightnessGrid; /// Gets the width of the grid. ref fn width() -> usize; ); wrap_method!( - sp_brightness_grid :: BrightnessGrid; + sp_brightness_grid::BrightnessGrid; /// Gets the height of the grid. ref fn height() -> usize; ); wrap_method!( - sp_brightness_grid :: BrightnessGrid; + sp_brightness_grid::BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index bc421b5..5db7897 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -39,11 +39,11 @@ pub unsafe extern "C" fn sp_char_grid_load( heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } -wrap_clone!(CharGrid, sp_char_grid); -wrap_free!(CharGrid, sp_char_grid); +wrap_clone!(sp_char_grid::CharGrid); +wrap_free!(sp_char_grid::CharGrid); wrap_method!( - sp_char_grid :: CharGrid; + sp_char_grid::CharGrid; /// Returns the current value at the specified position. /// /// # Arguments @@ -58,7 +58,7 @@ wrap_method!( ); wrap_method!( - sp_char_grid :: CharGrid; + sp_char_grid::CharGrid; /// Sets the value of the specified position in the grid. /// /// # Arguments @@ -77,7 +77,7 @@ wrap_method!( ); wrap_method!( - sp_char_grid :: CharGrid; + sp_char_grid::CharGrid; /// Sets the value of all cells in the grid. /// /// # Arguments @@ -89,13 +89,13 @@ wrap_method!( ); wrap_method!( - sp_char_grid :: CharGrid; + sp_char_grid::CharGrid; /// Gets the width of the grid. ref fn width() -> usize; ); wrap_method!( - sp_char_grid :: CharGrid; + sp_char_grid::CharGrid; /// Gets the height of the grid. ref fn height() -> usize; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 88fb2b0..ebf652e 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -30,11 +30,11 @@ pub unsafe extern "C" fn sp_cp437_grid_load( heap_move_some(Cp437Grid::load(width, height, data)) } -wrap_clone!(Cp437Grid, sp_cp437_grid); -wrap_free!(Cp437Grid, sp_cp437_grid); +wrap_clone!(sp_cp437_grid::Cp437Grid); +wrap_free!(sp_cp437_grid::Cp437Grid); wrap_method!( - sp_cp437 :: Cp437Grid; + sp_cp437::Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments @@ -48,7 +48,7 @@ wrap_method!( ); wrap_method!( - sp_cp437 :: Cp437Grid; + sp_cp437::Cp437Grid; /// Sets the value at the specified position. /// /// # Arguments @@ -65,7 +65,7 @@ wrap_method!( ); wrap_method!( - sp_cp437 :: Cp437Grid; + sp_cp437::Cp437Grid; /// Sets the value of all cells in the grid. /// /// # Arguments @@ -76,19 +76,19 @@ wrap_method!( ); wrap_method!( - sp_cp437 :: Cp437Grid; + sp_cp437::Cp437Grid; /// Gets the width of the grid. ref fn width() -> usize; ); wrap_method!( - sp_cp437 :: Cp437Grid; + sp_cp437::Cp437Grid; /// Gets the height of the grid. ref fn height() -> usize; ); wrap_method!( - sp_cp437 :: Cp437Grid; + sp_cp437::Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. diff --git a/src/macros.rs b/src/macros.rs index c91248e..80256dd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,5 @@ macro_rules! wrap_free { - ($typ:ty, $prefix:ident) => { + ($prefix:ident :: $typ:ty) => { paste::paste! { #[doc = concat!("Deallocates a [", stringify!($typ), "] instance.")] #[no_mangle] @@ -11,7 +11,7 @@ macro_rules! wrap_free { } macro_rules! wrap_clone { - ($typ:ty, $prefix:ident) => { + ($prefix:ident :: $typ:ty) => { paste::paste! { #[doc = concat!("Clones a [", stringify!($typ), "] instance.")] #[no_mangle] diff --git a/src/packet.rs b/src/packet.rs index 658265c..a9b347c 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -91,8 +91,8 @@ pub unsafe extern "C" fn sp_packet_serialize_to( } } -wrap_clone!(Packet, sp_packet); -wrap_free!(Packet, sp_packet); +wrap_clone!(sp_packet::Packet); +wrap_free!(sp_packet::Packet); /// Converts u16 into [CommandCode]. /// diff --git a/src/udp.rs b/src/udp.rs index eef82a8..e18476a 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -144,4 +144,4 @@ pub unsafe extern "C" fn sp_udp_send_header( .is_ok() } -wrap_free!(UdpSocket, sp_udp); +wrap_free!(sp_udp::UdpSocket); From 8296773779db74009770d85abd94aefa94ea8da9 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 17 Jun 2025 22:21:11 +0200 Subject: [PATCH 39/76] fix cp437 prefix, fix example --- example/src/brightness_tester.c | 2 +- include/servicepoint.h | 44 ++++++++++++++++----------------- src/containers/cp437_grid.rs | 12 ++++----- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 51790cf..2257162 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -12,7 +12,7 @@ void enable_all_pixels(void) { } void make_brightness_pattern(BrightnessGrid *grid) { - ByteSlice slice = sp_brightness_grid_unsafe_data_ref(grid); + ByteSlice slice = sp_brightness_grid_data_ref_mut(grid); for (size_t index = 0; index < slice.length; index++) { slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX)); } diff --git a/include/servicepoint.h b/include/servicepoint.h index 6d3b891..6c2da20 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1588,6 +1588,11 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); */ struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); +/** + *Clones a [Cp437Grid] instance. + */ +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); + /** * Calls [`servicepoint::Cp437Grid::data_ref_mut`]. * @@ -1595,7 +1600,7 @@ struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); * * The returned memory is valid for the lifetime of the instance. */ -struct ByteSlice sp_cp437_data_ref_mut(Cp437Grid */*notnull*/ instance); +struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** * Calls [`servicepoint::Cp437Grid::fill`]. @@ -1607,7 +1612,12 @@ struct ByteSlice sp_cp437_data_ref_mut(Cp437Grid */*notnull*/ instance); * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to */ -void sp_cp437_fill(Cp437Grid */*notnull*/ instance, uint8_t value); +void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); + +/** + *Deallocates a [Cp437Grid] instance. + */ +void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** * Calls [`servicepoint::Cp437Grid::get`]. @@ -1622,17 +1632,14 @@ void sp_cp437_fill(Cp437Grid */*notnull*/ instance, uint8_t value); * * - when accessing `x` or `y` out of bounds */ -uint8_t sp_cp437_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); +uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** - *Clones a [Cp437Grid] instance. + * Calls [`servicepoint::Cp437Grid::height`]. + * + * Gets the height of the grid. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); - -/** - *Deallocates a [Cp437Grid] instance. - */ -void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); +size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); /** * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. @@ -1659,13 +1666,6 @@ Cp437Grid *sp_cp437_grid_load(size_t width, */ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); -/** - * Calls [`servicepoint::Cp437Grid::height`]. - * - * Gets the height of the grid. - */ -size_t sp_cp437_height(Cp437Grid */*notnull*/ instance); - /** * Calls [`servicepoint::Cp437Grid::set`]. * @@ -1682,17 +1682,17 @@ size_t sp_cp437_height(Cp437Grid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds */ -void sp_cp437_set(Cp437Grid */*notnull*/ instance, - size_t x, - size_t y, - uint8_t value); +void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, + size_t x, + size_t y, + uint8_t value); /** * Calls [`servicepoint::Cp437Grid::width`]. * * Gets the width of the grid. */ -size_t sp_cp437_width(Cp437Grid */*notnull*/ instance); +size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); /** *Clones a [Packet] instance. diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index ebf652e..5d7b84f 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -34,7 +34,7 @@ wrap_clone!(sp_cp437_grid::Cp437Grid); wrap_free!(sp_cp437_grid::Cp437Grid); wrap_method!( - sp_cp437::Cp437Grid; + sp_cp437_grid::Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments @@ -48,7 +48,7 @@ wrap_method!( ); wrap_method!( - sp_cp437::Cp437Grid; + sp_cp437_grid::Cp437Grid; /// Sets the value at the specified position. /// /// # Arguments @@ -65,7 +65,7 @@ wrap_method!( ); wrap_method!( - sp_cp437::Cp437Grid; + sp_cp437_grid::Cp437Grid; /// Sets the value of all cells in the grid. /// /// # Arguments @@ -76,19 +76,19 @@ wrap_method!( ); wrap_method!( - sp_cp437::Cp437Grid; + sp_cp437_grid::Cp437Grid; /// Gets the width of the grid. ref fn width() -> usize; ); wrap_method!( - sp_cp437::Cp437Grid; + sp_cp437_grid::Cp437Grid; /// Gets the height of the grid. ref fn height() -> usize; ); wrap_method!( - sp_cp437::Cp437Grid; + sp_cp437_grid::Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. From 75e2df41fec1c064b53e6daa730e445266d7b488 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Tue, 17 Jun 2025 23:56:11 +0200 Subject: [PATCH 40/76] export value fields via macro --- include/servicepoint.h | 64 +++++++++++------- src/commands/bitmap_command.rs | 37 +++++------ src/commands/bitvec_command.rs | 75 +++++---------------- src/commands/global_brightness_command.rs | 26 +++----- src/macros.rs | 79 +++++++++++++++++++++-- src/packet.rs | 12 +++- 6 files changed, 165 insertions(+), 128 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 6c2da20..02d0688 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1166,9 +1166,9 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ command); /** - * Reads the compression kind of the [BitmapCommand]. + *Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. */ -CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ command); +CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** * Reads the origin field of the [BitmapCommand]. @@ -1196,10 +1196,10 @@ void sp_cmd_bitmap_set(struct BitmapCommand */*notnull*/ command, struct Bitmap */*notnull*/ bitmap); /** - * Overwrites the compression kind of the [BitmapCommand]. + *Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. */ -void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ command, - CompressionCode compression); +void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, + CompressionCode value); /** * Overwrites the origin field of the [BitmapCommand]. @@ -1231,19 +1231,19 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); BitVec *sp_cmd_bitvec_get(struct BitVecCommand */*notnull*/ command); /** - * Reads the compression kind of the [BitVecCommand]. + *Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. */ -CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ command); +CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** - * Reads the offset field of the [BitVecCommand]. + *Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. */ -Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ command); +Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** - * Returns the [BinaryOperation] of the command. + *Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. */ -BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ command); +BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); /** * Set pixel data starting at the pixel offset on screen. @@ -1271,22 +1271,22 @@ void sp_cmd_bitvec_set(struct BitVecCommand */*notnull*/ command, BitVec */*notnull*/ bitvec); /** - * Overwrites the compression kind of the [BitVecCommand]. + *Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. */ -void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ command, - CompressionCode compression); +void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, + CompressionCode value); /** - * Overwrites the offset field of the [BitVecCommand]. + *Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. */ -void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ command, - Offset offset); +void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, + Offset value); /** - * Overwrites the [BinaryOperation] of the command. + *Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. */ -void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ command, - BinaryOperation operation); +void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, + BinaryOperation value); /** * Tries to turn a [BitVecCommand] into a [Packet]. @@ -1305,7 +1305,10 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struc */ void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance); -Brightness sp_cmd_brightness_global_get(struct GlobalBrightnessCommand */*notnull*/ command); +/** + *Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + */ +Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); @@ -1317,10 +1320,10 @@ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBri struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** - * Moves the provided bitmap to be contained in the command. + *Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. */ -void sp_cmd_brightness_global_set(struct GlobalBrightnessCommand */*notnull*/ command, - Brightness brightness); +void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); /** *Clones a [BrightnessGridCommand] instance. @@ -1712,12 +1715,17 @@ void sp_packet_free(struct Packet */*notnull*/ instance); struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); +/** + *Gets the value of field `header` of the [`servicepoint::Packet`]. + */ +struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); + /** * Returns a pointer to the header field of the provided packet. * * The returned header can be changed and will be valid for the lifetime of the packet. */ -struct Header */*notnull*/ sp_packet_get_header(struct Packet */*notnull*/ packet); +struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ packet); /** * Returns a pointer to the current payload of the provided packet. @@ -1738,6 +1746,12 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); void sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); +/** + *Sets the value of field `header` of the [`servicepoint::Packet`]. + */ +void sp_packet_set_header(struct Packet */*notnull*/ instance, + struct Header value); + /** * Sets the payload of the provided packet to the provided data. * diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 24400c7..3bfcf20 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; @@ -48,6 +48,22 @@ pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet( wrap_clone!(sp_cmd_bitmap::BitmapCommand); wrap_free!(sp_cmd_bitmap::BitmapCommand); +wrap_fields!( + sp_cmd_bitmap::BitmapCommand; + //prop bitmap: NonNull { + // get() { + // return NonNull::from(bitmap); + // }; + // set(value) { + // return unsafe { heap_remove(value) }; + // }; + //}; + prop compression: CompressionCode { + get(); + set(value); + }; +); + /// Returns a pointer to the provided `BitmapCommand`. /// /// # Safety @@ -97,22 +113,3 @@ pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( command.as_mut().origin = Origin::new(origin_x, origin_y); } } - -/// Overwrites the compression kind of the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_set_compression( - mut command: NonNull, - compression: CompressionCode, -) { - unsafe { - command.as_mut().compression = compression; - } -} - -/// Reads the compression kind of the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get_compression( - command: NonNull, -) -> CompressionCode { - unsafe { command.as_ref().compression } -} diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index aeff1cd..7ccbf31 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -48,6 +48,22 @@ pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet( wrap_clone!(sp_cmd_bitvec::BitVecCommand); wrap_free!(sp_cmd_bitvec::BitVecCommand); +wrap_fields!( + sp_cmd_bitvec::BitVecCommand; + prop offset: Offset { + get(); + set(value); + }; + prop operation: BinaryOperation { + get(); + set(value); + }; + prop compression: CompressionCode { + get(); + set(value); + }; +); + /// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. #[no_mangle] pub unsafe extern "C" fn sp_cmd_bitvec_get( @@ -66,60 +82,3 @@ pub unsafe extern "C" fn sp_cmd_bitvec_set( command.as_mut().bitvec = heap_remove(bitvec); } } - -/// Reads the offset field of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_offset( - command: NonNull, -) -> Offset { - unsafe { command.as_ref().offset } -} - -/// Overwrites the offset field of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_offset( - mut command: NonNull, - offset: Offset, -) { - unsafe { - command.as_mut().offset = offset; - } -} - -/// Returns the [BinaryOperation] of the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_operation( - command: NonNull, -) -> BinaryOperation { - unsafe { command.as_ref().operation } -} - -/// Overwrites the [BinaryOperation] of the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_operation( - mut command: NonNull, - operation: BinaryOperation, -) { - unsafe { - command.as_mut().operation = operation; - } -} - -/// Overwrites the compression kind of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set_compression( - mut command: NonNull, - compression: CompressionCode, -) { - unsafe { - command.as_mut().compression = compression; - } -} - -/// Reads the compression kind of the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get_compression( - command: NonNull, -) -> CompressionCode { - unsafe { command.as_ref().compression } -} diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 20a350a..e695ef0 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_remove}, }; use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; @@ -25,20 +25,10 @@ pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand); wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand); -/// Moves the provided bitmap to be contained in the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_set( - mut command: NonNull, - brightness: Brightness, -) { - unsafe { - command.as_mut().brightness = brightness; - } -} - -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_get( - mut command: NonNull, -) -> Brightness { - unsafe { command.as_mut().brightness } -} +wrap_fields!( + sp_cmd_brightness_global::GlobalBrightnessCommand; + prop brightness: Brightness { + get(); + set(value); + }; +); diff --git a/src/macros.rs b/src/macros.rs index 80256dd..df21e23 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -54,7 +54,7 @@ macro_rules! wrap_method { instance: NonNull<$object_type>, $($param_name: $param_type),* ) $(-> $return_type)? { - let instance = unsafe {$crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance)}; + let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; $(let $param_let_name = $param_let_expr;)* #[allow( unused_variables, @@ -62,16 +62,85 @@ macro_rules! wrap_method { let result = instance.$function($($param_name),*); $( $( - let $it = result; - let result = $return_expr; + let $it = result; + let result = $return_expr; )? - return result; + return result; )? } } }; } +macro_rules! wrap_fields { + ( + $prefix:ident :: $object_type:ty; + $( + prop $prop_name:ident : $prop_type:ty { + $( + get() $({ + $(#[$get_meta:meta])* + $(return $get_expr:expr;)? + })?; + )? + $( + set($value:ident) + $({ + $(#[$set_meta:meta])* + $(return $set_expr:expr;)? + })?; + )? + }; + )+ + ) => { + paste::paste! { + $( + $( + #[doc = concat!("Gets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] + $($( + #[doc = ""] + #[$get_meta] + )*)? + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ get _ $prop_name>]( + instance: NonNull<$object_type> + ) -> $prop_type { + let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) }; + let $prop_name = instance.$prop_name; + $($( + let $prop_name = $get_expr; + )?)? + return $prop_name; + } + )? + + $( + #[doc = concat!("Sets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] + $($( + #[doc = ""] + #[$set_meta] + )*)? + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ set _ $prop_name>]( + instance: NonNull<$object_type>, + value: $prop_type, + ) { + let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; + $($( + let $value = value; + let value = $set_expr; + )?)? + instance.$prop_name = value; + } + )? + )+ + } + }; +} + pub(crate) use { - nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_free, wrap_method, + nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free, + wrap_method, }; diff --git a/src/packet.rs b/src/packet.rs index a9b347c..af49fc8 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; @@ -36,7 +36,7 @@ pub unsafe extern "C" fn sp_packet_from_parts( /// /// The returned header can be changed and will be valid for the lifetime of the packet. #[no_mangle] -pub unsafe extern "C" fn sp_packet_get_header( +pub unsafe extern "C" fn sp_packet_get_header_mut( packet: NonNull, ) -> NonNull
{ NonNull::from(unsafe { &mut (*packet.as_ptr()).header }) @@ -94,6 +94,14 @@ pub unsafe extern "C" fn sp_packet_serialize_to( wrap_clone!(sp_packet::Packet); wrap_free!(sp_packet::Packet); +wrap_fields!( + sp_packet::Packet; + prop header: Header { + get(); + set(value); + }; +); + /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed. From 85d4ed5a3bd7ef3aac2b390970d15853a5c536c1 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 00:11:18 +0200 Subject: [PATCH 41/76] fix flake, mark which packages work --- example/src/header_logger.c | 4 ++-- example/src/helpers.h | 3 +-- example/src/random_stuff.c | 2 +- nix-build-all.sh | 27 +++++++++++++++------------ packages.nix | 19 +++++++------------ 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 8fe1455..9cb3cd1 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -129,7 +129,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_cmd_brightness_global_get(command.data.global_brightness); + Brightness brightness = sp_cmd_brightness_global_get_brightness(command.data.global_brightness); printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); break; } @@ -191,7 +191,7 @@ int main(int argc, char **argv) { continue; } - struct Header *header = sp_packet_get_header(packet); + struct Header *header = sp_packet_get_header_mut(packet); ByteSlice payload = sp_packet_get_payload(packet); printf("Received packet: cc=%d, a=%d, b=%d, c=%d, d=%d, payload=%p (len %zu)\n", diff --git a/example/src/helpers.h b/example/src/helpers.h index 904e86b..bc6c119 100644 --- a/example/src/helpers.h +++ b/example/src/helpers.h @@ -23,8 +23,6 @@ void sock_init() { /// TODO: all of this for sleeping n ms? There should be a better way! int msleep(long msec) { - int res; - if (msec < 0) { errno = EINVAL; return -1; @@ -35,6 +33,7 @@ int msleep(long msec) { .tv_nsec = (msec % 1000) * 1000000, }; + int res; do { res = nanosleep(&ts, &ts); } while (res && errno == EINTR); diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 2d818e3..b55e42c 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -15,7 +15,7 @@ int main(void) { if (packet == NULL) return 1; - Header *header = sp_packet_get_header(packet); + Header *header = sp_packet_get_header_mut(packet); printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d); sp_udp_send_packet(sock, packet); diff --git a/nix-build-all.sh b/nix-build-all.sh index 8adce87..2d247e7 100755 --- a/nix-build-all.sh +++ b/nix-build-all.sh @@ -3,24 +3,27 @@ set -e set -x -BUILD="nix build -L" +BUILD="nom build -L" $BUILD .#servicepoint-binding-c -o result $BUILD .#servicepoint-binding-c-stable-release -o result-stable-release $BUILD .#servicepoint-binding-c-stable-size -o result-stable-size $BUILD .#servicepoint-binding-c-nightly-release -o result-nightly-release -$BUILD .#servicepoint-binding-c-nightly-size -o result-nightly-size -$BUILD .#servicepoint-binding-c-musl-stable-release -o result-musl-release -$BUILD .#servicepoint-binding-c-musl-stable-size -o result-musl-size - -# do not work yet: -# $BUILD .#servicepoint-binding-c-musl-nightly-release -# $BUILD .#servicepoint-binding-c-musl-nightly-size $BUILD .#all-examples -o result-examples $BUILD .#all-examples-size -o result-examples-size -$BUILD .#all-examples-nightly-size -o result-nightly-size -$BUILD .#all-examples-musl -o result-examples-musl -$BUILD .#all-examples-musl-static -o result-examples-musl-static -$BUILD .#all-examples-musl-static-size -o result-examples-musl-static-size + +# works, but needs to bootstrap a C and Rust compiler +# $BUILD .#servicepoint-binding-c-musl-stable-release -o result-musl-release +# $BUILD .#servicepoint-binding-c-musl-stable-size -o result-musl-size +# $BUILD .#all-examples-musl-static -o result-examples-musl-static +# $BUILD .#all-examples-musl-static-size -o result-examples-musl-static-size +# $BUILD .#all-examples-musl -o result-examples-musl + +# do not work yet: +# $BUILD .#servicepoint-binding-c-nightly-size -o result-nightly-size +# $BUILD .#servicepoint-binding-c-musl-nightly-release +# $BUILD .#servicepoint-binding-c-musl-nightly-size +# $BUILD .#all-examples-nightly-size -o result-examples-nightly-size + diff --git a/packages.nix b/packages.nix index 8096f54..acae2a5 100644 --- a/packages.nix +++ b/packages.nix @@ -14,10 +14,12 @@ let buildType ? "release", buildNoDefaultFeatures ? false, cargoBuildFlags ? [ ], - nativeBuildInputs ? [] + nativeBuildInputs ? [], + stdlib ? false, + }: rustPlatform.buildRustPackage (finalAttrs: { - inherit version buildType cargoBuildFlags; + inherit version buildType cargoBuildFlags stdlib; pname = "servicepoint-binding-c"; src = lib.filter { @@ -26,7 +28,7 @@ let ./Cargo.lock ./Cargo.toml ./src - ./build.rs + ./include ./LICENSE ./cbindgen.toml ]; @@ -41,16 +43,8 @@ let }; nativeBuildInputs = [ pkgs.pkg-config ] ++ nativeBuildInputs; buildInputs = [ pkgs.xz ]; - - preBuild = '' - mkdir -p include - export SERVICEPOINT_HEADER_OUT=$(realpath "include") - - echo "Rust version: $(rustc --version)" - echo "preBuild hook: set SERVICEPOINT_HEADER_OUT to $SERVICEPOINT_HEADER_OUT" - ''; postInstall = '' - cp -r include $out + cp -r $src/include $out mkdir -p $out/lib/pkgconfig sed "s:\$out:$out:g" ${./servicepoint.pc.in} | sed "s:\$version:$version:g" > $out/lib/pkgconfig/servicepoint.pc @@ -149,6 +143,7 @@ let "-Zbuild-std=core,std,alloc,proc_macro,panic_abort" "-Zbuild-std-features=panic_immediate_abort" ]; + stdlib = true; # TODO: remove hard-coded target nativeBuildInputs = [fenix.targets."x86_64-unknown-linux-gnu".latest.rust-std]; # TODO: those override the nix flags From 9ddab5e19a87fea40f40b8a3f1ae2144238d789c Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 00:14:18 +0200 Subject: [PATCH 42/76] update ci --- .github/workflows/rust.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d0f1da3..75139b5 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -30,11 +30,6 @@ jobs: - name: build run: cargo build - - name: generate bindings - run: ./generate-binding.sh - - name: check that generated files did not change - run: output=$(git status --porcelain) && [ -z "$output" ] - - name: build example -- glibc release run: cd example && make -r clean-all && make -r LIBC=gnu LINK=dynamic PROFILE=release - name: build example -- glibc debug @@ -52,6 +47,14 @@ jobs: - name: install rust targets run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update + - name: install csbindgen + run: rustup run nightly cargo install cbindgen@0.29.0 + + - name: generate bindings + run: ./generate-binding.sh + - name: check that generated files did not change + run: output=$(git status --porcelain) && [ -z "$output" ] + - name: build example -- glibc size_optimized run: cd example && make clean-all -r && make -r LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 From 7d52ccf638a08abd43a9f94a98c2477ea977adfe Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 17:25:11 +0200 Subject: [PATCH 43/76] fix Makefile --- example/Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/example/Makefile b/example/Makefile index 5659216..0a56e1a 100644 --- a/example/Makefile +++ b/example/Makefile @@ -47,6 +47,8 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ --target=$(RUST_TARGET) \ --target-dir=cargo +STATIC_LINK_LIBS += -lservicepoint_binding_c +_servicepoint_cflags := -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR) CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections size_optimized_CFLAGS += -Oz \ @@ -69,7 +71,7 @@ debug_CFLAGS += -Og static_CFLAGS += -static $(STATIC_LINK_LIBS) dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic -_servicepoint_cflags := $(pkg-config --libs servicepoint --cflags) +_servicepoint_cflags := $(shell pkg-config --libs servicepoint --cflags || echo -I $(REPO_ROOT)/include -L $(CARGO_OBJDIR)) CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_servicepoint_cflags) ifeq ($(LTO), 1) CFLAGS += -flto @@ -103,9 +105,9 @@ _unstripped_bins := $(addsuffix _unstripped, $(_bins)) .PHONY: all build-rust -all: $(_bins) +all: build-rust $(_bins) -$(_unstripped_bins): %_unstripped: src/%.c src/helpers.h build-rust +$(_unstripped_bins): %_unstripped : src/%.c src/helpers.h build-rust $(CC) $< $(CFLAGS) -o $@ $(_bins): %: %_unstripped From 35e9c36ccdd0fc9038ee475adda296990c646dfd Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 17:39:30 +0200 Subject: [PATCH 44/76] multiple methods in one macro invocation --- src/containers/bitmap.rs | 30 ++++++++---------------------- src/containers/bitvec.rs | 30 ++++++++---------------------- src/containers/brightness_grid.rs | 30 ++++++++---------------------- src/containers/char_grid.rs | 27 ++++++++------------------- src/containers/cp437_grid.rs | 21 +++------------------ src/macros.rs | 21 +++++++++++++-------- 6 files changed, 48 insertions(+), 111 deletions(-) diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 9b96d10..81af42d 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_method}, + macros::{wrap_clone, wrap_free, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -83,8 +83,9 @@ pub unsafe extern "C" fn sp_bitmap_from_bitvec( wrap_clone!(sp_bitmap::Bitmap); wrap_free!(sp_bitmap::Bitmap); -wrap_method!( +wrap_methods!( sp_bitmap::Bitmap; + /// Gets the current value at the specified position. /// /// # Arguments @@ -95,10 +96,7 @@ wrap_method!( /// /// - when accessing `x` or `y` out of bounds ref fn get(x: usize, y: usize) -> bool; -); - -wrap_method!( - sp_bitmap::Bitmap; + /// Sets the value of the specified position. /// /// # Arguments @@ -110,32 +108,20 @@ wrap_method!( /// /// - when accessing `x` or `y` out of bounds mut fn set(x: usize, y: usize, value: bool); -); - -wrap_method!( - sp_bitmap::Bitmap; + /// Sets the state of all pixels in the [Bitmap]. /// /// # Arguments /// /// - `value`: the value to set all pixels to mut fn fill(value: bool); -); - -wrap_method!( - sp_bitmap::Bitmap; + /// Gets the width in pixels. ref fn width() -> usize; -); - -wrap_method!( - sp_bitmap::Bitmap; + /// Gets the height in pixels. ref fn height() -> usize; -); - -wrap_method!( - sp_bitmap::Bitmap; + /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index aad2a88..bceae0b 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_method}, + macros::{wrap_clone, wrap_free, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -38,8 +38,9 @@ pub unsafe extern "C" fn sp_bitvec_load( wrap_clone!(sp_bitvec::DisplayBitVec); wrap_free!(sp_bitvec::DisplayBitVec); -wrap_method!( +wrap_methods!( sp_bitvec::DisplayBitVec; + /// Gets the value of a bit. /// /// # Arguments @@ -54,10 +55,7 @@ wrap_method!( /// - when accessing `index` out of bounds ref fn get(index: usize) -> bool; |result| result.map(|x| *x).unwrap_or(false); -); - -wrap_method!( - sp_bitvec::DisplayBitVec; + /// Sets the value of a bit. /// /// # Arguments @@ -69,32 +67,20 @@ wrap_method!( /// /// - when accessing `index` out of bounds mut fn set(index: usize, value: bool); -); - -wrap_method!( - sp_bitvec::DisplayBitVec; + /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to mut fn fill(value: bool); -); - -wrap_method!( - sp_bitvec::DisplayBitVec; + /// Gets the length in bits. ref fn len() -> usize; -); - -wrap_method!( - sp_bitvec::DisplayBitVec; + /// Returns true if length is 0. ref fn is_empty() -> bool; -); - -wrap_method!( - sp_bitvec::DisplayBitVec; + /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 54491c2..4e15599 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_method}, + macros::{wrap_clone, wrap_free, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -55,8 +55,9 @@ pub unsafe extern "C" fn sp_brightness_grid_load( wrap_clone!(sp_brightness_grid::BrightnessGrid); wrap_free!(sp_brightness_grid::BrightnessGrid); -wrap_method!( +wrap_methods!( sp_brightness_grid::BrightnessGrid; + /// Gets the current value at the specified position. /// /// # Arguments @@ -68,10 +69,7 @@ wrap_method!( /// # Panics /// - When accessing `x` or `y` out of bounds. ref fn get(x: usize, y: usize) -> Brightness; -); - -wrap_method!( - sp_brightness_grid::BrightnessGrid; + /// Sets the value of the specified position. /// /// # Arguments @@ -85,32 +83,20 @@ wrap_method!( /// /// - When accessing `x` or `y` out of bounds. mut fn set(x: usize, y: usize, value: Brightness); -); - -wrap_method!( - sp_brightness_grid::BrightnessGrid; + /// Sets the value of all cells. /// /// # Arguments /// /// - `value`: the value to set all cells to mut fn fill(value: Brightness); -); - -wrap_method!( - sp_brightness_grid::BrightnessGrid; + /// Gets the width of the grid. ref fn width() -> usize; -); - -wrap_method!( - sp_brightness_grid::BrightnessGrid; + /// Gets the height of the grid. ref fn height() -> usize; -); - -wrap_method!( - sp_brightness_grid::BrightnessGrid; + /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 5db7897..dddae0b 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_method}, + macros::{wrap_clone, wrap_free, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; @@ -42,8 +42,9 @@ pub unsafe extern "C" fn sp_char_grid_load( wrap_clone!(sp_char_grid::CharGrid); wrap_free!(sp_char_grid::CharGrid); -wrap_method!( +wrap_methods!( sp_char_grid::CharGrid; + /// Returns the current value at the specified position. /// /// # Arguments @@ -54,11 +55,8 @@ wrap_method!( /// /// - when accessing `x` or `y` out of bounds ref fn get(x: usize, y: usize) -> u32; - | char | char as u32 -); - -wrap_method!( - sp_char_grid::CharGrid; + | char | char as u32; + /// Sets the value of the specified position in the grid. /// /// # Arguments @@ -74,10 +72,7 @@ wrap_method!( /// - when providing values that cannot be converted to Rust's `char`. mut fn set(x: usize, y: usize, value: u32); let value = char::from_u32(value).unwrap(); -); - -wrap_method!( - sp_char_grid::CharGrid; + /// Sets the value of all cells in the grid. /// /// # Arguments @@ -86,16 +81,10 @@ wrap_method!( /// - when providing values that cannot be converted to Rust's `char`. mut fn fill(value: u32); let value = char::from_u32(value).unwrap(); -); - -wrap_method!( - sp_char_grid::CharGrid; + /// Gets the width of the grid. ref fn width() -> usize; -); - -wrap_method!( - sp_char_grid::CharGrid; + /// Gets the height of the grid. ref fn height() -> usize; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 5d7b84f..166083f 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_method}, + macros::{wrap_clone, wrap_free, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -33,7 +33,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load( wrap_clone!(sp_cp437_grid::Cp437Grid); wrap_free!(sp_cp437_grid::Cp437Grid); -wrap_method!( +wrap_methods!( sp_cp437_grid::Cp437Grid; /// Gets the current value at the specified position. /// @@ -45,10 +45,7 @@ wrap_method!( /// /// - when accessing `x` or `y` out of bounds ref fn get(x: usize, y: usize) -> u8; -); -wrap_method!( - sp_cp437_grid::Cp437Grid; /// Sets the value at the specified position. /// /// # Arguments @@ -62,10 +59,7 @@ wrap_method!( /// /// - when accessing `x` or `y` out of bounds mut fn set(x: usize, y: usize, value: u8); -); -wrap_method!( - sp_cp437_grid::Cp437Grid; /// Sets the value of all cells in the grid. /// /// # Arguments @@ -73,22 +67,13 @@ wrap_method!( /// - `cp437_grid`: instance to write to /// - `value`: the value to set all cells to mut fn fill(value: u8); -); -wrap_method!( - sp_cp437_grid::Cp437Grid; /// Gets the width of the grid. ref fn width() -> usize; -); -wrap_method!( - sp_cp437_grid::Cp437Grid; /// Gets the height of the grid. ref fn height() -> usize; -); - -wrap_method!( - sp_cp437_grid::Cp437Grid; + /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. diff --git a/src/macros.rs b/src/macros.rs index df21e23..fc6e524 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -35,16 +35,18 @@ macro_rules! nonnull_as_mut { } // meta required on purpose, because otherwise the added documentation would suppress warnings -macro_rules! wrap_method { +macro_rules! wrap_methods { ( $prefix:ident :: $object_type:ty; - $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) - $(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)? - $(; let $param_let_name:ident = $param_let_expr:expr)* - $(;)? + $( + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) + $(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)?; + $($(let $param_let_name:ident = $param_let_expr:expr);+;)? + )* ) => { paste::paste! { + $( #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), "::", stringify!($function), "`].")] #[doc = ""] @@ -55,7 +57,9 @@ macro_rules! wrap_method { $($param_name: $param_type),* ) $(-> $return_type)? { let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; - $(let $param_let_name = $param_let_expr;)* + $( + $(let $param_let_name = $param_let_expr;)* + )? #[allow( unused_variables, reason = "This variable may not be used depending on macro variables" )] @@ -68,6 +72,7 @@ macro_rules! wrap_method { return result; )? } + )* } }; } @@ -142,5 +147,5 @@ macro_rules! wrap_fields { pub(crate) use { nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free, - wrap_method, + wrap_methods, }; From bf4e35151497bb8550f15877c48c3c12136b7221 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 17:51:32 +0200 Subject: [PATCH 45/76] tweak dsl --- src/containers/bitmap.rs | 18 ++++++++++-------- src/containers/bitvec.rs | 23 +++++++++++++---------- src/containers/brightness_grid.rs | 23 ++++++++++++----------- src/containers/char_grid.rs | 25 ++++++++++++++----------- src/containers/cp437_grid.rs | 7 ++++--- src/macros.rs | 14 +++++++++----- 6 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 81af42d..ae0d2ae 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -85,7 +85,7 @@ wrap_free!(sp_bitmap::Bitmap); wrap_methods!( sp_bitmap::Bitmap; - + /// Gets the current value at the specified position. /// /// # Arguments @@ -96,7 +96,7 @@ wrap_methods!( /// /// - when accessing `x` or `y` out of bounds ref fn get(x: usize, y: usize) -> bool; - + /// Sets the value of the specified position. /// /// # Arguments @@ -108,25 +108,27 @@ wrap_methods!( /// /// - when accessing `x` or `y` out of bounds mut fn set(x: usize, y: usize, value: bool); - + /// Sets the state of all pixels in the [Bitmap]. /// /// # Arguments /// /// - `value`: the value to set all pixels to mut fn fill(value: bool); - + /// Gets the width in pixels. ref fn width() -> usize; - + /// Gets the height in pixels. ref fn height() -> usize; - + /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - mut fn data_ref_mut() -> ByteSlice; - |slice| unsafe { ByteSlice::from_slice(slice) }; + mut fn data_ref_mut() -> ByteSlice { + + return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + }; ); /// Consumes the Bitmap and returns the contained BitVec. diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index bceae0b..76d716a 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -40,7 +40,7 @@ wrap_free!(sp_bitvec::DisplayBitVec); wrap_methods!( sp_bitvec::DisplayBitVec; - + /// Gets the value of a bit. /// /// # Arguments @@ -53,9 +53,11 @@ wrap_methods!( /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(index: usize) -> bool; - |result| result.map(|x| *x).unwrap_or(false); - + ref fn get(index: usize) -> bool { + return(result) { result.map(|x| *x).unwrap_or(false) }; + }; + + /// Sets the value of a bit. /// /// # Arguments @@ -67,25 +69,26 @@ wrap_methods!( /// /// - when accessing `index` out of bounds mut fn set(index: usize, value: bool); - + /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to mut fn fill(value: bool); - + /// Gets the length in bits. ref fn len() -> usize; - + /// Returns true if length is 0. ref fn is_empty() -> bool; - + /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - mut fn as_raw_mut_slice() -> ByteSlice; - |slice| unsafe { ByteSlice::from_slice(slice) }; + mut fn as_raw_mut_slice() -> ByteSlice { + return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + }; ); /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 4e15599..a3590b8 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -57,7 +57,7 @@ wrap_free!(sp_brightness_grid::BrightnessGrid); wrap_methods!( sp_brightness_grid::BrightnessGrid; - + /// Gets the current value at the specified position. /// /// # Arguments @@ -69,7 +69,7 @@ wrap_methods!( /// # Panics /// - When accessing `x` or `y` out of bounds. ref fn get(x: usize, y: usize) -> Brightness; - + /// Sets the value of the specified position. /// /// # Arguments @@ -83,29 +83,30 @@ wrap_methods!( /// /// - When accessing `x` or `y` out of bounds. mut fn set(x: usize, y: usize, value: Brightness); - + /// Sets the value of all cells. /// /// # Arguments /// /// - `value`: the value to set all cells to mut fn fill(value: Brightness); - + /// Gets the width of the grid. ref fn width() -> usize; - + /// Gets the height of the grid. ref fn height() -> usize; - + /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - mut fn data_ref_mut() -> ByteSlice; - |br_slice| unsafe { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); + mut fn data_ref_mut() -> ByteSlice { + return(br_slice) { unsafe { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + }}; }; ); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index dddae0b..1b70202 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -44,7 +44,7 @@ wrap_free!(sp_char_grid::CharGrid); wrap_methods!( sp_char_grid::CharGrid; - + /// Returns the current value at the specified position. /// /// # Arguments @@ -54,9 +54,10 @@ wrap_methods!( /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> u32; - | char | char as u32; - + ref fn get(x: usize, y: usize) -> u32 { + return(char) { char as u32 }; + }; + /// Sets the value of the specified position in the grid. /// /// # Arguments @@ -70,21 +71,23 @@ wrap_methods!( /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - mut fn set(x: usize, y: usize, value: u32); - let value = char::from_u32(value).unwrap(); - + mut fn set(x: usize, y: usize, value: u32) { + prepare(value) { char::from_u32(value).unwrap() }; + }; + /// Sets the value of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(value: u32); - let value = char::from_u32(value).unwrap(); - + mut fn fill(value: u32) { + prepare(value) { char::from_u32(value).unwrap() }; + }; + /// Gets the width of the grid. ref fn width() -> usize; - + /// Gets the height of the grid. ref fn height() -> usize; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 166083f..d6ae17f 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -73,12 +73,13 @@ wrap_methods!( /// Gets the height of the grid. ref fn height() -> usize; - + /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - mut fn data_ref_mut() -> ByteSlice; - | slice | unsafe { ByteSlice::from_slice(slice) }; + mut fn data_ref_mut() -> ByteSlice { + return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + }; ); /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. diff --git a/src/macros.rs b/src/macros.rs index fc6e524..cbadc14 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -41,8 +41,12 @@ macro_rules! wrap_methods { $( $(#[$meta:meta])+ $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) - $(-> $return_type:ty$(; |$it:ident | $return_expr:expr)?)?; - $($(let $param_let_name:ident = $param_let_expr:expr);+;)? + $(-> $return_type:ty)? + $({ + $($(prepare($param_let_name:ident) $param_let_expr:block);+;)? + $(return($it:ident) $return_expr:block;)? + })? + ; )* ) => { paste::paste! { @@ -57,9 +61,9 @@ macro_rules! wrap_methods { $($param_name: $param_type),* ) $(-> $return_type)? { let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; - $( + $($( $(let $param_let_name = $param_let_expr;)* - )? + )?)? #[allow( unused_variables, reason = "This variable may not be used depending on macro variables" )] @@ -69,8 +73,8 @@ macro_rules! wrap_methods { let $it = result; let result = $return_expr; )? - return result; )? + return result; } )* } From 27f231eba02045fd85cb458a548a08c8dad1a817 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 20:30:55 +0200 Subject: [PATCH 46/76] wrap_function without defined signature --- include/servicepoint.h | 79 ++++++++- src/commands/bitmap_command.rs | 77 ++++----- src/commands/bitvec_command.rs | 74 ++++---- src/commands/brightness_grid_command.rs | 69 ++++---- src/commands/cc_only_commands.rs | 58 ++++--- src/commands/char_grid_command.rs | 73 ++++---- src/commands/cp437_grid_command.rs | 69 ++++---- src/commands/global_brightness_command.rs | 31 ++-- src/containers/bitmap.rs | 195 +++++++++++----------- src/containers/bitvec.rs | 93 +++++------ src/containers/brightness_grid.rs | 120 ++++++------- src/containers/char_grid.rs | 101 +++++------ src/containers/cp437_grid.rs | 78 ++++----- src/macros.rs | 32 +++- 14 files changed, 630 insertions(+), 519 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 02d0688..5960987 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -675,6 +675,8 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * The provided BitVec gets consumed. * * Returns NULL in case of error. + * + * This function is part of the sp_bitmap module. */ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); @@ -702,6 +704,8 @@ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** * Consumes the Bitmap and returns the contained BitVec. + * + * This function is part of the sp_bitmap module. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -711,6 +715,8 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); * The provided [Bitmap] gets consumed. * * Returns NULL in case of an error. + * + * This function is part of the sp_bitmap module. */ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, size_t x, @@ -726,6 +732,8 @@ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, * - `height`: size in pixels in y-direction * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. + * + * This function is part of the sp_bitmap module. */ struct Bitmap *sp_bitmap_load(size_t width, size_t height, @@ -755,6 +763,8 @@ struct Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_set(grid, 0, 0, false); * sp_bitmap_free(grid); * ``` + * + * This function is part of the sp_bitmap module. */ struct Bitmap *sp_bitmap_new(size_t width, size_t height); @@ -762,6 +772,8 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); * Creates a new [Bitmap] with a size matching the screen. * * returns: [Bitmap] initialized to all pixels off. + * + * This function is part of the sp_bitmap module. */ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); @@ -845,6 +857,8 @@ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); * The provided [DisplayBitVec] gets consumed. * * Returns NULL in case of an error. + * + * This function is part of the sp_bitvec module. */ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, size_t offset, @@ -869,6 +883,8 @@ size_t sp_bitvec_len(BitVec */*notnull*/ instance); * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. * * returns: [DisplayBitVec] instance containing data. + * + * This function is part of the sp_bitvec module. */ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); @@ -884,6 +900,8 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); * # Panics * * - when `size` is not divisible by 8. + * + * This function is part of the sp_bitvec module. */ BitVec */*notnull*/ sp_bitvec_new(size_t size); @@ -965,6 +983,8 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); * The provided [BrightnessGrid] gets consumed. * * Returns NULL in case of an error. + * + * This function is part of the sp_brightness_grid module. */ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, size_t x, @@ -976,6 +996,8 @@ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, * Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN]. * * returns: new [BrightnessGrid] instance, or NULL in case of an error. + * + * This function is part of the sp_brightness_grid module. */ BrightnessGrid *sp_brightness_grid_load(size_t width, size_t height, @@ -999,6 +1021,8 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * TypedCommand *command = sp_command_char_brightness(grid); * sp_udp_free(connection); * ``` + * + * This function is part of the sp_brightness_grid module. */ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); @@ -1080,6 +1104,8 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); * The provided [CharGrid] gets consumed. * * Returns NULL in case of an error. + * + * This function is part of the sp_char_grid module. */ struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, size_t x, @@ -1089,6 +1115,8 @@ struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, * Loads a [CharGrid] with the specified dimensions from the provided data. * * returns: new CharGrid or NULL in case of an error + * + * This function is part of the sp_char_grid module. */ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); @@ -1105,6 +1133,8 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); * sp_char_grid_set(grid, 0, 0, '!'); * sp_char_grid_free(grid); * ``` + * + * This function is part of the sp_char_grid module. */ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); @@ -1152,6 +1182,8 @@ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); * leaving other fields as their default values. * * Rust equivalent: `BitmapCommand::from(bitmap)` + * + * This function is part of the sp_cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); @@ -1183,6 +1215,8 @@ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, * The passed [Bitmap] gets consumed. * * Returns: a new [BitmapCommand] instance. + * + * This function is part of the sp_cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, size_t origin_x, @@ -1212,6 +1246,8 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, * Tries to turn a [BitmapCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the sp_cmd_bitmap module. */ struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); @@ -1258,6 +1294,8 @@ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ in * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. * * The contained [`DisplayBitVec`] is always uncompressed. + * + * This function is part of the sp_cmd_bitvec module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, size_t offset, @@ -1292,6 +1330,8 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, * Tries to turn a [BitVecCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the sp_cmd_bitvec module. */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); @@ -1310,12 +1350,19 @@ void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ i */ Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); +/** + * Turns the command into a packet + * + * This function is part of the sp_cmd_brightness_global module. + */ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); /** * Set the brightness of all tiles to the same value. * * Returns: a new [GlobalBrightnessCommand] instance. + * + * This function is part of the sp_cmd_brightness_global module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); @@ -1338,6 +1385,8 @@ void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ insta /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. + * + * This function is part of the sp_cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); @@ -1357,6 +1406,8 @@ void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ * Tries to turn a [BrightnessGridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the sp_cmd_brightness_grid module. */ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); @@ -1366,6 +1417,8 @@ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand * * The passed [BrightnessGrid] gets consumed. * * Returns: a new [BrightnessGridCommand] instance. + * + * This function is part of the sp_cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, size_t origin_x, @@ -1397,13 +1450,15 @@ void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. + * + * This function is part of the sp_cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); /** * Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. */ -CharGrid *sp_cmd_char_grid_get(struct CharGridCommand */*notnull*/ command); +CharGrid */*notnull*/ sp_cmd_char_grid_get(struct CharGridCommand */*notnull*/ command); /** * Reads the origin field of the [CharGridCommand]. @@ -1418,6 +1473,8 @@ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, * The passed [CharGrid] gets consumed. * * Returns: a new [CharGridCommand] instance. + * + * This function is part of the sp_cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, size_t origin_x, @@ -1440,6 +1497,8 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, * Tries to turn a [CharGridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the sp_cmd_char_grid module. */ struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); @@ -1454,6 +1513,8 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); * Does not affect brightness. * * Returns: a new [ClearCommand] instance. + * + * This function is part of the sp_cmd_clear module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); @@ -1470,6 +1531,8 @@ void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance); /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. + * + * This function is part of the sp_cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); @@ -1498,6 +1561,8 @@ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, * The text is sent in the form of a 2D grid of [CP-437] encoded characters. * * The origin is relative to the top-left of the display. + * + * This function is part of the sp_cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, size_t origin_x, @@ -1524,6 +1589,8 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, * Tries to turn a [Cp437GridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the sp_cmd_cp437_grid module. */ struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); @@ -1536,6 +1603,8 @@ void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance); * A yet-to-be-tested command. * * Returns: a new [FadeOutCommand] instance. + * + * This function is part of the sp_cmd_fade_out module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); @@ -1588,6 +1657,8 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); * Please do not send this in your normal program flow. * * Returns: a new [HardResetCommand] instance. + * + * This function is part of the sp_cmd_hard_reset module. */ struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); @@ -1650,6 +1721,8 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); * The provided [Cp437Grid] gets consumed. * * Returns NULL in case of an error. + * + * This function is part of the sp_cp437_grid module. */ struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, size_t x, @@ -1657,6 +1730,8 @@ struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. + * + * This function is part of the sp_cp437_grid module. */ Cp437Grid *sp_cp437_grid_load(size_t width, size_t height, @@ -1666,6 +1741,8 @@ Cp437Grid *sp_cp437_grid_load(size_t width, * Creates a new [Cp437Grid] with the specified dimensions. * * returns: [Cp437Grid] initialized to 0. + * + * This function is part of the sp_cp437_grid module. */ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 3bfcf20..f4c9a7e 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,49 +1,50 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use std::ptr::NonNull; -/// Sets a window of pixels to the specified values. -/// -/// The passed [Bitmap] gets consumed. -/// -/// Returns: a new [BitmapCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_new( - bitmap: NonNull, - origin_x: usize, - origin_y: usize, - compression: CompressionCode, -) -> NonNull { - heap_move_nonnull(BitmapCommand { - bitmap: unsafe { heap_remove(bitmap) }, - origin: Origin::new(origin_x, origin_y), - compression, - }) -} +wrap_functions!(sp_cmd_bitmap; -/// Move the provided [Bitmap] into a new [BitmapCommand], -/// leaving other fields as their default values. -/// -/// Rust equivalent: `BitmapCommand::from(bitmap)` -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_from_bitmap( - bitmap: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) -} + /// Sets a window of pixels to the specified values. + /// + /// The passed [Bitmap] gets consumed. + /// + /// Returns: a new [BitmapCommand] instance. + fn new( + bitmap: NonNull, + origin_x: usize, + origin_y: usize, + compression: CompressionCode, + ) -> NonNull { + heap_move_nonnull(BitmapCommand { + bitmap: unsafe { heap_remove(bitmap) }, + origin: Origin::new(origin_x, origin_y), + compression, + }) + } -/// Tries to turn a [BitmapCommand] into a [Packet]. -/// -/// Returns: NULL or a [Packet] containing the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_try_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} + /// Move the provided [Bitmap] into a new [BitmapCommand], + /// leaving other fields as their default values. + /// + /// Rust equivalent: `BitmapCommand::from(bitmap)` + fn from_bitmap( + bitmap: NonNull, + ) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) + } + + /// Tries to turn a [BitmapCommand] into a [Packet]. + /// + /// Returns: NULL or a [Packet] containing the command. + fn try_into_packet( + command: NonNull, + ) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) + } + +); wrap_clone!(sp_cmd_bitmap::BitmapCommand); wrap_free!(sp_cmd_bitmap::BitmapCommand); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 7ccbf31..7dfed38 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -8,42 +8,44 @@ use servicepoint::{ }; use std::ptr::NonNull; -/// Set pixel data starting at the pixel offset on screen. -/// -/// The screen will continuously overwrite more pixel data without regarding the offset, meaning -/// once the starting row is full, overwriting will continue on column 0. -/// -/// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. -/// -/// `new_bit = old_bit op sent_bit` -/// -/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. -/// -/// The contained [`DisplayBitVec`] is always uncompressed. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_new( - bitvec: NonNull, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode, -) -> NonNull { - heap_move_nonnull(BitVecCommand { - bitvec: unsafe { heap_remove(bitvec) }, - offset, - operation, - compression, - }) -} +wrap_functions!(sp_cmd_bitvec; -/// Tries to turn a [BitVecCommand] into a [Packet]. -/// -/// Returns: NULL or a [Packet] containing the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_try_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} + /// Set pixel data starting at the pixel offset on screen. + /// + /// The screen will continuously overwrite more pixel data without regarding the offset, meaning + /// once the starting row is full, overwriting will continue on column 0. + /// + /// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. + /// + /// `new_bit = old_bit op sent_bit` + /// + /// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. + /// + /// The contained [`DisplayBitVec`] is always uncompressed. + fn new( + bitvec: NonNull, + offset: usize, + operation: BinaryOperation, + compression: CompressionCode, + ) -> NonNull { + heap_move_nonnull(BitVecCommand { + bitvec: unsafe { heap_remove(bitvec) }, + offset, + operation, + compression, + }) + } + + /// Tries to turn a [BitVecCommand] into a [Packet]. + /// + /// Returns: NULL or a [Packet] containing the command. + fn try_into_packet( + command: NonNull, + ) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) + } + +); wrap_clone!(sp_cmd_bitvec::BitVecCommand); wrap_free!(sp_cmd_bitvec::BitVecCommand); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 9c5069e..b4e6e23 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,45 +1,46 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_functions}, 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. -/// -/// The passed [BrightnessGrid] gets consumed. -/// -/// Returns: a new [BrightnessGridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_new( - grid: NonNull, - origin_x: usize, - origin_y: usize, -) -> NonNull { - heap_move_nonnull(BrightnessGridCommand { - grid: unsafe { heap_remove(grid) }, - origin: Origin::new(origin_x, origin_y), - }) -} +wrap_functions!(sp_cmd_brightness_grid; -/// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], -/// leaving other fields as their default values. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_from_grid( - grid: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) -} + /// Set the brightness of individual tiles in a rectangular area of the display. + /// + /// The passed [BrightnessGrid] gets consumed. + /// + /// Returns: a new [BrightnessGridCommand] instance. + fn new( + grid: NonNull, + origin_x: usize, + origin_y: usize, + ) -> NonNull { + heap_move_nonnull(BrightnessGridCommand { + grid: unsafe { heap_remove(grid) }, + origin: Origin::new(origin_x, origin_y), + }) + } -/// Tries to turn a [BrightnessGridCommand] into a [Packet]. -/// -/// Returns: NULL or a [Packet] containing the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} + /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + /// leaving other fields as their default values. + fn from_grid( + grid: NonNull, + ) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + } + + /// Tries to turn a [BrightnessGridCommand] into a [Packet]. + /// + /// Returns: NULL or a [Packet] containing the command. + fn into_packet( + command: NonNull, + ) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) + } + +); wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand); wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index fc73303..234b781 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,37 +1,43 @@ -use crate::{macros::wrap_free, mem::heap_move_nonnull}; +use crate::{ + macros::{wrap_free, wrap_functions}, + mem::heap_move_nonnull, +}; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; -/// Set all pixels to the off state. -/// -/// Does not affect brightness. -/// -/// Returns: a new [ClearCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_clear_new() -> NonNull { - heap_move_nonnull(ClearCommand) -} +wrap_functions!(sp_cmd_clear; + /// Set all pixels to the off state. + /// + /// Does not affect brightness. + /// + /// Returns: a new [ClearCommand] instance. + fn new() -> NonNull { + heap_move_nonnull(ClearCommand) + } +); wrap_free!(sp_cmd_clear::ClearCommand); -/// Kills the udp daemon on the display, which usually results in a restart. -/// -/// Please do not send this in your normal program flow. -/// -/// Returns: a new [HardResetCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_hard_reset_new() -> NonNull { - heap_move_nonnull(HardResetCommand) -} +wrap_functions!(sp_cmd_hard_reset; + /// Kills the udp daemon on the display, which usually results in a restart. + /// + /// Please do not send this in your normal program flow. + /// + /// Returns: a new [HardResetCommand] instance. + fn new() -> NonNull { + heap_move_nonnull(HardResetCommand) + } +); wrap_free!(sp_cmd_hard_reset::HardResetCommand); -/// A yet-to-be-tested command. -/// -/// Returns: a new [FadeOutCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_fade_out_new() -> NonNull { - heap_move_nonnull(FadeOutCommand) -} +wrap_functions!(sp_cmd_fade_out; + /// A yet-to-be-tested command. + /// + /// Returns: a new [FadeOutCommand] instance. + fn new() -> NonNull { + heap_move_nonnull(FadeOutCommand) + } +); wrap_free!(sp_cmd_fade_out::FadeOutCommand); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index b895718..0ef90c0 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,45 +1,46 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Origin, Packet}; use std::ptr::NonNull; -/// Show UTF-8 encoded text on the screen. -/// -/// The passed [CharGrid] gets consumed. -/// -/// Returns: a new [CharGridCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_new( - grid: NonNull, - origin_x: usize, - origin_y: usize, -) -> NonNull { - heap_move_nonnull(CharGridCommand { - grid: unsafe { heap_remove(grid) }, - origin: Origin::new(origin_x, origin_y), - }) -} +wrap_functions!(sp_cmd_char_grid; -/// Moves the provided [CharGrid] into a new [CharGridCommand], -/// leaving other fields as their default values. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_from_grid( - grid: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) -} + /// Show UTF-8 encoded text on the screen. + /// + /// The passed [CharGrid] gets consumed. + /// + /// Returns: a new [CharGridCommand] instance. + fn new( + grid: NonNull, + origin_x: usize, + origin_y: usize, + ) -> NonNull { + heap_move_nonnull(CharGridCommand { + grid: unsafe { heap_remove(grid) }, + origin: Origin::new(origin_x, origin_y), + }) + } -/// Tries to turn a [CharGridCommand] into a [Packet]. -/// -/// Returns: NULL or a [Packet] containing the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_try_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} + /// Moves the provided [CharGrid] into a new [CharGridCommand], + /// leaving other fields as their default values. + fn from_grid( + grid: NonNull, + ) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + } + + /// Tries to turn a [CharGridCommand] into a [Packet]. + /// + /// Returns: NULL or a [Packet] containing the command. + fn try_into_packet( + command: NonNull, + ) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) + } + +); wrap_clone!(sp_cmd_char_grid::CharGridCommand); wrap_free!(sp_cmd_char_grid::CharGridCommand); @@ -59,8 +60,8 @@ pub unsafe extern "C" fn sp_cmd_char_grid_set( #[no_mangle] pub unsafe extern "C" fn sp_cmd_char_grid_get( mut command: NonNull, -) -> *mut CharGrid { - unsafe { &mut command.as_mut().grid } +) -> NonNull { + unsafe { NonNull::from(&mut command.as_mut().grid) } } /// Reads the origin field of the [CharGridCommand]. diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 3f1fa9f..da8380a 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,45 +1,46 @@ use crate::{ - macros::{wrap_clone, wrap_free}, + macros::{wrap_clone, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; use std::ptr::NonNull; -/// Show text on the screen. -/// -/// The text is sent in the form of a 2D grid of [CP-437] encoded characters. -/// -/// The origin is relative to the top-left of the display. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_new( - grid: NonNull, - origin_x: usize, - origin_y: usize, -) -> NonNull { - heap_move_nonnull(Cp437GridCommand { - grid: unsafe { heap_remove(grid) }, - origin: Origin::new(origin_x, origin_y), - }) -} +wrap_functions!(sp_cmd_cp437_grid; -/// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], -/// leaving other fields as their default values. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_from_grid( - grid: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) -} + /// Show text on the screen. + /// + /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. + /// + /// The origin is relative to the top-left of the display. + fn new( + grid: NonNull, + origin_x: usize, + origin_y: usize, + ) -> NonNull { + heap_move_nonnull(Cp437GridCommand { + grid: unsafe { heap_remove(grid) }, + origin: Origin::new(origin_x, origin_y), + }) + } -/// Tries to turn a [Cp437GridCommand] into a [Packet]. -/// -/// Returns: NULL or a [Packet] containing the command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_try_into_packet( - command: NonNull, -) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) -} + /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], + /// leaving other fields as their default values. + fn from_grid( + grid: NonNull, + ) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + } + + /// Tries to turn a [Cp437GridCommand] into a [Packet]. + /// + /// Returns: NULL or a [Packet] containing the command. + fn try_into_packet( + command: NonNull, + ) -> *mut Packet { + heap_move_ok(unsafe { heap_remove(command) }.try_into()) + } + +); wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index e695ef0..35bcbe0 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,26 +1,25 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, 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. -/// -/// Returns: a new [GlobalBrightnessCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_new( - brightness: Brightness, -) -> NonNull { - heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) -} +wrap_functions!(sp_cmd_brightness_global; -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_global_into_packet( - command: NonNull, -) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) -} + /// Set the brightness of all tiles to the same value. + /// + /// Returns: a new [GlobalBrightnessCommand] instance. + fn new(brightness: Brightness) -> NonNull { + heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) + } + + /// Turns the command into a packet + fn into_packet(command: NonNull) -> NonNull { + heap_move_nonnull(unsafe { heap_remove(command) }.into()) + } + +); wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand); wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand); diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index ae0d2ae..6d502d9 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_methods}, + macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -9,80 +9,102 @@ use servicepoint::{ }; use std::ptr::NonNull; -/// Creates a new [Bitmap] with the specified dimensions. -/// -/// # Arguments -/// -/// - `width`: size in pixels in x-direction -/// - `height`: size in pixels in y-direction -/// -/// returns: [Bitmap] initialized to all pixels off, or NULL in case of an error. -/// -/// # Errors -/// -/// In the following cases, this function will return NULL: -/// -/// - when the width is not dividable by 8 -/// -/// # Examples -/// -/// ```C -/// Cp437Grid grid = sp_bitmap_new(8, 3); -/// sp_bitmap_fill(grid, true); -/// sp_bitmap_set(grid, 0, 0, false); -/// sp_bitmap_free(grid); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_new( - width: usize, - height: usize, -) -> *mut Bitmap { - heap_move_some(Bitmap::new(width, height)) -} - -/// Creates a new [Bitmap] with a size matching the screen. -/// -/// returns: [Bitmap] initialized to all pixels off. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_new_max_sized() -> NonNull { - heap_move_nonnull(Bitmap::max_sized()) -} - -/// Loads a [Bitmap] with the specified dimensions from the provided data. -/// -/// # Arguments -/// -/// - `width`: size in pixels in x-direction -/// - `height`: size in pixels in y-direction -/// -/// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_load( - width: usize, - height: usize, - data: ByteSlice, -) -> *mut Bitmap { - let data = unsafe { data.as_slice() }; - heap_move_ok(Bitmap::load(width, height, data)) -} - -/// Tries to convert the BitVec to a Bitmap. -/// -/// The provided BitVec gets consumed. -/// -/// Returns NULL in case of error. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_from_bitvec( - width: usize, - bitvec: NonNull, -) -> *mut Bitmap { - let bitvec = unsafe { heap_remove(bitvec) }; - heap_move_ok(Bitmap::from_bitvec(width, bitvec)) -} - wrap_clone!(sp_bitmap::Bitmap); wrap_free!(sp_bitmap::Bitmap); +wrap_functions!(sp_bitmap; + + /// Creates a new [Bitmap] with the specified dimensions. + /// + /// # Arguments + /// + /// - `width`: size in pixels in x-direction + /// - `height`: size in pixels in y-direction + /// + /// returns: [Bitmap] initialized to all pixels off, or NULL in case of an error. + /// + /// # Errors + /// + /// In the following cases, this function will return NULL: + /// + /// - when the width is not dividable by 8 + /// + /// # Examples + /// + /// ```C + /// Cp437Grid grid = sp_bitmap_new(8, 3); + /// sp_bitmap_fill(grid, true); + /// sp_bitmap_set(grid, 0, 0, false); + /// sp_bitmap_free(grid); + /// ``` + fn new(width: usize, height: usize) -> *mut Bitmap { + heap_move_some(Bitmap::new(width, height)) + } + + /// Creates a new [Bitmap] with a size matching the screen. + /// + /// returns: [Bitmap] initialized to all pixels off. + fn new_max_sized() -> NonNull { + heap_move_nonnull(Bitmap::max_sized()) + } + + /// Loads a [Bitmap] with the specified dimensions from the provided data. + /// + /// # Arguments + /// + /// - `width`: size in pixels in x-direction + /// - `height`: size in pixels in y-direction + /// + /// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. + fn load( + width: usize, + height: usize, + data: ByteSlice, + ) -> *mut Bitmap { + let data = unsafe { data.as_slice() }; + heap_move_ok(Bitmap::load(width, height, data)) + } + + /// Tries to convert the BitVec to a Bitmap. + /// + /// The provided BitVec gets consumed. + /// + /// Returns NULL in case of error. + fn from_bitvec( + width: usize, + bitvec: NonNull, + ) -> *mut Bitmap { + let bitvec = unsafe { heap_remove(bitvec) }; + heap_move_ok(Bitmap::from_bitvec(width, bitvec)) + } + /// Consumes the Bitmap and returns the contained BitVec. + fn into_bitvec( + bitmap: NonNull + ) -> NonNull { + let bitmap = unsafe { heap_remove(bitmap) }; + heap_move_nonnull(bitmap.into()) + } + + /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. + /// + /// The provided [Bitmap] gets consumed. + /// + /// Returns NULL in case of an error. + fn into_packet( + bitmap: NonNull, + x: usize, + y: usize, + compression: CompressionCode, + ) -> *mut Packet { + let bitmap = unsafe { heap_remove(bitmap) }; + heap_move_ok(Packet::try_from(BitmapCommand { + bitmap, + origin: Origin::new(x, y), + compression, + })) + } +); + wrap_methods!( sp_bitmap::Bitmap; @@ -130,32 +152,3 @@ wrap_methods!( return(slice) { unsafe { ByteSlice::from_slice(slice) } }; }; ); - -/// Consumes the Bitmap and returns the contained BitVec. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_into_bitvec( - bitmap: NonNull, -) -> NonNull { - let bitmap = unsafe { heap_remove(bitmap) }; - heap_move_nonnull(bitmap.into()) -} - -/// Creates a [BitmapCommand] and immediately turns that into a [Packet]. -/// -/// The provided [Bitmap] gets consumed. -/// -/// Returns NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_bitmap_into_packet( - bitmap: NonNull, - x: usize, - y: usize, - compression: CompressionCode, -) -> *mut Packet { - let bitmap = unsafe { heap_remove(bitmap) }; - heap_move_ok(Packet::try_from(BitmapCommand { - bitmap, - origin: Origin::new(x, y), - compression, - })) -} diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 76d716a..e232ab5 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_methods}, + macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -8,32 +8,52 @@ use servicepoint::{ }; use std::ptr::NonNull; -/// Creates a new [DisplayBitVec] instance. -/// -/// # Arguments -/// -/// - `size`: size in bits. -/// -/// returns: [DisplayBitVec] with all bits set to false. -/// -/// # Panics -/// -/// - when `size` is not divisible by 8. -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull { - heap_move_nonnull(DisplayBitVec::repeat(false, size)) -} +wrap_functions!(sp_bitvec; -/// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. -/// -/// returns: [DisplayBitVec] instance containing data. -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_load( - data: ByteSlice, -) -> NonNull { - let data = unsafe { data.as_slice() }; - heap_move_nonnull(DisplayBitVec::from_slice(data)) -} + /// Creates a new [DisplayBitVec] instance. + /// + /// # Arguments + /// + /// - `size`: size in bits. + /// + /// returns: [DisplayBitVec] with all bits set to false. + /// + /// # Panics + /// + /// - when `size` is not divisible by 8. + fn new(size: usize) -> NonNull { + heap_move_nonnull(DisplayBitVec::repeat(false, size)) + } + + /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. + /// + /// returns: [DisplayBitVec] instance containing data. + fn load(data: ByteSlice) -> NonNull { + let data = unsafe { data.as_slice() }; + heap_move_nonnull(DisplayBitVec::from_slice(data)) + } + + /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. + /// + /// The provided [DisplayBitVec] gets consumed. + /// + /// Returns NULL in case of an error. + fn into_packet( + bitvec: NonNull, + offset: usize, + operation: BinaryOperation, + compression: CompressionCode, + ) -> *mut Packet { + let bitvec = unsafe { heap_remove(bitvec) }; + heap_move_ok(Packet::try_from(BitVecCommand { + bitvec, + offset, + operation, + compression, + })) + } + +); wrap_clone!(sp_bitvec::DisplayBitVec); wrap_free!(sp_bitvec::DisplayBitVec); @@ -90,24 +110,3 @@ wrap_methods!( return(slice) { unsafe { ByteSlice::from_slice(slice) } }; }; ); - -/// Creates a [BitVecCommand] and immediately turns that into a [Packet]. -/// -/// The provided [DisplayBitVec] gets consumed. -/// -/// Returns NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_bitvec_into_packet( - bitvec: NonNull, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode, -) -> *mut Packet { - let bitvec = unsafe { heap_remove(bitvec) }; - heap_move_ok(Packet::try_from(BitVecCommand { - bitvec, - offset, - operation, - compression, - })) -} diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a3590b8..2374b47 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,3 +1,4 @@ +use crate::macros::wrap_functions; use crate::{ containers::ByteSlice, macros::{wrap_clone, wrap_free, wrap_methods}, @@ -9,48 +10,67 @@ use servicepoint::{ }; use std::{mem::transmute, ptr::NonNull}; -/// Creates a new [BrightnessGrid] with the specified dimensions. -/// -/// returns: [BrightnessGrid] initialized to 0. -/// -/// # Examples -/// ```C -/// UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); -/// if (connection == NULL) -/// return 1; -/// -/// BrightnessGrid *grid = sp_brightness_grid_new(2, 2); -/// sp_brightness_grid_set(grid, 0, 0, 0); -/// sp_brightness_grid_set(grid, 1, 1, 10); -/// -/// TypedCommand *command = sp_command_char_brightness(grid); -/// sp_udp_free(connection); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_new( - width: usize, - height: usize, -) -> NonNull { - heap_move_nonnull(BrightnessGrid::new(width, height)) -} +wrap_functions!(sp_brightness_grid; -/// Loads a [BrightnessGrid] with the specified dimensions from the provided data. -/// -/// Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN]. -/// -/// returns: new [BrightnessGrid] instance, or NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_load( - width: usize, - height: usize, - data: ByteSlice, -) -> *mut BrightnessGrid { - let data = unsafe { data.as_slice() }; - heap_move_some( - ByteGrid::load(width, height, data) - .map(move |grid| grid.map(Brightness::saturating_from)), - ) -} + /// Creates a new [BrightnessGrid] with the specified dimensions. + /// + /// returns: [BrightnessGrid] initialized to 0. + /// + /// # Examples + /// ```C + /// UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); + /// if (connection == NULL) + /// return 1; + /// + /// BrightnessGrid *grid = sp_brightness_grid_new(2, 2); + /// sp_brightness_grid_set(grid, 0, 0, 0); + /// sp_brightness_grid_set(grid, 1, 1, 10); + /// + /// TypedCommand *command = sp_command_char_brightness(grid); + /// sp_udp_free(connection); + /// ``` + fn new( + width: usize, + height: usize, + ) -> NonNull { + heap_move_nonnull(BrightnessGrid::new(width, height)) + } + + /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. + /// + /// Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN]. + /// + /// returns: new [BrightnessGrid] instance, or NULL in case of an error. + fn load( + width: usize, + height: usize, + data: ByteSlice, + ) -> *mut BrightnessGrid { + let data = unsafe { data.as_slice() }; + heap_move_some( + ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)), + ) + } + + /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [BrightnessGrid] gets consumed. + /// + /// Returns NULL in case of an error. + fn into_packet( + grid: NonNull, + x: usize, + y: usize, + ) -> *mut Packet { + let grid = unsafe { heap_remove(grid) }; + heap_move_ok(Packet::try_from(BrightnessGridCommand { + grid, + origin: Origin::new(x, y), + })) + } + +); wrap_clone!(sp_brightness_grid::BrightnessGrid); wrap_free!(sp_brightness_grid::BrightnessGrid); @@ -109,21 +129,3 @@ wrap_methods!( }}; }; ); - -/// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. -/// -/// The provided [BrightnessGrid] gets consumed. -/// -/// Returns NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_brightness_grid_into_packet( - grid: NonNull, - x: usize, - y: usize, -) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(BrightnessGridCommand { - grid, - origin: Origin::new(x, y), - })) -} diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 1b70202..782e2aa 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,43 +1,62 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_methods}, + macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -/// Creates a new [CharGrid] with the specified dimensions. -/// -/// returns: [CharGrid] initialized to 0. -/// -/// # Examples -/// -/// ```C -/// CharGrid grid = sp_char_grid_new(4, 3); -/// sp_char_grid_fill(grid, '?'); -/// sp_char_grid_set(grid, 0, 0, '!'); -/// sp_char_grid_free(grid); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_new( - width: usize, - height: usize, -) -> NonNull { - heap_move_nonnull(CharGrid::new(width, height)) -} +wrap_functions!(sp_char_grid; -/// Loads a [CharGrid] with the specified dimensions from the provided data. -/// -/// returns: new CharGrid or NULL in case of an error -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_load( - width: usize, - height: usize, - data: ByteSlice, -) -> *mut CharGrid { - let data = unsafe { data.as_slice() }; - heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) -} + /// Creates a new [CharGrid] with the specified dimensions. + /// + /// returns: [CharGrid] initialized to 0. + /// + /// # Examples + /// + /// ```C + /// CharGrid grid = sp_char_grid_new(4, 3); + /// sp_char_grid_fill(grid, '?'); + /// sp_char_grid_set(grid, 0, 0, '!'); + /// sp_char_grid_free(grid); + /// ``` + fn new( + width: usize, + height: usize, + ) -> NonNull { + heap_move_nonnull(CharGrid::new(width, height)) + } + + /// Loads a [CharGrid] with the specified dimensions from the provided data. + /// + /// returns: new CharGrid or NULL in case of an error + fn load( + width: usize, + height: usize, + data: ByteSlice, + ) -> *mut CharGrid { + let data = unsafe { data.as_slice() }; + heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) + } + + /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [CharGrid] gets consumed. + /// + /// Returns NULL in case of an error. + fn into_packet( + grid: NonNull, + x: usize, + y: usize, + ) -> *mut Packet { + let grid = unsafe { heap_remove(grid) }; + heap_move_ok(Packet::try_from(CharGridCommand { + grid, + origin: Origin::new(x, y), + })) + } + +); wrap_clone!(sp_char_grid::CharGrid); wrap_free!(sp_char_grid::CharGrid); @@ -91,21 +110,3 @@ wrap_methods!( /// Gets the height of the grid. ref fn height() -> usize; ); - -/// Creates a [CharGridCommand] and immediately turns that into a [Packet]. -/// -/// The provided [CharGrid] gets consumed. -/// -/// Returns NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_char_grid_into_packet( - grid: NonNull, - x: usize, - y: usize, -) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(CharGridCommand { - grid, - origin: Origin::new(x, y), - })) -} diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index d6ae17f..2ed9aba 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,3 +1,4 @@ +use crate::macros::wrap_functions; use crate::{ containers::ByteSlice, macros::{wrap_clone, wrap_free, wrap_methods}, @@ -8,27 +9,46 @@ use servicepoint::{ }; use std::ptr::NonNull; -/// Creates a new [Cp437Grid] with the specified dimensions. -/// -/// returns: [Cp437Grid] initialized to 0. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_new( - width: usize, - height: usize, -) -> NonNull { - heap_move_nonnull(Cp437Grid::new(width, height)) -} +wrap_functions!(sp_cp437_grid; -/// Loads a [Cp437Grid] with the specified dimensions from the provided data. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_load( - width: usize, - height: usize, - data: ByteSlice, -) -> *mut Cp437Grid { - let data = unsafe { data.as_slice() }; - heap_move_some(Cp437Grid::load(width, height, data)) -} + /// Creates a new [Cp437Grid] with the specified dimensions. + /// + /// returns: [Cp437Grid] initialized to 0. + fn new( + width: usize, + height: usize, + ) -> NonNull { + heap_move_nonnull(Cp437Grid::new(width, height)) + } + + /// Loads a [Cp437Grid] with the specified dimensions from the provided data. + fn load( + width: usize, + height: usize, + data: ByteSlice, + ) -> *mut Cp437Grid { + let data = unsafe { data.as_slice() }; + heap_move_some(Cp437Grid::load(width, height, data)) + } + + /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [Cp437Grid] gets consumed. + /// + /// Returns NULL in case of an error. + fn into_packet( + grid: NonNull, + x: usize, + y: usize, + ) -> *mut Packet { + let grid = unsafe { heap_remove(grid) }; + heap_move_ok(Packet::try_from(Cp437GridCommand { + grid, + origin: Origin::new(x, y), + })) + } + +); wrap_clone!(sp_cp437_grid::Cp437Grid); wrap_free!(sp_cp437_grid::Cp437Grid); @@ -81,21 +101,3 @@ wrap_methods!( return(slice) { unsafe { ByteSlice::from_slice(slice) } }; }; ); - -/// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. -/// -/// The provided [Cp437Grid] gets consumed. -/// -/// Returns NULL in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_cp437_grid_into_packet( - grid: NonNull, - x: usize, - y: usize, -) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(Cp437GridCommand { - grid, - origin: Origin::new(x, y), - })) -} diff --git a/src/macros.rs b/src/macros.rs index cbadc14..48952db 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -47,7 +47,7 @@ macro_rules! wrap_methods { $(return($it:ident) $return_expr:block;)? })? ; - )* + )+ ) => { paste::paste! { $( @@ -76,7 +76,7 @@ macro_rules! wrap_methods { )? return result; } - )* + )+ } }; } @@ -149,7 +149,33 @@ macro_rules! wrap_fields { }; } +macro_rules! wrap_functions { + ( + $prefix:ident; + $( + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block + )+ + ) => { + ::paste::paste! { + $( + $(#[$meta])* + #[doc = ""] + #[doc = concat!(" This function is part of the ", stringify!($prefix), " module.")] + #[no_mangle] + pub unsafe extern "C" fn [<$prefix _ $function>]( + $($param_name: $param_type),* + ) $(-> $return_type)? + $block + + )+ + } + }; +} + pub(crate) use { nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free, - wrap_methods, + wrap_functions, wrap_methods, }; From 21987d05f37ab5696b3fbbffe794507df65f1ce9 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 20:41:22 +0200 Subject: [PATCH 47/76] use macros in macros --- include/servicepoint.h | 218 +++++++++++++++++++++++++++++++++-------- src/macros.rs | 141 +++++++++++++------------- 2 files changed, 248 insertions(+), 111 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 5960987..e001ddd 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -640,7 +640,9 @@ extern "C" { void init_env_logger(void); /** - *Clones a [Bitmap] instance. + *Clones a [`Bitmap`] instance. + * + * This function is part of the sp_bitmap module. */ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); @@ -650,6 +652,8 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); * Gets an unsafe reference to the data of the [Bitmap] instance. * * The returned memory is valid for the lifetime of the bitmap. + * + * This function is part of the sp_bitmap module. */ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); @@ -661,11 +665,15 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); * # Arguments * * - `value`: the value to set all pixels to + * + * This function is part of the sp_bitmap module. */ void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** - *Deallocates a [Bitmap] instance. + *Deallocates a [`Bitmap`] instance. + * + * This function is part of the sp_bitmap module. */ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); @@ -692,6 +700,8 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * # Panics * * - when accessing `x` or `y` out of bounds + * + * This function is part of the sp_bitmap module. */ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); @@ -699,6 +709,8 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); * Calls [`servicepoint::Bitmap::height`]. * * Gets the height in pixels. + * + * This function is part of the sp_bitmap module. */ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); @@ -790,6 +802,8 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * # Panics * * - when accessing `x` or `y` out of bounds + * + * This function is part of the sp_bitmap module. */ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, @@ -800,6 +814,8 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, * Calls [`servicepoint::Bitmap::width`]. * * Gets the width in pixels. + * + * This function is part of the sp_bitmap module. */ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); @@ -809,11 +825,15 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * * The returned memory is valid for the lifetime of the bitvec. + * + * This function is part of the sp_bitvec module. */ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** - *Clones a [DisplayBitVec] instance. + *Clones a [`DisplayBitVec`] instance. + * + * This function is part of the sp_bitvec module. */ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); @@ -825,11 +845,15 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); * # Arguments * * - `value`: the value to set all bits to + * + * This function is part of the sp_bitvec module. */ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** - *Deallocates a [DisplayBitVec] instance. + *Deallocates a [`DisplayBitVec`] instance. + * + * This function is part of the sp_bitvec module. */ void sp_bitvec_free(BitVec */*notnull*/ instance); @@ -848,6 +872,8 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * # Panics * * - when accessing `index` out of bounds + * + * This function is part of the sp_bitvec module. */ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); @@ -869,6 +895,8 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, * Calls [`servicepoint::DisplayBitVec::is_empty`]. * * Returns true if length is 0. + * + * This function is part of the sp_bitvec module. */ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); @@ -876,6 +904,8 @@ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); * Calls [`servicepoint::DisplayBitVec::len`]. * * Gets the length in bits. + * + * This function is part of the sp_bitvec module. */ size_t sp_bitvec_len(BitVec */*notnull*/ instance); @@ -918,11 +948,15 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * # Panics * * - when accessing `index` out of bounds + * + * This function is part of the sp_bitvec module. */ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** - *Clones a [BrightnessGrid] instance. + *Clones a [`BrightnessGrid`] instance. + * + * This function is part of the sp_brightness_grid module. */ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); @@ -932,6 +966,8 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ * Gets an unsafe reference to the data of the instance. * * The returned memory is valid for the lifetime of the grid. + * + * This function is part of the sp_brightness_grid module. */ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); @@ -943,12 +979,16 @@ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ ins * # Arguments * * - `value`: the value to set all cells to + * + * This function is part of the sp_brightness_grid module. */ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); /** - *Deallocates a [BrightnessGrid] instance. + *Deallocates a [`BrightnessGrid`] instance. + * + * This function is part of the sp_brightness_grid module. */ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); @@ -965,6 +1005,8 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * * # Panics * - When accessing `x` or `y` out of bounds. + * + * This function is part of the sp_brightness_grid module. */ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t x, @@ -974,6 +1016,8 @@ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, * Calls [`servicepoint::BrightnessGrid::height`]. * * Gets the height of the grid. + * + * This function is part of the sp_brightness_grid module. */ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); @@ -1041,6 +1085,8 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * # Panics * * - When accessing `x` or `y` out of bounds. + * + * This function is part of the sp_brightness_grid module. */ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, size_t x, @@ -1051,11 +1097,15 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, * Calls [`servicepoint::BrightnessGrid::width`]. * * Gets the width of the grid. + * + * This function is part of the sp_brightness_grid module. */ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); /** - *Clones a [CharGrid] instance. + *Clones a [`CharGrid`] instance. + * + * This function is part of the sp_char_grid module. */ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); @@ -1068,11 +1118,15 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); * * - `value`: the value to set all cells to * - when providing values that cannot be converted to Rust's `char`. + * + * This function is part of the sp_char_grid module. */ void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** - *Deallocates a [CharGrid] instance. + *Deallocates a [`CharGrid`] instance. + * + * This function is part of the sp_char_grid module. */ void sp_char_grid_free(CharGrid */*notnull*/ instance); @@ -1088,6 +1142,8 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); * # Panics * * - when accessing `x` or `y` out of bounds + * + * This function is part of the sp_char_grid module. */ uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); @@ -1095,6 +1151,8 @@ uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); * Calls [`servicepoint::CharGrid::height`]. * * Gets the height of the grid. + * + * This function is part of the sp_char_grid module. */ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); @@ -1154,6 +1212,8 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - when providing values that cannot be converted to Rust's `char`. + * + * This function is part of the sp_char_grid module. */ void sp_char_grid_set(CharGrid */*notnull*/ instance, size_t x, @@ -1164,16 +1224,22 @@ void sp_char_grid_set(CharGrid */*notnull*/ instance, * Calls [`servicepoint::CharGrid::width`]. * * Gets the width of the grid. + * + * This function is part of the sp_char_grid module. */ size_t sp_char_grid_width(CharGrid */*notnull*/ instance); /** - *Clones a [BitmapCommand] instance. + *Clones a [`BitmapCommand`] instance. + * + * This function is part of the sp_cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); /** - *Deallocates a [BitmapCommand] instance. + *Deallocates a [`BitmapCommand`] instance. + * + * This function is part of the sp_cmd_bitmap module. */ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); @@ -1198,7 +1264,9 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ command); /** - *Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * + * This function is part of the sp_cmd_bitmap module. */ CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); @@ -1230,7 +1298,9 @@ void sp_cmd_bitmap_set(struct BitmapCommand */*notnull*/ command, struct Bitmap */*notnull*/ bitmap); /** - *Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * + * This function is part of the sp_cmd_bitmap module. */ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, CompressionCode value); @@ -1252,12 +1322,16 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** - *Clones a [BitVecCommand] instance. + *Clones a [`BitVecCommand`] instance. + * + * This function is part of the sp_cmd_bitvec module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); /** - *Deallocates a [BitVecCommand] instance. + *Deallocates a [`BitVecCommand`] instance. + * + * This function is part of the sp_cmd_bitvec module. */ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); @@ -1267,17 +1341,23 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); BitVec *sp_cmd_bitvec_get(struct BitVecCommand */*notnull*/ command); /** - *Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the sp_cmd_bitvec module. */ CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** - *Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the sp_cmd_bitvec module. */ Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** - *Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the sp_cmd_bitvec module. */ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); @@ -1309,19 +1389,25 @@ void sp_cmd_bitvec_set(struct BitVecCommand */*notnull*/ command, BitVec */*notnull*/ bitvec); /** - *Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the sp_cmd_bitvec module. */ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, CompressionCode value); /** - *Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the sp_cmd_bitvec module. */ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); /** - *Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the sp_cmd_bitvec module. */ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); @@ -1336,17 +1422,23 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** - *Clones a [GlobalBrightnessCommand] instance. + *Clones a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the sp_cmd_brightness_global module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** - *Deallocates a [GlobalBrightnessCommand] instance. + *Deallocates a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the sp_cmd_brightness_global module. */ void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** - *Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the sp_cmd_brightness_global module. */ Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); @@ -1367,18 +1459,24 @@ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBri struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** - *Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the sp_cmd_brightness_global module. */ void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); /** - *Clones a [BrightnessGridCommand] instance. + *Clones a [`BrightnessGridCommand`] instance. + * + * This function is part of the sp_cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - *Deallocates a [BrightnessGridCommand] instance. + *Deallocates a [`BrightnessGridCommand`] instance. + * + * This function is part of the sp_cmd_brightness_grid module. */ void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ instance); @@ -1438,12 +1536,16 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ size_t origin_y); /** - *Clones a [CharGridCommand] instance. + *Clones a [`CharGridCommand`] instance. + * + * This function is part of the sp_cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ instance); /** - *Deallocates a [CharGridCommand] instance. + *Deallocates a [`CharGridCommand`] instance. + * + * This function is part of the sp_cmd_char_grid module. */ void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance); @@ -1503,7 +1605,9 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** - *Deallocates a [ClearCommand] instance. + *Deallocates a [`ClearCommand`] instance. + * + * This function is part of the sp_cmd_clear module. */ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); @@ -1519,12 +1623,16 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); /** - *Clones a [Cp437GridCommand] instance. + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the sp_cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ instance); /** - *Deallocates a [Cp437GridCommand] instance. + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the sp_cmd_cp437_grid module. */ void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance); @@ -1595,7 +1703,9 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** - *Deallocates a [FadeOutCommand] instance. + *Deallocates a [`FadeOutCommand`] instance. + * + * This function is part of the sp_cmd_fade_out module. */ void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance); @@ -1647,7 +1757,9 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** - *Deallocates a [HardResetCommand] instance. + *Deallocates a [`HardResetCommand`] instance. + * + * This function is part of the sp_cmd_hard_reset module. */ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); @@ -1663,7 +1775,9 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); /** - *Clones a [Cp437Grid] instance. + *Clones a [`Cp437Grid`] instance. + * + * This function is part of the sp_cp437_grid module. */ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); @@ -1673,6 +1787,8 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); * Gets an unsafe reference to the data of the grid. * * The returned memory is valid for the lifetime of the instance. + * + * This function is part of the sp_cp437_grid module. */ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); @@ -1685,11 +1801,15 @@ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); * * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to + * + * This function is part of the sp_cp437_grid module. */ void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** - *Deallocates a [Cp437Grid] instance. + *Deallocates a [`Cp437Grid`] instance. + * + * This function is part of the sp_cp437_grid module. */ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); @@ -1705,6 +1825,8 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); * # Panics * * - when accessing `x` or `y` out of bounds + * + * This function is part of the sp_cp437_grid module. */ uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); @@ -1712,6 +1834,8 @@ uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); * Calls [`servicepoint::Cp437Grid::height`]. * * Gets the height of the grid. + * + * This function is part of the sp_cp437_grid module. */ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); @@ -1761,6 +1885,8 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * # Panics * * - when accessing `x` or `y` out of bounds + * + * This function is part of the sp_cp437_grid module. */ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, size_t x, @@ -1771,16 +1897,22 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, * Calls [`servicepoint::Cp437Grid::width`]. * * Gets the width of the grid. + * + * This function is part of the sp_cp437_grid module. */ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); /** - *Clones a [Packet] instance. + *Clones a [`Packet`] instance. + * + * This function is part of the sp_packet module. */ struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** - *Deallocates a [Packet] instance. + *Deallocates a [`Packet`] instance. + * + * This function is part of the sp_packet module. */ void sp_packet_free(struct Packet */*notnull*/ instance); @@ -1793,7 +1925,9 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); /** - *Gets the value of field `header` of the [`servicepoint::Packet`]. + * Gets the value of field `header` of the [`servicepoint::Packet`]. + * + * This function is part of the sp_packet module. */ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); @@ -1824,7 +1958,9 @@ void sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); /** - *Sets the value of field `header` of the [`servicepoint::Packet`]. + * Sets the value of field `header` of the [`servicepoint::Packet`]. + * + * This function is part of the sp_packet module. */ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); @@ -1853,7 +1989,9 @@ bool sp_u16_to_command_code(uint16_t code, CommandCode *result); /** - *Deallocates a [UdpSocket] instance. + *Deallocates a [`UdpSocket`] instance. + * + * This function is part of the sp_udp module. */ void sp_udp_free(struct UdpSocket */*notnull*/ instance); diff --git a/src/macros.rs b/src/macros.rs index 48952db..8d4ba47 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,24 +1,22 @@ macro_rules! wrap_free { ($prefix:ident :: $typ:ty) => { - paste::paste! { - #[doc = concat!("Deallocates a [", stringify!($typ), "] instance.")] - #[no_mangle] - pub unsafe extern "C" fn [<$prefix _free>](instance: NonNull<$typ>) { + $crate::macros::wrap_functions!($prefix; + #[doc = concat!("Deallocates a [`", stringify!($typ), "`] instance.")] + fn free(instance: NonNull<$typ>) { unsafe { $crate::mem::heap_drop(instance) } } - } + ); }; } macro_rules! wrap_clone { ($prefix:ident :: $typ:ty) => { - paste::paste! { - #[doc = concat!("Clones a [", stringify!($typ), "] instance.")] - #[no_mangle] - pub unsafe extern "C" fn [<$prefix _clone>](instance: NonNull<$typ>) -> NonNull<$typ> { + $crate::macros::wrap_functions!($prefix; + #[doc = concat!("Clones a [`", stringify!($typ), "`] instance.")] + fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { unsafe { $crate::mem::heap_clone(instance) } } - } + ); }; } @@ -50,33 +48,34 @@ macro_rules! wrap_methods { )+ ) => { paste::paste! { + $crate::macros::wrap_functions!($prefix; $( - #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), - "::", stringify!($function), "`].")] - #[doc = ""] - $(#[$meta])* - #[no_mangle] - pub unsafe extern "C" fn [<$prefix _ $function>]( - instance: NonNull<$object_type>, - $($param_name: $param_type),* - ) $(-> $return_type)? { - let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; - $($( - $(let $param_let_name = $param_let_expr;)* - )?)? - #[allow( - unused_variables, - reason = "This variable may not be used depending on macro variables" )] - let result = instance.$function($($param_name),*); - $( + #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), + "::", stringify!($function), "`].")] + #[doc = ""] + $(#[$meta])* + fn $function( + instance: NonNull<$object_type>, + $($param_name: $param_type),* + ) $(-> $return_type)? { + let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; + $($( + $(let $param_let_name = $param_let_expr;)* + )?)? + #[allow( + unused_variables, + reason = "This variable may not be used depending on macro variables" )] + let result = instance.$function($($param_name),*); $( - let $it = result; - let result = $return_expr; + $( + let $it = result; + let result = $return_expr; + )? )? - )? - return result; - } + return result; + } )+ + ); } }; } @@ -103,48 +102,48 @@ macro_rules! wrap_fields { )+ ) => { paste::paste! { - $( + $crate::macros::wrap_functions!($prefix; $( - #[doc = concat!("Gets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - $($( - #[doc = ""] - #[$get_meta] - )*)? - #[no_mangle] - pub unsafe extern "C" fn [<$prefix _ get _ $prop_name>]( - instance: NonNull<$object_type> - ) -> $prop_type { - let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) }; - let $prop_name = instance.$prop_name; + $( + #[doc = concat!(" Gets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] $($( - let $prop_name = $get_expr; - )?)? - return $prop_name; - } - )? + #[doc = ""] + #[$get_meta] + )*)? + fn []( + instance: NonNull<$object_type> + ) -> $prop_type { + let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) }; + let $prop_name = instance.$prop_name; + $($( + let $prop_name = $get_expr; + )?)? + return $prop_name; + } + )? - $( - #[doc = concat!("Sets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - $($( - #[doc = ""] - #[$set_meta] - )*)? - #[no_mangle] - pub unsafe extern "C" fn [<$prefix _ set _ $prop_name>]( - instance: NonNull<$object_type>, - value: $prop_type, - ) { - let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; + $( + #[doc = concat!(" Sets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] $($( - let $value = value; - let value = $set_expr; - )?)? - instance.$prop_name = value; - } - )? - )+ + #[doc = ""] + #[$set_meta] + )*)? + fn []( + instance: NonNull<$object_type>, + value: $prop_type, + ) { + let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; + $($( + let $value = value; + let value = $set_expr; + )?)? + instance.$prop_name = value; + } + )? + )+ + ); } }; } From c492cfab6bb17d8eaba0c1745b79b7f28a63baba Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 22:40:04 +0200 Subject: [PATCH 48/76] even more functions wrapped with macro --- include/servicepoint.h | 126 ++++++++++++++++-------- src/commands/bitmap_command.rs | 82 +++------------ src/commands/bitvec_command.rs | 48 ++------- src/commands/brightness_grid_command.rs | 60 +++-------- src/commands/char_grid_command.rs | 60 +++-------- src/commands/cp437_grid_command.rs | 71 +++---------- src/commands/mod.rs | 32 ++++++ src/macros.rs | 101 +++++++++++++++---- 8 files changed, 249 insertions(+), 331 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index e001ddd..1e0182e 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1,7 +1,7 @@ #ifndef SERVICEPOINT_BINDINGS_C #define SERVICEPOINT_BINDINGS_C -/* Generated with cbindgen:0.29.0 */ +/* Generated with cbindgen:0.28.0 */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ @@ -1254,14 +1254,14 @@ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** - * Returns a pointer to the provided `BitmapCommand`. + * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. * - * # Safety - * - * - The returned bitmap inherits the lifetime of the command in which it is contained. + * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the sp_cmd_bitmap module. */ -struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ command); +struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. @@ -1271,7 +1271,9 @@ struct Bitmap */*notnull*/ sp_cmd_bitmap_get(struct BitmapCommand */*notnull*/ c CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** - * Reads the origin field of the [BitmapCommand]. + * Reads the origin field of the [`BitmapCommand`]. + * + * This function is part of the sp_cmd_bitmap module. */ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1292,10 +1294,13 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ b CompressionCode compression); /** - * Moves the provided [Bitmap] to be contained in the [BitmapCommand]. + * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the sp_cmd_bitmap module. */ -void sp_cmd_bitmap_set(struct BitmapCommand */*notnull*/ command, - struct Bitmap */*notnull*/ bitmap); +void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, + struct Bitmap */*notnull*/ value); /** * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. @@ -1306,7 +1311,9 @@ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, CompressionCode value); /** - * Overwrites the origin field of the [BitmapCommand]. + * Overwrites the origin field of the [`BitmapCommand`]. + * + * This function is part of the sp_cmd_bitmap module. */ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, @@ -1336,9 +1343,14 @@ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*no void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); /** - * Returns a pointer to the [BitVec] contained in the [BitVecCommand]. + * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the sp_cmd_bitvec module. */ -BitVec *sp_cmd_bitvec_get(struct BitVecCommand */*notnull*/ command); +BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. @@ -1383,10 +1395,13 @@ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, CompressionCode compression); /** - * Moves the provided [BitVec] to be contained in the [BitVecCommand]. + * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the sp_cmd_bitvec module. */ -void sp_cmd_bitvec_set(struct BitVecCommand */*notnull*/ command, - BitVec */*notnull*/ bitvec); +void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, + BitVec */*notnull*/ value); /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. @@ -1489,12 +1504,19 @@ void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ insta struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); /** - * Returns a pointer to the [BrightnessGrid] contained in the [BrightnessGridCommand]. + * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the sp_cmd_brightness_grid module. */ -BrightnessGrid *sp_cmd_brightness_grid_get(struct BrightnessGridCommand */*notnull*/ command); +BrightnessGrid */*notnull*/ sp_cmd_brightness_grid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** - * Overwrites the origin field of the [BrightnessGridCommand]. + * Reads the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the sp_cmd_brightness_grid module. */ void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1523,13 +1545,18 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessG size_t origin_y); /** - * Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. + * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the sp_cmd_brightness_grid module. */ -void sp_cmd_brightness_grid_set(struct BrightnessGridCommand */*notnull*/ command, - BrightnessGrid */*notnull*/ grid); +void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, + BrightnessGrid */*notnull*/ value); /** - * Reads the origin field of the [BrightnessGridCommand]. + * Overwrites the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the sp_cmd_brightness_grid module. */ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, size_t origin_x, @@ -1558,12 +1585,19 @@ void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); /** - * Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. + * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the sp_cmd_char_grid module. */ -CharGrid */*notnull*/ sp_cmd_char_grid_get(struct CharGridCommand */*notnull*/ command); +CharGrid */*notnull*/ sp_cmd_char_grid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** - * Reads the origin field of the [CharGridCommand]. + * Reads the origin field of the [`CharGridCommand`]. + * + * This function is part of the sp_cmd_char_grid module. */ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1583,13 +1617,18 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ g size_t origin_y); /** - * Moves the provided [CharGrid] to be contained in the [CharGridCommand]. + * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the sp_cmd_char_grid module. */ -void sp_cmd_char_grid_set(struct CharGridCommand */*notnull*/ command, - CharGrid */*notnull*/ grid); +void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, + CharGrid */*notnull*/ value); /** - * Overwrites the origin field of the [CharGridCommand]. + * Overwrites the origin field of the [`CharGridCommand`]. + * + * This function is part of the sp_cmd_char_grid module. */ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, @@ -1645,19 +1684,19 @@ void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); /** - * Show text on the screen. + * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. * - * The text is sent in the form of a 2D grid of [CP-437] encoded characters. - * For sending UTF-8 encoded characters, see [servicepoint::CharGridCommand]. + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 + * This function is part of the sp_cmd_cp437_grid module. */ -Cp437Grid *sp_cmd_cp437_grid_get(struct Cp437GridCommand */*notnull*/ command); +Cp437Grid */*notnull*/ sp_cmd_cp437_grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** - * Gets the origin field of the [Cp437GridCommand]. + * Reads the origin field of the [`Cp437GridCommand`]. * - * Rust equivalent: `cp437_command.origin` + * This function is part of the sp_cmd_cp437_grid module. */ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1677,17 +1716,18 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull* size_t origin_y); /** - * Moves the provided bitmap into the provided command. + * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This drops the previously contained [Cp437Grid]. + * This function is part of the sp_cmd_cp437_grid module. */ -void sp_cmd_cp437_grid_set(struct Cp437GridCommand */*notnull*/ command, - Cp437Grid */*notnull*/ grid); +void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, + Cp437Grid */*notnull*/ value); /** - * Sets the origin field of the [Cp437GridCommand]. + * Overwrites the origin field of the [`Cp437GridCommand`]. * - * Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` + * This function is part of the sp_cmd_cp437_grid module. */ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, size_t origin_x, diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index f4c9a7e..68f6408 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,12 +1,22 @@ use crate::{ + commands::wrap_origin_accessors, macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_cmd_bitmap; +wrap_clone!(sp_cmd_bitmap::BitmapCommand); +wrap_free!(sp_cmd_bitmap::BitmapCommand); +wrap_fields!(sp_cmd_bitmap::BitmapCommand; + prop bitmap: Bitmap { mut get(); move set(value); }; + prop compression: CompressionCode { get(); set(value); }; +); + +wrap_origin_accessors!(sp_cmd_bitmap::BitmapCommand); + +wrap_functions!(sp_cmd_bitmap; /// Sets a window of pixels to the specified values. /// /// The passed [Bitmap] gets consumed. @@ -43,74 +53,4 @@ wrap_functions!(sp_cmd_bitmap; ) -> *mut Packet { heap_move_ok(unsafe { heap_remove(command) }.try_into()) } - ); - -wrap_clone!(sp_cmd_bitmap::BitmapCommand); -wrap_free!(sp_cmd_bitmap::BitmapCommand); - -wrap_fields!( - sp_cmd_bitmap::BitmapCommand; - //prop bitmap: NonNull { - // get() { - // return NonNull::from(bitmap); - // }; - // set(value) { - // return unsafe { heap_remove(value) }; - // }; - //}; - prop compression: CompressionCode { - get(); - set(value); - }; -); - -/// Returns a pointer to the provided `BitmapCommand`. -/// -/// # Safety -/// -/// - The returned bitmap inherits the lifetime of the command in which it is contained. -/// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get( - mut command: NonNull, -) -> NonNull { - unsafe { NonNull::from(&mut (command.as_mut().bitmap)) } -} - -/// Moves the provided [Bitmap] to be contained in the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_set( - mut command: NonNull, - bitmap: NonNull, -) { - unsafe { - command.as_mut().bitmap = heap_remove(bitmap); - } -} - -/// Reads the origin field of the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; - } -} - -/// Overwrites the origin field of the [BitmapCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitmap_set_origin( - mut command: NonNull, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 7dfed38..6cec313 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -8,6 +8,16 @@ use servicepoint::{ }; use std::ptr::NonNull; +wrap_clone!(sp_cmd_bitvec::BitVecCommand); +wrap_free!(sp_cmd_bitvec::BitVecCommand); + +wrap_fields!(sp_cmd_bitvec::BitVecCommand; + prop bitvec: DisplayBitVec { mut get(); move set(value); }; + prop offset: Offset { get(); set(value); }; + prop operation: BinaryOperation { get(); set(value); }; + prop compression: CompressionCode { get(); set(value); }; +); + wrap_functions!(sp_cmd_bitvec; /// Set pixel data starting at the pixel offset on screen. @@ -46,41 +56,3 @@ wrap_functions!(sp_cmd_bitvec; } ); - -wrap_clone!(sp_cmd_bitvec::BitVecCommand); -wrap_free!(sp_cmd_bitvec::BitVecCommand); - -wrap_fields!( - sp_cmd_bitvec::BitVecCommand; - prop offset: Offset { - get(); - set(value); - }; - prop operation: BinaryOperation { - get(); - set(value); - }; - prop compression: CompressionCode { - get(); - set(value); - }; -); - -/// Returns a pointer to the [BitVec] contained in the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_get( - mut command: NonNull, -) -> *mut DisplayBitVec { - unsafe { &mut command.as_mut().bitvec } -} - -/// Moves the provided [BitVec] to be contained in the [BitVecCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_bitvec_set( - mut command: NonNull, - bitvec: NonNull, -) { - unsafe { - command.as_mut().bitvec = heap_remove(bitvec); - } -} diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index b4e6e23..cc26b77 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,10 +1,20 @@ use crate::{ - macros::{wrap_clone, wrap_free, wrap_functions}, + commands::wrap_origin_accessors, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet}; use std::ptr::NonNull; +wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand); +wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand); + +wrap_fields!(sp_cmd_brightness_grid::BrightnessGridCommand; + prop grid: BrightnessGrid { mut get(); move set(grid); }; +); + +wrap_origin_accessors!(sp_cmd_brightness_grid::BrightnessGridCommand); + wrap_functions!(sp_cmd_brightness_grid; /// Set the brightness of individual tiles in a rectangular area of the display. @@ -41,51 +51,3 @@ wrap_functions!(sp_cmd_brightness_grid; } ); - -wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand); -wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand); - -/// Moves the provided [BrightnessGrid] to be contained in the [BrightnessGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_set( - mut command: NonNull, - grid: NonNull, -) { - unsafe { - command.as_mut().grid = heap_remove(grid); - } -} - -/// Returns a pointer to the [BrightnessGrid] contained in the [BrightnessGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_get( - mut command: NonNull, -) -> *mut BrightnessGrid { - unsafe { &mut command.as_mut().grid } -} - -/// Overwrites the origin field of the [BrightnessGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; - } -} - -/// Reads the origin field of the [BrightnessGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_brightness_grid_set_origin( - mut command: NonNull, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 0ef90c0..fe97dc0 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,10 +1,20 @@ use crate::{ - macros::{wrap_clone, wrap_free, wrap_functions}, + commands::wrap_origin_accessors, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Origin, Packet}; use std::ptr::NonNull; +wrap_clone!(sp_cmd_char_grid::CharGridCommand); +wrap_free!(sp_cmd_char_grid::CharGridCommand); + +wrap_fields!(sp_cmd_char_grid::CharGridCommand; + prop grid: CharGrid { mut get(); move set(grid); }; +); + +wrap_origin_accessors!(sp_cmd_char_grid::CharGridCommand); + wrap_functions!(sp_cmd_char_grid; /// Show UTF-8 encoded text on the screen. @@ -41,51 +51,3 @@ wrap_functions!(sp_cmd_char_grid; } ); - -wrap_clone!(sp_cmd_char_grid::CharGridCommand); -wrap_free!(sp_cmd_char_grid::CharGridCommand); - -/// Moves the provided [CharGrid] to be contained in the [CharGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_set( - mut command: NonNull, - grid: NonNull, -) { - unsafe { - command.as_mut().grid = heap_remove(grid); - } -} - -/// Returns a pointer to the [CharGrid] contained in the [CharGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_get( - mut command: NonNull, -) -> NonNull { - unsafe { NonNull::from(&mut command.as_mut().grid) } -} - -/// Reads the origin field of the [CharGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; - } -} - -/// Overwrites the origin field of the [CharGridCommand]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_char_grid_set_origin( - mut command: NonNull, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index da8380a..25294bb 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,10 +1,20 @@ use crate::{ - macros::{wrap_clone, wrap_free, wrap_functions}, + commands::wrap_origin_accessors, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; use std::ptr::NonNull; +wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); +wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); + +wrap_fields!(sp_cmd_cp437_grid::Cp437GridCommand; + prop grid: Cp437Grid { mut get(); move set(grid); }; +); + +wrap_origin_accessors!(sp_cmd_cp437_grid::Cp437GridCommand); + wrap_functions!(sp_cmd_cp437_grid; /// Show text on the screen. @@ -41,62 +51,3 @@ wrap_functions!(sp_cmd_cp437_grid; } ); - -wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); -wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); - -/// Moves the provided bitmap into the provided command. -/// -/// This drops the previously contained [Cp437Grid]. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_set( - mut command: NonNull, - grid: NonNull, -) { - unsafe { - command.as_mut().grid = heap_remove(grid); - } -} - -/// Show text on the screen. -/// -/// The text is sent in the form of a 2D grid of [CP-437] encoded characters. -/// For sending UTF-8 encoded characters, see [servicepoint::CharGridCommand]. -/// -/// [CP-437]: https://en.wikipedia.org/wiki/Code_page_437 -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_get( - mut command: NonNull, -) -> *mut Cp437Grid { - unsafe { &mut command.as_mut().grid } -} - -/// Gets the origin field of the [Cp437GridCommand]. -/// -/// Rust equivalent: `cp437_command.origin` -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_get_origin( - command: NonNull, - origin_x: NonNull, - origin_y: NonNull, -) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; - } -} - -/// Sets the origin field of the [Cp437GridCommand]. -/// -/// Rust equivalent: `cp437_command.origin = Origin::new(origin_x, origin_y)` -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_cp437_grid_set_origin( - mut command: NonNull, - origin_x: usize, - origin_y: usize, -) { - unsafe { - command.as_mut().origin = Origin::new(origin_x, origin_y); - } -} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 3cbb99a..a6fa74d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -13,3 +13,35 @@ pub use brightness_grid_command::*; pub use char_grid_command::*; pub use cp437_grid_command::*; pub use generic_command::*; + +macro_rules! wrap_origin_accessors { + ( $prefix:ident :: $object_type:ty ) => { + $crate::macros::wrap_functions!($prefix; + #[doc = concat!(" Reads the origin field of the [`", stringify!($object_type), "`].")] + fn get_origin( + command: NonNull<$object_type>, + origin_x: NonNull, + origin_y: NonNull, + ) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } + } + + #[doc = concat!(" Overwrites the origin field of the [`", stringify!($object_type), "`].")] + fn set_origin( + command: NonNull<$object_type>, + origin_x: usize, + origin_y: usize, + ) { + unsafe { + $crate::macros::nonnull_as_mut!(command).origin = ::servicepoint::Origin::new(origin_x, origin_y); + } + } + ); + }; +} + +pub(crate) use wrap_origin_accessors; diff --git a/src/macros.rs b/src/macros.rs index 8d4ba47..4157f28 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -28,7 +28,7 @@ macro_rules! nonnull_as_ref { macro_rules! nonnull_as_mut { ($ident:ident) => { - &mut *$ident.as_ptr() + (&mut *$ident.as_ptr()) }; } @@ -84,21 +84,37 @@ macro_rules! wrap_fields { ( $prefix:ident :: $object_type:ty; $( - prop $prop_name:ident : $prop_type:ty { - $( - get() $({ - $(#[$get_meta:meta])* - $(return $get_expr:expr;)? - })?; - )? - $( - set($value:ident) - $({ - $(#[$set_meta:meta])* - $(return $set_expr:expr;)? - })?; - )? - }; + prop $prop_name:ident : $prop_type:ty { + $( + get() $({ + $(#[$get_meta:meta])* + $(return $get_expr:expr;)? + })?; + )? + + $( + mut get() $({ + $(#[$get_mut_meta:meta])* + $(return $get_mut_expr:expr;)? + })?; + )? + + $( + set($value:ident) + $({ + $(#[$set_meta:meta])* + $(return $set_expr:expr;)? + })?; + )? + + $( + move set( $set_move_value:ident) + $({ + $(#[$set_move_meta:meta])* + $(return $set_move_expr:expr;)? + })?; + )? + }; )+ ) => { paste::paste! { @@ -106,16 +122,15 @@ macro_rules! wrap_fields { $( $( #[doc = concat!(" Gets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] + "` of the [`servicepoint::", stringify!($object_type),"`].")] $($( #[doc = ""] #[$get_meta] )*)? fn []( - instance: NonNull<$object_type> + instance: ::core::ptr::NonNull<$object_type> ) -> $prop_type { - let instance = unsafe { $crate::macros::nonnull_as_ref!(instance) }; - let $prop_name = instance.$prop_name; + let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name }; $($( let $prop_name = $get_expr; )?)? @@ -123,6 +138,27 @@ macro_rules! wrap_fields { } )? + $( + #[doc = concat!(" Gets a reference to the field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] + $($( + #[doc = ""] + #[$get_mut_meta] + )*)? + #[doc = ""] + #[doc = " - The returned reference inherits the lifetime of object in which it is contained."] + #[doc = " - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command."] + fn []( + instance: ::core::ptr::NonNull<$object_type> + ) -> ::core::ptr::NonNull<$prop_type> { + let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name }; + $($( + let $prop_name = $get_mut_expr; + )?)? + return ::core::ptr::NonNull::from($prop_name); + } + )? + $( #[doc = concat!(" Sets the value of field `", stringify!($prop_name), "` of the [`servicepoint::",stringify!($object_type),"`].")] @@ -131,7 +167,7 @@ macro_rules! wrap_fields { #[$set_meta] )*)? fn []( - instance: NonNull<$object_type>, + instance: ::core::ptr::NonNull<$object_type>, value: $prop_type, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; @@ -142,6 +178,29 @@ macro_rules! wrap_fields { instance.$prop_name = value; } )? + + $( + #[doc = concat!(" Sets the value of field `", stringify!($prop_name), + "` of the [`servicepoint::",stringify!($object_type),"`].")] + #[doc = concat!(" The provided value is moved into the instance, ", + "potentially invalidating previously taken references.")] + $($( + #[doc = ""] + #[$set_move_meta] + )*)? + fn []( + instance: ::core::ptr::NonNull<$object_type>, + value: NonNull<$prop_type>, + ) { + let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; + let value = unsafe { $crate::mem::heap_remove(value) }; + $($( + let $set_move_value = value; + let value = $set_move_expr; + )?)? + instance.$prop_name = value; + } + )? )+ ); } From b79a2534fc272e0889b56c13582e7ba3988378fb Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Wed, 18 Jun 2025 23:14:51 +0200 Subject: [PATCH 49/76] the rest of the functions wrapped with macro --- include/servicepoint.h | 63 ++++- src/commands/cc_only_commands.rs | 42 ++- src/commands/generic_command.rs | 431 ++++++++++++++++--------------- src/containers/byte_slice.rs | 2 +- src/lib.rs | 15 +- src/packet.rs | 163 ++++++------ src/udp.rs | 254 +++++++++--------- 7 files changed, 485 insertions(+), 485 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 1e0182e..68d4c57 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -633,12 +633,6 @@ typedef struct Header { extern "C" { #endif // __cplusplus -/** - * Call this function at the beginning of main to enable rust logging controlled by the - * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). - */ -void init_env_logger(void); - /** *Clones a [`Bitmap`] instance. * @@ -1655,7 +1649,7 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); * * Does not affect brightness. * - * Returns: a new [ClearCommand] instance. + * Returns: a new [`ClearCommand`] instance. * * This function is part of the sp_cmd_clear module. */ @@ -1752,7 +1746,7 @@ void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance); /** * A yet-to-be-tested command. * - * Returns: a new [FadeOutCommand] instance. + * Returns: a new [`FadeOutCommand`] instance. * * This function is part of the sp_cmd_fade_out module. */ @@ -1762,6 +1756,8 @@ struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); * Clones an [SPCommand] instance. * * returns: a new [SPCommand] instance. + * + * This function is part of the sp_cmd_generic module. */ struct Command sp_cmd_generic_clone(struct Command command); @@ -1776,6 +1772,8 @@ struct Command sp_cmd_generic_clone(struct Command command); * SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); * sp_command_free(c); * ``` + * + * This function is part of the sp_cmd_generic module. */ void sp_cmd_generic_free(struct Command command); @@ -1784,6 +1782,8 @@ void sp_cmd_generic_free(struct Command command); * The [SPCommand] gets consumed. * * Returns tag [CommandTag::Invalid] in case of an error. + * + * This function is part of the sp_cmd_generic module. */ struct Packet *sp_cmd_generic_into_packet(struct Command command); @@ -1793,6 +1793,8 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); * The packet is dropped in the process. * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. + * + * This function is part of the sp_cmd_generic module. */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); @@ -1808,7 +1810,7 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); * * Please do not send this in your normal program flow. * - * Returns: a new [HardResetCommand] instance. + * Returns: a new [`HardResetCommand`] instance. * * This function is part of the sp_cmd_hard_reset module. */ @@ -1942,6 +1944,14 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, */ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); +/** + * Call this function at the beginning of main to enable rust logging controlled by the + * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). + * + * This function is part of the sp_envlogger module. + */ +void sp_envlogger_init(void); + /** *Clones a [`Packet`] instance. * @@ -1960,6 +1970,8 @@ void sp_packet_free(struct Packet */*notnull*/ instance); * Creates a raw [Packet] from parts. * * returns: new instance. Will never return null. + * + * This function is part of the sp_packet module. */ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); @@ -1972,11 +1984,14 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); /** - * Returns a pointer to the header field of the provided packet. + * Gets a reference to the field `header` of the [`servicepoint::Packet`]. * - * The returned header can be changed and will be valid for the lifetime of the packet. + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the sp_packet module. */ -struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ packet); +struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); /** * Returns a pointer to the current payload of the provided packet. @@ -1984,6 +1999,8 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ p * Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. * * The returned memory can be changed and will be valid until a new payload is set. + * + * This function is part of the sp_packet module. */ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); @@ -1993,9 +2010,11 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); * # Panics * * - if the buffer is not big enough to hold header+payload. + * + * This function is part of the sp_packet module. */ -void sp_packet_serialize_to(struct Packet */*notnull*/ packet, - struct ByteSlice buffer); +size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, + struct ByteSlice buffer); /** * Sets the value of field `header` of the [`servicepoint::Packet`]. @@ -2009,6 +2028,8 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, * Sets the payload of the provided packet to the provided data. * * This makes previous payload pointers invalid. + * + * This function is part of the sp_packet module. */ void sp_packet_set_payload(struct Packet */*notnull*/ packet, struct ByteSlice data); @@ -2017,6 +2038,8 @@ void sp_packet_set_payload(struct Packet */*notnull*/ packet, * Tries to load a [Packet] from the passed array with the specified length. * * returns: NULL in case of an error, pointer to the allocated packet otherwise + * + * This function is part of the sp_packet module. */ struct Packet *sp_packet_try_load(struct ByteSlice data); @@ -2024,6 +2047,8 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * Converts u16 into [CommandCode]. * * If the provided value is not valid, false is returned and result is not changed. + * + * This function is part of the sp module. */ bool sp_u16_to_command_code(uint16_t code, CommandCode *result); @@ -2047,6 +2072,8 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * if (connection != NULL) * sp_udp_send_command(connection, sp_command_clear()); * ``` + * + * This function is part of the sp_udp module. */ struct UdpSocket *sp_udp_open(char */*notnull*/ host); @@ -2062,6 +2089,8 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * if (connection != NULL) * sp_udp_send_command(connection, sp_command_clear()); * ``` + * + * This function is part of the sp_udp module. */ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint8_t ip2, @@ -2081,6 +2110,8 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * ```C * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` + * + * This function is part of the sp_udp module. */ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, struct Command command); @@ -2095,6 +2126,8 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * ```C * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` + * + * This function is part of the sp_udp module. */ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); @@ -2105,6 +2138,8 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * The passed `packet` gets consumed. * * returns: true in case of success + * + * This function is part of the sp_udp module. */ bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, struct Packet */*notnull*/ packet); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 234b781..f4162a0 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -5,39 +5,33 @@ use crate::{ use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; -wrap_functions!(sp_cmd_clear; +macro_rules! wrap_cc_only { + ($prefix:ident :: $typ:ident ; $(#[$meta:meta])*) => { + wrap_functions!($prefix; + $(#[$meta])* + /// + #[doc = concat!(" Returns: a new [`",stringify!($typ),"`] instance.")] + fn new() -> NonNull<$typ> { + heap_move_nonnull($typ) + } + ); + + wrap_free!($prefix :: $typ); + }; +} + +wrap_cc_only!(sp_cmd_clear::ClearCommand; /// Set all pixels to the off state. /// /// Does not affect brightness. - /// - /// Returns: a new [ClearCommand] instance. - fn new() -> NonNull { - heap_move_nonnull(ClearCommand) - } ); -wrap_free!(sp_cmd_clear::ClearCommand); - -wrap_functions!(sp_cmd_hard_reset; +wrap_cc_only!(sp_cmd_hard_reset::HardResetCommand; /// Kills the udp daemon on the display, which usually results in a restart. /// /// Please do not send this in your normal program flow. - /// - /// Returns: a new [HardResetCommand] instance. - fn new() -> NonNull { - heap_move_nonnull(HardResetCommand) - } ); -wrap_free!(sp_cmd_hard_reset::HardResetCommand); - -wrap_functions!(sp_cmd_fade_out; +wrap_cc_only!(sp_cmd_fade_out::FadeOutCommand; /// A yet-to-be-tested command. - /// - /// Returns: a new [FadeOutCommand] instance. - fn new() -> NonNull { - heap_move_nonnull(FadeOutCommand) - } ); - -wrap_free!(sp_cmd_fade_out::FadeOutCommand); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 840900f..9734d30 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,6 +1,9 @@ -use crate::mem::{ - heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, - heap_remove, +use crate::{ + macros::wrap_functions, + mem::{ + heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, + heap_remove, + }, }; use servicepoint::{ BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand, @@ -66,233 +69,233 @@ impl SPCommand { }; } -/// Tries to turn a [Packet] into a [SPCommand]. -/// -/// The packet is dropped in the process. -/// -/// Returns: pointer to new [SPCommand] instance or NULL if parsing failed. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( - packet: NonNull, -) -> SPCommand { - let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; - servicepoint::TypedCommand::try_from(packet) - .map(|value| match value { - TypedCommand::Clear(clear) => SPCommand { - tag: CommandTag::Clear, - data: CommandUnion { - clear: heap_move_nonnull(clear), - }, - }, - TypedCommand::CharGrid(char_grid) => SPCommand { - tag: CommandTag::CharGrid, - data: CommandUnion { - char_grid: heap_move_nonnull(char_grid), - }, - }, - TypedCommand::Cp437Grid(cp437_grid) => SPCommand { - tag: CommandTag::Cp437Grid, - data: CommandUnion { - cp437_grid: heap_move_nonnull(cp437_grid), - }, - }, - TypedCommand::Bitmap(bitmap) => SPCommand { - tag: CommandTag::Bitmap, - data: CommandUnion { - bitmap: heap_move_nonnull(bitmap), - }, - }, - TypedCommand::Brightness(global_brightness) => SPCommand { - tag: CommandTag::GlobalBrightness, - data: CommandUnion { - global_brightness: heap_move_nonnull(global_brightness), - }, - }, - TypedCommand::BrightnessGrid(brightness_grid) => SPCommand { - tag: CommandTag::BrightnessGrid, - data: CommandUnion { - brightness_grid: heap_move_nonnull(brightness_grid), - }, - }, - TypedCommand::BitVec(bitvec) => SPCommand { - tag: CommandTag::BitVec, - data: CommandUnion { - bitvec: heap_move_nonnull(bitvec), - }, - }, - TypedCommand::HardReset(hard_reset) => SPCommand { - tag: CommandTag::HardReset, - data: CommandUnion { - hard_reset: heap_move_nonnull(hard_reset), - }, - }, - TypedCommand::FadeOut(fade_out) => SPCommand { - tag: CommandTag::FadeOut, - data: CommandUnion { - fade_out: heap_move_nonnull(fade_out), - }, - }, - #[allow(deprecated)] - TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand { - tag: CommandTag::BitmapLegacy, - data: CommandUnion { - bitmap_legacy: heap_move_nonnull(bitmap_legacy), - }, - }, - }) - .unwrap_or_else(move |_| SPCommand { - tag: CommandTag::Invalid, - data: CommandUnion { null: null_mut() }, - }) -} +wrap_functions!(sp_cmd_generic; -/// Clones an [SPCommand] instance. -/// -/// returns: a new [SPCommand] instance. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { - unsafe { - match command.tag { - CommandTag::Clear => SPCommand { - tag: CommandTag::Clear, - data: CommandUnion { - clear: heap_clone(command.data.clear), + /// Tries to turn a [Packet] into a [SPCommand]. + /// + /// The packet is dropped in the process. + /// + /// Returns: pointer to new [SPCommand] instance or NULL if parsing failed. + fn try_from_packet( + packet: NonNull, + ) -> SPCommand { + let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; + servicepoint::TypedCommand::try_from(packet) + .map(|value| match value { + TypedCommand::Clear(clear) => SPCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_move_nonnull(clear), + }, }, - }, - CommandTag::CharGrid => SPCommand { - tag: CommandTag::CharGrid, - data: CommandUnion { - char_grid: heap_clone(command.data.char_grid), + TypedCommand::CharGrid(char_grid) => SPCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_move_nonnull(char_grid), + }, }, - }, - CommandTag::Cp437Grid => SPCommand { - tag: CommandTag::Cp437Grid, - data: CommandUnion { - cp437_grid: heap_clone(command.data.cp437_grid), + TypedCommand::Cp437Grid(cp437_grid) => SPCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_move_nonnull(cp437_grid), + }, }, - }, - CommandTag::Bitmap => SPCommand { - tag: CommandTag::Bitmap, - data: CommandUnion { - bitmap: heap_clone(command.data.bitmap), + TypedCommand::Bitmap(bitmap) => SPCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_move_nonnull(bitmap), + }, }, - }, - CommandTag::GlobalBrightness => SPCommand { - tag: CommandTag::GlobalBrightness, - data: CommandUnion { - global_brightness: heap_clone( - command.data.global_brightness, - ), + TypedCommand::Brightness(global_brightness) => SPCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_move_nonnull(global_brightness), + }, }, - }, - CommandTag::BrightnessGrid => SPCommand { - tag: CommandTag::BrightnessGrid, - data: CommandUnion { - brightness_grid: heap_clone(command.data.brightness_grid), + TypedCommand::BrightnessGrid(brightness_grid) => SPCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_move_nonnull(brightness_grid), + }, }, - }, - CommandTag::BitVec => SPCommand { - tag: CommandTag::BitVec, - data: CommandUnion { - bitvec: heap_clone(command.data.bitvec), + TypedCommand::BitVec(bitvec) => SPCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bitvec: heap_move_nonnull(bitvec), + }, }, - }, - CommandTag::HardReset => SPCommand { - tag: CommandTag::HardReset, - data: CommandUnion { - hard_reset: heap_clone(command.data.hard_reset), + TypedCommand::HardReset(hard_reset) => SPCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_move_nonnull(hard_reset), + }, }, - }, - CommandTag::FadeOut => SPCommand { - tag: CommandTag::FadeOut, - data: CommandUnion { - fade_out: heap_clone(command.data.fade_out), + TypedCommand::FadeOut(fade_out) => SPCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_move_nonnull(fade_out), + }, }, - }, - #[allow(deprecated)] - CommandTag::BitmapLegacy => SPCommand { - tag: CommandTag::BitmapLegacy, - data: CommandUnion { - bitmap_legacy: heap_clone(command.data.bitmap_legacy), + #[allow(deprecated)] + TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_move_nonnull(bitmap_legacy), + }, }, - }, - CommandTag::Invalid => SPCommand::INVALID, - } + }) + .unwrap_or_else(move |_| SPCommand { + tag: CommandTag::Invalid, + data: CommandUnion { null: null_mut() }, + }) } -} -/// Deallocates an [SPCommand]. -/// -/// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. -/// -/// # Examples -/// -/// ```C -/// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); -/// sp_command_free(c); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) { - unsafe { - match command.tag { - CommandTag::Invalid => (), - CommandTag::Bitmap => heap_drop(command.data.bitmap), - CommandTag::BitVec => heap_drop(command.data.bitvec), - CommandTag::BrightnessGrid => { - heap_drop(command.data.brightness_grid) + /// Clones an [SPCommand] instance. + /// + /// returns: a new [SPCommand] instance. + fn clone(command: SPCommand) -> SPCommand { + unsafe { + match command.tag { + CommandTag::Clear => SPCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_clone(command.data.clear), + }, + }, + CommandTag::CharGrid => SPCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_clone(command.data.char_grid), + }, + }, + CommandTag::Cp437Grid => SPCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_clone(command.data.cp437_grid), + }, + }, + CommandTag::Bitmap => SPCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_clone(command.data.bitmap), + }, + }, + CommandTag::GlobalBrightness => SPCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_clone( + command.data.global_brightness, + ), + }, + }, + CommandTag::BrightnessGrid => SPCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_clone(command.data.brightness_grid), + }, + }, + CommandTag::BitVec => SPCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bitvec: heap_clone(command.data.bitvec), + }, + }, + CommandTag::HardReset => SPCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_clone(command.data.hard_reset), + }, + }, + CommandTag::FadeOut => SPCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_clone(command.data.fade_out), + }, + }, + #[allow(deprecated)] + CommandTag::BitmapLegacy => SPCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_clone(command.data.bitmap_legacy), + }, + }, + CommandTag::Invalid => SPCommand::INVALID, } - CommandTag::CharGrid => heap_drop(command.data.char_grid), - CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid), - CommandTag::GlobalBrightness => { - heap_drop(command.data.global_brightness) - } - CommandTag::Clear => heap_drop(command.data.clear), - CommandTag::HardReset => heap_drop(command.data.hard_reset), - CommandTag::FadeOut => heap_drop(command.data.fade_out), - CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy), } } -} -/// Tries to turn a [SPCommand] into a [Packet]. -/// The [SPCommand] gets consumed. -/// -/// Returns tag [CommandTag::Invalid] in case of an error. -#[no_mangle] -pub unsafe extern "C" fn sp_cmd_generic_into_packet( - command: SPCommand, -) -> *mut Packet { - match command.tag { - CommandTag::Invalid => null_mut(), - CommandTag::Bitmap => { - heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) - } - CommandTag::BitVec => { - heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() }) - } - CommandTag::BrightnessGrid => heap_move_ok(unsafe { - heap_remove(command.data.brightness_grid).try_into() - }), - CommandTag::CharGrid => heap_move_ok(unsafe { - heap_remove(command.data.char_grid).try_into() - }), - CommandTag::Cp437Grid => heap_move_ok(unsafe { - heap_remove(command.data.cp437_grid).try_into() - }), - CommandTag::GlobalBrightness => heap_move(unsafe { - heap_remove(command.data.global_brightness).into() - }), - CommandTag::Clear => { - heap_move(unsafe { heap_remove(command.data.clear).into() }) - } - CommandTag::HardReset => { - heap_move(unsafe { heap_remove(command.data.hard_reset).into() }) - } - CommandTag::FadeOut => { - heap_move(unsafe { heap_remove(command.data.fade_out).into() }) - } - CommandTag::BitmapLegacy => { - heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) + /// Deallocates an [SPCommand]. + /// + /// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. + /// + /// # Examples + /// + /// ```C + /// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); + /// sp_command_free(c); + /// ``` + fn free(command: SPCommand) { + unsafe { + match command.tag { + CommandTag::Invalid => (), + CommandTag::Bitmap => heap_drop(command.data.bitmap), + CommandTag::BitVec => heap_drop(command.data.bitvec), + CommandTag::BrightnessGrid => { + heap_drop(command.data.brightness_grid) + } + CommandTag::CharGrid => heap_drop(command.data.char_grid), + CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid), + CommandTag::GlobalBrightness => { + heap_drop(command.data.global_brightness) + } + CommandTag::Clear => heap_drop(command.data.clear), + CommandTag::HardReset => heap_drop(command.data.hard_reset), + CommandTag::FadeOut => heap_drop(command.data.fade_out), + CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy), + } } } -} + + /// Tries to turn a [SPCommand] into a [Packet]. + /// The [SPCommand] gets consumed. + /// + /// Returns tag [CommandTag::Invalid] in case of an error. + fn into_packet( + command: SPCommand, + ) -> *mut Packet { + match command.tag { + CommandTag::Invalid => null_mut(), + CommandTag::Bitmap => { + heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) + } + CommandTag::BitVec => { + heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() }) + } + CommandTag::BrightnessGrid => heap_move_ok(unsafe { + heap_remove(command.data.brightness_grid).try_into() + }), + CommandTag::CharGrid => heap_move_ok(unsafe { + heap_remove(command.data.char_grid).try_into() + }), + CommandTag::Cp437Grid => heap_move_ok(unsafe { + heap_remove(command.data.cp437_grid).try_into() + }), + CommandTag::GlobalBrightness => heap_move(unsafe { + heap_remove(command.data.global_brightness).into() + }), + CommandTag::Clear => { + heap_move(unsafe { heap_remove(command.data.clear).into() }) + } + CommandTag::HardReset => { + heap_move(unsafe { heap_remove(command.data.hard_reset).into() }) + } + CommandTag::FadeOut => { + heap_move(unsafe { heap_remove(command.data.fade_out).into() }) + } + CommandTag::BitmapLegacy => { + heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) + } + } + } + +); diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 0642cfc..10ee22f 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -35,7 +35,7 @@ impl ByteSlice { unsafe { std::slice::from_raw_parts(self.start, self.length) } } - pub(crate) unsafe fn as_slice_mut(&mut self) -> &mut [u8] { + pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] { unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } diff --git a/src/lib.rs b/src/lib.rs index e08fbd7..344a1a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,10 +47,13 @@ pub struct DisplayBitVec; #[cfg(feature = "env_logger")] mod feature_env_logger { - /// Call this function at the beginning of main to enable rust logging controlled by the - /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). - #[no_mangle] - pub unsafe extern "C" fn init_env_logger() { - env_logger::init(); - } + use crate::macros::wrap_functions; + + wrap_functions!(sp_envlogger; + /// Call this function at the beginning of main to enable rust logging controlled by the + /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). + fn init() { + env_logger::init(); + } + ); } diff --git a/src/packet.rs b/src/packet.rs index af49fc8..4278fe0 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,95 +1,72 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_fields, wrap_free}, + macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -/// Tries to load a [Packet] from the passed array with the specified length. -/// -/// returns: NULL in case of an error, pointer to the allocated packet otherwise -#[no_mangle] -pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet { - let data = unsafe { data.as_slice() }; - heap_move_ok(servicepoint::Packet::try_from(data)) -} +wrap_functions!(sp_packet; -/// Creates a raw [Packet] from parts. -/// -/// returns: new instance. Will never return null. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_from_parts( - header: Header, - payload: ByteSlice, -) -> NonNull { - let payload = if payload == ByteSlice::INVALID { - None - } else { - Some(Vec::from(unsafe { payload.as_slice() })) - }; - - heap_move_nonnull(Packet { header, payload }) -} - -/// Returns a pointer to the header field of the provided packet. -/// -/// The returned header can be changed and will be valid for the lifetime of the packet. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_get_header_mut( - packet: NonNull, -) -> NonNull
{ - NonNull::from(unsafe { &mut (*packet.as_ptr()).header }) -} - -/// Returns a pointer to the current payload of the provided packet. -/// -/// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. -/// -/// The returned memory can be changed and will be valid until a new payload is set. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_get_payload( - packet: NonNull, -) -> ByteSlice { - unsafe { - match &mut (*packet.as_ptr()).payload { - None => ByteSlice::INVALID, - Some(payload) => ByteSlice::from_slice(payload), - } + /// Tries to load a [Packet] from the passed array with the specified length. + /// + /// returns: NULL in case of an error, pointer to the allocated packet otherwise + fn try_load(data: ByteSlice) -> *mut Packet { + let data = unsafe { data.as_slice() }; + heap_move_ok(servicepoint::Packet::try_from(data)) } -} -/// Sets the payload of the provided packet to the provided data. -/// -/// This makes previous payload pointers invalid. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_set_payload( - packet: NonNull, - data: ByteSlice, -) { - unsafe { - (*packet.as_ptr()).payload = if data == ByteSlice::INVALID { + /// Creates a raw [Packet] from parts. + /// + /// returns: new instance. Will never return null. + fn from_parts(header: Header, payload: ByteSlice) -> NonNull { + let payload = if payload == ByteSlice::INVALID { None } else { - Some(data.as_slice().to_vec()) + Some(Vec::from(unsafe { payload.as_slice() })) + }; + + heap_move_nonnull(Packet { header, payload }) + } + + /// Returns a pointer to the current payload of the provided packet. + /// + /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. + /// + /// The returned memory can be changed and will be valid until a new payload is set. + fn get_payload(packet: NonNull) -> ByteSlice { + unsafe { + match &mut (*packet.as_ptr()).payload { + None => ByteSlice::INVALID, + Some(payload) => ByteSlice::from_slice(payload), + } } } -} -/// Serialize the packet into the provided buffer. -/// -/// # Panics -/// -/// - if the buffer is not big enough to hold header+payload. -#[no_mangle] -pub unsafe extern "C" fn sp_packet_serialize_to( - packet: NonNull, - mut buffer: ByteSlice, -) { - unsafe { - packet.as_ref().serialize_to(buffer.as_slice_mut()); + /// Sets the payload of the provided packet to the provided data. + /// + /// This makes previous payload pointers invalid. + fn set_payload(packet: NonNull, data: ByteSlice) { + unsafe { + (*packet.as_ptr()).payload = if data == ByteSlice::INVALID { + None + } else { + Some(data.as_slice().to_vec()) + } + } } -} + + /// Serialize the packet into the provided buffer. + /// + /// # Panics + /// + /// - if the buffer is not big enough to hold header+payload. + fn serialize_to(packet: NonNull, buffer: ByteSlice) -> usize { + unsafe { + packet.as_ref().serialize_to(buffer.as_slice_mut()).unwrap_or(0) + } + } +); wrap_clone!(sp_packet::Packet); wrap_free!(sp_packet::Packet); @@ -98,25 +75,29 @@ wrap_fields!( sp_packet::Packet; prop header: Header { get(); + mut get(); set(value); }; ); -/// Converts u16 into [CommandCode]. -/// -/// If the provided value is not valid, false is returned and result is not changed. -#[no_mangle] -pub unsafe extern "C" fn sp_u16_to_command_code( - code: u16, - result: *mut CommandCode, -) -> bool { - match CommandCode::try_from(code) { - Ok(code) => { - unsafe { - *result = code; +wrap_functions!(sp; + + /// Converts u16 into [CommandCode]. + /// + /// If the provided value is not valid, false is returned and result is not changed. + fn u16_to_command_code( + code: u16, + result: *mut CommandCode, + ) -> bool { + match CommandCode::try_from(code) { + Ok(code) => { + unsafe { + *result = code; + } + true } - true + Err(_) => false, } - Err(_) => false, } -} + +); diff --git a/src/udp.rs b/src/udp.rs index e18476a..cb96547 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::{ commands::{CommandTag, SPCommand}, - macros::wrap_free, + macros::{wrap_free, wrap_functions}, mem::{heap_move_ok, heap_remove}, }; use servicepoint::{Header, Packet, UdpSocketExt}; @@ -10,138 +10,122 @@ use std::{ ptr::NonNull, }; -/// Creates a new instance of [UdpSocket]. -/// -/// returns: NULL if connection fails, or connected instance -/// -/// # Examples -/// -/// ```C -/// UdpSocket connection = sp_udp_open("172.23.42.29:2342"); -/// if (connection != NULL) -/// sp_udp_send_command(connection, sp_command_clear()); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_udp_open(host: NonNull) -> *mut UdpSocket { - let host = unsafe { CStr::from_ptr(host.as_ptr()) } - .to_str() - .expect("Bad encoding"); - - heap_move_ok(UdpSocket::bind_connect(host)) -} - -/// Creates a new instance of [UdpSocket]. -/// -/// returns: NULL if connection fails, or connected instance -/// -/// # Examples -/// -/// ```C -/// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); -/// if (connection != NULL) -/// sp_udp_send_command(connection, sp_command_clear()); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_udp_open_ipv4( - ip1: u8, - ip2: u8, - ip3: u8, - ip4: u8, - port: u16, -) -> *mut UdpSocket { - let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); - heap_move_ok(UdpSocket::bind_connect(addr)) -} - -/// Sends a [Packet] to the display using the [UdpSocket]. -/// -/// The passed `packet` gets consumed. -/// -/// returns: true in case of success -#[no_mangle] -pub unsafe extern "C" fn sp_udp_send_packet( - connection: NonNull, - packet: NonNull, -) -> bool { - let packet = unsafe { heap_remove(packet) }; - unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() -} - -/// Sends a [SPCommand] to the display using the [UdpSocket]. -/// -/// The passed `command` gets consumed. -/// -/// returns: true in case of success -/// -/// # Examples -/// -/// ```C -/// sp_udp_send_command(connection, sp_command_brightness(5)); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_udp_send_command( - connection: NonNull, - command: SPCommand, -) -> bool { - unsafe { - match command.tag { - CommandTag::Invalid => return false, - CommandTag::Bitmap => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap)), - CommandTag::BitVec => connection - .as_ref() - .send_command(heap_remove(command.data.bitvec)), - CommandTag::BrightnessGrid => connection - .as_ref() - .send_command(heap_remove(command.data.brightness_grid)), - CommandTag::CharGrid => connection - .as_ref() - .send_command(heap_remove(command.data.char_grid)), - CommandTag::Cp437Grid => connection - .as_ref() - .send_command(heap_remove(command.data.cp437_grid)), - CommandTag::GlobalBrightness => connection - .as_ref() - .send_command(heap_remove(command.data.global_brightness)), - CommandTag::Clear => connection - .as_ref() - .send_command(heap_remove(command.data.clear)), - CommandTag::HardReset => connection - .as_ref() - .send_command(heap_remove(command.data.hard_reset)), - CommandTag::FadeOut => connection - .as_ref() - .send_command(heap_remove(command.data.fade_out)), - CommandTag::BitmapLegacy => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap_legacy)), - } - } - .is_some() -} - -/// Sends a [Header] to the display using the [UdpSocket]. -/// -/// returns: true in case of success -/// -/// # Examples -/// -/// ```C -/// sp_udp_send_header(connection, sp_command_brightness(5)); -/// ``` -#[no_mangle] -pub unsafe extern "C" fn sp_udp_send_header( - udp_connection: NonNull, - header: Header, -) -> bool { - let packet = Packet { - header, - payload: None, - }; - unsafe { udp_connection.as_ref() } - .send(&Vec::from(packet)) - .is_ok() -} - wrap_free!(sp_udp::UdpSocket); + +wrap_functions!(sp_udp; + + /// Creates a new instance of [UdpSocket]. + /// + /// returns: NULL if connection fails, or connected instance + /// + /// # Examples + /// + /// ```C + /// UdpSocket connection = sp_udp_open("172.23.42.29:2342"); + /// if (connection != NULL) + /// sp_udp_send_command(connection, sp_command_clear()); + /// ``` + fn open(host: NonNull) -> *mut UdpSocket { + let host = unsafe { CStr::from_ptr(host.as_ptr()) } + .to_str() + .expect("Bad encoding"); + + heap_move_ok(UdpSocket::bind_connect(host)) + } + + /// Creates a new instance of [UdpSocket]. + /// + /// returns: NULL if connection fails, or connected instance + /// + /// # Examples + /// + /// ```C + /// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + /// if (connection != NULL) + /// sp_udp_send_command(connection, sp_command_clear()); + /// ``` + fn open_ipv4(ip1: u8, ip2: u8, ip3: u8, ip4: u8, port: u16) -> *mut UdpSocket { + let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); + heap_move_ok(UdpSocket::bind_connect(addr)) + } + + /// Sends a [Packet] to the display using the [UdpSocket]. + /// + /// The passed `packet` gets consumed. + /// + /// returns: true in case of success + fn send_packet(connection: NonNull, packet: NonNull) -> bool { + let packet = unsafe { heap_remove(packet) }; + unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() + } + + /// Sends a [SPCommand] to the display using the [UdpSocket]. + /// + /// The passed `command` gets consumed. + /// + /// returns: true in case of success + /// + /// # Examples + /// + /// ```C + /// sp_udp_send_command(connection, sp_command_brightness(5)); + /// ``` + fn send_command(connection: NonNull, command: SPCommand) -> bool { + unsafe { + match command.tag { + CommandTag::Invalid => return false, + CommandTag::Bitmap => connection + .as_ref() + .send_command(heap_remove(command.data.bitmap)), + CommandTag::BitVec => connection + .as_ref() + .send_command(heap_remove(command.data.bitvec)), + CommandTag::BrightnessGrid => connection + .as_ref() + .send_command(heap_remove(command.data.brightness_grid)), + CommandTag::CharGrid => connection + .as_ref() + .send_command(heap_remove(command.data.char_grid)), + CommandTag::Cp437Grid => connection + .as_ref() + .send_command(heap_remove(command.data.cp437_grid)), + CommandTag::GlobalBrightness => connection + .as_ref() + .send_command(heap_remove(command.data.global_brightness)), + CommandTag::Clear => connection + .as_ref() + .send_command(heap_remove(command.data.clear)), + CommandTag::HardReset => connection + .as_ref() + .send_command(heap_remove(command.data.hard_reset)), + CommandTag::FadeOut => connection + .as_ref() + .send_command(heap_remove(command.data.fade_out)), + CommandTag::BitmapLegacy => connection + .as_ref() + .send_command(heap_remove(command.data.bitmap_legacy)), + } + } + .is_some() + } + + /// Sends a [Header] to the display using the [UdpSocket]. + /// + /// returns: true in case of success + /// + /// # Examples + /// + /// ```C + /// sp_udp_send_header(connection, sp_command_brightness(5)); + /// ``` + fn send_header(udp_connection: NonNull, header: Header) -> bool { + let packet = Packet { + header, + payload: None, + }; + unsafe { udp_connection.as_ref() } + .send(&Vec::from(packet)) + .is_ok() + } + +); From 18f0be072a92d774ac02a170e32910ab0fb12ae6 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 19 Jun 2025 18:32:07 +0200 Subject: [PATCH 50/76] fix docs, misc tweaks --- include/servicepoint.h | 60 ++++++++++++++++--------------- src/containers/brightness_grid.rs | 11 +++--- src/containers/byte_slice.rs | 3 +- src/containers/mod.rs | 5 +++ src/lib.rs | 12 ++----- src/macros.rs | 2 +- src/udp.rs | 8 +++++ 7 files changed, 56 insertions(+), 45 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 68d4c57..8fa2ab5 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -412,6 +412,8 @@ typedef struct Packet Packet; /** * This is a type only used by cbindgen to have a type for pointers. + * + * See [servicepoint::UdpSocketExt]. */ typedef struct UdpSocket UdpSocket; @@ -641,7 +643,7 @@ extern "C" { struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); /** - * Calls [`servicepoint::Bitmap::data_ref_mut`]. + * Calls method [`servicepoint::Bitmap::data_ref_mut`]. * * Gets an unsafe reference to the data of the [Bitmap] instance. * @@ -652,7 +654,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); /** - * Calls [`servicepoint::Bitmap::fill`]. + * Calls method [`servicepoint::Bitmap::fill`]. * * Sets the state of all pixels in the [Bitmap]. * @@ -683,7 +685,7 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); /** - * Calls [`servicepoint::Bitmap::get`]. + * Calls method [`servicepoint::Bitmap::get`]. * * Gets the current value at the specified position. * @@ -700,7 +702,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); /** - * Calls [`servicepoint::Bitmap::height`]. + * Calls method [`servicepoint::Bitmap::height`]. * * Gets the height in pixels. * @@ -784,7 +786,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** - * Calls [`servicepoint::Bitmap::set`]. + * Calls method [`servicepoint::Bitmap::set`]. * * Sets the value of the specified position. * @@ -805,7 +807,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, bool value); /** - * Calls [`servicepoint::Bitmap::width`]. + * Calls method [`servicepoint::Bitmap::width`]. * * Gets the width in pixels. * @@ -814,7 +816,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. * * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * @@ -832,7 +834,7 @@ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::fill`]. + * Calls method [`servicepoint::DisplayBitVec::fill`]. * * Sets the value of all bits. * @@ -852,7 +854,7 @@ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); void sp_bitvec_free(BitVec */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::get`]. + * Calls method [`servicepoint::DisplayBitVec::get`]. * * Gets the value of a bit. * @@ -886,7 +888,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, CompressionCode compression); /** - * Calls [`servicepoint::DisplayBitVec::is_empty`]. + * Calls method [`servicepoint::DisplayBitVec::is_empty`]. * * Returns true if length is 0. * @@ -895,7 +897,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); /** - * Calls [`servicepoint::DisplayBitVec::len`]. + * Calls method [`servicepoint::DisplayBitVec::len`]. * * Gets the length in bits. * @@ -930,7 +932,7 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); BitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Calls [`servicepoint::DisplayBitVec::set`]. + * Calls method [`servicepoint::DisplayBitVec::set`]. * * Sets the value of a bit. * @@ -955,7 +957,7 @@ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); /** - * Calls [`servicepoint::BrightnessGrid::data_ref_mut`]. + * Calls method [`servicepoint::BrightnessGrid::data_ref_mut`]. * * Gets an unsafe reference to the data of the instance. * @@ -966,7 +968,7 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); /** - * Calls [`servicepoint::BrightnessGrid::fill`]. + * Calls method [`servicepoint::BrightnessGrid::fill`]. * * Sets the value of all cells. * @@ -987,7 +989,7 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); /** - * Calls [`servicepoint::BrightnessGrid::get`]. + * Calls method [`servicepoint::BrightnessGrid::get`]. * * Gets the current value at the specified position. * @@ -1007,7 +1009,7 @@ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t y); /** - * Calls [`servicepoint::BrightnessGrid::height`]. + * Calls method [`servicepoint::BrightnessGrid::height`]. * * Gets the height of the grid. * @@ -1065,7 +1067,7 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); /** - * Calls [`servicepoint::BrightnessGrid::set`]. + * Calls method [`servicepoint::BrightnessGrid::set`]. * * Sets the value of the specified position. * @@ -1088,7 +1090,7 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, Brightness value); /** - * Calls [`servicepoint::BrightnessGrid::width`]. + * Calls method [`servicepoint::BrightnessGrid::width`]. * * Gets the width of the grid. * @@ -1104,7 +1106,7 @@ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); /** - * Calls [`servicepoint::CharGrid::fill`]. + * Calls method [`servicepoint::CharGrid::fill`]. * * Sets the value of all cells in the grid. * @@ -1125,7 +1127,7 @@ void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); void sp_char_grid_free(CharGrid */*notnull*/ instance); /** - * Calls [`servicepoint::CharGrid::get`]. + * Calls method [`servicepoint::CharGrid::get`]. * * Returns the current value at the specified position. * @@ -1142,7 +1144,7 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** - * Calls [`servicepoint::CharGrid::height`]. + * Calls method [`servicepoint::CharGrid::height`]. * * Gets the height of the grid. * @@ -1191,7 +1193,7 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); /** - * Calls [`servicepoint::CharGrid::set`]. + * Calls method [`servicepoint::CharGrid::set`]. * * Sets the value of the specified position in the grid. * @@ -1215,7 +1217,7 @@ void sp_char_grid_set(CharGrid */*notnull*/ instance, uint32_t value); /** - * Calls [`servicepoint::CharGrid::width`]. + * Calls method [`servicepoint::CharGrid::width`]. * * Gets the width of the grid. * @@ -1824,7 +1826,7 @@ struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); /** - * Calls [`servicepoint::Cp437Grid::data_ref_mut`]. + * Calls method [`servicepoint::Cp437Grid::data_ref_mut`]. * * Gets an unsafe reference to the data of the grid. * @@ -1835,7 +1837,7 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** - * Calls [`servicepoint::Cp437Grid::fill`]. + * Calls method [`servicepoint::Cp437Grid::fill`]. * * Sets the value of all cells in the grid. * @@ -1856,7 +1858,7 @@ void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** - * Calls [`servicepoint::Cp437Grid::get`]. + * Calls method [`servicepoint::Cp437Grid::get`]. * * Gets the current value at the specified position. * @@ -1873,7 +1875,7 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** - * Calls [`servicepoint::Cp437Grid::height`]. + * Calls method [`servicepoint::Cp437Grid::height`]. * * Gets the height of the grid. * @@ -1913,7 +1915,7 @@ Cp437Grid *sp_cp437_grid_load(size_t width, Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); /** - * Calls [`servicepoint::Cp437Grid::set`]. + * Calls method [`servicepoint::Cp437Grid::set`]. * * Sets the value at the specified position. * @@ -1936,7 +1938,7 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, uint8_t value); /** - * Calls [`servicepoint::Cp437Grid::width`]. + * Calls method [`servicepoint::Cp437Grid::width`]. * * Gets the width of the grid. * diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 2374b47..e266bef 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,7 +1,6 @@ -use crate::macros::wrap_functions; use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_methods}, + macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -121,11 +120,13 @@ wrap_methods!( /// /// The returned memory is valid for the lifetime of the grid. mut fn data_ref_mut() -> ByteSlice { - return(br_slice) { unsafe { + return(br_slice) { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) - }}; + unsafe { + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + } + }; }; ); diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 10ee22f..66bdce2 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -26,7 +26,8 @@ pub struct ByteSlice { } impl ByteSlice { - pub(crate) const INVALID: ByteSlice = ByteSlice { + /// Represents an invalid [ByteSlice] instance. + pub const INVALID: ByteSlice = ByteSlice { start: std::ptr::null_mut(), length: 0, }; diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 62d51d0..b9460e9 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -11,3 +11,8 @@ pub use brightness_grid::*; pub use byte_slice::*; pub use char_grid::*; pub use cp437_grid::*; + +mod _hidden { + /// This is a type only used by cbindgen to have a type for pointers. + pub struct DisplayBitVec; +} diff --git a/src/lib.rs b/src/lib.rs index 344a1a2..4df864c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,22 +29,16 @@ pub mod commands; /// Functions related to [servicepoint::Bitmap], [servicepoint::CharGrid] and friends. pub mod containers; -mod macros; +pub(crate) mod macros; pub(crate) mod mem; -/// Functions related to [Packet]. +/// Functions related to [servicepoint::Packet]. pub mod packet; -/// Functions related to [UdpSocket]. +/// Functions related to [servicepoint::UdpSocketExt]. pub mod udp; /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. pub const SP_FRAME_PACING_MS: u128 = 30; -/// This is a type only used by cbindgen to have a type for pointers. -pub struct UdpSocket; - -/// This is a type only used by cbindgen to have a type for pointers. -pub struct DisplayBitVec; - #[cfg(feature = "env_logger")] mod feature_env_logger { use crate::macros::wrap_functions; diff --git a/src/macros.rs b/src/macros.rs index 4157f28..c66808d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -50,7 +50,7 @@ macro_rules! wrap_methods { paste::paste! { $crate::macros::wrap_functions!($prefix; $( - #[doc = concat!(" Calls [`servicepoint::", stringify!($object_type), + #[doc = concat!(" Calls method [`servicepoint::", stringify!($object_type), "::", stringify!($function), "`].")] #[doc = ""] $(#[$meta])* diff --git a/src/udp.rs b/src/udp.rs index cb96547..a969512 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -129,3 +129,11 @@ wrap_functions!(sp_udp; } ); + +mod _hidden { + /// This is a type only used by cbindgen to have a type for pointers. + /// + /// See [servicepoint::UdpSocketExt]. + #[allow(unused)] + pub struct UdpSocket; +} From 1eb59d986a2a09dfe58e1b708adf6bbc6cc82523 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 21 Jun 2025 23:25:52 +0200 Subject: [PATCH 51/76] wrap_command macro --- example/src/header_logger.c | 12 +- include/servicepoint.h | 137 +++++++++++++--------- src/commands/bitmap_command.rs | 7 +- src/commands/bitvec_command.rs | 6 +- src/commands/brightness_grid_command.rs | 7 +- src/commands/cc_only_commands.rs | 28 +++-- src/commands/char_grid_command.rs | 7 +- src/commands/cp437_grid_command.rs | 7 +- src/commands/generic_command.rs | 10 +- src/commands/global_brightness_command.rs | 6 +- src/commands/mod.rs | 33 +++++- src/macros.rs | 7 +- src/udp.rs | 2 +- 13 files changed, 162 insertions(+), 107 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 9cb3cd1..5f04ac4 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -33,7 +33,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); - Bitmap *bitmap = sp_cmd_bitmap_get(bitmapCommand); + Bitmap *bitmap = sp_cmd_bitmap_get_bitmap_mut(bitmapCommand); size_t w = sp_bitmap_width(bitmap); size_t h = sp_bitmap_height(bitmap); @@ -47,7 +47,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); - BrightnessGrid *grid = sp_cmd_brightness_grid_get(gridCommand); + BrightnessGrid *grid = sp_cmd_brightness_grid_get_grid_mut(gridCommand); size_t w = sp_brightness_grid_width(grid); size_t h = sp_brightness_grid_height(grid); @@ -61,7 +61,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_char_grid_get_origin(gridCommand, &x, &y); - CharGrid *grid = sp_cmd_char_grid_get(gridCommand); + CharGrid *grid = sp_cmd_char_grid_get_grid_mut(gridCommand); size_t w = sp_char_grid_width(grid); size_t h = sp_char_grid_height(grid); @@ -75,7 +75,7 @@ bool log_command(struct Command command) { size_t x, y; sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); - Cp437Grid *grid = sp_cmd_cp437_grid_get(gridCommand); + Cp437Grid *grid = sp_cmd_cp437_grid_get_grid_mut(gridCommand); size_t w = sp_cp437_grid_width(grid); size_t h = sp_cp437_grid_height(grid); @@ -84,7 +84,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_BIT_VEC: { - BitVecCommand *bitvecCommand = command.data.bitvec; + BitVecCommand *bitvecCommand = command.data.bit_vec; size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); @@ -109,7 +109,7 @@ bool log_command(struct Command command) { break; } - BitVec *bitvec = sp_cmd_bitvec_get(bitvecCommand); + BitVec *bitvec = sp_cmd_bitvec_get_bitvec_mut(bitvecCommand); size_t len = sp_bitvec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", diff --git a/include/servicepoint.h b/include/servicepoint.h index 8fa2ab5..50adba6 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -99,7 +99,7 @@ typedef uint8_t BinaryOperation; #endif // __cplusplus /** - * The u16 command codes used for the [Command]s. + * The u16 command codes used for the [`crate::Command`]s. */ enum CommandCode #ifdef __cplusplus @@ -560,7 +560,7 @@ typedef struct ValueGrid_u8 Cp437Grid; typedef union CommandUnion { uint8_t *null; struct BitmapCommand */*notnull*/ bitmap; - struct BitVecCommand */*notnull*/ bitvec; + struct BitVecCommand */*notnull*/ bit_vec; struct BrightnessGridCommand */*notnull*/ brightness_grid; struct CharGridCommand */*notnull*/ char_grid; struct Cp437GridCommand */*notnull*/ cp437_grid; @@ -1432,20 +1432,6 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); -/** - *Clones a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the sp_cmd_brightness_global module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_clone(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - *Deallocates a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the sp_cmd_brightness_global module. - */ -void sp_cmd_brightness_global_free(struct GlobalBrightnessCommand */*notnull*/ instance); - /** * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * @@ -1477,20 +1463,6 @@ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightn void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); -/** - *Clones a [`BrightnessGridCommand`] instance. - * - * This function is part of the sp_cmd_brightness_grid module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_clone(struct BrightnessGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`BrightnessGridCommand`] instance. - * - * This function is part of the sp_cmd_brightness_grid module. - */ -void sp_cmd_brightness_grid_free(struct BrightnessGridCommand */*notnull*/ instance); - /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. @@ -1559,18 +1531,18 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ size_t origin_y); /** - *Clones a [`CharGridCommand`] instance. + *Clones a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the sp_cmd_brightnessgrid module. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_clone(struct CharGridCommand */*notnull*/ instance); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - *Deallocates a [`CharGridCommand`] instance. + *Deallocates a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the sp_cmd_brightnessgrid module. */ -void sp_cmd_char_grid_free(struct CharGridCommand */*notnull*/ instance); +void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1639,6 +1611,27 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, */ struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); +/** + *Clones a [`CharGridCommand`] instance. + * + * This function is part of the sp_cmd_chargrid module. + */ +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); + +/** + *Deallocates a [`CharGridCommand`] instance. + * + * This function is part of the sp_cmd_chargrid module. + */ +void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); + +/** + *Clones a [`ClearCommand`] instance. + * + * This function is part of the sp_cmd_clear module. + */ +struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); + /** *Deallocates a [`ClearCommand`] instance. * @@ -1657,20 +1650,6 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); -/** - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the sp_cmd_cp437_grid module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the sp_cmd_cp437_grid module. - */ -void sp_cmd_cp437_grid_free(struct Cp437GridCommand */*notnull*/ instance); - /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. @@ -1738,21 +1717,42 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, */ struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); +/** + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the sp_cmd_cp437grid module. + */ +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the sp_cmd_cp437grid module. + */ +void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Clones a [`FadeOutCommand`] instance. + * + * This function is part of the sp_cmd_fadeout module. + */ +struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); + /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fade_out module. + * This function is part of the sp_cmd_fadeout module. */ -void sp_cmd_fade_out_free(struct FadeOutCommand */*notnull*/ instance); +void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); /** * A yet-to-be-tested command. * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fade_out module. + * This function is part of the sp_cmd_fadeout module. */ -struct FadeOutCommand */*notnull*/ sp_cmd_fade_out_new(void); +struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); /** * Clones an [SPCommand] instance. @@ -1800,12 +1800,33 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); +/** + *Clones a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the sp_cmd_globalbrightness module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + *Deallocates a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the sp_cmd_globalbrightness module. + */ +void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + *Clones a [`HardResetCommand`] instance. + * + * This function is part of the sp_cmd_hardreset module. + */ +struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); + /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hard_reset module. + * This function is part of the sp_cmd_hardreset module. */ -void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); +void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); /** * Kills the udp daemon on the display, which usually results in a restart. @@ -1814,9 +1835,9 @@ void sp_cmd_hard_reset_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hard_reset module. + * This function is part of the sp_cmd_hardreset module. */ -struct HardResetCommand */*notnull*/ sp_cmd_hard_reset_new(void); +struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); /** *Clones a [`Cp437Grid`] instance. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 68f6408..14f7626 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_bitmap::BitmapCommand); -wrap_free!(sp_cmd_bitmap::BitmapCommand); +wrap_command!(Bitmap); wrap_fields!(sp_cmd_bitmap::BitmapCommand; prop bitmap: Bitmap { mut get(); move set(value); }; diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 6cec313..38af213 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,5 +1,6 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::wrap_command, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -8,8 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(sp_cmd_bitvec::BitVecCommand); -wrap_free!(sp_cmd_bitvec::BitVecCommand); +wrap_command!(BitVec); wrap_fields!(sp_cmd_bitvec::BitVecCommand; prop bitvec: DisplayBitVec { mut get(); move set(value); }; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index cc26b77..361ae8b 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_brightness_grid::BrightnessGridCommand); -wrap_free!(sp_cmd_brightness_grid::BrightnessGridCommand); +wrap_command!(BrightnessGrid); wrap_fields!(sp_cmd_brightness_grid::BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index f4162a0..f90a045 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,37 +1,45 @@ use crate::{ - macros::{wrap_free, wrap_functions}, - mem::heap_move_nonnull, + commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull, }; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; macro_rules! wrap_cc_only { - ($prefix:ident :: $typ:ident ; $(#[$meta:meta])*) => { + ($(#[$meta:meta])*; $command:ident, $prefix:ident, $object_type:ident) => { + wrap_command!($command); + wrap_functions!($prefix; $(#[$meta])* /// - #[doc = concat!(" Returns: a new [`",stringify!($typ),"`] instance.")] - fn new() -> NonNull<$typ> { - heap_move_nonnull($typ) + #[doc = concat!(" Returns: a new [`",stringify!($object_type),"`] instance.")] + fn new() -> NonNull<$object_type> { + heap_move_nonnull($object_type) } ); + }; - wrap_free!($prefix :: $typ); + ($(#[$meta:meta])* $command:ident) => { + ::paste::paste!{ + wrap_cc_only!($(#[$meta])*; $command, [< sp_cmd_ $command:lower >], [< $command Command >]); + } }; } -wrap_cc_only!(sp_cmd_clear::ClearCommand; +wrap_cc_only!( /// Set all pixels to the off state. /// /// Does not affect brightness. + Clear ); -wrap_cc_only!(sp_cmd_hard_reset::HardResetCommand; +wrap_cc_only!( /// Kills the udp daemon on the display, which usually results in a restart. /// /// Please do not send this in your normal program flow. + HardReset ); -wrap_cc_only!(sp_cmd_fade_out::FadeOutCommand; +wrap_cc_only!( /// A yet-to-be-tested command. + FadeOut ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index fe97dc0..dfab435 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_char_grid::CharGridCommand); -wrap_free!(sp_cmd_char_grid::CharGridCommand); +wrap_command!(CharGrid); wrap_fields!(sp_cmd_char_grid::CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 25294bb..cc43813 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,13 +1,12 @@ use crate::{ - commands::wrap_origin_accessors, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::{wrap_command, wrap_origin_accessors}, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; use std::ptr::NonNull; -wrap_clone!(sp_cmd_cp437_grid::Cp437GridCommand); -wrap_free!(sp_cmd_cp437_grid::Cp437GridCommand); +wrap_command!(Cp437Grid); wrap_fields!(sp_cmd_cp437_grid::Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 9734d30..13dcd51 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -18,7 +18,7 @@ use std::ptr::{null_mut, NonNull}; pub union CommandUnion { pub null: *mut u8, pub bitmap: NonNull, - pub bitvec: NonNull, + pub bit_vec: NonNull, pub brightness_grid: NonNull, pub char_grid: NonNull, pub cp437_grid: NonNull, @@ -121,7 +121,7 @@ wrap_functions!(sp_cmd_generic; TypedCommand::BitVec(bitvec) => SPCommand { tag: CommandTag::BitVec, data: CommandUnion { - bitvec: heap_move_nonnull(bitvec), + bit_vec: heap_move_nonnull(bitvec), }, }, TypedCommand::HardReset(hard_reset) => SPCommand { @@ -197,7 +197,7 @@ wrap_functions!(sp_cmd_generic; CommandTag::BitVec => SPCommand { tag: CommandTag::BitVec, data: CommandUnion { - bitvec: heap_clone(command.data.bitvec), + bit_vec: heap_clone(command.data.bit_vec), }, }, CommandTag::HardReset => SPCommand { @@ -239,7 +239,7 @@ wrap_functions!(sp_cmd_generic; match command.tag { CommandTag::Invalid => (), CommandTag::Bitmap => heap_drop(command.data.bitmap), - CommandTag::BitVec => heap_drop(command.data.bitvec), + CommandTag::BitVec => heap_drop(command.data.bit_vec), CommandTag::BrightnessGrid => { heap_drop(command.data.brightness_grid) } @@ -269,7 +269,7 @@ wrap_functions!(sp_cmd_generic; heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) } CommandTag::BitVec => { - heap_move_ok(unsafe { heap_remove(command.data.bitvec).try_into() }) + heap_move_ok(unsafe { heap_remove(command.data.bit_vec).try_into() }) } CommandTag::BrightnessGrid => heap_move_ok(unsafe { heap_remove(command.data.brightness_grid).try_into() diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 35bcbe0..7f8f47c 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,5 +1,6 @@ use crate::{ - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + commands::wrap_command, + macros::{wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_remove}, }; use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; @@ -21,8 +22,7 @@ wrap_functions!(sp_cmd_brightness_global; ); -wrap_clone!(sp_cmd_brightness_global::GlobalBrightnessCommand); -wrap_free!(sp_cmd_brightness_global::GlobalBrightnessCommand); +wrap_command!(GlobalBrightness); wrap_fields!( sp_cmd_brightness_global::GlobalBrightnessCommand; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a6fa74d..6ec78cb 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -44,4 +44,35 @@ macro_rules! wrap_origin_accessors { }; } -pub(crate) use wrap_origin_accessors; +macro_rules! derive_command_from { + ($command:ident) => { + ::paste::paste! { + impl From<::servicepoint::[< $command Command >]> for $crate::commands::SPCommand { + fn from(command: ::servicepoint::[< $command Command >]) -> Self { + Self { + tag: $crate::commands::CommandTag::$command, + data: $crate::commands::CommandUnion { + [< $command:snake >]: $crate::mem::heap_move_nonnull(command) + }, + } + } + } + } + }; +} + +macro_rules! wrap_command { + ($command:ident, $prefix:ident, $object_type:ident) => { + $crate::macros::wrap_clone!($prefix::$object_type); + $crate::macros::wrap_free!($prefix::$object_type); + + $crate::commands::derive_command_from!($command); + }; + ($command:ident) => { + ::paste::paste!{ + wrap_command!($command, [< sp_cmd_ $command:lower >], [< $command Command >]); + } + }; +} + +pub(crate) use {derive_command_from, wrap_command, wrap_origin_accessors}; diff --git a/src/macros.rs b/src/macros.rs index c66808d..3b35e1d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -209,7 +209,7 @@ macro_rules! wrap_fields { macro_rules! wrap_functions { ( - $prefix:ident; + $module:ident; $( $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) @@ -221,13 +221,12 @@ macro_rules! wrap_functions { $( $(#[$meta])* #[doc = ""] - #[doc = concat!(" This function is part of the ", stringify!($prefix), " module.")] + #[doc = concat!(" This function is part of the ", stringify!($module), " module.")] #[no_mangle] - pub unsafe extern "C" fn [<$prefix _ $function>]( + pub unsafe extern "C" fn [< $module _ $function >]( $($param_name: $param_type),* ) $(-> $return_type)? $block - )+ } }; diff --git a/src/udp.rs b/src/udp.rs index a969512..7eebe27 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -79,7 +79,7 @@ wrap_functions!(sp_udp; .send_command(heap_remove(command.data.bitmap)), CommandTag::BitVec => connection .as_ref() - .send_command(heap_remove(command.data.bitvec)), + .send_command(heap_remove(command.data.bit_vec)), CommandTag::BrightnessGrid => connection .as_ref() .send_command(heap_remove(command.data.brightness_grid)), From 500cbbc872362848225f7ade10bc7deb1693e7ea Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 21 Jun 2025 23:51:10 +0200 Subject: [PATCH 52/76] add prefix centrally --- include/servicepoint.h | 290 +++++++++++----------- src/commands/bitmap_command.rs | 6 +- src/commands/bitvec_command.rs | 4 +- src/commands/brightness_grid_command.rs | 6 +- src/commands/cc_only_commands.rs | 2 +- src/commands/char_grid_command.rs | 6 +- src/commands/cp437_grid_command.rs | 6 +- src/commands/generic_command.rs | 2 +- src/commands/global_brightness_command.rs | 4 +- src/commands/mod.rs | 2 +- src/containers/bitmap.rs | 9 +- src/containers/bitvec.rs | 8 +- src/containers/brightness_grid.rs | 8 +- src/containers/char_grid.rs | 8 +- src/containers/cp437_grid.rs | 8 +- src/containers/mod.rs | 1 + src/lib.rs | 6 +- src/macros.rs | 2 +- src/packet.rs | 8 +- src/udp.rs | 4 +- 20 files changed, 195 insertions(+), 195 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 50adba6..63b2226 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -638,7 +638,7 @@ extern "C" { /** *Clones a [`Bitmap`] instance. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); @@ -649,7 +649,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitmap. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); @@ -662,14 +662,14 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); * * - `value`: the value to set all pixels to * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** *Deallocates a [`Bitmap`] instance. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); @@ -680,7 +680,7 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * * Returns NULL in case of error. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); @@ -697,7 +697,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); @@ -706,14 +706,14 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); * * Gets the height in pixels. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** * Consumes the Bitmap and returns the contained BitVec. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -724,7 +724,7 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); * * Returns NULL in case of an error. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, size_t x, @@ -741,7 +741,7 @@ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap *sp_bitmap_load(size_t width, size_t height, @@ -772,7 +772,7 @@ struct Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_free(grid); * ``` * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap *sp_bitmap_new(size_t width, size_t height); @@ -781,7 +781,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); * * returns: [Bitmap] initialized to all pixels off. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); @@ -799,7 +799,7 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, @@ -811,7 +811,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, * * Gets the width in pixels. * - * This function is part of the sp_bitmap module. + * This function is part of the bitmap module. */ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); @@ -822,14 +822,14 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitvec. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** *Clones a [`DisplayBitVec`] instance. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); @@ -842,14 +842,14 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); * * - `value`: the value to set all bits to * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ void sp_bitvec_free(BitVec */*notnull*/ instance); @@ -869,7 +869,7 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * * - when accessing `index` out of bounds * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); @@ -880,7 +880,7 @@ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); * * Returns NULL in case of an error. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, size_t offset, @@ -892,7 +892,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, * * Returns true if length is 0. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); @@ -901,7 +901,7 @@ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); * * Gets the length in bits. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ size_t sp_bitvec_len(BitVec */*notnull*/ instance); @@ -910,7 +910,7 @@ size_t sp_bitvec_len(BitVec */*notnull*/ instance); * * returns: [DisplayBitVec] instance containing data. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); @@ -927,7 +927,7 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); * * - when `size` is not divisible by 8. * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ BitVec */*notnull*/ sp_bitvec_new(size_t size); @@ -945,14 +945,14 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds * - * This function is part of the sp_bitvec module. + * This function is part of the bitvec module. */ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** *Clones a [`BrightnessGrid`] instance. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); @@ -963,7 +963,7 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ * * The returned memory is valid for the lifetime of the grid. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); @@ -976,7 +976,7 @@ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ ins * * - `value`: the value to set all cells to * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); @@ -984,7 +984,7 @@ void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, /** *Deallocates a [`BrightnessGrid`] instance. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); @@ -1002,7 +1002,7 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * # Panics * - When accessing `x` or `y` out of bounds. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, size_t x, @@ -1013,7 +1013,7 @@ Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, * * Gets the height of the grid. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); @@ -1024,7 +1024,7 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, size_t x, @@ -1037,7 +1037,7 @@ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, * * returns: new [BrightnessGrid] instance, or NULL in case of an error. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ BrightnessGrid *sp_brightness_grid_load(size_t width, size_t height, @@ -1062,7 +1062,7 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * sp_udp_free(connection); * ``` * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); @@ -1082,7 +1082,7 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * * - When accessing `x` or `y` out of bounds. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, size_t x, @@ -1094,14 +1094,14 @@ void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, * * Gets the width of the grid. * - * This function is part of the sp_brightness_grid module. + * This function is part of the brightness_grid module. */ size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); /** *Clones a [`CharGrid`] instance. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); @@ -1115,14 +1115,14 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); * - `value`: the value to set all cells to * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [`CharGrid`] instance. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ void sp_char_grid_free(CharGrid */*notnull*/ instance); @@ -1139,7 +1139,7 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); @@ -1148,7 +1148,7 @@ uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); * * Gets the height of the grid. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); @@ -1159,7 +1159,7 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, size_t x, @@ -1170,7 +1170,7 @@ struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, * * returns: new CharGrid or NULL in case of an error * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); @@ -1188,7 +1188,7 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); * sp_char_grid_free(grid); * ``` * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); @@ -1209,7 +1209,7 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * - when accessing `x` or `y` out of bounds * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ void sp_char_grid_set(CharGrid */*notnull*/ instance, size_t x, @@ -1221,21 +1221,21 @@ void sp_char_grid_set(CharGrid */*notnull*/ instance, * * Gets the width of the grid. * - * This function is part of the sp_char_grid module. + * This function is part of the char_grid module. */ size_t sp_char_grid_width(CharGrid */*notnull*/ instance); /** *Clones a [`BitmapCommand`] instance. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); /** *Deallocates a [`BitmapCommand`] instance. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); @@ -1245,7 +1245,7 @@ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); * * Rust equivalent: `BitmapCommand::from(bitmap)` * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); @@ -1255,21 +1255,21 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** * Reads the origin field of the [`BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1282,7 +1282,7 @@ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, * * Returns: a new [BitmapCommand] instance. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, size_t origin_x, @@ -1293,7 +1293,7 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ b * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); @@ -1301,7 +1301,7 @@ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, CompressionCode value); @@ -1309,7 +1309,7 @@ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`BitmapCommand`]. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, @@ -1320,21 +1320,21 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_bitmap module. + * This function is part of the cmd_bitmap module. */ struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** *Clones a [`BitVecCommand`] instance. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); /** *Deallocates a [`BitVecCommand`] instance. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); @@ -1344,28 +1344,28 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); @@ -1383,7 +1383,7 @@ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ in * * The contained [`DisplayBitVec`] is always uncompressed. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, size_t offset, @@ -1394,7 +1394,7 @@ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, BitVec */*notnull*/ value); @@ -1402,7 +1402,7 @@ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, CompressionCode value); @@ -1410,7 +1410,7 @@ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); @@ -1418,7 +1418,7 @@ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); @@ -1428,21 +1428,21 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_bitvec module. + * This function is part of the cmd_bitvec module. */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); /** * Turns the command into a packet * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); @@ -1451,14 +1451,14 @@ struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBri * * Returns: a new [GlobalBrightnessCommand] instance. * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); /** * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * - * This function is part of the sp_cmd_brightness_global module. + * This function is part of the cmd_brightness_global module. */ void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); @@ -1467,7 +1467,7 @@ void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*n * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); @@ -1477,14 +1477,14 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(Brigh * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ BrightnessGrid */*notnull*/ sp_cmd_brightness_grid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1495,7 +1495,7 @@ void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); @@ -1506,7 +1506,7 @@ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand * * * Returns: a new [BrightnessGridCommand] instance. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, size_t origin_x, @@ -1516,7 +1516,7 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessG * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, BrightnessGrid */*notnull*/ value); @@ -1524,7 +1524,7 @@ void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ i /** * Overwrites the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the sp_cmd_brightness_grid module. + * This function is part of the cmd_brightness_grid module. */ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, size_t origin_x, @@ -1533,14 +1533,14 @@ void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ /** *Clones a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_brightnessgrid module. + * This function is part of the cmd_brightnessgrid module. */ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** *Deallocates a [`BrightnessGridCommand`] instance. * - * This function is part of the sp_cmd_brightnessgrid module. + * This function is part of the cmd_brightnessgrid module. */ void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); @@ -1548,7 +1548,7 @@ void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instan * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); @@ -1558,14 +1558,14 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnu * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ CharGrid */*notnull*/ sp_cmd_char_grid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`CharGridCommand`]. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1578,7 +1578,7 @@ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, * * Returns: a new [CharGridCommand] instance. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, size_t origin_x, @@ -1588,7 +1588,7 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ g * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, CharGrid */*notnull*/ value); @@ -1596,7 +1596,7 @@ void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`CharGridCommand`]. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, @@ -1607,35 +1607,35 @@ void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_char_grid module. + * This function is part of the cmd_char_grid module. */ struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** *Clones a [`CharGridCommand`] instance. * - * This function is part of the sp_cmd_chargrid module. + * This function is part of the cmd_chargrid module. */ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); /** *Deallocates a [`CharGridCommand`] instance. * - * This function is part of the sp_cmd_chargrid module. + * This function is part of the cmd_chargrid module. */ void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); /** *Clones a [`ClearCommand`] instance. * - * This function is part of the sp_cmd_clear module. + * This function is part of the cmd_clear module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); /** *Deallocates a [`ClearCommand`] instance. * - * This function is part of the sp_cmd_clear module. + * This function is part of the cmd_clear module. */ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); @@ -1646,7 +1646,7 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); * * Returns: a new [`ClearCommand`] instance. * - * This function is part of the sp_cmd_clear module. + * This function is part of the cmd_clear module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); @@ -1654,7 +1654,7 @@ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); @@ -1664,14 +1664,14 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*no * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ Cp437Grid */*notnull*/ sp_cmd_cp437_grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** * Reads the origin field of the [`Cp437GridCommand`]. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1684,7 +1684,7 @@ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, * * The origin is relative to the top-left of the display. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, size_t origin_x, @@ -1694,7 +1694,7 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull* * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, Cp437Grid */*notnull*/ value); @@ -1702,7 +1702,7 @@ void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`Cp437GridCommand`]. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, size_t origin_x, @@ -1713,35 +1713,35 @@ void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the sp_cmd_cp437_grid module. + * This function is part of the cmd_cp437_grid module. */ struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** *Clones a [`Cp437GridCommand`] instance. * - * This function is part of the sp_cmd_cp437grid module. + * This function is part of the cmd_cp437grid module. */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); /** *Deallocates a [`Cp437GridCommand`] instance. * - * This function is part of the sp_cmd_cp437grid module. + * This function is part of the cmd_cp437grid module. */ void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); /** *Clones a [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fadeout module. + * This function is part of the cmd_fadeout module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fadeout module. + * This function is part of the cmd_fadeout module. */ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); @@ -1750,7 +1750,7 @@ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the sp_cmd_fadeout module. + * This function is part of the cmd_fadeout module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); @@ -1759,7 +1759,7 @@ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); * * returns: a new [SPCommand] instance. * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ struct Command sp_cmd_generic_clone(struct Command command); @@ -1775,7 +1775,7 @@ struct Command sp_cmd_generic_clone(struct Command command); * sp_command_free(c); * ``` * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ void sp_cmd_generic_free(struct Command command); @@ -1785,7 +1785,7 @@ void sp_cmd_generic_free(struct Command command); * * Returns tag [CommandTag::Invalid] in case of an error. * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ struct Packet *sp_cmd_generic_into_packet(struct Command command); @@ -1796,35 +1796,35 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. * - * This function is part of the sp_cmd_generic module. + * This function is part of the cmd_generic module. */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** *Clones a [`GlobalBrightnessCommand`] instance. * - * This function is part of the sp_cmd_globalbrightness module. + * This function is part of the cmd_globalbrightness module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Deallocates a [`GlobalBrightnessCommand`] instance. * - * This function is part of the sp_cmd_globalbrightness module. + * This function is part of the cmd_globalbrightness module. */ void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Clones a [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hardreset module. + * This function is part of the cmd_hardreset module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hardreset module. + * This function is part of the cmd_hardreset module. */ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); @@ -1835,14 +1835,14 @@ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the sp_cmd_hardreset module. + * This function is part of the cmd_hardreset module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); /** *Clones a [`Cp437Grid`] instance. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); @@ -1853,7 +1853,7 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); * * The returned memory is valid for the lifetime of the instance. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); @@ -1867,14 +1867,14 @@ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** *Deallocates a [`Cp437Grid`] instance. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); @@ -1891,7 +1891,7 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); @@ -1900,7 +1900,7 @@ uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); * * Gets the height of the grid. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); @@ -1911,7 +1911,7 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, size_t x, @@ -1920,7 +1920,7 @@ struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ Cp437Grid *sp_cp437_grid_load(size_t width, size_t height, @@ -1931,7 +1931,7 @@ Cp437Grid *sp_cp437_grid_load(size_t width, * * returns: [Cp437Grid] initialized to 0. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); @@ -1951,7 +1951,7 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, size_t x, @@ -1963,7 +1963,7 @@ void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, * * Gets the width of the grid. * - * This function is part of the sp_cp437_grid module. + * This function is part of the cp437_grid module. */ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); @@ -1971,21 +1971,21 @@ size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). * - * This function is part of the sp_envlogger module. + * This function is part of the envlogger module. */ void sp_envlogger_init(void); /** *Clones a [`Packet`] instance. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** *Deallocates a [`Packet`] instance. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ void sp_packet_free(struct Packet */*notnull*/ instance); @@ -1994,7 +1994,7 @@ void sp_packet_free(struct Packet */*notnull*/ instance); * * returns: new instance. Will never return null. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); @@ -2002,7 +2002,7 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, /** * Gets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); @@ -2012,7 +2012,7 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); @@ -2023,7 +2023,7 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i * * The returned memory can be changed and will be valid until a new payload is set. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); @@ -2034,7 +2034,7 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); * * - if the buffer is not big enough to hold header+payload. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); @@ -2042,7 +2042,7 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, /** * Sets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); @@ -2052,7 +2052,7 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, * * This makes previous payload pointers invalid. * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ void sp_packet_set_payload(struct Packet */*notnull*/ packet, struct ByteSlice data); @@ -2062,7 +2062,7 @@ void sp_packet_set_payload(struct Packet */*notnull*/ packet, * * returns: NULL in case of an error, pointer to the allocated packet otherwise * - * This function is part of the sp_packet module. + * This function is part of the packet module. */ struct Packet *sp_packet_try_load(struct ByteSlice data); @@ -2073,13 +2073,13 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * * This function is part of the sp module. */ -bool sp_u16_to_command_code(uint16_t code, - CommandCode *result); +bool sp_sp_u16_to_command_code(uint16_t code, + CommandCode *result); /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ void sp_udp_free(struct UdpSocket */*notnull*/ instance); @@ -2096,7 +2096,7 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ struct UdpSocket *sp_udp_open(char */*notnull*/ host); @@ -2113,7 +2113,7 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint8_t ip2, @@ -2134,7 +2134,7 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, struct Command command); @@ -2150,7 +2150,7 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); @@ -2162,7 +2162,7 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the sp_udp module. + * This function is part of the udp module. */ bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, struct Packet */*notnull*/ packet); diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 14f7626..06cc53f 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -8,14 +8,14 @@ use std::ptr::NonNull; wrap_command!(Bitmap); -wrap_fields!(sp_cmd_bitmap::BitmapCommand; +wrap_fields!(cmd_bitmap::BitmapCommand; prop bitmap: Bitmap { mut get(); move set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_origin_accessors!(sp_cmd_bitmap::BitmapCommand); +wrap_origin_accessors!(cmd_bitmap::BitmapCommand); -wrap_functions!(sp_cmd_bitmap; +wrap_functions!(cmd_bitmap; /// Sets a window of pixels to the specified values. /// /// The passed [Bitmap] gets consumed. diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 38af213..100fc93 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -11,14 +11,14 @@ use std::ptr::NonNull; wrap_command!(BitVec); -wrap_fields!(sp_cmd_bitvec::BitVecCommand; +wrap_fields!(cmd_bitvec::BitVecCommand; prop bitvec: DisplayBitVec { mut get(); move set(value); }; prop offset: Offset { get(); set(value); }; prop operation: BinaryOperation { get(); set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_functions!(sp_cmd_bitvec; +wrap_functions!(cmd_bitvec; /// Set pixel data starting at the pixel offset on screen. /// diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 361ae8b..7a10389 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(sp_cmd_brightness_grid::BrightnessGridCommand; +wrap_fields!(cmd_brightness_grid::BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(sp_cmd_brightness_grid::BrightnessGridCommand); +wrap_origin_accessors!(cmd_brightness_grid::BrightnessGridCommand); -wrap_functions!(sp_cmd_brightness_grid; +wrap_functions!(cmd_brightness_grid; /// Set the brightness of individual tiles in a rectangular area of the display. /// diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index f90a045..0d72078 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -20,7 +20,7 @@ macro_rules! wrap_cc_only { ($(#[$meta:meta])* $command:ident) => { ::paste::paste!{ - wrap_cc_only!($(#[$meta])*; $command, [< sp_cmd_ $command:lower >], [< $command Command >]); + wrap_cc_only!($(#[$meta])*; $command, [< cmd_ $command:lower >], [< $command Command >]); } }; } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index dfab435..07765b8 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(sp_cmd_char_grid::CharGridCommand; +wrap_fields!(cmd_char_grid::CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(sp_cmd_char_grid::CharGridCommand); +wrap_origin_accessors!(cmd_char_grid::CharGridCommand); -wrap_functions!(sp_cmd_char_grid; +wrap_functions!(cmd_char_grid; /// Show UTF-8 encoded text on the screen. /// diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index cc43813..2e76e3c 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(sp_cmd_cp437_grid::Cp437GridCommand; +wrap_fields!(cmd_cp437_grid::Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(sp_cmd_cp437_grid::Cp437GridCommand); +wrap_origin_accessors!(cmd_cp437_grid::Cp437GridCommand); -wrap_functions!(sp_cmd_cp437_grid; +wrap_functions!(cmd_cp437_grid; /// Show text on the screen. /// diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 13dcd51..a4775d9 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -69,7 +69,7 @@ impl SPCommand { }; } -wrap_functions!(sp_cmd_generic; +wrap_functions!(cmd_generic; /// Tries to turn a [Packet] into a [SPCommand]. /// diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 7f8f47c..6d00d7c 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_cmd_brightness_global; +wrap_functions!(cmd_brightness_global; /// Set the brightness of all tiles to the same value. /// @@ -25,7 +25,7 @@ wrap_functions!(sp_cmd_brightness_global; wrap_command!(GlobalBrightness); wrap_fields!( - sp_cmd_brightness_global::GlobalBrightnessCommand; + cmd_brightness_global::GlobalBrightnessCommand; prop brightness: Brightness { get(); set(value); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6ec78cb..0fce600 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -70,7 +70,7 @@ macro_rules! wrap_command { }; ($command:ident) => { ::paste::paste!{ - wrap_command!($command, [< sp_cmd_ $command:lower >], [< $command Command >]); + wrap_command!($command, [< cmd_ $command:lower >], [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 6d502d9..9ebf13a 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -9,10 +9,10 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(sp_bitmap::Bitmap); -wrap_free!(sp_bitmap::Bitmap); +wrap_clone!(bitmap::Bitmap); +wrap_free!(bitmap::Bitmap); -wrap_functions!(sp_bitmap; +wrap_functions!(bitmap; /// Creates a new [Bitmap] with the specified dimensions. /// @@ -106,7 +106,7 @@ wrap_functions!(sp_bitmap; ); wrap_methods!( - sp_bitmap::Bitmap; + bitmap::Bitmap; /// Gets the current value at the specified position. /// @@ -148,7 +148,6 @@ wrap_methods!( /// /// The returned memory is valid for the lifetime of the bitmap. mut fn data_ref_mut() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index e232ab5..4512ac1 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -8,7 +8,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(sp_bitvec; +wrap_functions!(bitvec; /// Creates a new [DisplayBitVec] instance. /// @@ -55,11 +55,11 @@ wrap_functions!(sp_bitvec; ); -wrap_clone!(sp_bitvec::DisplayBitVec); -wrap_free!(sp_bitvec::DisplayBitVec); +wrap_clone!(bitvec::DisplayBitVec); +wrap_free!(bitvec::DisplayBitVec); wrap_methods!( - sp_bitvec::DisplayBitVec; + bitvec::DisplayBitVec; /// Gets the value of a bit. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index e266bef..4212ec5 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::{mem::transmute, ptr::NonNull}; -wrap_functions!(sp_brightness_grid; +wrap_functions!(brightness_grid; /// Creates a new [BrightnessGrid] with the specified dimensions. /// @@ -71,11 +71,11 @@ wrap_functions!(sp_brightness_grid; ); -wrap_clone!(sp_brightness_grid::BrightnessGrid); -wrap_free!(sp_brightness_grid::BrightnessGrid); +wrap_clone!(brightness_grid::BrightnessGrid); +wrap_free!(brightness_grid::BrightnessGrid); wrap_methods!( - sp_brightness_grid::BrightnessGrid; + brightness_grid::BrightnessGrid; /// Gets the current value at the specified position. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 782e2aa..2a3667b 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_char_grid; +wrap_functions!(char_grid; /// Creates a new [CharGrid] with the specified dimensions. /// @@ -58,11 +58,11 @@ wrap_functions!(sp_char_grid; ); -wrap_clone!(sp_char_grid::CharGrid); -wrap_free!(sp_char_grid::CharGrid); +wrap_clone!(char_grid::CharGrid); +wrap_free!(char_grid::CharGrid); wrap_methods!( - sp_char_grid::CharGrid; + char_grid::CharGrid; /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 2ed9aba..a344b1a 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(sp_cp437_grid; +wrap_functions!(cp437_grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// @@ -50,11 +50,11 @@ wrap_functions!(sp_cp437_grid; ); -wrap_clone!(sp_cp437_grid::Cp437Grid); -wrap_free!(sp_cp437_grid::Cp437Grid); +wrap_clone!(cp437_grid::Cp437Grid); +wrap_free!(cp437_grid::Cp437Grid); wrap_methods!( - sp_cp437_grid::Cp437Grid; + cp437_grid::Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments diff --git a/src/containers/mod.rs b/src/containers/mod.rs index b9460e9..7576a7b 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -14,5 +14,6 @@ pub use cp437_grid::*; mod _hidden { /// This is a type only used by cbindgen to have a type for pointers. + #[allow(unused)] pub struct DisplayBitVec; } diff --git a/src/lib.rs b/src/lib.rs index 4df864c..5a1a5de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,6 @@ //! //! # Examples //! -//! Make sure to check out [this GitHub repo](https://github.com/arfst23/ServicePoint) as well! -//! //! ```C //! #include //! #include "servicepoint.h" @@ -24,6 +22,8 @@ //! return 0; //! } //! ``` +//! +//! There are more examples in the source repository. /// Functions related to commands. pub mod commands; @@ -43,7 +43,7 @@ pub const SP_FRAME_PACING_MS: u128 = 30; mod feature_env_logger { use crate::macros::wrap_functions; - wrap_functions!(sp_envlogger; + wrap_functions!(envlogger; /// Call this function at the beginning of main to enable rust logging controlled by the /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). fn init() { diff --git a/src/macros.rs b/src/macros.rs index 3b35e1d..8f96746 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -223,7 +223,7 @@ macro_rules! wrap_functions { #[doc = ""] #[doc = concat!(" This function is part of the ", stringify!($module), " module.")] #[no_mangle] - pub unsafe extern "C" fn [< $module _ $function >]( + pub unsafe extern "C" fn [< sp_ $module _ $function >]( $($param_name: $param_type),* ) $(-> $return_type)? $block diff --git a/src/packet.rs b/src/packet.rs index 4278fe0..18d63bd 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -wrap_functions!(sp_packet; +wrap_functions!(packet; /// Tries to load a [Packet] from the passed array with the specified length. /// @@ -68,11 +68,11 @@ wrap_functions!(sp_packet; } ); -wrap_clone!(sp_packet::Packet); -wrap_free!(sp_packet::Packet); +wrap_clone!(packet::Packet); +wrap_free!(packet::Packet); wrap_fields!( - sp_packet::Packet; + packet::Packet; prop header: Header { get(); mut get(); diff --git a/src/udp.rs b/src/udp.rs index 7eebe27..fd907be 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -10,9 +10,9 @@ use std::{ ptr::NonNull, }; -wrap_free!(sp_udp::UdpSocket); +wrap_free!(udp::UdpSocket); -wrap_functions!(sp_udp; +wrap_functions!(udp; /// Creates a new instance of [UdpSocket]. /// From 8f13ba61f0d8966b164bb981f27668de12e0e22a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 00:14:06 +0200 Subject: [PATCH 53/76] remove _ from type names in function names --- include/servicepoint.h | 574 +++++++++++----------- src/commands/brightness_grid_command.rs | 6 +- src/commands/char_grid_command.rs | 6 +- src/commands/cp437_grid_command.rs | 6 +- src/commands/global_brightness_command.rs | 4 +- src/containers/brightness_grid.rs | 8 +- src/containers/char_grid.rs | 8 +- src/containers/cp437_grid.rs | 8 +- src/macros.rs | 2 +- 9 files changed, 311 insertions(+), 311 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 63b2226..0c6490f 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -638,7 +638,7 @@ extern "C" { /** *Clones a [`Bitmap`] instance. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); @@ -649,7 +649,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitmap. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); @@ -662,14 +662,14 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); * * - `value`: the value to set all pixels to * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** *Deallocates a [`Bitmap`] instance. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); @@ -680,7 +680,7 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * * Returns NULL in case of error. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); @@ -697,7 +697,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); * * - when accessing `x` or `y` out of bounds * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); @@ -706,14 +706,14 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); * * Gets the height in pixels. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** * Consumes the Bitmap and returns the contained BitVec. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); @@ -724,7 +724,7 @@ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); * * Returns NULL in case of an error. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, size_t x, @@ -741,7 +741,7 @@ struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, * * returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap *sp_bitmap_load(size_t width, size_t height, @@ -772,7 +772,7 @@ struct Bitmap *sp_bitmap_load(size_t width, * sp_bitmap_free(grid); * ``` * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap *sp_bitmap_new(size_t width, size_t height); @@ -781,7 +781,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); * * returns: [Bitmap] initialized to all pixels off. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); @@ -799,7 +799,7 @@ struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); * * - when accessing `x` or `y` out of bounds * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t x, @@ -811,7 +811,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, * * Gets the width in pixels. * - * This function is part of the bitmap module. + * This function is part of the `bitmap` module. */ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); @@ -822,14 +822,14 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); * * The returned memory is valid for the lifetime of the bitvec. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); /** *Clones a [`DisplayBitVec`] instance. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); @@ -842,14 +842,14 @@ BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); * * - `value`: the value to set all bits to * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ void sp_bitvec_free(BitVec */*notnull*/ instance); @@ -869,7 +869,7 @@ void sp_bitvec_free(BitVec */*notnull*/ instance); * * - when accessing `index` out of bounds * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); @@ -880,7 +880,7 @@ bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); * * Returns NULL in case of an error. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, size_t offset, @@ -892,7 +892,7 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, * * Returns true if length is 0. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); @@ -901,7 +901,7 @@ bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); * * Gets the length in bits. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ size_t sp_bitvec_len(BitVec */*notnull*/ instance); @@ -910,7 +910,7 @@ size_t sp_bitvec_len(BitVec */*notnull*/ instance); * * returns: [DisplayBitVec] instance containing data. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); @@ -927,7 +927,7 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); * * - when `size` is not divisible by 8. * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ BitVec */*notnull*/ sp_bitvec_new(size_t size); @@ -945,16 +945,16 @@ BitVec */*notnull*/ sp_bitvec_new(size_t size); * * - when accessing `index` out of bounds * - * This function is part of the bitvec module. + * This function is part of the `bitvec` module. */ void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); /** *Clones a [`BrightnessGrid`] instance. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); +BrightnessGrid */*notnull*/ sp_brightnessgrid_clone(BrightnessGrid */*notnull*/ instance); /** * Calls method [`servicepoint::BrightnessGrid::data_ref_mut`]. @@ -963,9 +963,9 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ * * The returned memory is valid for the lifetime of the grid. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); +struct ByteSlice sp_brightnessgrid_data_ref_mut(BrightnessGrid */*notnull*/ instance); /** * Calls method [`servicepoint::BrightnessGrid::fill`]. @@ -976,17 +976,17 @@ struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ ins * * - `value`: the value to set all cells to * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, - Brightness value); +void sp_brightnessgrid_fill(BrightnessGrid */*notnull*/ instance, + Brightness value); /** *Deallocates a [`BrightnessGrid`] instance. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); +void sp_brightnessgrid_free(BrightnessGrid */*notnull*/ instance); /** * Calls method [`servicepoint::BrightnessGrid::get`]. @@ -1002,20 +1002,20 @@ void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); * # Panics * - When accessing `x` or `y` out of bounds. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, - size_t x, - size_t y); +Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance, + size_t x, + size_t y); /** * Calls method [`servicepoint::BrightnessGrid::height`]. * * Gets the height of the grid. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); +size_t sp_brightnessgrid_height(BrightnessGrid */*notnull*/ instance); /** * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. @@ -1024,11 +1024,11 @@ size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_brightnessgrid_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -1037,11 +1037,11 @@ struct Packet *sp_brightness_grid_into_packet(BrightnessGrid */*notnull*/ grid, * * returns: new [BrightnessGrid] instance, or NULL in case of an error. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -BrightnessGrid *sp_brightness_grid_load(size_t width, - size_t height, - struct ByteSlice data); +BrightnessGrid *sp_brightnessgrid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [BrightnessGrid] with the specified dimensions. @@ -1062,9 +1062,9 @@ BrightnessGrid *sp_brightness_grid_load(size_t width, * sp_udp_free(connection); * ``` * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); +BrightnessGrid */*notnull*/ sp_brightnessgrid_new(size_t width, size_t height); /** * Calls method [`servicepoint::BrightnessGrid::set`]. @@ -1082,28 +1082,28 @@ BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); * * - When accessing `x` or `y` out of bounds. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, - size_t x, - size_t y, - Brightness value); +void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, + size_t x, + size_t y, + Brightness value); /** * Calls method [`servicepoint::BrightnessGrid::width`]. * * Gets the width of the grid. * - * This function is part of the brightness_grid module. + * This function is part of the `brightnessgrid` module. */ -size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); +size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); /** *Clones a [`CharGrid`] instance. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); +CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); /** * Calls method [`servicepoint::CharGrid::fill`]. @@ -1115,16 +1115,16 @@ CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); * - `value`: the value to set all cells to * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); +void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [`CharGrid`] instance. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -void sp_char_grid_free(CharGrid */*notnull*/ instance); +void sp_chargrid_free(CharGrid */*notnull*/ instance); /** * Calls method [`servicepoint::CharGrid::get`]. @@ -1139,18 +1139,18 @@ void sp_char_grid_free(CharGrid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); +uint32_t sp_chargrid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** * Calls method [`servicepoint::CharGrid::height`]. * * Gets the height of the grid. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -size_t sp_char_grid_height(CharGrid */*notnull*/ instance); +size_t sp_chargrid_height(CharGrid */*notnull*/ instance); /** * Creates a [CharGridCommand] and immediately turns that into a [Packet]. @@ -1159,20 +1159,20 @@ size_t sp_char_grid_height(CharGrid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -struct Packet *sp_char_grid_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_chargrid_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [CharGrid] with the specified dimensions from the provided data. * * returns: new CharGrid or NULL in case of an error * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); +CharGrid *sp_chargrid_load(size_t width, size_t height, struct ByteSlice data); /** * Creates a new [CharGrid] with the specified dimensions. @@ -1188,9 +1188,9 @@ CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); * sp_char_grid_free(grid); * ``` * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); +CharGrid */*notnull*/ sp_chargrid_new(size_t width, size_t height); /** * Calls method [`servicepoint::CharGrid::set`]. @@ -1209,33 +1209,33 @@ CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); * - when accessing `x` or `y` out of bounds * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -void sp_char_grid_set(CharGrid */*notnull*/ instance, - size_t x, - size_t y, - uint32_t value); +void sp_chargrid_set(CharGrid */*notnull*/ instance, + size_t x, + size_t y, + uint32_t value); /** * Calls method [`servicepoint::CharGrid::width`]. * * Gets the width of the grid. * - * This function is part of the char_grid module. + * This function is part of the `chargrid` module. */ -size_t sp_char_grid_width(CharGrid */*notnull*/ instance); +size_t sp_chargrid_width(CharGrid */*notnull*/ instance); /** *Clones a [`BitmapCommand`] instance. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); /** *Deallocates a [`BitmapCommand`] instance. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); @@ -1245,7 +1245,7 @@ void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); * * Rust equivalent: `BitmapCommand::from(bitmap)` * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); @@ -1255,21 +1255,21 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*not * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); /** * Reads the origin field of the [`BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, size_t */*notnull*/ origin_x, @@ -1282,7 +1282,7 @@ void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, * * Returns: a new [BitmapCommand] instance. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, size_t origin_x, @@ -1293,7 +1293,7 @@ struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ b * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); @@ -1301,7 +1301,7 @@ void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, CompressionCode value); @@ -1309,7 +1309,7 @@ void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, /** * Overwrites the origin field of the [`BitmapCommand`]. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_x, @@ -1320,21 +1320,21 @@ void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_bitmap module. + * This function is part of the `cmd_bitmap` module. */ struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); /** *Clones a [`BitVecCommand`] instance. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); /** *Deallocates a [`BitVecCommand`] instance. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); @@ -1344,28 +1344,28 @@ void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); @@ -1383,7 +1383,7 @@ BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ in * * The contained [`DisplayBitVec`] is always uncompressed. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, size_t offset, @@ -1394,7 +1394,7 @@ struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, BitVec */*notnull*/ value); @@ -1402,7 +1402,7 @@ void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, CompressionCode value); @@ -1410,7 +1410,7 @@ void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); @@ -1418,7 +1418,7 @@ void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, /** * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); @@ -1428,48 +1428,31 @@ void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_bitvec module. + * This function is part of the `cmd_bitvec` module. */ struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); /** - * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + *Clones a [`BrightnessGridCommand`] instance. * - * This function is part of the cmd_brightness_global module. + * This function is part of the `cmd_brightnessgrid` module. */ -Brightness sp_cmd_brightness_global_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - * Turns the command into a packet + *Deallocates a [`BrightnessGridCommand`] instance. * - * This function is part of the cmd_brightness_global module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct Packet */*notnull*/ sp_cmd_brightness_global_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); - -/** - * Set the brightness of all tiles to the same value. - * - * Returns: a new [GlobalBrightnessCommand] instance. - * - * This function is part of the cmd_brightness_global module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_brightness_global_new(Brightness brightness); - -/** - * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. - * - * This function is part of the cmd_brightness_global module. - */ -void sp_cmd_brightness_global_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, - Brightness value); +void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); /** * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], * leaving other fields as their default values. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(BrightnessGrid */*notnull*/ grid); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_from_grid(BrightnessGrid */*notnull*/ grid); /** * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. @@ -1477,27 +1460,27 @@ struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_from_grid(Brigh * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -BrightnessGrid */*notnull*/ sp_cmd_brightness_grid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); +BrightnessGrid */*notnull*/ sp_cmd_brightnessgrid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -void sp_cmd_brightness_grid_get_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_cmd_brightnessgrid_get_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** * Tries to turn a [BrightnessGridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand */*notnull*/ command); +struct Packet *sp_cmd_brightnessgrid_into_packet(struct BrightnessGridCommand */*notnull*/ command); /** * Set the brightness of individual tiles in a rectangular area of the display. @@ -1506,51 +1489,51 @@ struct Packet *sp_cmd_brightness_grid_into_packet(struct BrightnessGridCommand * * * Returns: a new [BrightnessGridCommand] instance. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightness_grid_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -void sp_cmd_brightness_grid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, - BrightnessGrid */*notnull*/ value); +void sp_cmd_brightnessgrid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, + BrightnessGrid */*notnull*/ value); /** * Overwrites the origin field of the [`BrightnessGridCommand`]. * - * This function is part of the cmd_brightness_grid module. + * This function is part of the `cmd_brightnessgrid` module. */ -void sp_cmd_brightness_grid_set_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_cmd_brightnessgrid_set_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** - *Clones a [`BrightnessGridCommand`] instance. + *Clones a [`CharGridCommand`] instance. * - * This function is part of the cmd_brightnessgrid module. + * This function is part of the `cmd_chargrid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); /** - *Deallocates a [`BrightnessGridCommand`] instance. + *Deallocates a [`CharGridCommand`] instance. * - * This function is part of the cmd_brightnessgrid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); +void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnull*/ grid); +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_from_grid(CharGrid */*notnull*/ grid); /** * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. @@ -1558,18 +1541,18 @@ struct CharGridCommand */*notnull*/ sp_cmd_char_grid_from_grid(CharGrid */*notnu * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -CharGrid */*notnull*/ sp_cmd_char_grid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); +CharGrid */*notnull*/ sp_cmd_chargrid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** * Reads the origin field of the [`CharGridCommand`]. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_cmd_chargrid_get_origin(struct CharGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** * Show UTF-8 encoded text on the screen. @@ -1578,64 +1561,50 @@ void sp_cmd_char_grid_get_origin(struct CharGridCommand */*notnull*/ command, * * Returns: a new [CharGridCommand] instance. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -struct CharGridCommand */*notnull*/ sp_cmd_char_grid_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_char_grid_set_grid(struct CharGridCommand */*notnull*/ instance, - CharGrid */*notnull*/ value); +void sp_cmd_chargrid_set_grid(struct CharGridCommand */*notnull*/ instance, + CharGrid */*notnull*/ value); /** * Overwrites the origin field of the [`CharGridCommand`]. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -void sp_cmd_char_grid_set_origin(struct CharGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_cmd_chargrid_set_origin(struct CharGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** * Tries to turn a [CharGridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_char_grid module. + * This function is part of the `cmd_chargrid` module. */ -struct Packet *sp_cmd_char_grid_try_into_packet(struct CharGridCommand */*notnull*/ command); - -/** - *Clones a [`CharGridCommand`] instance. - * - * This function is part of the cmd_chargrid module. - */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`CharGridCommand`] instance. - * - * This function is part of the cmd_chargrid module. - */ -void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); +struct Packet *sp_cmd_chargrid_try_into_packet(struct CharGridCommand */*notnull*/ command); /** *Clones a [`ClearCommand`] instance. * - * This function is part of the cmd_clear module. + * This function is part of the `cmd_clear` module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); /** *Deallocates a [`ClearCommand`] instance. * - * This function is part of the cmd_clear module. + * This function is part of the `cmd_clear` module. */ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); @@ -1646,17 +1615,31 @@ void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); * * Returns: a new [`ClearCommand`] instance. * - * This function is part of the cmd_clear module. + * This function is part of the `cmd_clear` module. */ struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); +/** + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the `cmd_cp437grid` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the `cmd_cp437grid` module. + */ +void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); + /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*notnull*/ grid); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_from_grid(Cp437Grid */*notnull*/ grid); /** * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. @@ -1664,18 +1647,18 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_from_grid(Cp437Grid */*no * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -Cp437Grid */*notnull*/ sp_cmd_cp437_grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); +Cp437Grid */*notnull*/ sp_cmd_cp437grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** * Reads the origin field of the [`Cp437GridCommand`]. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_cmd_cp437grid_get_origin(struct Cp437GridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** * Show text on the screen. @@ -1684,64 +1667,50 @@ void sp_cmd_cp437_grid_get_origin(struct Cp437GridCommand */*notnull*/ command, * * The origin is relative to the top-left of the display. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437_grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -void sp_cmd_cp437_grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, - Cp437Grid */*notnull*/ value); +void sp_cmd_cp437grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, + Cp437Grid */*notnull*/ value); /** * Overwrites the origin field of the [`Cp437GridCommand`]. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -void sp_cmd_cp437_grid_set_origin(struct Cp437GridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_cmd_cp437grid_set_origin(struct Cp437GridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** * Tries to turn a [Cp437GridCommand] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the cmd_cp437_grid module. + * This function is part of the `cmd_cp437grid` module. */ -struct Packet *sp_cmd_cp437_grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); - -/** - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the cmd_cp437grid module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the cmd_cp437grid module. - */ -void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); +struct Packet *sp_cmd_cp437grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); /** *Clones a [`FadeOutCommand`] instance. * - * This function is part of the cmd_fadeout module. + * This function is part of the `cmd_fadeout` module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the cmd_fadeout module. + * This function is part of the `cmd_fadeout` module. */ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); @@ -1750,7 +1719,7 @@ void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the cmd_fadeout module. + * This function is part of the `cmd_fadeout` module. */ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); @@ -1759,7 +1728,7 @@ struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); * * returns: a new [SPCommand] instance. * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ struct Command sp_cmd_generic_clone(struct Command command); @@ -1775,7 +1744,7 @@ struct Command sp_cmd_generic_clone(struct Command command); * sp_command_free(c); * ``` * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ void sp_cmd_generic_free(struct Command command); @@ -1785,7 +1754,7 @@ void sp_cmd_generic_free(struct Command command); * * Returns tag [CommandTag::Invalid] in case of an error. * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ struct Packet *sp_cmd_generic_into_packet(struct Command command); @@ -1796,35 +1765,66 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); * * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. * - * This function is part of the cmd_generic module. + * This function is part of the `cmd_generic` module. */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); /** *Clones a [`GlobalBrightnessCommand`] instance. * - * This function is part of the cmd_globalbrightness module. + * This function is part of the `cmd_globalbrightness` module. */ struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Deallocates a [`GlobalBrightnessCommand`] instance. * - * This function is part of the cmd_globalbrightness module. + * This function is part of the `cmd_globalbrightness` module. */ void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); +/** + * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `cmd_globalbrightness` module. + */ +Brightness sp_cmd_globalbrightness_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + * Turns the command into a packet + * + * This function is part of the `cmd_globalbrightness` module. + */ +struct Packet */*notnull*/ sp_cmd_globalbrightness_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); + +/** + * Set the brightness of all tiles to the same value. + * + * Returns: a new [GlobalBrightnessCommand] instance. + * + * This function is part of the `cmd_globalbrightness` module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_new(Brightness brightness); + +/** + * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `cmd_globalbrightness` module. + */ +void sp_cmd_globalbrightness_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); + /** *Clones a [`HardResetCommand`] instance. * - * This function is part of the cmd_hardreset module. + * This function is part of the `cmd_hardreset` module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the cmd_hardreset module. + * This function is part of the `cmd_hardreset` module. */ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); @@ -1835,16 +1835,16 @@ void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the cmd_hardreset module. + * This function is part of the `cmd_hardreset` module. */ struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); /** *Clones a [`Cp437Grid`] instance. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); +Cp437Grid */*notnull*/ sp_cp437grid_clone(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::data_ref_mut`]. @@ -1853,9 +1853,9 @@ Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); * * The returned memory is valid for the lifetime of the instance. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); +struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::fill`]. @@ -1867,16 +1867,16 @@ struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); +void sp_cp437grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** *Deallocates a [`Cp437Grid`] instance. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); +void sp_cp437grid_free(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::get`]. @@ -1891,18 +1891,18 @@ void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); +uint8_t sp_cp437grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** * Calls method [`servicepoint::Cp437Grid::height`]. * * Gets the height of the grid. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); +size_t sp_cp437grid_height(Cp437Grid */*notnull*/ instance); /** * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. @@ -1911,29 +1911,29 @@ size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); * * Returns NULL in case of an error. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -struct Packet *sp_cp437_grid_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_cp437grid_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -Cp437Grid *sp_cp437_grid_load(size_t width, - size_t height, - struct ByteSlice data); +Cp437Grid *sp_cp437grid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Cp437Grid] with the specified dimensions. * * returns: [Cp437Grid] initialized to 0. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); +Cp437Grid */*notnull*/ sp_cp437grid_new(size_t width, size_t height); /** * Calls method [`servicepoint::Cp437Grid::set`]. @@ -1951,41 +1951,41 @@ Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, - size_t x, - size_t y, - uint8_t value); +void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, + size_t x, + size_t y, + uint8_t value); /** * Calls method [`servicepoint::Cp437Grid::width`]. * * Gets the width of the grid. * - * This function is part of the cp437_grid module. + * This function is part of the `cp437grid` module. */ -size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); +size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); /** * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). * - * This function is part of the envlogger module. + * This function is part of the `envlogger` module. */ void sp_envlogger_init(void); /** *Clones a [`Packet`] instance. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** *Deallocates a [`Packet`] instance. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ void sp_packet_free(struct Packet */*notnull*/ instance); @@ -1994,7 +1994,7 @@ void sp_packet_free(struct Packet */*notnull*/ instance); * * returns: new instance. Will never return null. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); @@ -2002,7 +2002,7 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, /** * Gets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); @@ -2012,7 +2012,7 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); @@ -2023,7 +2023,7 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i * * The returned memory can be changed and will be valid until a new payload is set. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); @@ -2034,7 +2034,7 @@ struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); * * - if the buffer is not big enough to hold header+payload. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); @@ -2042,7 +2042,7 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, /** * Sets the value of field `header` of the [`servicepoint::Packet`]. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); @@ -2052,7 +2052,7 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, * * This makes previous payload pointers invalid. * - * This function is part of the packet module. + * This function is part of the `packet` module. */ void sp_packet_set_payload(struct Packet */*notnull*/ packet, struct ByteSlice data); @@ -2062,7 +2062,7 @@ void sp_packet_set_payload(struct Packet */*notnull*/ packet, * * returns: NULL in case of an error, pointer to the allocated packet otherwise * - * This function is part of the packet module. + * This function is part of the `packet` module. */ struct Packet *sp_packet_try_load(struct ByteSlice data); @@ -2071,7 +2071,7 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * * If the provided value is not valid, false is returned and result is not changed. * - * This function is part of the sp module. + * This function is part of the `sp` module. */ bool sp_sp_u16_to_command_code(uint16_t code, CommandCode *result); @@ -2079,7 +2079,7 @@ bool sp_sp_u16_to_command_code(uint16_t code, /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the udp module. + * This function is part of the `udp` module. */ void sp_udp_free(struct UdpSocket */*notnull*/ instance); @@ -2096,7 +2096,7 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ struct UdpSocket *sp_udp_open(char */*notnull*/ host); @@ -2113,7 +2113,7 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, uint8_t ip2, @@ -2134,7 +2134,7 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, struct Command command); @@ -2150,7 +2150,7 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the udp module. + * This function is part of the `udp` module. */ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); @@ -2162,7 +2162,7 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the udp module. + * This function is part of the `udp` module. */ bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, struct Packet */*notnull*/ packet); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7a10389..b66952d 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(cmd_brightness_grid::BrightnessGridCommand; +wrap_fields!(cmd_brightnessgrid::BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_brightness_grid::BrightnessGridCommand); +wrap_origin_accessors!(cmd_brightnessgrid::BrightnessGridCommand); -wrap_functions!(cmd_brightness_grid; +wrap_functions!(cmd_brightnessgrid; /// Set the brightness of individual tiles in a rectangular area of the display. /// diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 07765b8..f614808 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(cmd_char_grid::CharGridCommand; +wrap_fields!(cmd_chargrid::CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_char_grid::CharGridCommand); +wrap_origin_accessors!(cmd_chargrid::CharGridCommand); -wrap_functions!(cmd_char_grid; +wrap_functions!(cmd_chargrid; /// Show UTF-8 encoded text on the screen. /// diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 2e76e3c..4ecd058 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(cmd_cp437_grid::Cp437GridCommand; +wrap_fields!(cmd_cp437grid::Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_cp437_grid::Cp437GridCommand); +wrap_origin_accessors!(cmd_cp437grid::Cp437GridCommand); -wrap_functions!(cmd_cp437_grid; +wrap_functions!(cmd_cp437grid; /// Show text on the screen. /// diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6d00d7c..1b3fb34 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; -wrap_functions!(cmd_brightness_global; +wrap_functions!(cmd_globalbrightness; /// Set the brightness of all tiles to the same value. /// @@ -25,7 +25,7 @@ wrap_functions!(cmd_brightness_global; wrap_command!(GlobalBrightness); wrap_fields!( - cmd_brightness_global::GlobalBrightnessCommand; + cmd_globalbrightness::GlobalBrightnessCommand; prop brightness: Brightness { get(); set(value); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 4212ec5..01fc010 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::{mem::transmute, ptr::NonNull}; -wrap_functions!(brightness_grid; +wrap_functions!(brightnessgrid; /// Creates a new [BrightnessGrid] with the specified dimensions. /// @@ -71,11 +71,11 @@ wrap_functions!(brightness_grid; ); -wrap_clone!(brightness_grid::BrightnessGrid); -wrap_free!(brightness_grid::BrightnessGrid); +wrap_clone!(brightnessgrid::BrightnessGrid); +wrap_free!(brightnessgrid::BrightnessGrid); wrap_methods!( - brightness_grid::BrightnessGrid; + brightnessgrid::BrightnessGrid; /// Gets the current value at the specified position. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 2a3667b..a79af3a 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -wrap_functions!(char_grid; +wrap_functions!(chargrid; /// Creates a new [CharGrid] with the specified dimensions. /// @@ -58,11 +58,11 @@ wrap_functions!(char_grid; ); -wrap_clone!(char_grid::CharGrid); -wrap_free!(char_grid::CharGrid); +wrap_clone!(chargrid::CharGrid); +wrap_free!(chargrid::CharGrid); wrap_methods!( - char_grid::CharGrid; + chargrid::CharGrid; /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a344b1a..a78cecf 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -9,7 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(cp437_grid; +wrap_functions!(cp437grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// @@ -50,11 +50,11 @@ wrap_functions!(cp437_grid; ); -wrap_clone!(cp437_grid::Cp437Grid); -wrap_free!(cp437_grid::Cp437Grid); +wrap_clone!(cp437grid::Cp437Grid); +wrap_free!(cp437grid::Cp437Grid); wrap_methods!( - cp437_grid::Cp437Grid; + cp437grid::Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments diff --git a/src/macros.rs b/src/macros.rs index 8f96746..93e06b8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -221,7 +221,7 @@ macro_rules! wrap_functions { $( $(#[$meta])* #[doc = ""] - #[doc = concat!(" This function is part of the ", stringify!($module), " module.")] + #[doc = " This function is part of the `" $module "` module."] #[no_mangle] pub unsafe extern "C" fn [< sp_ $module _ $function >]( $($param_name: $param_type),* From 8116375fd023deabf7e313eaed2d83557eb2f8ae Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:16:45 +0200 Subject: [PATCH 54/76] more type name based naming --- example/Makefile | 2 +- example/src/announce.c | 28 +- example/src/brightness_tester.c | 14 +- example/src/header_logger.c | 42 +- example/src/helpers.h | 6 +- example/src/moving_line.c | 6 +- example/src/random_stuff.c | 2 +- example/src/undefined.c | 22 + example/src/wiping_clear.c | 6 +- include/servicepoint.h | 1184 +++++++++++---------- src/commands/bitmap_command.rs | 6 +- src/commands/bitvec_command.rs | 4 +- src/commands/brightness_grid_command.rs | 6 +- src/commands/cc_only_commands.rs | 24 +- src/commands/char_grid_command.rs | 4 +- src/commands/cp437_grid_command.rs | 4 +- src/commands/global_brightness_command.rs | 5 +- src/commands/mod.rs | 60 +- src/containers/bitmap.rs | 7 +- src/containers/bitvec.rs | 7 +- src/containers/brightness_grid.rs | 7 +- src/containers/byte_slice.rs | 1 + src/containers/char_grid.rs | 7 +- src/containers/cp437_grid.rs | 7 +- src/macros.rs | 65 +- src/packet.rs | 7 +- src/udp.rs | 4 +- 27 files changed, 787 insertions(+), 750 deletions(-) create mode 100644 example/src/undefined.c diff --git a/example/Makefile b/example/Makefile index 0a56e1a..15e53d1 100644 --- a/example/Makefile +++ b/example/Makefile @@ -96,7 +96,7 @@ endif # ADD NEW EXAMPLES HERE _c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ - src/moving_line.c src/random_stuff.c src/wiping_clear.c + src/moving_line.c src/random_stuff.c src/wiping_clear.c src/undefined.c _programs := $(basename $(notdir $(_c_src))) _bins := $(_programs) _unstripped_bins := $(addsuffix _unstripped, $(_bins)) diff --git a/example/src/announce.c b/example/src/announce.c index bfdc48f..33da334 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -3,27 +3,27 @@ int main(void) { sock_init(); - sp_udp_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); + sp_udpsocket_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); - CharGrid *grid = sp_char_grid_new(5, 2); + CharGrid *grid = sp_chargrid_new(5, 2); if (grid == NULL) return 1; - sp_char_grid_set(grid, 0, 0, 'H'); - sp_char_grid_set(grid, 1, 0, 'e'); - sp_char_grid_set(grid, 2, 0, 'l'); - sp_char_grid_set(grid, 3, 0, 'l'); - sp_char_grid_set(grid, 4, 0, 'o'); - sp_char_grid_set(grid, 0, 1, 'W'); - sp_char_grid_set(grid, 1, 1, 'o'); - sp_char_grid_set(grid, 2, 1, 'r'); - sp_char_grid_set(grid, 3, 1, 'l'); - sp_char_grid_set(grid, 4, 1, 'd'); + sp_chargrid_set(grid, 0, 0, 'H'); + sp_chargrid_set(grid, 1, 0, 'e'); + sp_chargrid_set(grid, 2, 0, 'l'); + sp_chargrid_set(grid, 3, 0, 'l'); + sp_chargrid_set(grid, 4, 0, 'o'); + sp_chargrid_set(grid, 0, 1, 'W'); + sp_chargrid_set(grid, 1, 1, 'o'); + sp_chargrid_set(grid, 2, 1, 'r'); + sp_chargrid_set(grid, 3, 1, 'l'); + sp_chargrid_set(grid, 4, 1, 'd'); - Packet *packet = sp_char_grid_into_packet(grid, 0, 0); + Packet *packet = sp_chargrid_into_packet(grid, 0, 0); if (packet == NULL) return 1; - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); return 0; } diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 2257162..9d17dc7 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -5,14 +5,14 @@ void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); sp_bitmap_fill(all_on, true); - BitmapCommand *bitmapCommand = sp_cmd_bitmap_from_bitmap(all_on); - Packet *packet = sp_cmd_bitmap_try_into_packet(bitmapCommand); + BitmapCommand *bitmapCommand = sp_bitmapcommand_from_bitmap(all_on); + Packet *packet = sp_bitmapcommand_try_into_packet(bitmapCommand); if (packet != NULL) - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); } void make_brightness_pattern(BrightnessGrid *grid) { - ByteSlice slice = sp_brightness_grid_data_ref_mut(grid); + ByteSlice slice = sp_brightnessgrid_data_ref_mut(grid); for (size_t index = 0; index < slice.length; index++) { slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX)); } @@ -23,13 +23,13 @@ int main(void) { enable_all_pixels(); - BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT); + BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT); make_brightness_pattern(grid); - Packet *packet = sp_cmd_brightness_grid_into_packet(sp_cmd_brightness_grid_from_grid(grid)); + Packet *packet = sp_brightnessgridcommand_into_packet(sp_brightnessgridcommand_from_grid(grid)); if (packet == NULL) return -2; - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); return 0; } diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 5f04ac4..ec421dd 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -28,12 +28,12 @@ bool log_command(struct Command command) { case COMMAND_TAG_BITMAP: { BitmapCommand *bitmapCommand = command.data.bitmap; - CompressionCode compression = sp_cmd_bitmap_get_compression(bitmapCommand); + CompressionCode compression = sp_bitmapcommand_get_compression(bitmapCommand); size_t x, y; - sp_cmd_bitmap_get_origin(bitmapCommand, &x, &y); + sp_bitmapcommand_get_origin(bitmapCommand, &x, &y); - Bitmap *bitmap = sp_cmd_bitmap_get_bitmap_mut(bitmapCommand); + Bitmap *bitmap = sp_bitmapcommand_get_bitmap_mut(bitmapCommand); size_t w = sp_bitmap_width(bitmap); size_t h = sp_bitmap_height(bitmap); @@ -45,11 +45,11 @@ bool log_command(struct Command command) { BrightnessGridCommand *gridCommand = command.data.brightness_grid; size_t x, y; - sp_cmd_brightness_grid_get_origin(gridCommand, &x, &y); + sp_brightnessgridcommand_get_origin(gridCommand, &x, &y); - BrightnessGrid *grid = sp_cmd_brightness_grid_get_grid_mut(gridCommand); - size_t w = sp_brightness_grid_width(grid); - size_t h = sp_brightness_grid_height(grid); + BrightnessGrid *grid = sp_brightnessgridcommand_get_grid_mut(gridCommand); + size_t w = sp_brightnessgrid_width(grid); + size_t h = sp_brightnessgrid_height(grid); printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -59,11 +59,11 @@ bool log_command(struct Command command) { CharGridCommand *gridCommand = command.data.char_grid; size_t x, y; - sp_cmd_char_grid_get_origin(gridCommand, &x, &y); + sp_chargridcommand_get_origin(gridCommand, &x, &y); - CharGrid *grid = sp_cmd_char_grid_get_grid_mut(gridCommand); - size_t w = sp_char_grid_width(grid); - size_t h = sp_char_grid_height(grid); + CharGrid *grid = sp_chargridcommand_get_grid_mut(gridCommand); + size_t w = sp_chargrid_width(grid); + size_t h = sp_chargrid_height(grid); printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -73,11 +73,11 @@ bool log_command(struct Command command) { Cp437GridCommand *gridCommand = command.data.cp437_grid; size_t x, y; - sp_cmd_cp437_grid_get_origin(gridCommand, &x, &y); + sp_cp437gridcommand_get_origin(gridCommand, &x, &y); - Cp437Grid *grid = sp_cmd_cp437_grid_get_grid_mut(gridCommand); - size_t w = sp_cp437_grid_width(grid); - size_t h = sp_cp437_grid_height(grid); + Cp437Grid *grid = sp_cp437gridcommand_get_grid_mut(gridCommand); + size_t w = sp_cp437grid_width(grid); + size_t h = sp_cp437grid_height(grid); printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -86,10 +86,10 @@ bool log_command(struct Command command) { case COMMAND_TAG_BIT_VEC: { BitVecCommand *bitvecCommand = command.data.bit_vec; - size_t offset = sp_cmd_bitvec_get_offset(bitvecCommand); - CompressionCode compression = sp_cmd_bitvec_get_compression(bitvecCommand); + size_t offset = sp_bitveccommand_get_offset(bitvecCommand); + CompressionCode compression = sp_bitveccommand_get_compression(bitvecCommand); - BinaryOperation operation = sp_cmd_bitvec_get_operation(bitvecCommand); + BinaryOperation operation = sp_bitveccommand_get_operation(bitvecCommand); char *operationText; switch (operation) { case BINARY_OPERATION_AND: @@ -109,8 +109,8 @@ bool log_command(struct Command command) { break; } - BitVec *bitvec = sp_cmd_bitvec_get_bitvec_mut(bitvecCommand); - size_t len = sp_bitvec_len(bitvec); + BitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); + size_t len = sp_displaybitvec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", offset, len, compression, operationText); @@ -129,7 +129,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_cmd_brightness_global_get_brightness(command.data.global_brightness); + Brightness brightness = sp_globalbrightnesscommand_get_brightness(command.data.global_brightness); printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); break; } diff --git a/example/src/helpers.h b/example/src/helpers.h index bc6c119..ee415f2 100644 --- a/example/src/helpers.h +++ b/example/src/helpers.h @@ -10,12 +10,12 @@ static UdpSocket *sock = NULL; void sock_free() { - sp_udp_free(sock); + sp_udpsocket_free(sock); } void sock_init() { - //sock = sp_udp_open_ipv4(127, 0, 0, 1, 2342); - sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + sock = sp_udpsocket_open_ipv4(127, 0, 0, 1, 2342); + //sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); if (sock == NULL) exit(-1); atexit(sock_free); diff --git a/example/src/moving_line.c b/example/src/moving_line.c index 35ba20d..12a1a9b 100644 --- a/example/src/moving_line.c +++ b/example/src/moving_line.c @@ -13,14 +13,14 @@ int main(void) { sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true); } - BitmapCommand *command = sp_cmd_bitmap_from_bitmap(sp_bitmap_clone(bitmap)); - Packet *packet = sp_cmd_bitmap_try_into_packet(command); + BitmapCommand *command = sp_bitmapcommand_from_bitmap(sp_bitmap_clone(bitmap)); + Packet *packet = sp_bitmapcommand_try_into_packet(command); if (packet == NULL) { result = -2; goto exit; } - if (!sp_udp_send_packet(sock, packet)) { + if (!sp_udpsocket_send_packet(sock, packet)) { result = -3; goto exit; } diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index b55e42c..8838123 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -18,6 +18,6 @@ int main(void) { Header *header = sp_packet_get_header_mut(packet); printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d); - sp_udp_send_packet(sock, packet); + sp_udpsocket_send_packet(sock, packet); return 0; } diff --git a/example/src/undefined.c b/example/src/undefined.c new file mode 100644 index 0000000..44aaf6d --- /dev/null +++ b/example/src/undefined.c @@ -0,0 +1,22 @@ +#include +#include "servicepoint.h" +#include "helpers.h" + +/// DO NOT DO ANY OF THIS! +int main(void) { + BitmapCommand *bmcmd = sp_bitmapcommand_new(sp_bitmap_new_max_sized(), 0, 0, COMPRESSION_CODE_UNCOMPRESSED); + BitVecCommand *bvcmd = (BitVecCommand *) bmcmd; + sp_bitveccommand_free(bvcmd); + + uint8_t *data = calloc(1024, 1); + struct Command generic = { + .tag = COMMAND_TAG_BRIGHTNESS_GRID, + .data = {.null = data}, + }; + + sock_init(); + + sp_udpsocket_send_command(sock, generic); + + return 0; +} diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c index a3f7017..3984169 100644 --- a/example/src/wiping_clear.c +++ b/example/src/wiping_clear.c @@ -14,13 +14,13 @@ int main() { } BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); - BitVecCommand *command = sp_cmd_bitvec_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); - Packet *packet = sp_cmd_bitvec_try_into_packet(command); + BitVecCommand *command = sp_bitveccommand_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); + Packet *packet = sp_bitveccommand_try_into_packet(command); if (packet == NULL) { result = -2; goto exit; } - if (!sp_udp_send_packet(sock, packet)) { + if (!sp_udpsocket_send_packet(sock, packet)) { result = -3; goto exit; } diff --git a/include/servicepoint.h b/include/servicepoint.h index 0c6490f..e0e6cc4 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -476,6 +476,11 @@ typedef struct ByteSlice { size_t length; } ByteSlice; +/** + * Type alias for documenting the meaning of the u16 in enum values + */ +typedef size_t Offset; + /** * A grid containing brightness values. * @@ -542,11 +547,6 @@ typedef uint8_t Brightness; */ typedef struct ValueGrid_char CharGrid; -/** - * Type alias for documenting the meaning of the u16 in enum values - */ -typedef size_t Offset; - /** * A grid containing codepage 437 characters. * @@ -816,62 +816,103 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + *Clones a [`BitmapCommand`] instance. * - * Gets an unsafe reference to the data of the [DisplayBitVec] instance. - * - * The returned memory is valid for the lifetime of the bitvec. - * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -struct ByteSlice sp_bitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); +struct BitmapCommand */*notnull*/ sp_bitmapcommand_clone(struct BitmapCommand */*notnull*/ instance); /** - *Clones a [`DisplayBitVec`] instance. + *Deallocates a [`BitmapCommand`] instance. * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -BitVec */*notnull*/ sp_bitvec_clone(BitVec */*notnull*/ instance); +void sp_bitmapcommand_free(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::fill`]. + * Move the provided [Bitmap] into a new [BitmapCommand], + * leaving other fields as their default values. * - * Sets the value of all bits. + * Rust equivalent: `BitmapCommand::from(bitmap)` * - * # Arguments - * - * - `value`: the value to set all bits to - * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -void sp_bitvec_fill(BitVec */*notnull*/ instance, bool value); +struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** - *Deallocates a [`DisplayBitVec`] instance. + * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. * - * This function is part of the `bitvec` module. + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `bitmapcommand` module. */ -void sp_bitvec_free(BitVec */*notnull*/ instance); +struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::get`]. + * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * - * Gets the value of a bit. - * - * # Arguments - * - * - `bit_vec`: instance to read from - * - `index`: the bit index to read - * - * returns: value of the bit - * - * # Panics - * - * - when accessing `index` out of bounds - * - * This function is part of the `bitvec` module. + * This function is part of the `bitmapcommand` module. */ -bool sp_bitvec_get(BitVec */*notnull*/ instance, size_t index); +CompressionCode sp_bitmapcommand_get_compression(struct BitmapCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`BitmapCommand`]. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_get_origin(struct BitmapCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets a window of pixels to the specified values. + * + * The passed [Bitmap] gets consumed. + * + * Returns: a new [BitmapCommand] instance. + * + * This function is part of the `bitmapcommand` module. + */ +struct BitmapCommand */*notnull*/ sp_bitmapcommand_new(struct Bitmap */*notnull*/ bitmap, + size_t origin_x, + size_t origin_y, + CompressionCode compression); + +/** + * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_set_bitmap(struct BitmapCommand */*notnull*/ instance, + struct Bitmap */*notnull*/ value); + +/** + * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_set_compression(struct BitmapCommand */*notnull*/ instance, + CompressionCode value); + +/** + * Overwrites the origin field of the [`BitmapCommand`]. + * + * This function is part of the `bitmapcommand` module. + */ +void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + * Tries to turn a [BitmapCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `bitmapcommand` module. + */ +struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ command); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -887,24 +928,6 @@ struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, BinaryOperation operation, CompressionCode compression); -/** - * Calls method [`servicepoint::DisplayBitVec::is_empty`]. - * - * Returns true if length is 0. - * - * This function is part of the `bitvec` module. - */ -bool sp_bitvec_is_empty(BitVec */*notnull*/ instance); - -/** - * Calls method [`servicepoint::DisplayBitVec::len`]. - * - * Gets the length in bits. - * - * This function is part of the `bitvec` module. - */ -size_t sp_bitvec_len(BitVec */*notnull*/ instance); - /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. * @@ -932,22 +955,112 @@ BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); BitVec */*notnull*/ sp_bitvec_new(size_t size); /** - * Calls method [`servicepoint::DisplayBitVec::set`]. + *Clones a [`BitVecCommand`] instance. * - * Sets the value of a bit. - * - * # Arguments - * - * - `index`: the bit index to edit - * - `value`: the value to set the bit to - * - * # Panics - * - * - when accessing `index` out of bounds - * - * This function is part of the `bitvec` module. + * This function is part of the `bitveccommand` module. */ -void sp_bitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); +struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */*notnull*/ instance); + +/** + *Deallocates a [`BitVecCommand`] instance. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `bitveccommand` module. + */ +BitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +Offset sp_bitveccommand_get_offset(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +BinaryOperation sp_bitveccommand_get_operation(struct BitVecCommand */*notnull*/ instance); + +/** + * Set pixel data starting at the pixel offset on screen. + * + * The screen will continuously overwrite more pixel data without regarding the offset, meaning + * once the starting row is full, overwriting will continue on column 0. + * + * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. + * + * `new_bit = old_bit op sent_bit` + * + * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. + * + * The contained [`DisplayBitVec`] is always uncompressed. + * + * This function is part of the `bitveccommand` module. + */ +struct BitVecCommand */*notnull*/ sp_bitveccommand_new(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + +/** + * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, + BitVec */*notnull*/ value); + +/** + * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_compression(struct BitVecCommand */*notnull*/ instance, + CompressionCode value); + +/** + * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_offset(struct BitVecCommand */*notnull*/ instance, + Offset value); + +/** + * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bitveccommand` module. + */ +void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, + BinaryOperation value); + +/** + * Tries to turn a [BitVecCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `bitveccommand` module. + */ +struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ command); /** *Clones a [`BrightnessGrid`] instance. @@ -1098,6 +1211,87 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, */ size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); +/** + *Clones a [`BrightnessGridCommand`] instance. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_clone(struct BrightnessGridCommand */*notnull*/ instance); + +/** + *Deallocates a [`BrightnessGridCommand`] instance. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_free(struct BrightnessGridCommand */*notnull*/ instance); + +/** + * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + * leaving other fields as their default values. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(BrightnessGrid */*notnull*/ grid); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `brightnessgridcommand` module. + */ +BrightnessGrid */*notnull*/ sp_brightnessgridcommand_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_get_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Tries to turn a [BrightnessGridCommand] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct Packet *sp_brightnessgridcommand_into_packet(struct BrightnessGridCommand */*notnull*/ command); + +/** + * Set the brightness of individual tiles in a rectangular area of the display. + * + * The passed [BrightnessGrid] gets consumed. + * + * Returns: a new [BrightnessGridCommand] instance. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_set_grid(struct BrightnessGridCommand */*notnull*/ instance, + BrightnessGrid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the `brightnessgridcommand` module. + */ +void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + /** *Clones a [`CharGrid`] instance. * @@ -1225,307 +1419,81 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, */ size_t sp_chargrid_width(CharGrid */*notnull*/ instance); -/** - *Clones a [`BitmapCommand`] instance. - * - * This function is part of the `cmd_bitmap` module. - */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_clone(struct BitmapCommand */*notnull*/ instance); - -/** - *Deallocates a [`BitmapCommand`] instance. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_free(struct BitmapCommand */*notnull*/ instance); - -/** - * Move the provided [Bitmap] into a new [BitmapCommand], - * leaving other fields as their default values. - * - * Rust equivalent: `BitmapCommand::from(bitmap)` - * - * This function is part of the `cmd_bitmap` module. - */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_from_bitmap(struct Bitmap */*notnull*/ bitmap); - -/** - * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_bitmap` module. - */ -struct Bitmap */*notnull*/ sp_cmd_bitmap_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); - -/** - * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -CompressionCode sp_cmd_bitmap_get_compression(struct BitmapCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_get_origin(struct BitmapCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Sets a window of pixels to the specified values. - * - * The passed [Bitmap] gets consumed. - * - * Returns: a new [BitmapCommand] instance. - * - * This function is part of the `cmd_bitmap` module. - */ -struct BitmapCommand */*notnull*/ sp_cmd_bitmap_new(struct Bitmap */*notnull*/ bitmap, - size_t origin_x, - size_t origin_y, - CompressionCode compression); - -/** - * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_set_bitmap(struct BitmapCommand */*notnull*/ instance, - struct Bitmap */*notnull*/ value); - -/** - * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_set_compression(struct BitmapCommand */*notnull*/ instance, - CompressionCode value); - -/** - * Overwrites the origin field of the [`BitmapCommand`]. - * - * This function is part of the `cmd_bitmap` module. - */ -void sp_cmd_bitmap_set_origin(struct BitmapCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - -/** - * Tries to turn a [BitmapCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_bitmap` module. - */ -struct Packet *sp_cmd_bitmap_try_into_packet(struct BitmapCommand */*notnull*/ command); - -/** - *Clones a [`BitVecCommand`] instance. - * - * This function is part of the `cmd_bitvec` module. - */ -struct BitVecCommand */*notnull*/ sp_cmd_bitvec_clone(struct BitVecCommand */*notnull*/ instance); - -/** - *Deallocates a [`BitVecCommand`] instance. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_free(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_bitvec` module. - */ -BitVec */*notnull*/ sp_cmd_bitvec_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -CompressionCode sp_cmd_bitvec_get_compression(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -Offset sp_cmd_bitvec_get_offset(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -BinaryOperation sp_cmd_bitvec_get_operation(struct BitVecCommand */*notnull*/ instance); - -/** - * Set pixel data starting at the pixel offset on screen. - * - * The screen will continuously overwrite more pixel data without regarding the offset, meaning - * once the starting row is full, overwriting will continue on column 0. - * - * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. - * - * `new_bit = old_bit op sent_bit` - * - * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. - * - * The contained [`DisplayBitVec`] is always uncompressed. - * - * This function is part of the `cmd_bitvec` module. - */ -struct BitVecCommand */*notnull*/ sp_cmd_bitvec_new(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); - -/** - * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_bitvec(struct BitVecCommand */*notnull*/ instance, - BitVec */*notnull*/ value); - -/** - * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_compression(struct BitVecCommand */*notnull*/ instance, - CompressionCode value); - -/** - * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_offset(struct BitVecCommand */*notnull*/ instance, - Offset value); - -/** - * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `cmd_bitvec` module. - */ -void sp_cmd_bitvec_set_operation(struct BitVecCommand */*notnull*/ instance, - BinaryOperation value); - -/** - * Tries to turn a [BitVecCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_bitvec` module. - */ -struct Packet *sp_cmd_bitvec_try_into_packet(struct BitVecCommand */*notnull*/ command); - -/** - *Clones a [`BrightnessGridCommand`] instance. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_clone(struct BrightnessGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`BrightnessGridCommand`] instance. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_free(struct BrightnessGridCommand */*notnull*/ instance); - -/** - * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_from_grid(BrightnessGrid */*notnull*/ grid); - -/** - * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -BrightnessGrid */*notnull*/ sp_cmd_brightnessgrid_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`BrightnessGridCommand`]. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_get_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Tries to turn a [BrightnessGridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct Packet *sp_cmd_brightnessgrid_into_packet(struct BrightnessGridCommand */*notnull*/ command); - -/** - * Set the brightness of individual tiles in a rectangular area of the display. - * - * The passed [BrightnessGrid] gets consumed. - * - * Returns: a new [BrightnessGridCommand] instance. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -struct BrightnessGridCommand */*notnull*/ sp_cmd_brightnessgrid_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - -/** - * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_set_grid(struct BrightnessGridCommand */*notnull*/ instance, - BrightnessGrid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`BrightnessGridCommand`]. - * - * This function is part of the `cmd_brightnessgrid` module. - */ -void sp_cmd_brightnessgrid_set_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - /** *Clones a [`CharGridCommand`] instance. * - * This function is part of the `cmd_chargrid` module. + * This function is part of the `chargridcommand` module. */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_clone(struct CharGridCommand */*notnull*/ instance); +struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridCommand */*notnull*/ instance); /** *Deallocates a [`CharGridCommand`] instance. * - * This function is part of the `cmd_chargrid` module. + * This function is part of the `chargridcommand` module. */ -void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); +void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `chargridcommand` module. + */ +CharGrid */*notnull*/ sp_chargridcommand_get_grid_mut(struct CharGridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`CharGridCommand`]. + * + * This function is part of the `chargridcommand` module. + */ +void sp_chargridcommand_get_origin(struct CharGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `chargridcommand` module. + */ +void sp_chargridcommand_set_grid(struct CharGridCommand */*notnull*/ instance, + CharGrid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`CharGridCommand`]. + * + * This function is part of the `chargridcommand` module. + */ +void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + *Clones a [`ClearCommand`] instance. + * + * This function is part of the `clearcommand` module. + */ +struct ClearCommand */*notnull*/ sp_clearcommand_clone(struct ClearCommand */*notnull*/ instance); + +/** + *Deallocates a [`ClearCommand`] instance. + * + * This function is part of the `clearcommand` module. + */ +void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); + +/** + * Set all pixels to the off state. + * + * Does not affect brightness. + * + * Returns: a new [`ClearCommand`] instance. + * + * This function is part of the `clearcommand` module. + */ +struct ClearCommand */*notnull*/ sp_clearcommand_new(void); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1535,25 +1503,6 @@ void sp_cmd_chargrid_free(struct CharGridCommand */*notnull*/ instance); */ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_from_grid(CharGrid */*notnull*/ grid); -/** - * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_chargrid` module. - */ -CharGrid */*notnull*/ sp_cmd_chargrid_get_grid_mut(struct CharGridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`CharGridCommand`]. - * - * This function is part of the `cmd_chargrid` module. - */ -void sp_cmd_chargrid_get_origin(struct CharGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - /** * Show UTF-8 encoded text on the screen. * @@ -1567,24 +1516,6 @@ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ gr size_t origin_x, size_t origin_y); -/** - * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_chargrid` module. - */ -void sp_cmd_chargrid_set_grid(struct CharGridCommand */*notnull*/ instance, - CharGrid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`CharGridCommand`]. - * - * This function is part of the `cmd_chargrid` module. - */ -void sp_cmd_chargrid_set_origin(struct CharGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - /** * Tries to turn a [CharGridCommand] into a [Packet]. * @@ -1594,45 +1525,6 @@ void sp_cmd_chargrid_set_origin(struct CharGridCommand */*notnull*/ command, */ struct Packet *sp_cmd_chargrid_try_into_packet(struct CharGridCommand */*notnull*/ command); -/** - *Clones a [`ClearCommand`] instance. - * - * This function is part of the `cmd_clear` module. - */ -struct ClearCommand */*notnull*/ sp_cmd_clear_clone(struct ClearCommand */*notnull*/ instance); - -/** - *Deallocates a [`ClearCommand`] instance. - * - * This function is part of the `cmd_clear` module. - */ -void sp_cmd_clear_free(struct ClearCommand */*notnull*/ instance); - -/** - * Set all pixels to the off state. - * - * Does not affect brightness. - * - * Returns: a new [`ClearCommand`] instance. - * - * This function is part of the `cmd_clear` module. - */ -struct ClearCommand */*notnull*/ sp_cmd_clear_new(void); - -/** - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); - /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. @@ -1641,25 +1533,6 @@ void sp_cmd_cp437grid_free(struct Cp437GridCommand */*notnull*/ instance); */ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_from_grid(Cp437Grid */*notnull*/ grid); -/** - * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cmd_cp437grid` module. - */ -Cp437Grid */*notnull*/ sp_cmd_cp437grid_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`Cp437GridCommand`]. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_get_origin(struct Cp437GridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - /** * Show text on the screen. * @@ -1673,24 +1546,6 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ size_t origin_x, size_t origin_y); -/** - * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_set_grid(struct Cp437GridCommand */*notnull*/ instance, - Cp437Grid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`Cp437GridCommand`]. - * - * This function is part of the `cmd_cp437grid` module. - */ -void sp_cmd_cp437grid_set_origin(struct Cp437GridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - /** * Tries to turn a [Cp437GridCommand] into a [Packet]. * @@ -1700,29 +1555,6 @@ void sp_cmd_cp437grid_set_origin(struct Cp437GridCommand */*notnull*/ command, */ struct Packet *sp_cmd_cp437grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); -/** - *Clones a [`FadeOutCommand`] instance. - * - * This function is part of the `cmd_fadeout` module. - */ -struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_clone(struct FadeOutCommand */*notnull*/ instance); - -/** - *Deallocates a [`FadeOutCommand`] instance. - * - * This function is part of the `cmd_fadeout` module. - */ -void sp_cmd_fadeout_free(struct FadeOutCommand */*notnull*/ instance); - -/** - * A yet-to-be-tested command. - * - * Returns: a new [`FadeOutCommand`] instance. - * - * This function is part of the `cmd_fadeout` module. - */ -struct FadeOutCommand */*notnull*/ sp_cmd_fadeout_new(void); - /** * Clones an [SPCommand] instance. * @@ -1769,76 +1601,6 @@ struct Packet *sp_cmd_generic_into_packet(struct Command command); */ struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); -/** - *Clones a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the `cmd_globalbrightness` module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_clone(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - *Deallocates a [`GlobalBrightnessCommand`] instance. - * - * This function is part of the `cmd_globalbrightness` module. - */ -void sp_cmd_globalbrightness_free(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. - * - * This function is part of the `cmd_globalbrightness` module. - */ -Brightness sp_cmd_globalbrightness_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); - -/** - * Turns the command into a packet - * - * This function is part of the `cmd_globalbrightness` module. - */ -struct Packet */*notnull*/ sp_cmd_globalbrightness_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); - -/** - * Set the brightness of all tiles to the same value. - * - * Returns: a new [GlobalBrightnessCommand] instance. - * - * This function is part of the `cmd_globalbrightness` module. - */ -struct GlobalBrightnessCommand */*notnull*/ sp_cmd_globalbrightness_new(Brightness brightness); - -/** - * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. - * - * This function is part of the `cmd_globalbrightness` module. - */ -void sp_cmd_globalbrightness_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, - Brightness value); - -/** - *Clones a [`HardResetCommand`] instance. - * - * This function is part of the `cmd_hardreset` module. - */ -struct HardResetCommand */*notnull*/ sp_cmd_hardreset_clone(struct HardResetCommand */*notnull*/ instance); - -/** - *Deallocates a [`HardResetCommand`] instance. - * - * This function is part of the `cmd_hardreset` module. - */ -void sp_cmd_hardreset_free(struct HardResetCommand */*notnull*/ instance); - -/** - * Kills the udp daemon on the display, which usually results in a restart. - * - * Please do not send this in your normal program flow. - * - * Returns: a new [`HardResetCommand`] instance. - * - * This function is part of the `cmd_hardreset` module. - */ -struct HardResetCommand */*notnull*/ sp_cmd_hardreset_new(void); - /** *Clones a [`Cp437Grid`] instance. * @@ -1967,6 +1729,153 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, */ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); +/** + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `cp437gridcommand` module. + */ +Cp437Grid */*notnull*/ sp_cp437gridcommand_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`Cp437GridCommand`]. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_get_origin(struct Cp437GridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_set_grid(struct Cp437GridCommand */*notnull*/ instance, + Cp437Grid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`Cp437GridCommand`]. + * + * This function is part of the `cp437gridcommand` module. + */ +void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + * + * Gets an unsafe reference to the data of the [DisplayBitVec] instance. + * + * The returned memory is valid for the lifetime of the bitvec. + * + * This function is part of the `displaybitvec` module. + */ +struct ByteSlice sp_displaybitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); + +/** + *Clones a [`DisplayBitVec`] instance. + * + * This function is part of the `displaybitvec` module. + */ +BitVec */*notnull*/ sp_displaybitvec_clone(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::fill`]. + * + * Sets the value of all bits. + * + * # Arguments + * + * - `value`: the value to set all bits to + * + * This function is part of the `displaybitvec` module. + */ +void sp_displaybitvec_fill(BitVec */*notnull*/ instance, bool value); + +/** + *Deallocates a [`DisplayBitVec`] instance. + * + * This function is part of the `displaybitvec` module. + */ +void sp_displaybitvec_free(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::get`]. + * + * Gets the value of a bit. + * + * # Arguments + * + * - `bit_vec`: instance to read from + * - `index`: the bit index to read + * + * returns: value of the bit + * + * # Panics + * + * - when accessing `index` out of bounds + * + * This function is part of the `displaybitvec` module. + */ +bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); + +/** + * Calls method [`servicepoint::DisplayBitVec::is_empty`]. + * + * Returns true if length is 0. + * + * This function is part of the `displaybitvec` module. + */ +bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::len`]. + * + * Gets the length in bits. + * + * This function is part of the `displaybitvec` module. + */ +size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); + +/** + * Calls method [`servicepoint::DisplayBitVec::set`]. + * + * Sets the value of a bit. + * + * # Arguments + * + * - `index`: the bit index to edit + * - `value`: the value to set the bit to + * + * # Panics + * + * - when accessing `index` out of bounds + * + * This function is part of the `displaybitvec` module. + */ +void sp_displaybitvec_set(BitVec */*notnull*/ instance, + size_t index, + bool value); + /** * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). @@ -1975,6 +1884,99 @@ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); */ void sp_envlogger_init(void); +/** + *Clones a [`FadeOutCommand`] instance. + * + * This function is part of the `fadeoutcommand` module. + */ +struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_clone(struct FadeOutCommand */*notnull*/ instance); + +/** + *Deallocates a [`FadeOutCommand`] instance. + * + * This function is part of the `fadeoutcommand` module. + */ +void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); + +/** + * A yet-to-be-tested command. + * + * Returns: a new [`FadeOutCommand`] instance. + * + * This function is part of the `fadeoutcommand` module. + */ +struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); + +/** + *Clones a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + *Deallocates a [`GlobalBrightnessCommand`] instance. + * + * This function is part of the `globalbrightnesscommand` module. + */ +void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `globalbrightnesscommand` module. + */ +Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); + +/** + * Turns the command into a packet + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct Packet */*notnull*/ sp_globalbrightnesscommand_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); + +/** + * Set the brightness of all tiles to the same value. + * + * Returns: a new [GlobalBrightnessCommand] instance. + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brightness brightness); + +/** + * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. + * + * This function is part of the `globalbrightnesscommand` module. + */ +void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); + +/** + *Clones a [`HardResetCommand`] instance. + * + * This function is part of the `hardresetcommand` module. + */ +struct HardResetCommand */*notnull*/ sp_hardresetcommand_clone(struct HardResetCommand */*notnull*/ instance); + +/** + *Deallocates a [`HardResetCommand`] instance. + * + * This function is part of the `hardresetcommand` module. + */ +void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); + +/** + * Kills the udp daemon on the display, which usually results in a restart. + * + * Please do not send this in your normal program flow. + * + * Returns: a new [`HardResetCommand`] instance. + * + * This function is part of the `hardresetcommand` module. + */ +struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); + /** *Clones a [`Packet`] instance. * @@ -2079,9 +2081,9 @@ bool sp_sp_u16_to_command_code(uint16_t code, /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -void sp_udp_free(struct UdpSocket */*notnull*/ instance); +void sp_udpsocket_free(struct UdpSocket */*notnull*/ instance); /** * Creates a new instance of [UdpSocket]. @@ -2096,9 +2098,9 @@ void sp_udp_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -struct UdpSocket *sp_udp_open(char */*notnull*/ host); +struct UdpSocket *sp_udpsocket_open(char */*notnull*/ host); /** * Creates a new instance of [UdpSocket]. @@ -2113,13 +2115,13 @@ struct UdpSocket *sp_udp_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, - uint8_t ip2, - uint8_t ip3, - uint8_t ip4, - uint16_t port); +struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, + uint8_t ip2, + uint8_t ip3, + uint8_t ip4, + uint16_t port); /** * Sends a [SPCommand] to the display using the [UdpSocket]. @@ -2134,10 +2136,10 @@ struct UdpSocket *sp_udp_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, - struct Command command); +bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, + struct Command command); /** * Sends a [Header] to the display using the [UdpSocket]. @@ -2150,10 +2152,10 @@ bool sp_udp_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, - struct Header header); +bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, + struct Header header); /** * Sends a [Packet] to the display using the [UdpSocket]. @@ -2162,10 +2164,10 @@ bool sp_udp_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the `udp` module. + * This function is part of the `udpsocket` module. */ -bool sp_udp_send_packet(struct UdpSocket */*notnull*/ connection, - struct Packet */*notnull*/ packet); +bool sp_udpsocket_send_packet(struct UdpSocket */*notnull*/ connection, + struct Packet */*notnull*/ packet); #ifdef __cplusplus } // extern "C" diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 06cc53f..6fa5357 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -8,14 +8,14 @@ use std::ptr::NonNull; wrap_command!(Bitmap); -wrap_fields!(cmd_bitmap::BitmapCommand; +wrap_fields!(BitmapCommand; prop bitmap: Bitmap { mut get(); move set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_origin_accessors!(cmd_bitmap::BitmapCommand); +wrap_origin_accessors!(BitmapCommand); -wrap_functions!(cmd_bitmap; +wrap_functions!(associate BitmapCommand; /// Sets a window of pixels to the specified values. /// /// The passed [Bitmap] gets consumed. diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 100fc93..fab6a2d 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -11,14 +11,14 @@ use std::ptr::NonNull; wrap_command!(BitVec); -wrap_fields!(cmd_bitvec::BitVecCommand; +wrap_fields!(BitVecCommand; prop bitvec: DisplayBitVec { mut get(); move set(value); }; prop offset: Offset { get(); set(value); }; prop operation: BinaryOperation { get(); set(value); }; prop compression: CompressionCode { get(); set(value); }; ); -wrap_functions!(cmd_bitvec; +wrap_functions!(associate BitVecCommand; /// Set pixel data starting at the pixel offset on screen. /// diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index b66952d..8aed3d7 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -8,13 +8,13 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(cmd_brightnessgrid::BrightnessGridCommand; +wrap_fields!(BrightnessGridCommand; prop grid: BrightnessGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_brightnessgrid::BrightnessGridCommand); +wrap_origin_accessors!(BrightnessGridCommand); -wrap_functions!(cmd_brightnessgrid; +wrap_functions!(associate BrightnessGridCommand; /// Set the brightness of individual tiles in a rectangular area of the display. /// diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 0d72078..a34e5c8 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -5,22 +5,18 @@ use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; use std::ptr::NonNull; macro_rules! wrap_cc_only { - ($(#[$meta:meta])*; $command:ident, $prefix:ident, $object_type:ident) => { - wrap_command!($command); - - wrap_functions!($prefix; - $(#[$meta])* - /// - #[doc = concat!(" Returns: a new [`",stringify!($object_type),"`] instance.")] - fn new() -> NonNull<$object_type> { - heap_move_nonnull($object_type) - } - ); - }; - ($(#[$meta:meta])* $command:ident) => { ::paste::paste!{ - wrap_cc_only!($(#[$meta])*; $command, [< cmd_ $command:lower >], [< $command Command >]); + wrap_command!($command); + + wrap_functions!(associate [< $command Command >]; + $(#[$meta])* + /// + #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] + fn new() -> NonNull<[< $command Command >]> { + heap_move_nonnull([< $command Command >]) + } + ); } }; } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index f614808..551533e 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -8,11 +8,11 @@ use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(cmd_chargrid::CharGridCommand; +wrap_fields!(CharGridCommand; prop grid: CharGrid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_chargrid::CharGridCommand); +wrap_origin_accessors!(CharGridCommand); wrap_functions!(cmd_chargrid; diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 4ecd058..913c393 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -8,11 +8,11 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(cmd_cp437grid::Cp437GridCommand; +wrap_fields!(Cp437GridCommand; prop grid: Cp437Grid { mut get(); move set(grid); }; ); -wrap_origin_accessors!(cmd_cp437grid::Cp437GridCommand); +wrap_origin_accessors!(Cp437GridCommand); wrap_functions!(cmd_cp437grid; diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 1b3fb34..247ef55 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -6,7 +6,7 @@ use crate::{ use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; use std::ptr::NonNull; -wrap_functions!(cmd_globalbrightness; +wrap_functions!(associate GlobalBrightnessCommand; /// Set the brightness of all tiles to the same value. /// @@ -24,8 +24,7 @@ wrap_functions!(cmd_globalbrightness; wrap_command!(GlobalBrightness); -wrap_fields!( - cmd_globalbrightness::GlobalBrightnessCommand; +wrap_fields!(GlobalBrightnessCommand; prop brightness: Brightness { get(); set(value); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0fce600..a00ab89 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -15,32 +15,35 @@ pub use cp437_grid_command::*; pub use generic_command::*; macro_rules! wrap_origin_accessors { - ( $prefix:ident :: $object_type:ty ) => { - $crate::macros::wrap_functions!($prefix; - #[doc = concat!(" Reads the origin field of the [`", stringify!($object_type), "`].")] - fn get_origin( - command: NonNull<$object_type>, - origin_x: NonNull, - origin_y: NonNull, - ) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; + ( $object_type:ident ) => { + ::paste::paste! { + $crate::macros::wrap_functions!(associate $object_type; + #[doc = " Reads the origin field of the [`" $object_type "`]."] + fn get_origin( + command: NonNull<$object_type>, + origin_x: NonNull, + origin_y: NonNull, + ) { + unsafe { + let origin = &command.as_ref().origin; + *origin_x.as_ptr() = origin.x; + *origin_y.as_ptr() = origin.y; + } } - } - #[doc = concat!(" Overwrites the origin field of the [`", stringify!($object_type), "`].")] - fn set_origin( - command: NonNull<$object_type>, - origin_x: usize, - origin_y: usize, - ) { - unsafe { - $crate::macros::nonnull_as_mut!(command).origin = ::servicepoint::Origin::new(origin_x, origin_y); + #[doc = " Overwrites the origin field of the [`" $object_type "`]."] + fn set_origin( + command: NonNull<$object_type>, + origin_x: usize, + origin_y: usize, + ) { + unsafe { + $crate::macros::nonnull_as_mut!(command).origin = + ::servicepoint::Origin::new(origin_x, origin_y); + } } - } - ); + ); + } }; } @@ -62,15 +65,14 @@ macro_rules! derive_command_from { } macro_rules! wrap_command { - ($command:ident, $prefix:ident, $object_type:ident) => { - $crate::macros::wrap_clone!($prefix::$object_type); - $crate::macros::wrap_free!($prefix::$object_type); - + ($command:ident, $object_type:ident) => { + $crate::macros::wrap_clone!($object_type); + $crate::macros::wrap_free!($object_type); $crate::commands::derive_command_from!($command); }; ($command:ident) => { - ::paste::paste!{ - wrap_command!($command, [< cmd_ $command:lower >], [< $command Command >]); + ::paste::paste! { + wrap_command!($command, [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 9ebf13a..1d482b4 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -9,8 +9,8 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(bitmap::Bitmap); -wrap_free!(bitmap::Bitmap); +wrap_clone!(Bitmap); +wrap_free!(Bitmap); wrap_functions!(bitmap; @@ -105,8 +105,7 @@ wrap_functions!(bitmap; } ); -wrap_methods!( - bitmap::Bitmap; +wrap_methods!(Bitmap; /// Gets the current value at the specified position. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 4512ac1..b0782c4 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -55,11 +55,10 @@ wrap_functions!(bitvec; ); -wrap_clone!(bitvec::DisplayBitVec); -wrap_free!(bitvec::DisplayBitVec); +wrap_clone!(DisplayBitVec); +wrap_free!(DisplayBitVec); -wrap_methods!( - bitvec::DisplayBitVec; +wrap_methods!(DisplayBitVec; /// Gets the value of a bit. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 01fc010..8a9d3cc 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -71,11 +71,10 @@ wrap_functions!(brightnessgrid; ); -wrap_clone!(brightnessgrid::BrightnessGrid); -wrap_free!(brightnessgrid::BrightnessGrid); +wrap_clone!(BrightnessGrid); +wrap_free!(BrightnessGrid); -wrap_methods!( - brightnessgrid::BrightnessGrid; +wrap_methods!(BrightnessGrid; /// Gets the current value at the specified position. /// diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index 66bdce2..aac6615 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -36,6 +36,7 @@ impl ByteSlice { unsafe { std::slice::from_raw_parts(self.start, self.length) } } + #[allow(clippy::mut_from_ref, reason = "This is used to get a pointer from the C side")] pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] { unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a79af3a..f158702 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -58,11 +58,10 @@ wrap_functions!(chargrid; ); -wrap_clone!(chargrid::CharGrid); -wrap_free!(chargrid::CharGrid); +wrap_clone!(CharGrid); +wrap_free!(CharGrid); -wrap_methods!( - chargrid::CharGrid; +wrap_methods!(CharGrid; /// Returns the current value at the specified position. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a78cecf..5822019 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -50,11 +50,10 @@ wrap_functions!(cp437grid; ); -wrap_clone!(cp437grid::Cp437Grid); -wrap_free!(cp437grid::Cp437Grid); +wrap_clone!(Cp437Grid); +wrap_free!(Cp437Grid); -wrap_methods!( - cp437grid::Cp437Grid; +wrap_methods!(Cp437Grid; /// Gets the current value at the specified position. /// /// # Arguments diff --git a/src/macros.rs b/src/macros.rs index 93e06b8..7cd0a71 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,22 +1,26 @@ macro_rules! wrap_free { - ($prefix:ident :: $typ:ty) => { - $crate::macros::wrap_functions!($prefix; - #[doc = concat!("Deallocates a [`", stringify!($typ), "`] instance.")] - fn free(instance: NonNull<$typ>) { - unsafe { $crate::mem::heap_drop(instance) } - } - ); + ($typ:ident) => { + ::paste::paste! { + $crate::macros::wrap_functions!([< $typ:lower >]; + #[doc = "Deallocates a [`" $typ "`] instance."] + fn free(instance: NonNull<$typ>) { + unsafe { $crate::mem::heap_drop(instance) } + } + ); + } }; } macro_rules! wrap_clone { - ($prefix:ident :: $typ:ty) => { - $crate::macros::wrap_functions!($prefix; - #[doc = concat!("Clones a [`", stringify!($typ), "`] instance.")] - fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { - unsafe { $crate::mem::heap_clone(instance) } - } - ); + ($typ:ident) => { + ::paste::paste! { + $crate::macros::wrap_functions!([< $typ:lower >]; + #[doc = "Clones a [`" $typ "`] instance."] + fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { + unsafe { $crate::mem::heap_clone(instance) } + } + ); + } }; } @@ -35,7 +39,7 @@ macro_rules! nonnull_as_mut { // meta required on purpose, because otherwise the added documentation would suppress warnings macro_rules! wrap_methods { ( - $prefix:ident :: $object_type:ty; + $object_type:ident; $( $(#[$meta:meta])+ $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) @@ -48,10 +52,9 @@ macro_rules! wrap_methods { )+ ) => { paste::paste! { - $crate::macros::wrap_functions!($prefix; + $crate::macros::wrap_functions!([< $object_type:lower >]; $( - #[doc = concat!(" Calls method [`servicepoint::", stringify!($object_type), - "::", stringify!($function), "`].")] + #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] #[doc = ""] $(#[$meta])* fn $function( @@ -82,7 +85,7 @@ macro_rules! wrap_methods { macro_rules! wrap_fields { ( - $prefix:ident :: $object_type:ty; + $object_type:ident; $( prop $prop_name:ident : $prop_type:ty { $( @@ -118,11 +121,10 @@ macro_rules! wrap_fields { )+ ) => { paste::paste! { - $crate::macros::wrap_functions!($prefix; + $crate::macros::wrap_functions!([< $object_type:lower >]; $( $( - #[doc = concat!(" Gets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::", stringify!($object_type),"`].")] + #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] $($( #[doc = ""] #[$get_meta] @@ -230,6 +232,25 @@ macro_rules! wrap_functions { )+ } }; + ( + associate $object_type:ident; + $( + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block + )+ + ) => { + ::paste::paste! { + $crate::macros::wrap_functions!{[< $object_type:lower >]; + $( + $(#[$meta])+ + fn $function($($param_name: $param_type),*) $(-> $return_type)? + $block + )+ + } + } + }; } pub(crate) use { diff --git a/src/packet.rs b/src/packet.rs index 18d63bd..df532b7 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -68,11 +68,10 @@ wrap_functions!(packet; } ); -wrap_clone!(packet::Packet); -wrap_free!(packet::Packet); +wrap_clone!(Packet); +wrap_free!(Packet); -wrap_fields!( - packet::Packet; +wrap_fields!(Packet; prop header: Header { get(); mut get(); diff --git a/src/udp.rs b/src/udp.rs index fd907be..21c067e 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -10,9 +10,9 @@ use std::{ ptr::NonNull, }; -wrap_free!(udp::UdpSocket); +wrap_free!(UdpSocket); -wrap_functions!(udp; +wrap_functions!(associate UdpSocket; /// Creates a new instance of [UdpSocket]. /// From 5ecb84ed16a3cb3880255cb47d549642646dc1ba Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:27:42 +0200 Subject: [PATCH 55/76] derive try_into_packet automatically --- include/servicepoint.h | 101 ++++++++++++++-------- src/commands/bitmap_command.rs | 13 +-- src/commands/bitvec_command.rs | 12 +-- src/commands/brightness_grid_command.rs | 13 +-- src/commands/char_grid_command.rs | 13 +-- src/commands/cp437_grid_command.rs | 13 +-- src/commands/global_brightness_command.rs | 9 +- src/commands/mod.rs | 23 ++++- src/containers/byte_slice.rs | 5 +- 9 files changed, 102 insertions(+), 100 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index e0e6cc4..34a3518 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -906,7 +906,7 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_y); /** - * Tries to turn a [BitmapCommand] into a [Packet]. + *Tries to turn a [`BitmapCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * @@ -1054,7 +1054,7 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); /** - * Tries to turn a [BitVecCommand] into a [Packet]. + *Tries to turn a [`BitVecCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * @@ -1252,15 +1252,6 @@ void sp_brightnessgridcommand_get_origin(struct BrightnessGridCommand */*notnull size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); -/** - * Tries to turn a [BrightnessGridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `brightnessgridcommand` module. - */ -struct Packet *sp_brightnessgridcommand_into_packet(struct BrightnessGridCommand */*notnull*/ command); - /** * Set the brightness of individual tiles in a rectangular area of the display. * @@ -1292,6 +1283,15 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull size_t origin_x, size_t origin_y); +/** + *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `brightnessgridcommand` module. + */ +struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ command); + /** *Clones a [`CharGrid`] instance. * @@ -1470,6 +1470,15 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_x, size_t origin_y); +/** + *Tries to turn a [`CharGridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `chargridcommand` module. + */ +struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ command); + /** *Clones a [`ClearCommand`] instance. * @@ -1495,6 +1504,15 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); */ struct ClearCommand */*notnull*/ sp_clearcommand_new(void); +/** + *Tries to turn a [`ClearCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `clearcommand` module. + */ +struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ command); + /** * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. @@ -1516,15 +1534,6 @@ struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ gr size_t origin_x, size_t origin_y); -/** - * Tries to turn a [CharGridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_chargrid` module. - */ -struct Packet *sp_cmd_chargrid_try_into_packet(struct CharGridCommand */*notnull*/ command); - /** * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], * leaving other fields as their default values. @@ -1546,15 +1555,6 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ size_t origin_x, size_t origin_y); -/** - * Tries to turn a [Cp437GridCommand] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Packet *sp_cmd_cp437grid_try_into_packet(struct Cp437GridCommand */*notnull*/ command); - /** * Clones an [SPCommand] instance. * @@ -1780,6 +1780,15 @@ void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command size_t origin_x, size_t origin_y); +/** + *Tries to turn a [`Cp437GridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ command); + /** * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. * @@ -1907,6 +1916,15 @@ void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); */ struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); +/** + *Tries to turn a [`FadeOutCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `fadeoutcommand` module. + */ +struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ command); + /** *Clones a [`GlobalBrightnessCommand`] instance. * @@ -1928,13 +1946,6 @@ void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ */ Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); -/** - * Turns the command into a packet - * - * This function is part of the `globalbrightnesscommand` module. - */ -struct Packet */*notnull*/ sp_globalbrightnesscommand_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); - /** * Set the brightness of all tiles to the same value. * @@ -1952,6 +1963,15 @@ struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brigh void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, Brightness value); +/** + *Tries to turn a [`GlobalBrightnessCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `globalbrightnesscommand` module. + */ +struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); + /** *Clones a [`HardResetCommand`] instance. * @@ -1977,6 +1997,15 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); */ struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); +/** + *Tries to turn a [`HardResetCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * This function is part of the `hardresetcommand` module. + */ +struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ command); + /** *Clones a [`Packet`] instance. * diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 6fa5357..102e27d 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; +use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; wrap_command!(Bitmap); @@ -43,13 +43,4 @@ wrap_functions!(associate BitmapCommand; ) -> NonNull { heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) } - - /// Tries to turn a [BitmapCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index fab6a2d..02cb8c8 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,11 +1,10 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, - Packet, }; use std::ptr::NonNull; @@ -46,13 +45,4 @@ wrap_functions!(associate BitVecCommand; }) } - /// Tries to turn a [BitVecCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 8aed3d7..7bd5aef 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin, Packet}; +use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(BrightnessGrid); @@ -40,13 +40,4 @@ wrap_functions!(associate BrightnessGridCommand; heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } - /// Tries to turn a [BrightnessGridCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 551533e..a0ad2db 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{CharGrid, CharGridCommand, Origin, Packet}; +use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(CharGrid); @@ -40,13 +40,4 @@ wrap_functions!(cmd_chargrid; heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } - /// Tries to turn a [CharGridCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 913c393..7ccb1a3 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_remove}, }; -use servicepoint::{Cp437Grid, Cp437GridCommand, Origin, Packet}; +use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; wrap_command!(Cp437Grid); @@ -40,13 +40,4 @@ wrap_functions!(cmd_cp437grid; heap_move_nonnull(unsafe { heap_remove(grid) }.into()) } - /// Tries to turn a [Cp437GridCommand] into a [Packet]. - /// - /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: NonNull, - ) -> *mut Packet { - heap_move_ok(unsafe { heap_remove(command) }.try_into()) - } - ); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 247ef55..ef7ce6e 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,9 +1,9 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; -use servicepoint::{Brightness, GlobalBrightnessCommand, Packet}; +use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; wrap_functions!(associate GlobalBrightnessCommand; @@ -15,11 +15,6 @@ wrap_functions!(associate GlobalBrightnessCommand; heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) } - /// Turns the command into a packet - fn into_packet(command: NonNull) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(command) }.into()) - } - ); wrap_command!(GlobalBrightness); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a00ab89..c5e751b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -64,11 +64,29 @@ macro_rules! derive_command_from { }; } +macro_rules! derive_command_into_packet { + ($command_type:ident) => { + ::paste::paste! { + wrap_functions!(associate $command_type; + #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] + /// + /// Returns: NULL or a [Packet] containing the command. + fn try_into_packet( + command: NonNull<$command_type>, + ) -> *mut ::servicepoint::Packet { + $crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into()) + } + ); + } + } +} + macro_rules! wrap_command { ($command:ident, $object_type:ident) => { $crate::macros::wrap_clone!($object_type); $crate::macros::wrap_free!($object_type); $crate::commands::derive_command_from!($command); + $crate::commands::derive_command_into_packet!($object_type); }; ($command:ident) => { ::paste::paste! { @@ -77,4 +95,7 @@ macro_rules! wrap_command { }; } -pub(crate) use {derive_command_from, wrap_command, wrap_origin_accessors}; +pub(crate) use { + derive_command_from, derive_command_into_packet, wrap_command, + wrap_origin_accessors, +}; diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index aac6615..d12cd88 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -36,7 +36,10 @@ impl ByteSlice { unsafe { std::slice::from_raw_parts(self.start, self.length) } } - #[allow(clippy::mut_from_ref, reason = "This is used to get a pointer from the C side")] + #[allow( + clippy::mut_from_ref, + reason = "This is used to get a pointer from the C side." + )] pub(crate) unsafe fn as_slice_mut(&self) -> &mut [u8] { unsafe { std::slice::from_raw_parts_mut(self.start, self.length) } } From b4730ffdf38505fa3aeb76f36dd57cda93231137 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:52:53 +0200 Subject: [PATCH 56/76] container macro code dedup --- include/servicepoint.h | 118 ++++++++++++++---------------- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 43 +---------- src/containers/bitvec.rs | 12 ++- src/containers/brightness_grid.rs | 51 ++----------- src/containers/char_grid.rs | 18 ++--- src/containers/cp437_grid.rs | 49 +------------ src/containers/mod.rs | 59 +++++++++++++++ src/macros.rs | 6 +- src/packet.rs | 6 +- src/udp.rs | 4 +- 11 files changed, 148 insertions(+), 222 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 34a3518..85f479d 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -656,11 +656,11 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); /** * Calls method [`servicepoint::Bitmap::fill`]. * - * Sets the state of all pixels in the [Bitmap]. + * Sets the state of all cells in the grid. * * # Arguments * - * - `value`: the value to set all pixels to + * - `value`: the value to set all cells to * * This function is part of the `bitmap` module. */ @@ -704,7 +704,7 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); /** * Calls method [`servicepoint::Bitmap::height`]. * - * Gets the height in pixels. + * Gets the height. * * This function is part of the `bitmap` module. */ @@ -809,7 +809,7 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, /** * Calls method [`servicepoint::Bitmap::width`]. * - * Gets the width in pixels. + * Gets the width. * * This function is part of the `bitmap` module. */ @@ -914,46 +914,6 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, */ struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ command); -/** - * Creates a [BitVecCommand] and immediately turns that into a [Packet]. - * - * The provided [DisplayBitVec] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `bitvec` module. - */ -struct Packet *sp_bitvec_into_packet(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); - -/** - * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. - * - * returns: [DisplayBitVec] instance containing data. - * - * This function is part of the `bitvec` module. - */ -BitVec */*notnull*/ sp_bitvec_load(struct ByteSlice data); - -/** - * Creates a new [DisplayBitVec] instance. - * - * # Arguments - * - * - `size`: size in bits. - * - * returns: [DisplayBitVec] with all bits set to false. - * - * # Panics - * - * - when `size` is not divisible by 8. - * - * This function is part of the `bitvec` module. - */ -BitVec */*notnull*/ sp_bitvec_new(size_t size); - /** *Clones a [`BitVecCommand`] instance. * @@ -1083,7 +1043,7 @@ struct ByteSlice sp_brightnessgrid_data_ref_mut(BrightnessGrid */*notnull*/ inst /** * Calls method [`servicepoint::BrightnessGrid::fill`]. * - * Sets the value of all cells. + * Sets the state of all cells in the grid. * * # Arguments * @@ -1110,10 +1070,9 @@ void sp_brightnessgrid_free(BrightnessGrid */*notnull*/ instance); * * - `x` and `y`: position of the cell to read * - * returns: value at position - * * # Panics - * - When accessing `x` or `y` out of bounds. + * + * - when accessing `x` or `y` out of bounds * * This function is part of the `brightnessgrid` module. */ @@ -1124,7 +1083,7 @@ Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance, /** * Calls method [`servicepoint::BrightnessGrid::height`]. * - * Gets the height of the grid. + * Gets the height. * * This function is part of the `brightnessgrid` module. */ @@ -1189,11 +1148,9 @@ BrightnessGrid */*notnull*/ sp_brightnessgrid_new(size_t width, size_t height); * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * - * returns: old value of the cell - * * # Panics * - * - When accessing `x` or `y` out of bounds. + * - when accessing `x` or `y` out of bounds * * This function is part of the `brightnessgrid` module. */ @@ -1205,7 +1162,7 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, /** * Calls method [`servicepoint::BrightnessGrid::width`]. * - * Gets the width of the grid. + * Gets the width. * * This function is part of the `brightnessgrid` module. */ @@ -1340,7 +1297,7 @@ uint32_t sp_chargrid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** * Calls method [`servicepoint::CharGrid::height`]. * - * Gets the height of the grid. + * Gets the height. * * This function is part of the `chargrid` module. */ @@ -1413,7 +1370,7 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, /** * Calls method [`servicepoint::CharGrid::width`]. * - * Gets the width of the grid. + * Gets the width. * * This function is part of the `chargrid` module. */ @@ -1622,11 +1579,10 @@ struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** * Calls method [`servicepoint::Cp437Grid::fill`]. * - * Sets the value of all cells in the grid. + * Sets the state of all cells in the grid. * * # Arguments * - * - `cp437_grid`: instance to write to * - `value`: the value to set all cells to * * This function is part of the `cp437grid` module. @@ -1660,7 +1616,7 @@ uint8_t sp_cp437grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** * Calls method [`servicepoint::Cp437Grid::height`]. * - * Gets the height of the grid. + * Gets the height. * * This function is part of the `cp437grid` module. */ @@ -1700,15 +1656,13 @@ Cp437Grid */*notnull*/ sp_cp437grid_new(size_t width, size_t height); /** * Calls method [`servicepoint::Cp437Grid::set`]. * - * Sets the value at the specified position. + * Sets the value of the specified position. * * # Arguments * * - `x` and `y`: position of the cell * - `value`: the value to write to the cell * - * returns: old value of the cell - * * # Panics * * - when accessing `x` or `y` out of bounds @@ -1723,7 +1677,7 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, /** * Calls method [`servicepoint::Cp437Grid::width`]. * - * Gets the width of the grid. + * Gets the width. * * This function is part of the `cp437grid` module. */ @@ -1847,6 +1801,20 @@ void sp_displaybitvec_free(BitVec */*notnull*/ instance); */ bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); +/** + * Creates a [BitVecCommand] and immediately turns that into a [Packet]. + * + * The provided [DisplayBitVec] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `displaybitvec` module. + */ +struct Packet *sp_displaybitvec_into_packet(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + /** * Calls method [`servicepoint::DisplayBitVec::is_empty`]. * @@ -1865,6 +1833,32 @@ bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); */ size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); +/** + * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. + * + * returns: [DisplayBitVec] instance containing data. + * + * This function is part of the `displaybitvec` module. + */ +BitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); + +/** + * Creates a new [DisplayBitVec] instance. + * + * # Arguments + * + * - `size`: size in bits. + * + * returns: [DisplayBitVec] with all bits set to false. + * + * # Panics + * + * - when `size` is not divisible by 8. + * + * This function is part of the `displaybitvec` module. + */ +BitVec */*notnull*/ sp_displaybitvec_new(size_t size); + /** * Calls method [`servicepoint::DisplayBitVec::set`]. * diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c5e751b..e15ba1f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -83,8 +83,8 @@ macro_rules! derive_command_into_packet { macro_rules! wrap_command { ($command:ident, $object_type:ident) => { - $crate::macros::wrap_clone!($object_type); - $crate::macros::wrap_free!($object_type); + $crate::macros::derive_clone!($object_type); + $crate::macros::derive_free!($object_type); $crate::commands::derive_command_from!($command); $crate::commands::derive_command_into_packet!($object_type); }; diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 1d482b4..b7e3a09 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ - containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, + containers::{wrap_grid, ByteSlice}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -9,8 +9,7 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_clone!(Bitmap); -wrap_free!(Bitmap); +wrap_grid!(Bitmap, bool); wrap_functions!(bitmap; @@ -107,42 +106,6 @@ wrap_functions!(bitmap; wrap_methods!(Bitmap; - /// Gets the current value at the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell to read - /// - /// # Panics - /// - /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> bool; - - /// Sets the value of the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell - /// - `value`: the value to write to the cell - /// - /// # Panics - /// - /// - when accessing `x` or `y` out of bounds - mut fn set(x: usize, y: usize, value: bool); - - /// Sets the state of all pixels in the [Bitmap]. - /// - /// # Arguments - /// - /// - `value`: the value to set all pixels to - mut fn fill(value: bool); - - /// Gets the width in pixels. - ref fn width() -> usize; - - /// Gets the height in pixels. - ref fn height() -> usize; - /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index b0782c4..6db5e6d 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ - containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, + containers::{wrap_container, ByteSlice}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{ @@ -8,7 +8,9 @@ use servicepoint::{ }; use std::ptr::NonNull; -wrap_functions!(bitvec; +wrap_container!(DisplayBitVec); + +wrap_functions!(associate DisplayBitVec; /// Creates a new [DisplayBitVec] instance. /// @@ -55,9 +57,6 @@ wrap_functions!(bitvec; ); -wrap_clone!(DisplayBitVec); -wrap_free!(DisplayBitVec); - wrap_methods!(DisplayBitVec; /// Gets the value of a bit. @@ -76,7 +75,6 @@ wrap_methods!(DisplayBitVec; return(result) { result.map(|x| *x).unwrap_or(false) }; }; - /// Sets the value of a bit. /// /// # Arguments diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 8a9d3cc..a7854e3 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ - containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, + containers::{wrap_grid, ByteSlice}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -9,7 +9,9 @@ use servicepoint::{ }; use std::{mem::transmute, ptr::NonNull}; -wrap_functions!(brightnessgrid; +wrap_grid!(BrightnessGrid, Brightness); + +wrap_functions!(associate BrightnessGrid; /// Creates a new [BrightnessGrid] with the specified dimensions. /// @@ -71,50 +73,7 @@ wrap_functions!(brightnessgrid; ); -wrap_clone!(BrightnessGrid); -wrap_free!(BrightnessGrid); - wrap_methods!(BrightnessGrid; - - /// Gets the current value at the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell to read - /// - /// returns: value at position - /// - /// # Panics - /// - When accessing `x` or `y` out of bounds. - ref fn get(x: usize, y: usize) -> Brightness; - - /// Sets the value of the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell - /// - `value`: the value to write to the cell - /// - /// returns: old value of the cell - /// - /// # Panics - /// - /// - When accessing `x` or `y` out of bounds. - mut fn set(x: usize, y: usize, value: Brightness); - - /// Sets the value of all cells. - /// - /// # Arguments - /// - /// - `value`: the value to set all cells to - mut fn fill(value: Brightness); - - /// Gets the width of the grid. - ref fn width() -> usize; - - /// Gets the height of the grid. - ref fn height() -> usize; - /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index f158702..6cc8a4a 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,12 +1,15 @@ use crate::{ - containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_functions, wrap_methods}, + containers::{derive_get_width_height, wrap_container, ByteSlice}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_remove}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -wrap_functions!(chargrid; +wrap_container!(CharGrid); +derive_get_width_height!(CharGrid); + +wrap_functions!(associate CharGrid; /// Creates a new [CharGrid] with the specified dimensions. /// @@ -58,9 +61,6 @@ wrap_functions!(chargrid; ); -wrap_clone!(CharGrid); -wrap_free!(CharGrid); - wrap_methods!(CharGrid; /// Returns the current value at the specified position. @@ -102,10 +102,4 @@ wrap_methods!(CharGrid; mut fn fill(value: u32) { prepare(value) { char::from_u32(value).unwrap() }; }; - - /// Gets the width of the grid. - ref fn width() -> usize; - - /// Gets the height of the grid. - ref fn height() -> usize; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 5822019..f25a502 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,7 +1,6 @@ -use crate::macros::wrap_functions; use crate::{ - containers::ByteSlice, - macros::{wrap_clone, wrap_free, wrap_methods}, + containers::{wrap_grid, ByteSlice}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, }; use servicepoint::{ @@ -9,6 +8,8 @@ use servicepoint::{ }; use std::ptr::NonNull; +wrap_grid!(Cp437Grid, u8); + wrap_functions!(cp437grid; /// Creates a new [Cp437Grid] with the specified dimensions. @@ -50,49 +51,7 @@ wrap_functions!(cp437grid; ); -wrap_clone!(Cp437Grid); -wrap_free!(Cp437Grid); - wrap_methods!(Cp437Grid; - /// Gets the current value at the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell to read - /// - /// # Panics - /// - /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> u8; - - /// Sets the value at the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell - /// - `value`: the value to write to the cell - /// - /// returns: old value of the cell - /// - /// # Panics - /// - /// - when accessing `x` or `y` out of bounds - mut fn set(x: usize, y: usize, value: u8); - - /// Sets the value of all cells in the grid. - /// - /// # Arguments - /// - /// - `cp437_grid`: instance to write to - /// - `value`: the value to set all cells to - mut fn fill(value: u8); - - /// Gets the width of the grid. - ref fn width() -> usize; - - /// Gets the height of the grid. - ref fn height() -> usize; - /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 7576a7b..8726038 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -12,6 +12,65 @@ pub use byte_slice::*; pub use char_grid::*; pub use cp437_grid::*; +macro_rules! wrap_container { + ($object_type:ident) => { + derive_clone!($object_type); + derive_free!($object_type); + }; +} + +macro_rules! derive_get_width_height { + ($object_type:ident) => { + $crate::macros::wrap_methods! {$object_type; + /// Gets the width. + ref fn width() -> usize; + + /// Gets the height. + ref fn height() -> usize; + } + }; +} + +macro_rules! wrap_grid { + ($object_type:ident, $value_type:ident) => { + $crate::containers::wrap_container!($object_type); + $crate::containers::derive_get_width_height!($object_type); + $crate::macros::wrap_methods! {$object_type; + /// Gets the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + ref fn get(x: usize, y: usize) -> $value_type; + + /// Sets the value of the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + mut fn set(x: usize, y: usize, value: $value_type); + + /// Sets the state of all cells in the grid. + /// + /// # Arguments + /// + /// - `value`: the value to set all cells to + mut fn fill(value: $value_type); + } + }; +} + +pub(crate) use {derive_get_width_height, wrap_container, wrap_grid}; + mod _hidden { /// This is a type only used by cbindgen to have a type for pointers. #[allow(unused)] diff --git a/src/macros.rs b/src/macros.rs index 7cd0a71..f737205 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,4 +1,4 @@ -macro_rules! wrap_free { +macro_rules! derive_free { ($typ:ident) => { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; @@ -11,7 +11,7 @@ macro_rules! wrap_free { }; } -macro_rules! wrap_clone { +macro_rules! derive_clone { ($typ:ident) => { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; @@ -254,6 +254,6 @@ macro_rules! wrap_functions { } pub(crate) use { - nonnull_as_mut, nonnull_as_ref, wrap_clone, wrap_fields, wrap_free, + derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, wrap_functions, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index df532b7..b719b8a 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,6 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{wrap_clone, wrap_fields, wrap_free, wrap_functions}, + macros::{derive_clone, derive_free, wrap_fields, wrap_functions}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; @@ -68,8 +68,8 @@ wrap_functions!(packet; } ); -wrap_clone!(Packet); -wrap_free!(Packet); +derive_clone!(Packet); +derive_free!(Packet); wrap_fields!(Packet; prop header: Header { diff --git a/src/udp.rs b/src/udp.rs index 21c067e..321c4d1 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::{ commands::{CommandTag, SPCommand}, - macros::{wrap_free, wrap_functions}, + macros::{derive_free, wrap_functions}, mem::{heap_move_ok, heap_remove}, }; use servicepoint::{Header, Packet, UdpSocketExt}; @@ -10,7 +10,7 @@ use std::{ ptr::NonNull, }; -wrap_free!(UdpSocket); +derive_free!(UdpSocket); wrap_functions!(associate UdpSocket; From 7a836783e1044a6dbfee087f8180d69eeeb51a74 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 12:59:13 +0200 Subject: [PATCH 57/76] rename function to ...try to indicate possible null return --- include/servicepoint.h | 134 +++++++++++++++--------------- src/commands/cc_only_commands.rs | 3 +- src/commands/mod.rs | 10 +-- src/containers/bitmap.rs | 2 +- src/containers/bitvec.rs | 2 +- src/containers/brightness_grid.rs | 2 +- src/containers/char_grid.rs | 2 +- src/containers/cp437_grid.rs | 2 +- src/macros.rs | 8 +- 9 files changed, 82 insertions(+), 83 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 85f479d..4d77ba8 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -717,20 +717,6 @@ size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); */ BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); -/** - * Creates a [BitmapCommand] and immediately turns that into a [Packet]. - * - * The provided [Bitmap] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `bitmap` module. - */ -struct Packet *sp_bitmap_into_packet(struct Bitmap */*notnull*/ bitmap, - size_t x, - size_t y, - CompressionCode compression); - /** * Loads a [Bitmap] with the specified dimensions from the provided data. * @@ -806,6 +792,20 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, size_t y, bool value); +/** + * Creates a [BitmapCommand] and immediately turns that into a [Packet]. + * + * The provided [Bitmap] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `bitmap` module. + */ +struct Packet *sp_bitmap_try_into_packet(struct Bitmap */*notnull*/ bitmap, + size_t x, + size_t y, + CompressionCode compression); + /** * Calls method [`servicepoint::Bitmap::width`]. * @@ -1089,19 +1089,6 @@ Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance, */ size_t sp_brightnessgrid_height(BrightnessGrid */*notnull*/ instance); -/** - * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. - * - * The provided [BrightnessGrid] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `brightnessgrid` module. - */ -struct Packet *sp_brightnessgrid_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); - /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. * @@ -1159,6 +1146,19 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, size_t y, Brightness value); +/** + * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. + * + * The provided [BrightnessGrid] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `brightnessgrid` module. + */ +struct Packet *sp_brightnessgrid_try_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); + /** * Calls method [`servicepoint::BrightnessGrid::width`]. * @@ -1303,19 +1303,6 @@ uint32_t sp_chargrid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); */ size_t sp_chargrid_height(CharGrid */*notnull*/ instance); -/** - * Creates a [CharGridCommand] and immediately turns that into a [Packet]. - * - * The provided [CharGrid] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `chargrid` module. - */ -struct Packet *sp_chargrid_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); - /** * Loads a [CharGrid] with the specified dimensions from the provided data. * @@ -1367,6 +1354,19 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, size_t y, uint32_t value); +/** + * Creates a [CharGridCommand] and immediately turns that into a [Packet]. + * + * The provided [CharGrid] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `chargrid` module. + */ +struct Packet *sp_chargrid_try_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); + /** * Calls method [`servicepoint::CharGrid::width`]. * @@ -1622,19 +1622,6 @@ uint8_t sp_cp437grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); */ size_t sp_cp437grid_height(Cp437Grid */*notnull*/ instance); -/** - * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. - * - * The provided [Cp437Grid] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `cp437grid` module. - */ -struct Packet *sp_cp437grid_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); - /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * @@ -1674,6 +1661,19 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, size_t y, uint8_t value); +/** + * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. + * + * The provided [Cp437Grid] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `cp437grid` module. + */ +struct Packet *sp_cp437grid_try_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); + /** * Calls method [`servicepoint::Cp437Grid::width`]. * @@ -1801,20 +1801,6 @@ void sp_displaybitvec_free(BitVec */*notnull*/ instance); */ bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); -/** - * Creates a [BitVecCommand] and immediately turns that into a [Packet]. - * - * The provided [DisplayBitVec] gets consumed. - * - * Returns NULL in case of an error. - * - * This function is part of the `displaybitvec` module. - */ -struct Packet *sp_displaybitvec_into_packet(BitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); - /** * Calls method [`servicepoint::DisplayBitVec::is_empty`]. * @@ -1879,6 +1865,20 @@ void sp_displaybitvec_set(BitVec */*notnull*/ instance, size_t index, bool value); +/** + * Creates a [BitVecCommand] and immediately turns that into a [Packet]. + * + * The provided [DisplayBitVec] gets consumed. + * + * Returns NULL in case of an error. + * + * This function is part of the `displaybitvec` module. + */ +struct Packet *sp_displaybitvec_try_into_packet(BitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + /** * Call this function at the beginning of main to enable rust logging controlled by the * `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index a34e5c8..6635ae9 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -2,7 +2,6 @@ use crate::{ commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull, }; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; -use std::ptr::NonNull; macro_rules! wrap_cc_only { ($(#[$meta:meta])* $command:ident) => { @@ -13,7 +12,7 @@ macro_rules! wrap_cc_only { $(#[$meta])* /// #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] - fn new() -> NonNull<[< $command Command >]> { + fn new() -> ::core::ptr::NonNull<[< $command Command >]> { heap_move_nonnull([< $command Command >]) } ); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e15ba1f..9da0a22 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -20,9 +20,9 @@ macro_rules! wrap_origin_accessors { $crate::macros::wrap_functions!(associate $object_type; #[doc = " Reads the origin field of the [`" $object_type "`]."] fn get_origin( - command: NonNull<$object_type>, - origin_x: NonNull, - origin_y: NonNull, + command: ::core::ptr::NonNull<$object_type>, + origin_x: ::core::ptr::NonNull, + origin_y: ::core::ptr::NonNull, ) { unsafe { let origin = &command.as_ref().origin; @@ -33,7 +33,7 @@ macro_rules! wrap_origin_accessors { #[doc = " Overwrites the origin field of the [`" $object_type "`]."] fn set_origin( - command: NonNull<$object_type>, + command: ::core::ptr::NonNull<$object_type>, origin_x: usize, origin_y: usize, ) { @@ -72,7 +72,7 @@ macro_rules! derive_command_into_packet { /// /// Returns: NULL or a [Packet] containing the command. fn try_into_packet( - command: NonNull<$command_type>, + command: ::core::ptr::NonNull<$command_type>, ) -> *mut ::servicepoint::Packet { $crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into()) } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index b7e3a09..ffda27e 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -89,7 +89,7 @@ wrap_functions!(bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( bitmap: NonNull, x: usize, y: usize, diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 6db5e6d..1549d60 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -40,7 +40,7 @@ wrap_functions!(associate DisplayBitVec; /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( bitvec: NonNull, offset: usize, operation: BinaryOperation, diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a7854e3..033bbdd 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -59,7 +59,7 @@ wrap_functions!(associate BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( grid: NonNull, x: usize, y: usize, diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 6cc8a4a..365f1ad 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -47,7 +47,7 @@ wrap_functions!(associate CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( grid: NonNull, x: usize, y: usize, diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index f25a502..001ebb8 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -37,7 +37,7 @@ wrap_functions!(cp437grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn into_packet( + fn try_into_packet( grid: NonNull, x: usize, y: usize, diff --git a/src/macros.rs b/src/macros.rs index f737205..20259fc 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,7 +3,7 @@ macro_rules! derive_free { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Deallocates a [`" $typ "`] instance."] - fn free(instance: NonNull<$typ>) { + fn free(instance: ::core::ptr::NonNull<$typ>) { unsafe { $crate::mem::heap_drop(instance) } } ); @@ -16,7 +16,7 @@ macro_rules! derive_clone { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Clones a [`" $typ "`] instance."] - fn clone(instance: NonNull<$typ>) -> NonNull<$typ> { + fn clone(instance: ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { unsafe { $crate::mem::heap_clone(instance) } } ); @@ -58,7 +58,7 @@ macro_rules! wrap_methods { #[doc = ""] $(#[$meta])* fn $function( - instance: NonNull<$object_type>, + instance: ::core::ptr::NonNull<$object_type>, $($param_name: $param_type),* ) $(-> $return_type)? { let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; @@ -192,7 +192,7 @@ macro_rules! wrap_fields { )*)? fn []( instance: ::core::ptr::NonNull<$object_type>, - value: NonNull<$prop_type>, + value: ::core::ptr::NonNull<$prop_type>, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; let value = unsafe { $crate::mem::heap_remove(value) }; From 92ce27af68db703b41cbd9e26878a3194201cf45 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 22 Jun 2025 14:52:44 +0200 Subject: [PATCH 58/76] simplify property exports --- example/src/announce.c | 2 +- example/src/brightness_tester.c | 2 +- example/src/random_stuff.c | 2 +- src/commands/bitmap_command.rs | 4 +- src/commands/bitvec_command.rs | 8 +- src/commands/brightness_grid_command.rs | 2 +- src/commands/char_grid_command.rs | 2 +- src/commands/cp437_grid_command.rs | 2 +- src/commands/global_brightness_command.rs | 5 +- src/macros.rs | 154 ++++++++-------------- src/packet.rs | 6 +- 11 files changed, 70 insertions(+), 119 deletions(-) diff --git a/example/src/announce.c b/example/src/announce.c index 33da334..a0bdf9a 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -20,7 +20,7 @@ int main(void) { sp_chargrid_set(grid, 3, 1, 'l'); sp_chargrid_set(grid, 4, 1, 'd'); - Packet *packet = sp_chargrid_into_packet(grid, 0, 0); + Packet *packet = sp_chargrid_try_into_packet(grid, 0, 0); if (packet == NULL) return 1; sp_udpsocket_send_packet(sock, packet); diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 9d17dc7..921bbd0 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -26,7 +26,7 @@ int main(void) { BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT); make_brightness_pattern(grid); - Packet *packet = sp_brightnessgridcommand_into_packet(sp_brightnessgridcommand_from_grid(grid)); + Packet *packet = sp_brightnessgridcommand_try_into_packet(sp_brightnessgridcommand_from_grid(grid)); if (packet == NULL) return -2; diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 8838123..7a58c3a 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -11,7 +11,7 @@ int main(void) { sp_bitmap_fill(pixels, true); - Packet *packet = sp_bitmap_into_packet(pixels, 0, 0, COMPRESSION_CODE_UNCOMPRESSED); + Packet *packet = sp_bitmap_try_into_packet(pixels, 0, 0, COMPRESSION_CODE_UNCOMPRESSED); if (packet == NULL) return 1; diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 102e27d..01e3620 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -9,8 +9,8 @@ use std::ptr::NonNull; wrap_command!(Bitmap); wrap_fields!(BitmapCommand; - prop bitmap: Bitmap { mut get(); move set(value); }; - prop compression: CompressionCode { get(); set(value); }; + prop bitmap: Bitmap { get mut; set move; }; + prop compression: CompressionCode { get; set; }; ); wrap_origin_accessors!(BitmapCommand); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 02cb8c8..e796380 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -11,10 +11,10 @@ use std::ptr::NonNull; wrap_command!(BitVec); wrap_fields!(BitVecCommand; - prop bitvec: DisplayBitVec { mut get(); move set(value); }; - prop offset: Offset { get(); set(value); }; - prop operation: BinaryOperation { get(); set(value); }; - prop compression: CompressionCode { get(); set(value); }; + prop bitvec: DisplayBitVec { get mut; set move; }; + prop offset: Offset { get; set; }; + prop operation: BinaryOperation { get; set; }; + prop compression: CompressionCode { get; set; }; ); wrap_functions!(associate BitVecCommand; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 7bd5aef..853a3fe 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(BrightnessGrid); wrap_fields!(BrightnessGridCommand; - prop grid: BrightnessGrid { mut get(); move set(grid); }; + prop grid: BrightnessGrid { get mut; set move; }; ); wrap_origin_accessors!(BrightnessGridCommand); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index a0ad2db..f8d5107 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(CharGrid); wrap_fields!(CharGridCommand; - prop grid: CharGrid { mut get(); move set(grid); }; + prop grid: CharGrid { get mut; set move; }; ); wrap_origin_accessors!(CharGridCommand); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 7ccb1a3..4977195 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(Cp437Grid); wrap_fields!(Cp437GridCommand; - prop grid: Cp437Grid { mut get(); move set(grid); }; + prop grid: Cp437Grid { get mut; set move; }; ); wrap_origin_accessors!(Cp437GridCommand); diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index ef7ce6e..76cbeb5 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -20,8 +20,5 @@ wrap_functions!(associate GlobalBrightnessCommand; wrap_command!(GlobalBrightness); wrap_fields!(GlobalBrightnessCommand; - prop brightness: Brightness { - get(); - set(value); - }; + prop brightness: Brightness { get; set; }; ); diff --git a/src/macros.rs b/src/macros.rs index 20259fc..8dcaf55 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -55,7 +55,7 @@ macro_rules! wrap_methods { $crate::macros::wrap_functions!([< $object_type:lower >]; $( #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] - #[doc = ""] + /// $(#[$meta])* fn $function( instance: ::core::ptr::NonNull<$object_type>, @@ -83,132 +83,90 @@ macro_rules! wrap_methods { }; } -macro_rules! wrap_fields { - ( - $object_type:ident; - $( - prop $prop_name:ident : $prop_type:ty { - $( - get() $({ - $(#[$get_meta:meta])* - $(return $get_expr:expr;)? - })?; - )? - - $( - mut get() $({ - $(#[$get_mut_meta:meta])* - $(return $get_mut_expr:expr;)? - })?; - )? - - $( - set($value:ident) - $({ - $(#[$set_meta:meta])* - $(return $set_expr:expr;)? - })?; - )? - - $( - move set( $set_move_value:ident) - $({ - $(#[$set_move_meta:meta])* - $(return $set_move_expr:expr;)? - })?; - )? - }; - )+ - ) => { +macro_rules! wrap_fields_accessor { + (get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions!([< $object_type:lower >]; - $( - $( - #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - $($( - #[doc = ""] - #[$get_meta] - )*)? + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Gets the value of field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] fn []( instance: ::core::ptr::NonNull<$object_type> ) -> $prop_type { let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name }; - $($( - let $prop_name = $get_expr; - )?)? return $prop_name; } - )? - - $( - #[doc = concat!(" Gets a reference to the field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - $($( - #[doc = ""] - #[$get_mut_meta] - )*)? - #[doc = ""] - #[doc = " - The returned reference inherits the lifetime of object in which it is contained."] - #[doc = " - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command."] + } + } + }; + (mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { + paste::paste! { + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Gets a reference to the field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] + /// + /// - The returned reference inherits the lifetime of object in which it is contained. + /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. fn []( instance: ::core::ptr::NonNull<$object_type> ) -> ::core::ptr::NonNull<$prop_type> { let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name }; - $($( - let $prop_name = $get_mut_expr; - )?)? return ::core::ptr::NonNull::from($prop_name); } - )? - - $( - #[doc = concat!(" Sets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - $($( - #[doc = ""] - #[$set_meta] - )*)? + } + } + }; + (set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { + paste::paste! { + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Sets the value of field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] fn []( instance: ::core::ptr::NonNull<$object_type>, value: $prop_type, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; - $($( - let $value = value; - let value = $set_expr; - )?)? instance.$prop_name = value; } - )? - - $( - #[doc = concat!(" Sets the value of field `", stringify!($prop_name), - "` of the [`servicepoint::",stringify!($object_type),"`].")] - #[doc = concat!(" The provided value is moved into the instance, ", - "potentially invalidating previously taken references.")] - $($( - #[doc = ""] - #[$set_move_meta] - )*)? + } + } + }; + (move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { + paste::paste! { + $crate::macros::wrap_functions! {associate $object_type; + #[doc = " Sets the value of field `" $prop_name + "` of the [`servicepoint::" $object_type "`]."] + /// The provided value is moved into the instance, potentially invalidating previously taken references. fn []( instance: ::core::ptr::NonNull<$object_type>, value: ::core::ptr::NonNull<$prop_type>, ) { let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; - let value = unsafe { $crate::mem::heap_remove(value) }; - $($( - let $set_move_value = value; - let value = $set_move_expr; - )?)? - instance.$prop_name = value; + let $prop_name = unsafe { $crate::mem::heap_remove(value) }; + instance.$prop_name = $prop_name; } - )? - )+ - ); + } } }; } +macro_rules! wrap_fields { + ( + $object_type:ident; + $( + prop $prop_name:ident : $prop_type:ty { $($accessor:ident $($modifier:ident)?;)+ }; + )+ + ) => { + $($( + ::paste::paste!{ + $crate::macros::wrap_fields_accessor! { + $($modifier)? $accessor; + $object_type :: $prop_name: $prop_type + } + } + )+)+ + }; +} + macro_rules! wrap_functions { ( $module:ident; @@ -255,5 +213,5 @@ macro_rules! wrap_functions { pub(crate) use { derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, + wrap_functions, wrap_methods, wrap_fields_accessor }; diff --git a/src/packet.rs b/src/packet.rs index b719b8a..28ade85 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -72,11 +72,7 @@ derive_clone!(Packet); derive_free!(Packet); wrap_fields!(Packet; - prop header: Header { - get(); - mut get(); - set(value); - }; + prop header: Header { get; get mut; set; }; ); wrap_functions!(sp; From e4341307844754658d658a4055b6b42853d4be25 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 19:43:52 +0200 Subject: [PATCH 59/76] unifiy special handling of params and return --- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 4 +- src/containers/bitvec.rs | 16 +++--- src/containers/brightness_grid.rs | 15 +++--- src/containers/char_grid.rs | 12 ++--- src/containers/cp437_grid.rs | 4 +- src/containers/mod.rs | 10 ++-- src/macros.rs | 83 +++++++++++++++++++------------ 8 files changed, 82 insertions(+), 66 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9da0a22..58e5957 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -67,7 +67,7 @@ macro_rules! derive_command_from { macro_rules! derive_command_into_packet { ($command_type:ident) => { ::paste::paste! { - wrap_functions!(associate $command_type; + $crate::macros::wrap_functions!(associate $command_type; #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. @@ -90,7 +90,7 @@ macro_rules! wrap_command { }; ($command:ident) => { ::paste::paste! { - wrap_command!($command, [< $command Command >]); + $crate::commands::wrap_command!($command, [< $command Command >]); } }; } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index ffda27e..9bae852 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -109,7 +109,7 @@ wrap_methods!(Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - mut fn data_ref_mut() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn data_ref_mut(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 1549d60..2a76cf7 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -71,8 +71,8 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(index: usize) -> bool { - return(result) { result.map(|x| *x).unwrap_or(false) }; + ref fn get(instance, index: usize) -> bool { + instance.get(index).map(|x| *x).unwrap_or(false) }; /// Sets the value of a bit. @@ -85,25 +85,25 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - mut fn set(index: usize, value: bool); + mut fn set(instance, index: usize, value: bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - mut fn fill(value: bool); + mut fn fill(instance, value: bool); /// Gets the length in bits. - ref fn len() -> usize; + ref fn len(instance) -> usize; /// Returns true if length is 0. - ref fn is_empty() -> bool; + ref fn is_empty(instance) -> bool; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - mut fn as_raw_mut_slice() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn as_raw_mut_slice(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 033bbdd..a5d01f2 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -77,14 +77,13 @@ wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - mut fn data_ref_mut() -> ByteSlice { - return(br_slice) { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); + mut fn data_ref_mut(instance) -> ByteSlice { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) - } - }; + let br_slice = instance.data_ref_mut(); + unsafe { + ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + } }; ); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 365f1ad..1a59fdd 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -72,8 +72,8 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> u32 { - return(char) { char as u32 }; + ref fn get(instance, x: usize, y: usize) -> u32 { + instance.get(x, y) as u32 }; /// Sets the value of the specified position in the grid. @@ -89,8 +89,8 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - mut fn set(x: usize, y: usize, value: u32) { - prepare(value) { char::from_u32(value).unwrap() }; + mut fn set(instance, x: usize, y: usize, value: u32) { + instance.set(x, y, char::from_u32(value).unwrap()) }; /// Sets the value of all cells in the grid. @@ -99,7 +99,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(value: u32) { - prepare(value) { char::from_u32(value).unwrap() }; + mut fn fill(instance, value: u32) { + instance.fill(char::from_u32(value).unwrap()) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 001ebb8..fc31d13 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -55,7 +55,7 @@ wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - mut fn data_ref_mut() -> ByteSlice { - return(slice) { unsafe { ByteSlice::from_slice(slice) } }; + mut fn data_ref_mut(instance) -> ByteSlice { + unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 8726038..fcd76a1 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - ref fn width() -> usize; + ref fn width(instance) -> usize; /// Gets the height. - ref fn height() -> usize; + ref fn height(instance) -> usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(x: usize, y: usize) -> $value_type; + ref fn get(instance, x: usize, y: usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - mut fn set(x: usize, y: usize, value: $value_type); + mut fn set(instance, x: usize, y: usize, value: $value_type); /// Sets the state of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to - mut fn fill(value: $value_type); + mut fn fill(instance, value: $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index 8dcaf55..e0560f5 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,49 +36,66 @@ macro_rules! nonnull_as_mut { }; } -// meta required on purpose, because otherwise the added documentation would suppress warnings +macro_rules! wrap_method { + ( + $object_type:ident; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + $(-> $return_type:ty)? + ) => { + ::paste::paste!{ + $crate::macros::wrap_method!( + $object_type; + $(#[$meta])+ + $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + $(-> $return_type)? { + $instance.$function($($($param_name),*)?) + } + ); + } + }; + ($object_type:ident; + $(#[$meta:meta])+ + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + $(-> $return_type:ty)? + $impl:block + ) => { + paste::paste! { + $crate::macros::wrap_functions!([< $object_type:lower >]; + #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] + /// + $(#[$meta])* + fn $function( + $instance: ::core::ptr::NonNull<$object_type> + $(,$($param_name: $param_type),*)? + ) $(-> $return_type)? { + let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) }; + $impl + } + ); + } + }; +} + macro_rules! wrap_methods { ( $object_type:ident; $( $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($($param_name:ident: $param_type:ty),*) + $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? - $({ - $($(prepare($param_let_name:ident) $param_let_expr:block);+;)? - $(return($it:ident) $return_expr:block;)? - })? - ; + $($impl:block)?; )+ ) => { paste::paste! { - $crate::macros::wrap_functions!([< $object_type:lower >]; - $( - #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] - /// + $( + $crate::macros::wrap_method!($object_type; $(#[$meta])* - fn $function( - instance: ::core::ptr::NonNull<$object_type>, - $($param_name: $param_type),* - ) $(-> $return_type)? { - let instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !(instance) }; - $($( - $(let $param_let_name = $param_let_expr;)* - )?)? - #[allow( - unused_variables, - reason = "This variable may not be used depending on macro variables" )] - let result = instance.$function($($param_name),*); - $( - $( - let $it = result; - let result = $return_expr; - )? - )? - return result; - } - )+ + $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + $(-> $return_type)? + $($impl)? ); + )+ } }; } @@ -213,5 +230,5 @@ macro_rules! wrap_functions { pub(crate) use { derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, wrap_fields_accessor + wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method }; From 323ba6128e0af019bc66f41bbc04bfee300149ef Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 20:26:07 +0200 Subject: [PATCH 60/76] add move fn to dsl, rename SPCommand to GenericCommand, remove DisplayBitVec command --- example/src/header_logger.c | 24 +-- example/src/undefined.c | 4 +- example/src/wiping_clear.c | 2 +- include/servicepoint.h | 180 +++++++++++--------- src/commands/generic_command.rs | 265 +++++++++++++++--------------- src/commands/mod.rs | 10 +- src/containers/bitmap.rs | 24 +-- src/containers/bitvec.rs | 18 +- src/containers/brightness_grid.rs | 17 +- src/containers/char_grid.rs | 41 ++--- src/containers/cp437_grid.rs | 17 +- src/macros.rs | 11 +- src/udp.rs | 78 ++++----- 13 files changed, 335 insertions(+), 356 deletions(-) diff --git a/example/src/header_logger.c b/example/src/header_logger.c index ec421dd..5793da9 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -15,8 +15,8 @@ void handle_error(const char *msg) { exit(EXIT_FAILURE); } -bool log_command(struct Command command) { - switch (command.tag) { +bool log_command(struct GenericCommand *command) { + switch (command->tag) { case COMMAND_TAG_INVALID: { printf("-> this is an invalid command\n"); break; @@ -26,7 +26,7 @@ bool log_command(struct Command command) { return true; } case COMMAND_TAG_BITMAP: { - BitmapCommand *bitmapCommand = command.data.bitmap; + BitmapCommand *bitmapCommand = command->data.bitmap; CompressionCode compression = sp_bitmapcommand_get_compression(bitmapCommand); @@ -42,7 +42,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_BRIGHTNESS_GRID: { - BrightnessGridCommand *gridCommand = command.data.brightness_grid; + BrightnessGridCommand *gridCommand = command->data.brightness_grid; size_t x, y; sp_brightnessgridcommand_get_origin(gridCommand, &x, &y); @@ -56,7 +56,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_CHAR_GRID: { - CharGridCommand *gridCommand = command.data.char_grid; + CharGridCommand *gridCommand = command->data.char_grid; size_t x, y; sp_chargridcommand_get_origin(gridCommand, &x, &y); @@ -70,7 +70,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_CP437_GRID: { - Cp437GridCommand *gridCommand = command.data.cp437_grid; + Cp437GridCommand *gridCommand = command->data.cp437_grid; size_t x, y; sp_cp437gridcommand_get_origin(gridCommand, &x, &y); @@ -84,7 +84,7 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_BIT_VEC: { - BitVecCommand *bitvecCommand = command.data.bit_vec; + BitVecCommand *bitvecCommand = command->data.bit_vec; size_t offset = sp_bitveccommand_get_offset(bitvecCommand); CompressionCode compression = sp_bitveccommand_get_compression(bitvecCommand); @@ -109,7 +109,7 @@ bool log_command(struct Command command) { break; } - BitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); + DisplayBitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); size_t len = sp_displaybitvec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", @@ -129,12 +129,12 @@ bool log_command(struct Command command) { break; } case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_globalbrightnesscommand_get_brightness(command.data.global_brightness); + Brightness brightness = sp_globalbrightnesscommand_get_brightness(command->data.global_brightness); printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); break; } default: { - printf("-> unknown command tag %d\n", command.tag); + printf("-> unknown command tag %d\n", command->tag); break; } } @@ -198,10 +198,10 @@ int main(int argc, char **argv) { header->command_code, header->a, header->b, header->c, header->d, payload.start, payload.length); - struct Command command = sp_cmd_generic_try_from_packet(packet); + struct GenericCommand *command = sp_genericcommand_try_from_packet(packet); done = log_command(command); - sp_cmd_generic_free(command); + sp_genericcommand_free(command); } close(udp_socket); diff --git a/example/src/undefined.c b/example/src/undefined.c index 44aaf6d..b869d71 100644 --- a/example/src/undefined.c +++ b/example/src/undefined.c @@ -9,14 +9,14 @@ int main(void) { sp_bitveccommand_free(bvcmd); uint8_t *data = calloc(1024, 1); - struct Command generic = { + struct GenericCommand generic = { .tag = COMMAND_TAG_BRIGHTNESS_GRID, .data = {.null = data}, }; sock_init(); - sp_udpsocket_send_command(sock, generic); + sp_udpsocket_send_command(sock, &generic); return 0; } diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c index 3984169..1d0440b 100644 --- a/example/src/wiping_clear.c +++ b/example/src/wiping_clear.c @@ -13,7 +13,7 @@ int main() { sp_bitmap_set(enabled_pixels, x, y, false); } - BitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); + DisplayBitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); BitVecCommand *command = sp_bitveccommand_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); Packet *packet = sp_bitveccommand_try_into_packet(command); if (packet == NULL) { diff --git a/include/servicepoint.h b/include/servicepoint.h index 4d77ba8..095c6cc 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -355,7 +355,7 @@ typedef struct Cp437GridCommand Cp437GridCommand; /** * This is a type only used by cbindgen to have a type for pointers. */ -typedef struct BitVec BitVec; +typedef struct DisplayBitVec DisplayBitVec; /** *
Untested
@@ -578,7 +578,7 @@ typedef union CommandUnion { * * Rust equivalent: [TypedCommand]. */ -typedef struct Command { +typedef struct GenericCommand { /** * Specifies which kind of command struct is contained in `data` */ @@ -587,7 +587,7 @@ typedef struct Command { * The pointer to the command struct */ union CommandUnion data; -} Command; +} GenericCommand; /** * A raw header. @@ -682,7 +682,8 @@ void sp_bitmap_free(struct Bitmap */*notnull*/ instance); * * This function is part of the `bitmap` module. */ -struct Bitmap *sp_bitmap_from_bitvec(size_t width, BitVec */*notnull*/ bitvec); +struct Bitmap *sp_bitmap_from_bitvec(size_t width, + DisplayBitVec */*notnull*/ bitvec); /** * Calls method [`servicepoint::Bitmap::get`]. @@ -711,11 +712,13 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** + * Calls method [`servicepoint::Bitmap::into_bitvec`]. + * * Consumes the Bitmap and returns the contained BitVec. * * This function is part of the `bitmap` module. */ -BitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); +DisplayBitVec */*notnull*/ sp_bitmap_into_bitvec(struct Bitmap */*notnull*/ bitmap); /** * Loads a [Bitmap] with the specified dimensions from the provided data. @@ -793,6 +796,8 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, bool value); /** + * Calls method [`servicepoint::Bitmap::try_into_packet`]. + * * Creates a [BitmapCommand] and immediately turns that into a [Packet]. * * The provided [Bitmap] gets consumed. @@ -906,13 +911,15 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_y); /** + * Calls method [`servicepoint::BitmapCommand::try_into_packet`]. + * *Tries to turn a [`BitmapCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `bitmapcommand` module. */ -struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ command); +struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ instance); /** *Clones a [`BitVecCommand`] instance. @@ -936,7 +943,7 @@ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); * * This function is part of the `bitveccommand` module. */ -BitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); +DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. @@ -975,7 +982,7 @@ BinaryOperation sp_bitveccommand_get_operation(struct BitVecCommand */*notnull*/ * * This function is part of the `bitveccommand` module. */ -struct BitVecCommand */*notnull*/ sp_bitveccommand_new(BitVec */*notnull*/ bitvec, +struct BitVecCommand */*notnull*/ sp_bitveccommand_new(DisplayBitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -987,7 +994,7 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_new(BitVec */*notnull*/ bitve * This function is part of the `bitveccommand` module. */ void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, - BitVec */*notnull*/ value); + DisplayBitVec */*notnull*/ value); /** * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. @@ -1014,13 +1021,15 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); /** + * Calls method [`servicepoint::BitVecCommand::try_into_packet`]. + * *Tries to turn a [`BitVecCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `bitveccommand` module. */ -struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ command); +struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ instance); /** *Clones a [`BrightnessGrid`] instance. @@ -1147,6 +1156,8 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, Brightness value); /** + * Calls method [`servicepoint::BrightnessGrid::try_into_packet`]. + * * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. * * The provided [BrightnessGrid] gets consumed. @@ -1241,13 +1252,15 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull size_t origin_y); /** + * Calls method [`servicepoint::BrightnessGridCommand::try_into_packet`]. + * *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `brightnessgridcommand` module. */ -struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ command); +struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ instance); /** *Clones a [`CharGrid`] instance. @@ -1355,6 +1368,8 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, uint32_t value); /** + * Calls method [`servicepoint::CharGrid::try_into_packet`]. + * * Creates a [CharGridCommand] and immediately turns that into a [Packet]. * * The provided [CharGrid] gets consumed. @@ -1428,13 +1443,15 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_y); /** + * Calls method [`servicepoint::CharGridCommand::try_into_packet`]. + * *Tries to turn a [`CharGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `chargridcommand` module. */ -struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ command); +struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ instance); /** *Clones a [`ClearCommand`] instance. @@ -1462,13 +1479,15 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); struct ClearCommand */*notnull*/ sp_clearcommand_new(void); /** + * Calls method [`servicepoint::ClearCommand::try_into_packet`]. + * *Tries to turn a [`ClearCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `clearcommand` module. */ -struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ command); +struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); /** * Moves the provided [CharGrid] into a new [CharGridCommand], @@ -1512,52 +1531,6 @@ struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ size_t origin_x, size_t origin_y); -/** - * Clones an [SPCommand] instance. - * - * returns: a new [SPCommand] instance. - * - * This function is part of the `cmd_generic` module. - */ -struct Command sp_cmd_generic_clone(struct Command command); - -/** - * Deallocates an [SPCommand]. - * - * Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. - * - * # Examples - * - * ```C - * SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); - * sp_command_free(c); - * ``` - * - * This function is part of the `cmd_generic` module. - */ -void sp_cmd_generic_free(struct Command command); - -/** - * Tries to turn a [SPCommand] into a [Packet]. - * The [SPCommand] gets consumed. - * - * Returns tag [CommandTag::Invalid] in case of an error. - * - * This function is part of the `cmd_generic` module. - */ -struct Packet *sp_cmd_generic_into_packet(struct Command command); - -/** - * Tries to turn a [Packet] into a [SPCommand]. - * - * The packet is dropped in the process. - * - * Returns: pointer to new [SPCommand] instance or NULL if parsing failed. - * - * This function is part of the `cmd_generic` module. - */ -struct Command sp_cmd_generic_try_from_packet(struct Packet */*notnull*/ packet); - /** *Clones a [`Cp437Grid`] instance. * @@ -1662,6 +1635,8 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, uint8_t value); /** + * Calls method [`servicepoint::Cp437Grid::try_into_packet`]. + * * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. * * The provided [Cp437Grid] gets consumed. @@ -1735,13 +1710,15 @@ void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command size_t origin_y); /** + * Calls method [`servicepoint::Cp437GridCommand::try_into_packet`]. + * *Tries to turn a [`Cp437GridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `cp437gridcommand` module. */ -struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ command); +struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. @@ -1752,14 +1729,14 @@ struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*no * * This function is part of the `displaybitvec` module. */ -struct ByteSlice sp_displaybitvec_as_raw_mut_slice(BitVec */*notnull*/ instance); +struct ByteSlice sp_displaybitvec_as_raw_mut_slice(DisplayBitVec */*notnull*/ instance); /** *Clones a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. */ -BitVec */*notnull*/ sp_displaybitvec_clone(BitVec */*notnull*/ instance); +DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::fill`]. @@ -1772,14 +1749,14 @@ BitVec */*notnull*/ sp_displaybitvec_clone(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -void sp_displaybitvec_fill(BitVec */*notnull*/ instance, bool value); +void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. */ -void sp_displaybitvec_free(BitVec */*notnull*/ instance); +void sp_displaybitvec_free(DisplayBitVec */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::get`]. @@ -1799,7 +1776,7 @@ void sp_displaybitvec_free(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); +bool sp_displaybitvec_get(DisplayBitVec */*notnull*/ instance, size_t index); /** * Calls method [`servicepoint::DisplayBitVec::is_empty`]. @@ -1808,7 +1785,7 @@ bool sp_displaybitvec_get(BitVec */*notnull*/ instance, size_t index); * * This function is part of the `displaybitvec` module. */ -bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); +bool sp_displaybitvec_is_empty(DisplayBitVec */*notnull*/ instance); /** * Calls method [`servicepoint::DisplayBitVec::len`]. @@ -1817,7 +1794,7 @@ bool sp_displaybitvec_is_empty(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); +size_t sp_displaybitvec_len(DisplayBitVec */*notnull*/ instance); /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. @@ -1826,7 +1803,7 @@ size_t sp_displaybitvec_len(BitVec */*notnull*/ instance); * * This function is part of the `displaybitvec` module. */ -BitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); +DisplayBitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); /** * Creates a new [DisplayBitVec] instance. @@ -1843,7 +1820,7 @@ BitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); * * This function is part of the `displaybitvec` module. */ -BitVec */*notnull*/ sp_displaybitvec_new(size_t size); +DisplayBitVec */*notnull*/ sp_displaybitvec_new(size_t size); /** * Calls method [`servicepoint::DisplayBitVec::set`]. @@ -1861,11 +1838,13 @@ BitVec */*notnull*/ sp_displaybitvec_new(size_t size); * * This function is part of the `displaybitvec` module. */ -void sp_displaybitvec_set(BitVec */*notnull*/ instance, +void sp_displaybitvec_set(DisplayBitVec */*notnull*/ instance, size_t index, bool value); /** + * Calls method [`servicepoint::DisplayBitVec::try_into_packet`]. + * * Creates a [BitVecCommand] and immediately turns that into a [Packet]. * * The provided [DisplayBitVec] gets consumed. @@ -1874,7 +1853,7 @@ void sp_displaybitvec_set(BitVec */*notnull*/ instance, * * This function is part of the `displaybitvec` module. */ -struct Packet *sp_displaybitvec_try_into_packet(BitVec */*notnull*/ bitvec, +struct Packet *sp_displaybitvec_try_into_packet(DisplayBitVec */*notnull*/ bitvec, size_t offset, BinaryOperation operation, CompressionCode compression); @@ -1911,13 +1890,52 @@ void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); /** + * Calls method [`servicepoint::FadeOutCommand::try_into_packet`]. + * *Tries to turn a [`FadeOutCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `fadeoutcommand` module. */ -struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ command); +struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ instance); + +/** + *Clones a [`GenericCommand`] instance. + * + * This function is part of the `genericcommand` module. + */ +struct GenericCommand */*notnull*/ sp_genericcommand_clone(struct GenericCommand */*notnull*/ instance); + +/** + *Deallocates a [`GenericCommand`] instance. + * + * This function is part of the `genericcommand` module. + */ +void sp_genericcommand_free(struct GenericCommand */*notnull*/ instance); + +/** + * Tries to turn a [Packet] into a [GenericCommand]. + * + * The packet is dropped in the process. + * + * Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. + * + * This function is part of the `genericcommand` module. + */ +struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Packet */*notnull*/ packet); + +/** + * Calls method [`servicepoint::GenericCommand::try_into_packet`]. + * + * Tries to turn a [GenericCommand] into a [Packet]. + * The [GenericCommand] gets consumed. + * + * Returns tag [CommandTag::Invalid] in case of an error. + * + * This function is part of the `genericcommand` module. + */ +struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnull*/ command); /** *Clones a [`GlobalBrightnessCommand`] instance. @@ -1958,13 +1976,15 @@ void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */ Brightness value); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::try_into_packet`]. + * *Tries to turn a [`GlobalBrightnessCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `globalbrightnesscommand` module. */ -struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ command); +struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Clones a [`HardResetCommand`] instance. @@ -1992,13 +2012,15 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); /** + * Calls method [`servicepoint::HardResetCommand::try_into_packet`]. + * *Tries to turn a [`HardResetCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * * This function is part of the `hardresetcommand` module. */ -struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ command); +struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ instance); /** *Clones a [`Packet`] instance. @@ -2147,7 +2169,9 @@ struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, uint16_t port); /** - * Sends a [SPCommand] to the display using the [UdpSocket]. + * Calls method [`servicepoint::UdpSocket::send_command`]. + * + * Sends a [GenericCommand] to the display using the [UdpSocket]. * * The passed `command` gets consumed. * @@ -2162,9 +2186,11 @@ struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, * This function is part of the `udpsocket` module. */ bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, - struct Command command); + struct GenericCommand */*notnull*/ command); /** + * Calls method [`servicepoint::UdpSocket::send_header`]. + * * Sends a [Header] to the display using the [UdpSocket]. * * returns: true in case of success @@ -2181,6 +2207,8 @@ bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); /** + * Calls method [`servicepoint::UdpSocket::send_packet`]. + * * Sends a [Packet] to the display using the [UdpSocket]. * * The passed `packet` gets consumed. diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index a4775d9..609daac 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::wrap_functions, + macros::{derive_clone, derive_free, wrap_functions}, mem::{ heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, heap_remove, @@ -11,6 +11,7 @@ use servicepoint::{ HardResetCommand, Packet, TypedCommand, }; use std::ptr::{null_mut, NonNull}; +use crate::macros::wrap_methods; /// Pointer to one of the available command structs. #[repr(C)] @@ -55,214 +56,211 @@ pub enum CommandTag { /// /// Rust equivalent: [TypedCommand]. #[repr(C)] -pub struct SPCommand { +pub struct GenericCommand { /// Specifies which kind of command struct is contained in `data` pub tag: CommandTag, /// The pointer to the command struct pub data: CommandUnion, } -impl SPCommand { - const INVALID: SPCommand = SPCommand { +impl GenericCommand { + pub(crate) const INVALID: GenericCommand = GenericCommand { tag: CommandTag::Invalid, data: CommandUnion { null: null_mut() }, }; } -wrap_functions!(cmd_generic; +derive_clone!(GenericCommand); - /// Tries to turn a [Packet] into a [SPCommand]. +impl Clone for GenericCommand { + fn clone(&self) -> Self { + unsafe { + match self.tag { + CommandTag::Clear => GenericCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_clone(self.data.clear), + }, + }, + CommandTag::CharGrid => GenericCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_clone(self.data.char_grid), + }, + }, + CommandTag::Cp437Grid => GenericCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_clone(self.data.cp437_grid), + }, + }, + CommandTag::Bitmap => GenericCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_clone(self.data.bitmap), + }, + }, + CommandTag::GlobalBrightness => GenericCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_clone( + self.data.global_brightness, + ), + }, + }, + CommandTag::BrightnessGrid => GenericCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_clone(self.data.brightness_grid), + }, + }, + CommandTag::BitVec => GenericCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bit_vec: heap_clone(self.data.bit_vec), + }, + }, + CommandTag::HardReset => GenericCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_clone(self.data.hard_reset), + }, + }, + CommandTag::FadeOut => GenericCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_clone(self.data.fade_out), + }, + }, + #[allow(deprecated)] + CommandTag::BitmapLegacy => GenericCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_clone(self.data.bitmap_legacy), + }, + }, + CommandTag::Invalid => GenericCommand::INVALID, + } + } + } +} + +derive_free!(GenericCommand); + +impl Drop for GenericCommand { + fn drop(&mut self) { + unsafe { + match self.tag { + CommandTag::Invalid => (), + CommandTag::Bitmap => heap_drop(self.data.bitmap), + CommandTag::BitVec => heap_drop(self.data.bit_vec), + CommandTag::BrightnessGrid => { + heap_drop(self.data.brightness_grid) + } + CommandTag::CharGrid => heap_drop(self.data.char_grid), + CommandTag::Cp437Grid => heap_drop(self.data.cp437_grid), + CommandTag::GlobalBrightness => { + heap_drop(self.data.global_brightness) + } + CommandTag::Clear => heap_drop(self.data.clear), + CommandTag::HardReset => heap_drop(self.data.hard_reset), + CommandTag::FadeOut => heap_drop(self.data.fade_out), + CommandTag::BitmapLegacy => heap_drop(self.data.bitmap_legacy), + } + } + } +} + +wrap_functions!(associate GenericCommand; + + /// Tries to turn a [Packet] into a [GenericCommand]. /// /// The packet is dropped in the process. /// - /// Returns: pointer to new [SPCommand] instance or NULL if parsing failed. + /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. fn try_from_packet( packet: NonNull, - ) -> SPCommand { + ) -> NonNull { let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; - servicepoint::TypedCommand::try_from(packet) + let result = servicepoint::TypedCommand::try_from(packet) .map(|value| match value { - TypedCommand::Clear(clear) => SPCommand { + TypedCommand::Clear(clear) => GenericCommand { tag: CommandTag::Clear, data: CommandUnion { clear: heap_move_nonnull(clear), }, }, - TypedCommand::CharGrid(char_grid) => SPCommand { + TypedCommand::CharGrid(char_grid) => GenericCommand { tag: CommandTag::CharGrid, data: CommandUnion { char_grid: heap_move_nonnull(char_grid), }, }, - TypedCommand::Cp437Grid(cp437_grid) => SPCommand { + TypedCommand::Cp437Grid(cp437_grid) => GenericCommand { tag: CommandTag::Cp437Grid, data: CommandUnion { cp437_grid: heap_move_nonnull(cp437_grid), }, }, - TypedCommand::Bitmap(bitmap) => SPCommand { + TypedCommand::Bitmap(bitmap) => GenericCommand { tag: CommandTag::Bitmap, data: CommandUnion { bitmap: heap_move_nonnull(bitmap), }, }, - TypedCommand::Brightness(global_brightness) => SPCommand { + TypedCommand::Brightness(global_brightness) => GenericCommand { tag: CommandTag::GlobalBrightness, data: CommandUnion { global_brightness: heap_move_nonnull(global_brightness), }, }, - TypedCommand::BrightnessGrid(brightness_grid) => SPCommand { + TypedCommand::BrightnessGrid(brightness_grid) => GenericCommand { tag: CommandTag::BrightnessGrid, data: CommandUnion { brightness_grid: heap_move_nonnull(brightness_grid), }, }, - TypedCommand::BitVec(bitvec) => SPCommand { + TypedCommand::BitVec(bitvec) => GenericCommand { tag: CommandTag::BitVec, data: CommandUnion { bit_vec: heap_move_nonnull(bitvec), }, }, - TypedCommand::HardReset(hard_reset) => SPCommand { + TypedCommand::HardReset(hard_reset) => GenericCommand { tag: CommandTag::HardReset, data: CommandUnion { hard_reset: heap_move_nonnull(hard_reset), }, }, - TypedCommand::FadeOut(fade_out) => SPCommand { + TypedCommand::FadeOut(fade_out) => GenericCommand { tag: CommandTag::FadeOut, data: CommandUnion { fade_out: heap_move_nonnull(fade_out), }, }, #[allow(deprecated)] - TypedCommand::BitmapLegacy(bitmap_legacy) => SPCommand { + TypedCommand::BitmapLegacy(bitmap_legacy) => GenericCommand { tag: CommandTag::BitmapLegacy, data: CommandUnion { bitmap_legacy: heap_move_nonnull(bitmap_legacy), }, }, }) - .unwrap_or_else(move |_| SPCommand { + .unwrap_or_else(move |_| GenericCommand { tag: CommandTag::Invalid, data: CommandUnion { null: null_mut() }, - }) + }); + heap_move_nonnull(result) } - /// Clones an [SPCommand] instance. - /// - /// returns: a new [SPCommand] instance. - fn clone(command: SPCommand) -> SPCommand { - unsafe { - match command.tag { - CommandTag::Clear => SPCommand { - tag: CommandTag::Clear, - data: CommandUnion { - clear: heap_clone(command.data.clear), - }, - }, - CommandTag::CharGrid => SPCommand { - tag: CommandTag::CharGrid, - data: CommandUnion { - char_grid: heap_clone(command.data.char_grid), - }, - }, - CommandTag::Cp437Grid => SPCommand { - tag: CommandTag::Cp437Grid, - data: CommandUnion { - cp437_grid: heap_clone(command.data.cp437_grid), - }, - }, - CommandTag::Bitmap => SPCommand { - tag: CommandTag::Bitmap, - data: CommandUnion { - bitmap: heap_clone(command.data.bitmap), - }, - }, - CommandTag::GlobalBrightness => SPCommand { - tag: CommandTag::GlobalBrightness, - data: CommandUnion { - global_brightness: heap_clone( - command.data.global_brightness, - ), - }, - }, - CommandTag::BrightnessGrid => SPCommand { - tag: CommandTag::BrightnessGrid, - data: CommandUnion { - brightness_grid: heap_clone(command.data.brightness_grid), - }, - }, - CommandTag::BitVec => SPCommand { - tag: CommandTag::BitVec, - data: CommandUnion { - bit_vec: heap_clone(command.data.bit_vec), - }, - }, - CommandTag::HardReset => SPCommand { - tag: CommandTag::HardReset, - data: CommandUnion { - hard_reset: heap_clone(command.data.hard_reset), - }, - }, - CommandTag::FadeOut => SPCommand { - tag: CommandTag::FadeOut, - data: CommandUnion { - fade_out: heap_clone(command.data.fade_out), - }, - }, - #[allow(deprecated)] - CommandTag::BitmapLegacy => SPCommand { - tag: CommandTag::BitmapLegacy, - data: CommandUnion { - bitmap_legacy: heap_clone(command.data.bitmap_legacy), - }, - }, - CommandTag::Invalid => SPCommand::INVALID, - } - } - } +); - /// Deallocates an [SPCommand]. - /// - /// Commands with an invalid `tag` do not have to be freed as the `data` pointer should be null. - /// - /// # Examples - /// - /// ```C - /// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new()); - /// sp_command_free(c); - /// ``` - fn free(command: SPCommand) { - unsafe { - match command.tag { - CommandTag::Invalid => (), - CommandTag::Bitmap => heap_drop(command.data.bitmap), - CommandTag::BitVec => heap_drop(command.data.bit_vec), - CommandTag::BrightnessGrid => { - heap_drop(command.data.brightness_grid) - } - CommandTag::CharGrid => heap_drop(command.data.char_grid), - CommandTag::Cp437Grid => heap_drop(command.data.cp437_grid), - CommandTag::GlobalBrightness => { - heap_drop(command.data.global_brightness) - } - CommandTag::Clear => heap_drop(command.data.clear), - CommandTag::HardReset => heap_drop(command.data.hard_reset), - CommandTag::FadeOut => heap_drop(command.data.fade_out), - CommandTag::BitmapLegacy => heap_drop(command.data.bitmap_legacy), - } - } - } - - /// Tries to turn a [SPCommand] into a [Packet]. - /// The [SPCommand] gets consumed. +wrap_methods!{GenericCommand; + /// Tries to turn a [GenericCommand] into a [Packet]. + /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - fn into_packet( - command: SPCommand, - ) -> *mut Packet { + move fn try_into_packet(command) -> *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { @@ -296,6 +294,5 @@ wrap_functions!(cmd_generic; heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) } } - } - -); + }; +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 58e5957..74d22e3 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -50,7 +50,7 @@ macro_rules! wrap_origin_accessors { macro_rules! derive_command_from { ($command:ident) => { ::paste::paste! { - impl From<::servicepoint::[< $command Command >]> for $crate::commands::SPCommand { + impl From<::servicepoint::[< $command Command >]> for $crate::commands::GenericCommand { fn from(command: ::servicepoint::[< $command Command >]) -> Self { Self { tag: $crate::commands::CommandTag::$command, @@ -67,14 +67,12 @@ macro_rules! derive_command_from { macro_rules! derive_command_into_packet { ($command_type:ident) => { ::paste::paste! { - $crate::macros::wrap_functions!(associate $command_type; + $crate::macros::wrap_method!($command_type; #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet( - command: ::core::ptr::NonNull<$command_type>, - ) -> *mut ::servicepoint::Packet { - $crate::mem::heap_move_ok(unsafe { $crate::mem::heap_remove(command) }.try_into()) + move fn try_into_packet(instance) -> *mut ::servicepoint::Packet { + $crate::mem::heap_move_ok(instance.try_into()) } ); } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 9bae852..4ce7972 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -12,7 +12,6 @@ use std::ptr::NonNull; wrap_grid!(Bitmap, bool); wrap_functions!(bitmap; - /// Creates a new [Bitmap] with the specified dimensions. /// /// # Arguments @@ -76,35 +75,26 @@ wrap_functions!(bitmap; let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Bitmap::from_bitvec(width, bitvec)) } +); + +wrap_methods!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - fn into_bitvec( - bitmap: NonNull - ) -> NonNull { - let bitmap = unsafe { heap_remove(bitmap) }; + move fn into_bitvec(bitmap) -> NonNull { heap_move_nonnull(bitmap.into()) - } + }; /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. /// /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - bitmap: NonNull, - x: usize, - y: usize, - compression: CompressionCode, - ) -> *mut Packet { - let bitmap = unsafe { heap_remove(bitmap) }; + move fn try_into_packet(bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), compression, })) - } -); - -wrap_methods!(Bitmap; + }; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 2a76cf7..bc5da06 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,7 +1,7 @@ use crate::{ containers::{wrap_container, ByteSlice}, macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -35,29 +35,27 @@ wrap_functions!(associate DisplayBitVec; heap_move_nonnull(DisplayBitVec::from_slice(data)) } +); + +wrap_methods!(DisplayBitVec; /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - bitvec: NonNull, + move fn try_into_packet( + bitvec, offset: usize, operation: BinaryOperation, - compression: CompressionCode, + compression: CompressionCode ) -> *mut Packet { - let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Packet::try_from(BitVecCommand { bitvec, offset, operation, compression, })) - } - -); - -wrap_methods!(DisplayBitVec; + }; /// Gets the value of a bit. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a5d01f2..7c27d5c 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,7 +1,7 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -54,26 +54,21 @@ wrap_functions!(associate BrightnessGrid; ) } +); + +wrap_methods!(BrightnessGrid; /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. /// /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - grid: NonNull, - x: usize, - y: usize, - ) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; + move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), })) - } + }; -); - -wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 1a59fdd..1966f4a 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,7 +1,7 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_remove}, + mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -23,41 +23,18 @@ wrap_functions!(associate CharGrid; /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` - fn new( - width: usize, - height: usize, - ) -> NonNull { + fn new(width: usize, height: usize) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) } /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load( - width: usize, - height: usize, - data: ByteSlice, - ) -> *mut CharGrid { + fn load(width: usize, height: usize, data: ByteSlice) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } - /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. - /// - /// The provided [CharGrid] gets consumed. - /// - /// Returns NULL in case of an error. - fn try_into_packet( - grid: NonNull, - x: usize, - y: usize, - ) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; - heap_move_ok(Packet::try_from(CharGridCommand { - grid, - origin: Origin::new(x, y), - })) - } ); @@ -102,4 +79,16 @@ wrap_methods!(CharGrid; mut fn fill(instance, value: u32) { instance.fill(char::from_u32(value).unwrap()) }; + + /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [CharGrid] gets consumed. + /// + /// Returns NULL in case of an error. + move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + heap_move_ok(Packet::try_from(CharGridCommand { + grid, + origin: Origin::new(x, y), + })) + }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index fc31d13..38efc58 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,7 +1,7 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -32,26 +32,21 @@ wrap_functions!(cp437grid; heap_move_some(Cp437Grid::load(width, height, data)) } +); + +wrap_methods!(Cp437Grid; /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. /// /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( - grid: NonNull, - x: usize, - y: usize, - ) -> *mut Packet { - let grid = unsafe { heap_remove(grid) }; + move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), })) - } + }; -); - -wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. diff --git a/src/macros.rs b/src/macros.rs index e0560f5..768254b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -36,6 +36,12 @@ macro_rules! nonnull_as_mut { }; } +macro_rules! nonnull_as_move { + ($ident:ident) => { + $crate::mem::heap_remove($ident) + }; +} + macro_rules! wrap_method { ( $object_type:ident; @@ -229,6 +235,7 @@ macro_rules! wrap_functions { } pub(crate) use { - derive_clone, derive_free, nonnull_as_mut, nonnull_as_ref, wrap_fields, - wrap_functions, wrap_methods, wrap_fields_accessor, wrap_method + derive_clone, derive_free, nonnull_as_move, nonnull_as_mut, nonnull_as_ref, + wrap_fields, wrap_fields_accessor, wrap_functions, wrap_method, + wrap_methods, }; diff --git a/src/udp.rs b/src/udp.rs index 321c4d1..7889937 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::{ - commands::{CommandTag, SPCommand}, - macros::{derive_free, wrap_functions}, + commands::{CommandTag, GenericCommand}, + macros::{derive_free, wrap_functions, wrap_methods}, mem::{heap_move_ok, heap_remove}, }; use servicepoint::{Header, Packet, UdpSocketExt}; @@ -49,17 +49,20 @@ wrap_functions!(associate UdpSocket; heap_move_ok(UdpSocket::bind_connect(addr)) } +); + +wrap_methods! {UdpSocket; /// Sends a [Packet] to the display using the [UdpSocket]. /// /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(connection: NonNull, packet: NonNull) -> bool { + ref fn send_packet(connection, packet: NonNull) -> bool { let packet = unsafe { heap_remove(packet) }; - unsafe { connection.as_ref().send(&Vec::from(packet)) }.is_ok() - } + connection.send(&Vec::from(packet)).is_ok() + }; - /// Sends a [SPCommand] to the display using the [UdpSocket]. + /// Sends a [GenericCommand] to the display using the [UdpSocket]. /// /// The passed `command` gets consumed. /// @@ -70,44 +73,26 @@ wrap_functions!(associate UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(connection: NonNull, command: SPCommand) -> bool { + ref fn send_command(connection, command: NonNull) -> bool { unsafe { - match command.tag { + let command = crate::macros::nonnull_as_mut!(command); + let result = match command.tag { CommandTag::Invalid => return false, - CommandTag::Bitmap => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap)), - CommandTag::BitVec => connection - .as_ref() - .send_command(heap_remove(command.data.bit_vec)), - CommandTag::BrightnessGrid => connection - .as_ref() - .send_command(heap_remove(command.data.brightness_grid)), - CommandTag::CharGrid => connection - .as_ref() - .send_command(heap_remove(command.data.char_grid)), - CommandTag::Cp437Grid => connection - .as_ref() - .send_command(heap_remove(command.data.cp437_grid)), - CommandTag::GlobalBrightness => connection - .as_ref() - .send_command(heap_remove(command.data.global_brightness)), - CommandTag::Clear => connection - .as_ref() - .send_command(heap_remove(command.data.clear)), - CommandTag::HardReset => connection - .as_ref() - .send_command(heap_remove(command.data.hard_reset)), - CommandTag::FadeOut => connection - .as_ref() - .send_command(heap_remove(command.data.fade_out)), - CommandTag::BitmapLegacy => connection - .as_ref() - .send_command(heap_remove(command.data.bitmap_legacy)), - } + CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), + CommandTag::BitVec => connection.send_command(heap_remove(command.data.bit_vec)), + CommandTag::BrightnessGrid => connection.send_command(heap_remove(command.data.brightness_grid)), + CommandTag::CharGrid => connection.send_command(heap_remove(command.data.char_grid)), + CommandTag::Cp437Grid => connection.send_command(heap_remove(command.data.cp437_grid)), + CommandTag::GlobalBrightness => connection.send_command(heap_remove(command.data.global_brightness)), + CommandTag::Clear => connection.send_command(heap_remove(command.data.clear)), + CommandTag::HardReset => connection.send_command(heap_remove(command.data.hard_reset)), + CommandTag::FadeOut => connection.send_command(heap_remove(command.data.fade_out)), + CommandTag::BitmapLegacy => connection.send_command(heap_remove(command.data.bitmap_legacy)), + }.is_some(); + *command = GenericCommand::INVALID; + result } - .is_some() - } + }; /// Sends a [Header] to the display using the [UdpSocket]. /// @@ -118,17 +103,14 @@ wrap_functions!(associate UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(udp_connection: NonNull, header: Header) -> bool { + ref fn send_header(udp_connection, header: Header) -> bool { let packet = Packet { header, payload: None, }; - unsafe { udp_connection.as_ref() } - .send(&Vec::from(packet)) - .is_ok() - } - -); + udp_connection.send(&Vec::from(packet)).is_ok() + }; +} mod _hidden { /// This is a type only used by cbindgen to have a type for pointers. From 5a849a87c7bb608ba0dcabbe2e82dd702329eee7 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 21:16:22 +0200 Subject: [PATCH 61/76] fix missing renames --- include/servicepoint.h | 84 +++++++++++++++--------------- src/commands/char_grid_command.rs | 2 +- src/commands/cp437_grid_command.rs | 2 +- src/commands/generic_command.rs | 11 ++-- src/containers/bitmap.rs | 2 +- src/containers/cp437_grid.rs | 15 ++---- 6 files changed, 53 insertions(+), 63 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 095c6cc..2648383 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -1405,6 +1405,14 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridComm */ void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); +/** + * Moves the provided [CharGrid] into a new [CharGridCommand], + * leaving other fields as their default values. + * + * This function is part of the `chargridcommand` module. + */ +struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*notnull*/ grid); + /** * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. * @@ -1424,6 +1432,19 @@ void sp_chargridcommand_get_origin(struct CharGridCommand */*notnull*/ command, size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); +/** + * Show UTF-8 encoded text on the screen. + * + * The passed [CharGrid] gets consumed. + * + * Returns: a new [CharGridCommand] instance. + * + * This function is part of the `chargridcommand` module. + */ +struct CharGridCommand */*notnull*/ sp_chargridcommand_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + /** * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. @@ -1489,48 +1510,6 @@ struct ClearCommand */*notnull*/ sp_clearcommand_new(void); */ struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); -/** - * Moves the provided [CharGrid] into a new [CharGridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cmd_chargrid` module. - */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_from_grid(CharGrid */*notnull*/ grid); - -/** - * Show UTF-8 encoded text on the screen. - * - * The passed [CharGrid] gets consumed. - * - * Returns: a new [CharGridCommand] instance. - * - * This function is part of the `cmd_chargrid` module. - */ -struct CharGridCommand */*notnull*/ sp_cmd_chargrid_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - -/** - * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_from_grid(Cp437Grid */*notnull*/ grid); - -/** - * Show text on the screen. - * - * The text is sent in the form of a 2D grid of [CP-437] encoded characters. - * - * The origin is relative to the top-left of the display. - * - * This function is part of the `cmd_cp437grid` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cmd_cp437grid_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - /** *Clones a [`Cp437Grid`] instance. * @@ -1672,6 +1651,14 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridC */ void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); +/** + * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], + * leaving other fields as their default values. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */*notnull*/ grid); + /** * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. * @@ -1691,6 +1678,19 @@ void sp_cp437gridcommand_get_origin(struct Cp437GridCommand */*notnull*/ command size_t */*notnull*/ origin_x, size_t */*notnull*/ origin_y); +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of [CP-437] encoded characters. + * + * The origin is relative to the top-left of the display. + * + * This function is part of the `cp437gridcommand` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + /** * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index f8d5107..41e5000 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -14,7 +14,7 @@ wrap_fields!(CharGridCommand; wrap_origin_accessors!(CharGridCommand); -wrap_functions!(cmd_chargrid; +wrap_functions!(associate CharGridCommand; /// Show UTF-8 encoded text on the screen. /// diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 4977195..b43afb4 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -14,7 +14,7 @@ wrap_fields!(Cp437GridCommand; wrap_origin_accessors!(Cp437GridCommand); -wrap_functions!(cmd_cp437grid; +wrap_functions!(associate Cp437GridCommand; /// Show text on the screen. /// diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 609daac..d288a27 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{derive_clone, derive_free, wrap_functions}, + macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, mem::{ heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, heap_remove, @@ -11,7 +11,6 @@ use servicepoint::{ HardResetCommand, Packet, TypedCommand, }; use std::ptr::{null_mut, NonNull}; -use crate::macros::wrap_methods; /// Pointer to one of the available command structs. #[repr(C)] @@ -168,11 +167,12 @@ impl Drop for GenericCommand { CommandTag::BitmapLegacy => heap_drop(self.data.bitmap_legacy), } } + + *self = Self::INVALID; } } wrap_functions!(associate GenericCommand; - /// Tries to turn a [Packet] into a [GenericCommand]. /// /// The packet is dropped in the process. @@ -181,7 +181,7 @@ wrap_functions!(associate GenericCommand; fn try_from_packet( packet: NonNull, ) -> NonNull { - let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; + let packet = unsafe { heap_remove(packet) }; let result = servicepoint::TypedCommand::try_from(packet) .map(|value| match value { TypedCommand::Clear(clear) => GenericCommand { @@ -252,10 +252,9 @@ wrap_functions!(associate GenericCommand; }); heap_move_nonnull(result) } - ); -wrap_methods!{GenericCommand; +wrap_methods! { GenericCommand; /// Tries to turn a [GenericCommand] into a [Packet]. /// The [GenericCommand] gets consumed. /// diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 4ce7972..738925b 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -11,7 +11,7 @@ use std::ptr::NonNull; wrap_grid!(Bitmap, bool); -wrap_functions!(bitmap; +wrap_functions!(associate Bitmap; /// Creates a new [Bitmap] with the specified dimensions. /// /// # Arguments diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 38efc58..8905610 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -10,28 +10,19 @@ use std::ptr::NonNull; wrap_grid!(Cp437Grid, u8); -wrap_functions!(cp437grid; - +wrap_functions!(associate Cp437Grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. - fn new( - width: usize, - height: usize, - ) -> NonNull { + fn new(width: usize, height: usize) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) } /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load( - width: usize, - height: usize, - data: ByteSlice, - ) -> *mut Cp437Grid { + fn load(width: usize, height: usize, data: ByteSlice) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; heap_move_some(Cp437Grid::load(width, height, data)) } - ); wrap_methods!(Cp437Grid; From 664625402f88bac5aeb61b649b1f64ef55b3c0d3 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 21:36:01 +0200 Subject: [PATCH 62/76] move where the modifier is --- src/commands/generic_command.rs | 2 +- src/commands/mod.rs | 2 +- src/containers/bitmap.rs | 6 +++--- src/containers/bitvec.rs | 16 ++++++++-------- src/containers/brightness_grid.rs | 4 ++-- src/containers/char_grid.rs | 8 ++++---- src/containers/cp437_grid.rs | 4 ++-- src/containers/mod.rs | 10 +++++----- src/macros.rs | 10 +++++----- src/udp.rs | 6 +++--- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index d288a27..dad7584 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -259,7 +259,7 @@ wrap_methods! { GenericCommand; /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - move fn try_into_packet(command) -> *mut Packet { + fn try_into_packet(move command) -> *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 74d22e3..026d0f1 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -71,7 +71,7 @@ macro_rules! derive_command_into_packet { #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. - move fn try_into_packet(instance) -> *mut ::servicepoint::Packet { + fn try_into_packet(move instance) -> *mut ::servicepoint::Packet { $crate::mem::heap_move_ok(instance.try_into()) } ); diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 738925b..4fb55a2 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -79,7 +79,7 @@ wrap_functions!(associate Bitmap; wrap_methods!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - move fn into_bitvec(bitmap) -> NonNull { + fn into_bitvec(move bitmap) -> NonNull { heap_move_nonnull(bitmap.into()) }; @@ -88,7 +88,7 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { + fn try_into_packet(move bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), @@ -99,7 +99,7 @@ wrap_methods!(Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - mut fn data_ref_mut(instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index bc5da06..6ae2cf6 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -43,8 +43,8 @@ wrap_methods!(DisplayBitVec; /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet( - bitvec, + fn try_into_packet( + move bitvec, offset: usize, operation: BinaryOperation, compression: CompressionCode @@ -69,7 +69,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - ref fn get(instance, index: usize) -> bool { + fn get(ref instance, index: usize) -> bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -83,25 +83,25 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - mut fn set(instance, index: usize, value: bool); + fn set(mut instance, index: usize, value: bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - mut fn fill(instance, value: bool); + fn fill(mut instance, value: bool); /// Gets the length in bits. - ref fn len(instance) -> usize; + fn len(ref instance) -> usize; /// Returns true if length is 0. - ref fn is_empty(instance) -> bool; + fn is_empty(ref instance) -> bool; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - mut fn as_raw_mut_slice(instance) -> ByteSlice { + fn as_raw_mut_slice(mut instance) -> ByteSlice { unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 7c27d5c..a71a953 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -62,7 +62,7 @@ wrap_methods!(BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), @@ -72,7 +72,7 @@ wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - mut fn data_ref_mut(instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 1966f4a..a360a26 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -49,7 +49,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(instance, x: usize, y: usize) -> u32 { + fn get(ref instance, x: usize, y: usize) -> u32 { instance.get(x, y) as u32 }; @@ -66,7 +66,7 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - mut fn set(instance, x: usize, y: usize, value: u32) { + fn set(mut instance, x: usize, y: usize, value: u32) { instance.set(x, y, char::from_u32(value).unwrap()) }; @@ -76,7 +76,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - mut fn fill(instance, value: u32) { + fn fill(mut instance, value: u32) { instance.fill(char::from_u32(value).unwrap()) }; @@ -85,7 +85,7 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 8905610..d4ed8a1 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -31,7 +31,7 @@ wrap_methods!(Cp437Grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - move fn try_into_packet(grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), @@ -41,7 +41,7 @@ wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - mut fn data_ref_mut(instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index fcd76a1..0a9eb38 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - ref fn width(instance) -> usize; + fn width(ref instance) -> usize; /// Gets the height. - ref fn height(instance) -> usize; + fn height(ref instance) -> usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - ref fn get(instance, x: usize, y: usize) -> $value_type; + fn get(ref instance, x: usize, y: usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - mut fn set(instance, x: usize, y: usize, value: $value_type); + fn set(mut instance, x: usize, y: usize, value: $value_type); /// Sets the state of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to - mut fn fill(instance, value: $value_type); + fn fill(mut instance, value: $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index 768254b..d2b974c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -46,14 +46,14 @@ macro_rules! wrap_method { ( $object_type:ident; $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? ) => { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; $(#[$meta])+ - $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) $(-> $return_type)? { $instance.$function($($($param_name),*)?) } @@ -62,7 +62,7 @@ macro_rules! wrap_method { }; ($object_type:ident; $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? $impl:block ) => { @@ -88,7 +88,7 @@ macro_rules! wrap_methods { $object_type:ident; $( $(#[$meta:meta])+ - $ref_or_mut:ident fn $function:ident($instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) $(-> $return_type:ty)? $($impl:block)?; )+ @@ -97,7 +97,7 @@ macro_rules! wrap_methods { $( $crate::macros::wrap_method!($object_type; $(#[$meta])* - $ref_or_mut fn $function($instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) $(-> $return_type)? $($impl)? ); diff --git a/src/udp.rs b/src/udp.rs index 7889937..aa307b0 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -57,7 +57,7 @@ wrap_methods! {UdpSocket; /// The passed `packet` gets consumed. /// /// returns: true in case of success - ref fn send_packet(connection, packet: NonNull) -> bool { + fn send_packet(ref connection, packet: NonNull) -> bool { let packet = unsafe { heap_remove(packet) }; connection.send(&Vec::from(packet)).is_ok() }; @@ -73,7 +73,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - ref fn send_command(connection, command: NonNull) -> bool { + fn send_command(ref connection, command: NonNull) -> bool { unsafe { let command = crate::macros::nonnull_as_mut!(command); let result = match command.tag { @@ -103,7 +103,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - ref fn send_header(udp_connection, header: Header) -> bool { + fn send_header(ref udp_connection, header: Header) -> bool { let packet = Packet { header, payload: None, From e8f11c08eab0da20e6fd7194c184222ceecd8d47 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 23 Jun 2025 23:28:30 +0200 Subject: [PATCH 63/76] add parameter modifiers --- src/commands/bitmap_command.rs | 18 ++- src/commands/bitvec_command.rs | 12 +- src/commands/brightness_grid_command.rs | 16 ++- src/commands/char_grid_command.rs | 16 ++- src/commands/cp437_grid_command.rs | 16 ++- src/commands/generic_command.rs | 5 +- src/commands/global_brightness_command.rs | 2 +- src/commands/mod.rs | 31 ++--- src/containers/bitmap.rs | 19 ++- src/containers/bitvec.rs | 18 +-- src/containers/brightness_grid.rs | 15 +-- src/containers/char_grid.rs | 14 +-- src/containers/cp437_grid.rs | 8 +- src/containers/mod.rs | 10 +- src/macros.rs | 138 ++++++++++++---------- src/packet.rs | 94 +++++++-------- src/udp.rs | 12 +- 17 files changed, 214 insertions(+), 230 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 01e3620..5aaab26 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; @@ -22,13 +22,13 @@ wrap_functions!(associate BitmapCommand; /// /// Returns: a new [BitmapCommand] instance. fn new( - bitmap: NonNull, - origin_x: usize, - origin_y: usize, - compression: CompressionCode, + bitmap: move NonNull, + origin_x: val usize, + origin_y: val usize, + compression: val CompressionCode, ) -> NonNull { heap_move_nonnull(BitmapCommand { - bitmap: unsafe { heap_remove(bitmap) }, + bitmap, origin: Origin::new(origin_x, origin_y), compression, }) @@ -38,9 +38,7 @@ wrap_functions!(associate BitmapCommand; /// leaving other fields as their default values. /// /// Rust equivalent: `BitmapCommand::from(bitmap)` - fn from_bitmap( - bitmap: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(bitmap) }.into()) + fn from_bitmap(bitmap: move NonNull) -> NonNull { + heap_move_nonnull(bitmap.into()) } ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index e796380..4dd9ca9 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, @@ -32,13 +32,13 @@ wrap_functions!(associate BitVecCommand; /// /// The contained [`DisplayBitVec`] is always uncompressed. fn new( - bitvec: NonNull, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode, + bitvec: move NonNull, + offset: val usize, + operation: val BinaryOperation, + compression: val CompressionCode, ) -> NonNull { heap_move_nonnull(BitVecCommand { - bitvec: unsafe { heap_remove(bitvec) }, + bitvec, offset, operation, compression, diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 853a3fe..f05b820 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; @@ -22,22 +22,20 @@ wrap_functions!(associate BrightnessGridCommand; /// /// Returns: a new [BrightnessGridCommand] instance. fn new( - grid: NonNull, - origin_x: usize, - origin_y: usize, + grid: move NonNull, + origin_x: val usize, + origin_y: val usize ) -> NonNull { heap_move_nonnull(BrightnessGridCommand { - grid: unsafe { heap_remove(grid) }, + grid, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], /// leaving other fields as their default values. - fn from_grid( - grid: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + fn from_grid(grid: move NonNull) -> NonNull { + heap_move_nonnull(grid.into()) } ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 41e5000..12b58ca 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; @@ -22,22 +22,20 @@ wrap_functions!(associate CharGridCommand; /// /// Returns: a new [CharGridCommand] instance. fn new( - grid: NonNull, - origin_x: usize, - origin_y: usize, + grid: move NonNull, + origin_x: val usize, + origin_y: val usize, ) -> NonNull { heap_move_nonnull(CharGridCommand { - grid: unsafe { heap_remove(grid) }, + grid, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [CharGrid] into a new [CharGridCommand], /// leaving other fields as their default values. - fn from_grid( - grid: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + fn from_grid(grid: move NonNull) -> NonNull { + heap_move_nonnull(grid.into()) } ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index b43afb4..921e2a4 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,7 +1,7 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::{heap_move_nonnull, heap_remove}, + mem::heap_move_nonnull, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; @@ -22,22 +22,20 @@ wrap_functions!(associate Cp437GridCommand; /// /// The origin is relative to the top-left of the display. fn new( - grid: NonNull, - origin_x: usize, - origin_y: usize, + grid: move NonNull, + origin_x: val usize, + origin_y: val usize, ) -> NonNull { heap_move_nonnull(Cp437GridCommand { - grid: unsafe { heap_remove(grid) }, + grid, origin: Origin::new(origin_x, origin_y), }) } /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. - fn from_grid( - grid: NonNull, - ) -> NonNull { - heap_move_nonnull(unsafe { heap_remove(grid) }.into()) + fn from_grid(grid: move NonNull) -> NonNull { + heap_move_nonnull(grid.into()) } ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index dad7584..efd402c 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -178,10 +178,7 @@ wrap_functions!(associate GenericCommand; /// The packet is dropped in the process. /// /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. - fn try_from_packet( - packet: NonNull, - ) -> NonNull { - let packet = unsafe { heap_remove(packet) }; + fn try_from_packet(packet: move NonNull) -> NonNull { let result = servicepoint::TypedCommand::try_from(packet) .map(|value| match value { TypedCommand::Clear(clear) => GenericCommand { diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 76cbeb5..d2fef10 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -11,7 +11,7 @@ wrap_functions!(associate GlobalBrightnessCommand; /// Set the brightness of all tiles to the same value. /// /// Returns: a new [GlobalBrightnessCommand] instance. - fn new(brightness: Brightness) -> NonNull { + fn new(brightness: val Brightness) -> NonNull { heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 026d0f1..da16c18 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -17,31 +17,22 @@ pub use generic_command::*; macro_rules! wrap_origin_accessors { ( $object_type:ident ) => { ::paste::paste! { - $crate::macros::wrap_functions!(associate $object_type; + $crate::macros::wrap_methods!($object_type; #[doc = " Reads the origin field of the [`" $object_type "`]."] fn get_origin( - command: ::core::ptr::NonNull<$object_type>, - origin_x: ::core::ptr::NonNull, - origin_y: ::core::ptr::NonNull, + ref command, + origin_x: mut ::core::ptr::NonNull, + origin_y: mut ::core::ptr::NonNull ) { - unsafe { - let origin = &command.as_ref().origin; - *origin_x.as_ptr() = origin.x; - *origin_y.as_ptr() = origin.y; - } - } + let origin = command.origin; + *origin_x = origin.x; + *origin_y = origin.y; + }; #[doc = " Overwrites the origin field of the [`" $object_type "`]."] - fn set_origin( - command: ::core::ptr::NonNull<$object_type>, - origin_x: usize, - origin_y: usize, - ) { - unsafe { - $crate::macros::nonnull_as_mut!(command).origin = - ::servicepoint::Origin::new(origin_x, origin_y); - } - } + fn set_origin(mut command, origin_x: val usize, origin_y: val usize) { + command.origin = ::servicepoint::Origin::new(origin_x, origin_y); + }; ); } }; diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 4fb55a2..1386a8d 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,7 +1,7 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some, heap_remove}, + macros::{wrap_functions, wrap_methods}, + mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, @@ -35,7 +35,7 @@ wrap_functions!(associate Bitmap; /// sp_bitmap_set(grid, 0, 0, false); /// sp_bitmap_free(grid); /// ``` - fn new(width: usize, height: usize) -> *mut Bitmap { + fn new(width: val usize, height: val usize) -> *mut Bitmap { heap_move_some(Bitmap::new(width, height)) } @@ -55,9 +55,9 @@ wrap_functions!(associate Bitmap; /// /// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. fn load( - width: usize, - height: usize, - data: ByteSlice, + width: val usize, + height: val usize, + data: val ByteSlice, ) -> *mut Bitmap { let data = unsafe { data.as_slice() }; heap_move_ok(Bitmap::load(width, height, data)) @@ -69,10 +69,9 @@ wrap_functions!(associate Bitmap; /// /// Returns NULL in case of error. fn from_bitvec( - width: usize, - bitvec: NonNull, + width: val usize, + bitvec: move NonNull, ) -> *mut Bitmap { - let bitvec = unsafe { heap_remove(bitvec) }; heap_move_ok(Bitmap::from_bitvec(width, bitvec)) } ); @@ -88,7 +87,7 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move bitmap, x: usize, y: usize, compression: CompressionCode) -> *mut Packet { + fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> *mut Packet { heap_move_ok(Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 6ae2cf6..cdb7c36 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_container, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{ @@ -23,14 +23,14 @@ wrap_functions!(associate DisplayBitVec; /// # Panics /// /// - when `size` is not divisible by 8. - fn new(size: usize) -> NonNull { + fn new(size: val usize) -> NonNull { heap_move_nonnull(DisplayBitVec::repeat(false, size)) } /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// /// returns: [DisplayBitVec] instance containing data. - fn load(data: ByteSlice) -> NonNull { + fn load(data: val ByteSlice) -> NonNull { let data = unsafe { data.as_slice() }; heap_move_nonnull(DisplayBitVec::from_slice(data)) } @@ -45,9 +45,9 @@ wrap_methods!(DisplayBitVec; /// Returns NULL in case of an error. fn try_into_packet( move bitvec, - offset: usize, - operation: BinaryOperation, - compression: CompressionCode + offset: val usize, + operation: val BinaryOperation, + compression: val CompressionCode ) -> *mut Packet { heap_move_ok(Packet::try_from(BitVecCommand { bitvec, @@ -69,7 +69,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn get(ref instance, index: usize) -> bool { + fn get(ref instance, index: val usize) -> bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -83,14 +83,14 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn set(mut instance, index: usize, value: bool); + fn set(mut instance, index: val usize, value: val bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - fn fill(mut instance, value: bool); + fn fill(mut instance, value: val bool); /// Gets the length in bits. fn len(ref instance) -> usize; diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a71a953..b0da48a 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ @@ -30,10 +30,7 @@ wrap_functions!(associate BrightnessGrid; /// TypedCommand *command = sp_command_char_brightness(grid); /// sp_udp_free(connection); /// ``` - fn new( - width: usize, - height: usize, - ) -> NonNull { + fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(BrightnessGrid::new(width, height)) } @@ -43,9 +40,9 @@ wrap_functions!(associate BrightnessGrid; /// /// returns: new [BrightnessGrid] instance, or NULL in case of an error. fn load( - width: usize, - height: usize, - data: ByteSlice, + width: val usize, + height: val usize, + data: val ByteSlice, ) -> *mut BrightnessGrid { let data = unsafe { data.as_slice() }; heap_move_some( @@ -62,7 +59,7 @@ wrap_methods!(BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { heap_move_ok(Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a360a26..715588f 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; @@ -23,14 +23,14 @@ wrap_functions!(associate CharGrid; /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` - fn new(width: usize, height: usize) -> NonNull { + fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) } /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load(width: usize, height: usize, data: ByteSlice) -> *mut CharGrid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) } @@ -49,7 +49,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: usize, y: usize) -> u32 { + fn get(ref instance, x: val usize, y: val usize) -> u32 { instance.get(x, y) as u32 }; @@ -66,7 +66,7 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - fn set(mut instance, x: usize, y: usize, value: u32) { + fn set(mut instance, x: val usize, y: val usize, value: val u32) { instance.set(x, y, char::from_u32(value).unwrap()) }; @@ -76,7 +76,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - fn fill(mut instance, value: u32) { + fn fill(mut instance, value: val u32) { instance.fill(char::from_u32(value).unwrap()) }; @@ -85,7 +85,7 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { heap_move_ok(Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index d4ed8a1..ecc36c6 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{wrap_functions, wrap_methods}, mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ @@ -14,12 +14,12 @@ wrap_functions!(associate Cp437Grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. - fn new(width: usize, height: usize) -> NonNull { + fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) } /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: usize, height: usize, data: ByteSlice) -> *mut Cp437Grid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; heap_move_some(Cp437Grid::load(width, height, data)) } @@ -31,7 +31,7 @@ wrap_methods!(Cp437Grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: usize, y: usize) -> *mut Packet { + fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { heap_move_ok(Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 0a9eb38..35bbe3b 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -14,8 +14,8 @@ pub use cp437_grid::*; macro_rules! wrap_container { ($object_type:ident) => { - derive_clone!($object_type); - derive_free!($object_type); + $crate::macros::derive_clone!($object_type); + $crate::macros::derive_free!($object_type); }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: usize, y: usize) -> $value_type; + fn get(ref instance, x: val usize, y: val usize) -> $value_type; /// Sets the value of the specified position. /// @@ -57,14 +57,14 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn set(mut instance, x: usize, y: usize, value: $value_type); + fn set(mut instance, x: val usize, y: val usize, value: val $value_type); /// Sets the state of all cells in the grid. /// /// # Arguments /// /// - `value`: the value to set all cells to - fn fill(mut instance, value: $value_type); + fn fill(mut instance, value: val $value_type); } }; } diff --git a/src/macros.rs b/src/macros.rs index d2b974c..8cae819 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,8 +3,9 @@ macro_rules! derive_free { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Deallocates a [`" $typ "`] instance."] - fn free(instance: ::core::ptr::NonNull<$typ>) { - unsafe { $crate::mem::heap_drop(instance) } + #[allow(dropping_copy_types)] + fn free(instance: move ::core::ptr::NonNull<$typ>) { + ::std::mem::drop(instance) } ); } @@ -16,8 +17,8 @@ macro_rules! derive_clone { ::paste::paste! { $crate::macros::wrap_functions!([< $typ:lower >]; #[doc = "Clones a [`" $typ "`] instance."] - fn clone(instance: ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { - unsafe { $crate::mem::heap_clone(instance) } + fn clone(instance: ref ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { + $crate::mem::heap_move_nonnull(instance.clone()) } ); } @@ -36,24 +37,18 @@ macro_rules! nonnull_as_mut { }; } -macro_rules! nonnull_as_move { - ($ident:ident) => { - $crate::mem::heap_remove($ident) - }; -} - macro_rules! wrap_method { ( $object_type:ident; $(#[$meta:meta])+ - fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? ) => { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; $(#[$meta])+ - fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? { $instance.$function($($($param_name),*)?) } @@ -62,7 +57,7 @@ macro_rules! wrap_method { }; ($object_type:ident; $(#[$meta:meta])+ - fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? $impl:block ) => { @@ -72,12 +67,10 @@ macro_rules! wrap_method { /// $(#[$meta])* fn $function( - $instance: ::core::ptr::NonNull<$object_type> - $(,$($param_name: $param_type),*)? - ) $(-> $return_type)? { - let $instance = unsafe { $crate::macros:: [< nonnull_as_ $ref_or_mut >] !($instance) }; - $impl - } + $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> + $(,$($param_name: $param_modifier $param_type),*)? + ) $(-> $return_type)? + $impl ); } }; @@ -88,7 +81,7 @@ macro_rules! wrap_methods { $object_type:ident; $( $(#[$meta:meta])+ - fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_type:ty),*)?) + fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? $($impl:block)?; )+ @@ -97,7 +90,7 @@ macro_rules! wrap_methods { $( $crate::macros::wrap_method!($object_type; $(#[$meta])* - fn $function($ref_or_mut $instance $(, $($param_name: $param_type),*)?) + fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? $($impl)? ); @@ -109,45 +102,35 @@ macro_rules! wrap_methods { macro_rules! wrap_fields_accessor { (get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - fn []( - instance: ::core::ptr::NonNull<$object_type> - ) -> $prop_type { - let $prop_name = unsafe { $crate::macros::nonnull_as_ref!(instance).$prop_name }; - return $prop_name; + fn [](ref instance) -> $prop_type { + return instance.$prop_name; } } } }; (mut get; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Gets a reference to the field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] /// /// - The returned reference inherits the lifetime of object in which it is contained. /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - fn []( - instance: ::core::ptr::NonNull<$object_type> - ) -> ::core::ptr::NonNull<$prop_type> { - let $prop_name = unsafe { &mut $crate::macros::nonnull_as_mut!(instance).$prop_name }; - return ::core::ptr::NonNull::from($prop_name); + fn [](mut instance) -> ::core::ptr::NonNull<$prop_type> { + return ::core::ptr::NonNull::from(&mut instance.$prop_name); } } } }; (set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Sets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - fn []( - instance: ::core::ptr::NonNull<$object_type>, - value: $prop_type, - ) { - let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; + fn [](mut instance, value: val $prop_type) { instance.$prop_name = value; } } @@ -155,17 +138,12 @@ macro_rules! wrap_fields_accessor { }; (move set; $object_type:ident :: $prop_name:ident : $prop_type:ty) => { paste::paste! { - $crate::macros::wrap_functions! {associate $object_type; + $crate::macros::wrap_method! {$object_type; #[doc = " Sets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] /// The provided value is moved into the instance, potentially invalidating previously taken references. - fn []( - instance: ::core::ptr::NonNull<$object_type>, - value: ::core::ptr::NonNull<$prop_type>, - ) { - let instance = unsafe { $crate::macros::nonnull_as_mut!(instance) }; - let $prop_name = unsafe { $crate::mem::heap_remove(value) }; - instance.$prop_name = $prop_name; + fn [](mut instance, value: move ::core::ptr::NonNull<$prop_type>) { + instance.$prop_name = value; } } } @@ -190,18 +168,30 @@ macro_rules! wrap_fields { }; } -macro_rules! wrap_functions { +macro_rules! apply_param_modifier { + (move, $param_name:ident) => { + unsafe { $crate::mem::heap_remove($param_name) } + }; + (val, $param_name:ident) => { + $param_name + }; + (mut, $param_name:ident) => { + unsafe { $crate::macros::nonnull_as_mut!($param_name) } + }; + (ref, $param_name:ident) => { + unsafe { $crate::macros::nonnull_as_ref!($param_name) } + }; +} + +macro_rules! wrap_function { ( $module:ident; - $( - $(#[$meta:meta])+ - fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) - $(-> $return_type:ty)? - $block:block - )+ + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block ) => { ::paste::paste! { - $( $(#[$meta])* #[doc = ""] #[doc = " This function is part of the `" $module "` module."] @@ -209,7 +199,33 @@ macro_rules! wrap_functions { pub unsafe extern "C" fn [< sp_ $module _ $function >]( $($param_name: $param_type),* ) $(-> $return_type)? - $block + { + $( + let $param_name = $crate::macros::apply_param_modifier!($param_modifier, $param_name); + )* + $block + } + } + } +} + +macro_rules! wrap_functions { + ( + $module:ident; + $( + $(#[$meta:meta])+ + fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) + $(-> $return_type:ty)? + $block:block + )+ + ) => { + ::paste::paste! { + $( + $crate::macros::wrap_function!($module; + $(#[$meta])+ + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? + $block + ); )+ } }; @@ -217,7 +233,7 @@ macro_rules! wrap_functions { associate $object_type:ident; $( $(#[$meta:meta])+ - fn $function:ident($($param_name:ident: $param_type:ty),*$(,)?) + fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? $block:block )+ @@ -226,7 +242,7 @@ macro_rules! wrap_functions { $crate::macros::wrap_functions!{[< $object_type:lower >]; $( $(#[$meta])+ - fn $function($($param_name: $param_type),*) $(-> $return_type)? + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? $block )+ } @@ -235,7 +251,7 @@ macro_rules! wrap_functions { } pub(crate) use { - derive_clone, derive_free, nonnull_as_move, nonnull_as_mut, nonnull_as_ref, - wrap_fields, wrap_fields_accessor, wrap_functions, wrap_method, - wrap_methods, + apply_param_modifier, derive_clone, derive_free, nonnull_as_mut, + nonnull_as_ref, wrap_fields, wrap_fields_accessor, wrap_function, + wrap_functions, wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index 28ade85..5dc172d 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,17 +1,18 @@ use crate::{ containers::ByteSlice, - macros::{derive_clone, derive_free, wrap_fields, wrap_functions}, + macros::{ + derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods, + }, mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -wrap_functions!(packet; - +wrap_functions!(associate Packet; /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: ByteSlice) -> *mut Packet { + fn try_load(data: val ByteSlice) -> *mut Packet { let data = unsafe { data.as_slice() }; heap_move_ok(servicepoint::Packet::try_from(data)) } @@ -19,7 +20,7 @@ wrap_functions!(packet; /// Creates a raw [Packet] from parts. /// /// returns: new instance. Will never return null. - fn from_parts(header: Header, payload: ByteSlice) -> NonNull { + fn from_parts(header: val Header, payload: val ByteSlice) -> NonNull { let payload = if payload == ByteSlice::INVALID { None } else { @@ -28,44 +29,6 @@ wrap_functions!(packet; heap_move_nonnull(Packet { header, payload }) } - - /// Returns a pointer to the current payload of the provided packet. - /// - /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. - /// - /// The returned memory can be changed and will be valid until a new payload is set. - fn get_payload(packet: NonNull) -> ByteSlice { - unsafe { - match &mut (*packet.as_ptr()).payload { - None => ByteSlice::INVALID, - Some(payload) => ByteSlice::from_slice(payload), - } - } - } - - /// Sets the payload of the provided packet to the provided data. - /// - /// This makes previous payload pointers invalid. - fn set_payload(packet: NonNull, data: ByteSlice) { - unsafe { - (*packet.as_ptr()).payload = if data == ByteSlice::INVALID { - None - } else { - Some(data.as_slice().to_vec()) - } - } - } - - /// Serialize the packet into the provided buffer. - /// - /// # Panics - /// - /// - if the buffer is not big enough to hold header+payload. - fn serialize_to(packet: NonNull, buffer: ByteSlice) -> usize { - unsafe { - packet.as_ref().serialize_to(buffer.as_slice_mut()).unwrap_or(0) - } - } ); derive_clone!(Packet); @@ -75,20 +38,51 @@ wrap_fields!(Packet; prop header: Header { get; get mut; set; }; ); +wrap_methods! { Packet; + /// Returns a pointer to the current payload of the provided packet. + /// + /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. + /// + /// The returned memory can be changed and will be valid until a new payload is set. + fn get_payload(mut packet) -> ByteSlice { + match &mut packet.payload { + None => ByteSlice::INVALID, + Some(payload) => unsafe { ByteSlice::from_slice(payload) }, + } + }; + + /// Sets the payload of the provided packet to the provided data. + /// + /// This makes previous payload pointers invalid. + fn set_payload(mut packet, data: val ByteSlice) { + packet.payload = if data == ByteSlice::INVALID { + None + } else { + Some(unsafe { data.as_slice().to_vec() }) + } + }; + + /// Serialize the packet into the provided buffer. + /// + /// # Panics + /// + /// - if the buffer is not big enough to hold header+payload. + fn serialize_to(mut packet, buffer: val ByteSlice) -> usize { + unsafe { + packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) + } + }; +} + wrap_functions!(sp; /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed. - fn u16_to_command_code( - code: u16, - result: *mut CommandCode, - ) -> bool { + fn u16_to_command_code(code: val u16, result: mut NonNull) -> bool { match CommandCode::try_from(code) { Ok(code) => { - unsafe { - *result = code; - } + *result = code; true } Err(_) => false, diff --git a/src/udp.rs b/src/udp.rs index aa307b0..13718f4 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -25,7 +25,7 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open(host: NonNull) -> *mut UdpSocket { + fn open(host: val NonNull) -> *mut UdpSocket { let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); @@ -44,7 +44,7 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open_ipv4(ip1: u8, ip2: u8, ip3: u8, ip4: u8, port: u16) -> *mut UdpSocket { + fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); heap_move_ok(UdpSocket::bind_connect(addr)) } @@ -57,8 +57,7 @@ wrap_methods! {UdpSocket; /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(ref connection, packet: NonNull) -> bool { - let packet = unsafe { heap_remove(packet) }; + fn send_packet(ref connection, packet: move NonNull) -> bool { connection.send(&Vec::from(packet)).is_ok() }; @@ -73,9 +72,8 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(ref connection, command: NonNull) -> bool { + fn send_command(ref connection, command: mut NonNull) -> bool { unsafe { - let command = crate::macros::nonnull_as_mut!(command); let result = match command.tag { CommandTag::Invalid => return false, CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), @@ -103,7 +101,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(ref udp_connection, header: Header) -> bool { + fn send_header(ref udp_connection, header: val Header) -> bool { let packet = Packet { header, payload: None, From 0968605d0b3605a9dedd11dae6e0a166e3a147f6 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 17:29:03 +0200 Subject: [PATCH 64/76] cleanups, add semicolon everywhere --- include/servicepoint.h | 136 +++++++++++++++++++++- src/commands/bitmap_command.rs | 4 +- src/commands/bitvec_command.rs | 4 +- src/commands/brightness_grid_command.rs | 6 +- src/commands/cc_only_commands.rs | 2 +- src/commands/char_grid_command.rs | 6 +- src/commands/cp437_grid_command.rs | 6 +- src/commands/generic_command.rs | 2 +- src/commands/global_brightness_command.rs | 4 +- src/commands/mod.rs | 2 +- src/containers/bitmap.rs | 8 +- src/containers/bitvec.rs | 6 +- src/containers/brightness_grid.rs | 4 +- src/containers/char_grid.rs | 7 +- src/containers/cp437_grid.rs | 4 +- src/lib.rs | 2 +- src/macros.rs | 64 +++++----- src/packet.rs | 7 +- src/udp.rs | 5 +- 19 files changed, 192 insertions(+), 87 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 2648383..703ab37 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -636,6 +636,8 @@ extern "C" { #endif // __cplusplus /** + * Calls method [`servicepoint::Bitmap::clone`]. + * *Clones a [`Bitmap`] instance. * * This function is part of the `bitmap` module. @@ -667,6 +669,8 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** + * Calls method [`servicepoint::Bitmap::free`]. + * *Deallocates a [`Bitmap`] instance. * * This function is part of the `bitmap` module. @@ -821,6 +825,8 @@ struct Packet *sp_bitmap_try_into_packet(struct Bitmap */*notnull*/ bitmap, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::clone`]. + * *Clones a [`BitmapCommand`] instance. * * This function is part of the `bitmapcommand` module. @@ -828,6 +834,8 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_bitmapcommand_clone(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::free`]. + * *Deallocates a [`BitmapCommand`] instance. * * This function is part of the `bitmapcommand` module. @@ -845,6 +853,8 @@ void sp_bitmapcommand_free(struct BitmapCommand */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** + * Calls method [`servicepoint::BitmapCommand::get_bitmap_mut`]. + * * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -855,6 +865,8 @@ struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */* struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::get_compression`]. + * * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -862,6 +874,8 @@ struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand CompressionCode sp_bitmapcommand_get_compression(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitmapCommand::get_origin`]. + * * Reads the origin field of the [`BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -885,6 +899,8 @@ struct BitmapCommand */*notnull*/ sp_bitmapcommand_new(struct Bitmap */*notnull* CompressionCode compression); /** + * Calls method [`servicepoint::BitmapCommand::set_bitmap`]. + * * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -894,6 +910,8 @@ void sp_bitmapcommand_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); /** + * Calls method [`servicepoint::BitmapCommand::set_compression`]. + * * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -902,6 +920,8 @@ void sp_bitmapcommand_set_compression(struct BitmapCommand */*notnull*/ instance CompressionCode value); /** + * Calls method [`servicepoint::BitmapCommand::set_origin`]. + * * Overwrites the origin field of the [`BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -922,6 +942,8 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::clone`]. + * *Clones a [`BitVecCommand`] instance. * * This function is part of the `bitveccommand` module. @@ -929,6 +951,8 @@ struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull* struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::free`]. + * *Deallocates a [`BitVecCommand`] instance. * * This function is part of the `bitveccommand` module. @@ -936,6 +960,8 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_bitvec_mut`]. + * * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -946,6 +972,8 @@ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_compression`]. + * * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -953,6 +981,8 @@ DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_offset`]. + * * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -960,6 +990,8 @@ CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull Offset sp_bitveccommand_get_offset(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BitVecCommand::get_operation`]. + * * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -988,6 +1020,8 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_new(DisplayBitVec */*notnull* CompressionCode compression); /** + * Calls method [`servicepoint::BitVecCommand::set_bitvec`]. + * * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -997,6 +1031,8 @@ void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, DisplayBitVec */*notnull*/ value); /** + * Calls method [`servicepoint::BitVecCommand::set_compression`]. + * * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1005,6 +1041,8 @@ void sp_bitveccommand_set_compression(struct BitVecCommand */*notnull*/ instance CompressionCode value); /** + * Calls method [`servicepoint::BitVecCommand::set_offset`]. + * * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1013,6 +1051,8 @@ void sp_bitveccommand_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); /** + * Calls method [`servicepoint::BitVecCommand::set_operation`]. + * * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1032,6 +1072,8 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGrid::clone`]. + * *Clones a [`BrightnessGrid`] instance. * * This function is part of the `brightnessgrid` module. @@ -1064,6 +1106,8 @@ void sp_brightnessgrid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); /** + * Calls method [`servicepoint::BrightnessGrid::free`]. + * *Deallocates a [`BrightnessGrid`] instance. * * This function is part of the `brightnessgrid` module. @@ -1180,6 +1224,8 @@ struct Packet *sp_brightnessgrid_try_into_packet(BrightnessGrid */*notnull*/ gri size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGridCommand::clone`]. + * *Clones a [`BrightnessGridCommand`] instance. * * This function is part of the `brightnessgridcommand` module. @@ -1187,6 +1233,8 @@ size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_clone(struct BrightnessGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGridCommand::free`]. + * *Deallocates a [`BrightnessGridCommand`] instance. * * This function is part of the `brightnessgridcommand` module. @@ -1202,6 +1250,8 @@ void sp_brightnessgridcommand_free(struct BrightnessGridCommand */*notnull*/ ins struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(BrightnessGrid */*notnull*/ grid); /** + * Calls method [`servicepoint::BrightnessGridCommand::get_grid_mut`]. + * * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1212,6 +1262,8 @@ struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(Bri BrightnessGrid */*notnull*/ sp_brightnessgridcommand_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::BrightnessGridCommand::get_origin`]. + * * Reads the origin field of the [`BrightnessGridCommand`]. * * This function is part of the `brightnessgridcommand` module. @@ -1234,6 +1286,8 @@ struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_new(Brightnes size_t origin_y); /** + * Calls method [`servicepoint::BrightnessGridCommand::set_grid`]. + * * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1243,6 +1297,8 @@ void sp_brightnessgridcommand_set_grid(struct BrightnessGridCommand */*notnull*/ BrightnessGrid */*notnull*/ value); /** + * Calls method [`servicepoint::BrightnessGridCommand::set_origin`]. + * * Overwrites the origin field of the [`BrightnessGridCommand`]. * * This function is part of the `brightnessgridcommand` module. @@ -1263,6 +1319,8 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGrid::clone`]. + * *Clones a [`CharGrid`] instance. * * This function is part of the `chargrid` module. @@ -1284,6 +1342,8 @@ CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** + * Calls method [`servicepoint::CharGrid::free`]. + * *Deallocates a [`CharGrid`] instance. * * This function is part of the `chargrid` module. @@ -1392,6 +1452,8 @@ struct Packet *sp_chargrid_try_into_packet(CharGrid */*notnull*/ grid, size_t sp_chargrid_width(CharGrid */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGridCommand::clone`]. + * *Clones a [`CharGridCommand`] instance. * * This function is part of the `chargridcommand` module. @@ -1399,6 +1461,8 @@ size_t sp_chargrid_width(CharGrid */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGridCommand::free`]. + * *Deallocates a [`CharGridCommand`] instance. * * This function is part of the `chargridcommand` module. @@ -1414,6 +1478,8 @@ void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*notnull*/ grid); /** + * Calls method [`servicepoint::CharGridCommand::get_grid_mut`]. + * * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1424,6 +1490,8 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*not CharGrid */*notnull*/ sp_chargridcommand_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::CharGridCommand::get_origin`]. + * * Reads the origin field of the [`CharGridCommand`]. * * This function is part of the `chargridcommand` module. @@ -1446,6 +1514,8 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_new(CharGrid */*notnull*/ size_t origin_y); /** + * Calls method [`servicepoint::CharGridCommand::set_grid`]. + * * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1455,6 +1525,8 @@ void sp_chargridcommand_set_grid(struct CharGridCommand */*notnull*/ instance, CharGrid */*notnull*/ value); /** + * Calls method [`servicepoint::CharGridCommand::set_origin`]. + * * Overwrites the origin field of the [`CharGridCommand`]. * * This function is part of the `chargridcommand` module. @@ -1475,6 +1547,8 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::ClearCommand::clone`]. + * *Clones a [`ClearCommand`] instance. * * This function is part of the `clearcommand` module. @@ -1482,6 +1556,8 @@ struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notn struct ClearCommand */*notnull*/ sp_clearcommand_clone(struct ClearCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::ClearCommand::free`]. + * *Deallocates a [`ClearCommand`] instance. * * This function is part of the `clearcommand` module. @@ -1511,6 +1587,8 @@ struct ClearCommand */*notnull*/ sp_clearcommand_new(void); struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437Grid::clone`]. + * *Clones a [`Cp437Grid`] instance. * * This function is part of the `cp437grid` module. @@ -1542,6 +1620,8 @@ struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); void sp_cp437grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** + * Calls method [`servicepoint::Cp437Grid::free`]. + * *Deallocates a [`Cp437Grid`] instance. * * This function is part of the `cp437grid` module. @@ -1638,6 +1718,8 @@ struct Packet *sp_cp437grid_try_into_packet(Cp437Grid */*notnull*/ grid, size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437GridCommand::clone`]. + * *Clones a [`Cp437GridCommand`] instance. * * This function is part of the `cp437gridcommand` module. @@ -1645,6 +1727,8 @@ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437GridCommand::free`]. + * *Deallocates a [`Cp437GridCommand`] instance. * * This function is part of the `cp437gridcommand` module. @@ -1660,6 +1744,8 @@ void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */*notnull*/ grid); /** + * Calls method [`servicepoint::Cp437GridCommand::get_grid_mut`]. + * * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1670,6 +1756,8 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */* Cp437Grid */*notnull*/ sp_cp437gridcommand_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Cp437GridCommand::get_origin`]. + * * Reads the origin field of the [`Cp437GridCommand`]. * * This function is part of the `cp437gridcommand` module. @@ -1692,6 +1780,8 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_new(Cp437Grid */*notnul size_t origin_y); /** + * Calls method [`servicepoint::Cp437GridCommand::set_grid`]. + * * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1701,6 +1791,8 @@ void sp_cp437gridcommand_set_grid(struct Cp437GridCommand */*notnull*/ instance, Cp437Grid */*notnull*/ value); /** + * Calls method [`servicepoint::Cp437GridCommand::set_origin`]. + * * Overwrites the origin field of the [`Cp437GridCommand`]. * * This function is part of the `cp437gridcommand` module. @@ -1732,6 +1824,8 @@ struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*no struct ByteSlice sp_displaybitvec_as_raw_mut_slice(DisplayBitVec */*notnull*/ instance); /** + * Calls method [`servicepoint::DisplayBitVec::clone`]. + * *Clones a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. @@ -1752,6 +1846,8 @@ DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ ins void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); /** + * Calls method [`servicepoint::DisplayBitVec::free`]. + * *Deallocates a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. @@ -1867,6 +1963,8 @@ struct Packet *sp_displaybitvec_try_into_packet(DisplayBitVec */*notnull*/ bitve void sp_envlogger_init(void); /** + * Calls method [`servicepoint::FadeOutCommand::clone`]. + * *Clones a [`FadeOutCommand`] instance. * * This function is part of the `fadeoutcommand` module. @@ -1874,6 +1972,8 @@ void sp_envlogger_init(void); struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_clone(struct FadeOutCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::FadeOutCommand::free`]. + * *Deallocates a [`FadeOutCommand`] instance. * * This function is part of the `fadeoutcommand` module. @@ -1901,6 +2001,8 @@ struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GenericCommand::clone`]. + * *Clones a [`GenericCommand`] instance. * * This function is part of the `genericcommand` module. @@ -1908,6 +2010,8 @@ struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnul struct GenericCommand */*notnull*/ sp_genericcommand_clone(struct GenericCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GenericCommand::free`]. + * *Deallocates a [`GenericCommand`] instance. * * This function is part of the `genericcommand` module. @@ -1938,6 +2042,8 @@ struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Pack struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnull*/ command); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::clone`]. + * *Clones a [`GlobalBrightnessCommand`] instance. * * This function is part of the `globalbrightnesscommand` module. @@ -1945,6 +2051,8 @@ struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnul struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::free`]. + * *Deallocates a [`GlobalBrightnessCommand`] instance. * * This function is part of the `globalbrightnesscommand` module. @@ -1952,6 +2060,8 @@ struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(str void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::get_brightness`]. + * * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * * This function is part of the `globalbrightnesscommand` module. @@ -1968,6 +2078,8 @@ Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessComm struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brightness brightness); /** + * Calls method [`servicepoint::GlobalBrightnessCommand::set_brightness`]. + * * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * * This function is part of the `globalbrightnesscommand` module. @@ -1987,6 +2099,8 @@ void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */ struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::HardResetCommand::clone`]. + * *Clones a [`HardResetCommand`] instance. * * This function is part of the `hardresetcommand` module. @@ -1994,6 +2108,8 @@ struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnes struct HardResetCommand */*notnull*/ sp_hardresetcommand_clone(struct HardResetCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::HardResetCommand::free`]. + * *Deallocates a [`HardResetCommand`] instance. * * This function is part of the `hardresetcommand` module. @@ -2023,6 +2139,8 @@ struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::clone`]. + * *Clones a [`Packet`] instance. * * This function is part of the `packet` module. @@ -2030,6 +2148,8 @@ struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*no struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::free`]. + * *Deallocates a [`Packet`] instance. * * This function is part of the `packet` module. @@ -2047,6 +2167,8 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); /** + * Calls method [`servicepoint::Packet::get_header`]. + * * Gets the value of field `header` of the [`servicepoint::Packet`]. * * This function is part of the `packet` module. @@ -2054,6 +2176,8 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::get_header_mut`]. + * * Gets a reference to the field `header` of the [`servicepoint::Packet`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -2064,6 +2188,8 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); /** + * Calls method [`servicepoint::Packet::get_payload`]. + * * Returns a pointer to the current payload of the provided packet. * * Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. @@ -2075,6 +2201,8 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); /** + * Calls method [`servicepoint::Packet::serialize_to`]. + * * Serialize the packet into the provided buffer. * * # Panics @@ -2087,6 +2215,8 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); /** + * Calls method [`servicepoint::Packet::set_header`]. + * * Sets the value of field `header` of the [`servicepoint::Packet`]. * * This function is part of the `packet` module. @@ -2095,6 +2225,8 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); /** + * Calls method [`servicepoint::Packet::set_payload`]. + * * Sets the payload of the provided packet to the provided data. * * This makes previous payload pointers invalid. @@ -2121,9 +2253,11 @@ struct Packet *sp_packet_try_load(struct ByteSlice data); * This function is part of the `sp` module. */ bool sp_sp_u16_to_command_code(uint16_t code, - CommandCode *result); + CommandCode */*notnull*/ result); /** + * Calls method [`servicepoint::UdpSocket::free`]. + * *Deallocates a [`UdpSocket`] instance. * * This function is part of the `udpsocket` module. diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 5aaab26..c7290ce 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -32,7 +32,7 @@ wrap_functions!(associate BitmapCommand; origin: Origin::new(origin_x, origin_y), compression, }) - } + }; /// Move the provided [Bitmap] into a new [BitmapCommand], /// leaving other fields as their default values. @@ -40,5 +40,5 @@ wrap_functions!(associate BitmapCommand; /// Rust equivalent: `BitmapCommand::from(bitmap)` fn from_bitmap(bitmap: move NonNull) -> NonNull { heap_move_nonnull(bitmap.into()) - } + }; ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 4dd9ca9..6b26227 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -18,7 +18,6 @@ wrap_fields!(BitVecCommand; ); wrap_functions!(associate BitVecCommand; - /// Set pixel data starting at the pixel offset on screen. /// /// The screen will continuously overwrite more pixel data without regarding the offset, meaning @@ -43,6 +42,5 @@ wrap_functions!(associate BitVecCommand; operation, compression, }) - } - + }; ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index f05b820..e53a1a0 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -15,7 +15,6 @@ wrap_fields!(BrightnessGridCommand; wrap_origin_accessors!(BrightnessGridCommand); wrap_functions!(associate BrightnessGridCommand; - /// Set the brightness of individual tiles in a rectangular area of the display. /// /// The passed [BrightnessGrid] gets consumed. @@ -30,12 +29,11 @@ wrap_functions!(associate BrightnessGridCommand; grid, origin: Origin::new(origin_x, origin_y), }) - } + }; /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], /// leaving other fields as their default values. fn from_grid(grid: move NonNull) -> NonNull { heap_move_nonnull(grid.into()) - } - + }; ); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 6635ae9..6f06bd8 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -14,7 +14,7 @@ macro_rules! wrap_cc_only { #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] fn new() -> ::core::ptr::NonNull<[< $command Command >]> { heap_move_nonnull([< $command Command >]) - } + }; ); } }; diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 12b58ca..fb9f7d0 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -15,7 +15,6 @@ wrap_fields!(CharGridCommand; wrap_origin_accessors!(CharGridCommand); wrap_functions!(associate CharGridCommand; - /// Show UTF-8 encoded text on the screen. /// /// The passed [CharGrid] gets consumed. @@ -30,12 +29,11 @@ wrap_functions!(associate CharGridCommand; grid, origin: Origin::new(origin_x, origin_y), }) - } + }; /// Moves the provided [CharGrid] into a new [CharGridCommand], /// leaving other fields as their default values. fn from_grid(grid: move NonNull) -> NonNull { heap_move_nonnull(grid.into()) - } - + }; ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 921e2a4..9e7e75b 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -15,7 +15,6 @@ wrap_fields!(Cp437GridCommand; wrap_origin_accessors!(Cp437GridCommand); wrap_functions!(associate Cp437GridCommand; - /// Show text on the screen. /// /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. @@ -30,12 +29,11 @@ wrap_functions!(associate Cp437GridCommand; grid, origin: Origin::new(origin_x, origin_y), }) - } + }; /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. fn from_grid(grid: move NonNull) -> NonNull { heap_move_nonnull(grid.into()) - } - + }; ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index efd402c..d4282c4 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -248,7 +248,7 @@ wrap_functions!(associate GenericCommand; data: CommandUnion { null: null_mut() }, }); heap_move_nonnull(result) - } + }; ); wrap_methods! { GenericCommand; diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index d2fef10..2217c43 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -7,14 +7,12 @@ use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; wrap_functions!(associate GlobalBrightnessCommand; - /// Set the brightness of all tiles to the same value. /// /// Returns: a new [GlobalBrightnessCommand] instance. fn new(brightness: val Brightness) -> NonNull { heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) - } - + }; ); wrap_command!(GlobalBrightness); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index da16c18..37f9e14 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -64,7 +64,7 @@ macro_rules! derive_command_into_packet { /// Returns: NULL or a [Packet] containing the command. fn try_into_packet(move instance) -> *mut ::servicepoint::Packet { $crate::mem::heap_move_ok(instance.try_into()) - } + }; ); } } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 1386a8d..6367b81 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -37,14 +37,14 @@ wrap_functions!(associate Bitmap; /// ``` fn new(width: val usize, height: val usize) -> *mut Bitmap { heap_move_some(Bitmap::new(width, height)) - } + }; /// Creates a new [Bitmap] with a size matching the screen. /// /// returns: [Bitmap] initialized to all pixels off. fn new_max_sized() -> NonNull { heap_move_nonnull(Bitmap::max_sized()) - } + }; /// Loads a [Bitmap] with the specified dimensions from the provided data. /// @@ -61,7 +61,7 @@ wrap_functions!(associate Bitmap; ) -> *mut Bitmap { let data = unsafe { data.as_slice() }; heap_move_ok(Bitmap::load(width, height, data)) - } + }; /// Tries to convert the BitVec to a Bitmap. /// @@ -73,7 +73,7 @@ wrap_functions!(associate Bitmap; bitvec: move NonNull, ) -> *mut Bitmap { heap_move_ok(Bitmap::from_bitvec(width, bitvec)) - } + }; ); wrap_methods!(Bitmap; diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index cdb7c36..76297b0 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -11,7 +11,6 @@ use std::ptr::NonNull; wrap_container!(DisplayBitVec); wrap_functions!(associate DisplayBitVec; - /// Creates a new [DisplayBitVec] instance. /// /// # Arguments @@ -25,7 +24,7 @@ wrap_functions!(associate DisplayBitVec; /// - when `size` is not divisible by 8. fn new(size: val usize) -> NonNull { heap_move_nonnull(DisplayBitVec::repeat(false, size)) - } + }; /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// @@ -33,8 +32,7 @@ wrap_functions!(associate DisplayBitVec; fn load(data: val ByteSlice) -> NonNull { let data = unsafe { data.as_slice() }; heap_move_nonnull(DisplayBitVec::from_slice(data)) - } - + }; ); wrap_methods!(DisplayBitVec; diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index b0da48a..a9bcad4 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -32,7 +32,7 @@ wrap_functions!(associate BrightnessGrid; /// ``` fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(BrightnessGrid::new(width, height)) - } + }; /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. /// @@ -49,7 +49,7 @@ wrap_functions!(associate BrightnessGrid; ByteGrid::load(width, height, data) .map(move |grid| grid.map(Brightness::saturating_from)), ) - } + }; ); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 715588f..9001d2c 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -10,7 +10,6 @@ wrap_container!(CharGrid); derive_get_width_height!(CharGrid); wrap_functions!(associate CharGrid; - /// Creates a new [CharGrid] with the specified dimensions. /// /// returns: [CharGrid] initialized to 0. @@ -25,7 +24,7 @@ wrap_functions!(associate CharGrid; /// ``` fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(CharGrid::new(width, height)) - } + }; /// Loads a [CharGrid] with the specified dimensions from the provided data. /// @@ -33,9 +32,7 @@ wrap_functions!(associate CharGrid; fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid { let data = unsafe { data.as_slice() }; heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) - } - - + }; ); wrap_methods!(CharGrid; diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index ecc36c6..48f62fc 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -16,13 +16,13 @@ wrap_functions!(associate Cp437Grid; /// returns: [Cp437Grid] initialized to 0. fn new(width: val usize, height: val usize) -> NonNull { heap_move_nonnull(Cp437Grid::new(width, height)) - } + }; /// Loads a [Cp437Grid] with the specified dimensions from the provided data. fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid { let data = unsafe { data.as_slice() }; heap_move_some(Cp437Grid::load(width, height, data)) - } + }; ); wrap_methods!(Cp437Grid; diff --git a/src/lib.rs b/src/lib.rs index 5a1a5de..f884a83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,6 @@ mod feature_env_logger { /// `RUST_LOG` environment variable. See [env_logger](https://docs.rs/env_logger/latest/env_logger/). fn init() { env_logger::init(); - } + }; ); } diff --git a/src/macros.rs b/src/macros.rs index 8cae819..bfbd4b3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,48 +1,36 @@ macro_rules! derive_free { ($typ:ident) => { ::paste::paste! { - $crate::macros::wrap_functions!([< $typ:lower >]; + $crate::macros::wrap_method!($typ; #[doc = "Deallocates a [`" $typ "`] instance."] #[allow(dropping_copy_types)] - fn free(instance: move ::core::ptr::NonNull<$typ>) { + fn free(move instance) { ::std::mem::drop(instance) - } + }; ); } }; } macro_rules! derive_clone { - ($typ:ident) => { + ($object_type:ident) => { ::paste::paste! { - $crate::macros::wrap_functions!([< $typ:lower >]; - #[doc = "Clones a [`" $typ "`] instance."] - fn clone(instance: ref ::core::ptr::NonNull<$typ>) -> ::core::ptr::NonNull<$typ> { + $crate::macros::wrap_method!($object_type; + #[doc = "Clones a [`" $object_type "`] instance."] + fn clone(ref instance) -> ::core::ptr::NonNull<$object_type> { $crate::mem::heap_move_nonnull(instance.clone()) - } + }; ); } }; } -macro_rules! nonnull_as_ref { - ($ident:ident) => { - $ident.as_ref() - }; -} - -macro_rules! nonnull_as_mut { - ($ident:ident) => { - (&mut *$ident.as_ptr()) - }; -} - macro_rules! wrap_method { ( $object_type:ident; $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)? + $(-> $return_type:ty)?; ) => { ::paste::paste!{ $crate::macros::wrap_method!( @@ -51,7 +39,7 @@ macro_rules! wrap_method { fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? { $instance.$function($($($param_name),*)?) - } + }; ); } }; @@ -59,7 +47,7 @@ macro_rules! wrap_method { $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) $(-> $return_type:ty)? - $impl:block + $impl:block; ) => { paste::paste! { $crate::macros::wrap_functions!([< $object_type:lower >]; @@ -70,7 +58,7 @@ macro_rules! wrap_method { $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> $(,$($param_name: $param_modifier $param_type),*)? ) $(-> $return_type)? - $impl + $impl; ); } }; @@ -92,7 +80,7 @@ macro_rules! wrap_methods { $(#[$meta])* fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_type)? - $($impl)? + $($impl)?; ); )+ } @@ -107,7 +95,7 @@ macro_rules! wrap_fields_accessor { "` of the [`servicepoint::" $object_type "`]."] fn [](ref instance) -> $prop_type { return instance.$prop_name; - } + }; } } }; @@ -121,7 +109,7 @@ macro_rules! wrap_fields_accessor { /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. fn [](mut instance) -> ::core::ptr::NonNull<$prop_type> { return ::core::ptr::NonNull::from(&mut instance.$prop_name); - } + }; } } }; @@ -132,7 +120,7 @@ macro_rules! wrap_fields_accessor { "` of the [`servicepoint::" $object_type "`]."] fn [](mut instance, value: val $prop_type) { instance.$prop_name = value; - } + }; } } }; @@ -144,7 +132,7 @@ macro_rules! wrap_fields_accessor { /// The provided value is moved into the instance, potentially invalidating previously taken references. fn [](mut instance, value: move ::core::ptr::NonNull<$prop_type>) { instance.$prop_name = value; - } + }; } } }; @@ -176,10 +164,10 @@ macro_rules! apply_param_modifier { $param_name }; (mut, $param_name:ident) => { - unsafe { $crate::macros::nonnull_as_mut!($param_name) } + unsafe { (&mut *$param_name.as_ptr()) } }; (ref, $param_name:ident) => { - unsafe { $crate::macros::nonnull_as_ref!($param_name) } + unsafe { $param_name.as_ref() } }; } @@ -189,7 +177,7 @@ macro_rules! wrap_function { $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? - $block:block + $block:block; ) => { ::paste::paste! { $(#[$meta])* @@ -216,7 +204,7 @@ macro_rules! wrap_functions { $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? - $block:block + $block:block; )+ ) => { ::paste::paste! { @@ -224,7 +212,7 @@ macro_rules! wrap_functions { $crate::macros::wrap_function!($module; $(#[$meta])+ fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? - $block + $block; ); )+ } @@ -235,7 +223,7 @@ macro_rules! wrap_functions { $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) $(-> $return_type:ty)? - $block:block + $block:block; )+ ) => { ::paste::paste! { @@ -243,7 +231,7 @@ macro_rules! wrap_functions { $( $(#[$meta])+ fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? - $block + $block; )+ } } @@ -251,7 +239,7 @@ macro_rules! wrap_functions { } pub(crate) use { - apply_param_modifier, derive_clone, derive_free, nonnull_as_mut, - nonnull_as_ref, wrap_fields, wrap_fields_accessor, wrap_function, + apply_param_modifier, derive_clone, derive_free, + wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions, wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index 5dc172d..4f1d38f 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -15,7 +15,7 @@ wrap_functions!(associate Packet; fn try_load(data: val ByteSlice) -> *mut Packet { let data = unsafe { data.as_slice() }; heap_move_ok(servicepoint::Packet::try_from(data)) - } + }; /// Creates a raw [Packet] from parts. /// @@ -28,7 +28,7 @@ wrap_functions!(associate Packet; }; heap_move_nonnull(Packet { header, payload }) - } + }; ); derive_clone!(Packet); @@ -87,6 +87,5 @@ wrap_functions!(sp; } Err(_) => false, } - } - + }; ); diff --git a/src/udp.rs b/src/udp.rs index 13718f4..a12281f 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -29,9 +29,8 @@ wrap_functions!(associate UdpSocket; let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); - heap_move_ok(UdpSocket::bind_connect(host)) - } + }; /// Creates a new instance of [UdpSocket]. /// @@ -47,7 +46,7 @@ wrap_functions!(associate UdpSocket; fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); heap_move_ok(UdpSocket::bind_connect(addr)) - } + }; ); From 39c7c27c868d69ef642768756c033f89a3488e76 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 18:59:46 +0200 Subject: [PATCH 65/76] implement return modifiers --- src/commands/bitmap_command.rs | 11 ++-- src/commands/bitvec_command.rs | 7 ++- src/commands/brightness_grid_command.rs | 11 ++-- src/commands/cc_only_commands.rs | 8 ++- src/commands/char_grid_command.rs | 11 ++-- src/commands/cp437_grid_command.rs | 11 ++-- src/commands/generic_command.rs | 9 ++-- src/commands/global_brightness_command.rs | 5 +- src/commands/mod.rs | 4 +- src/containers/bitmap.rs | 29 +++++----- src/containers/bitvec.rs | 23 ++++---- src/containers/brightness_grid.rs | 23 ++++---- src/containers/char_grid.rs | 17 +++--- src/containers/cp437_grid.rs | 17 +++--- src/containers/mod.rs | 6 +-- src/macros.rs | 65 +++++++++++++++-------- src/packet.rs | 15 +++--- src/udp.rs | 18 +++---- 18 files changed, 144 insertions(+), 146 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index c7290ce..40fbc1c 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; @@ -26,19 +25,19 @@ wrap_functions!(associate BitmapCommand; origin_x: val usize, origin_y: val usize, compression: val CompressionCode, - ) -> NonNull { - heap_move_nonnull(BitmapCommand { + ) -> move NonNull { + BitmapCommand { bitmap, origin: Origin::new(origin_x, origin_y), compression, - }) + } }; /// Move the provided [Bitmap] into a new [BitmapCommand], /// leaving other fields as their default values. /// /// Rust equivalent: `BitmapCommand::from(bitmap)` - fn from_bitmap(bitmap: move NonNull) -> NonNull { - heap_move_nonnull(bitmap.into()) + fn from_bitmap(bitmap: move NonNull) -> move NonNull { + bitmap.into() }; ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 6b26227..f62b492 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, @@ -35,12 +34,12 @@ wrap_functions!(associate BitVecCommand; offset: val usize, operation: val BinaryOperation, compression: val CompressionCode, - ) -> NonNull { - heap_move_nonnull(BitVecCommand { + ) -> move NonNull { + BitVecCommand { bitvec, offset, operation, compression, - }) + } }; ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index e53a1a0..007d645 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; @@ -24,16 +23,16 @@ wrap_functions!(associate BrightnessGridCommand; grid: move NonNull, origin_x: val usize, origin_y: val usize - ) -> NonNull { - heap_move_nonnull(BrightnessGridCommand { + ) -> move NonNull { + BrightnessGridCommand { grid, origin: Origin::new(origin_x, origin_y), - }) + } }; /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> NonNull { - heap_move_nonnull(grid.into()) + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() }; ); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 6f06bd8..ef49fa1 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,6 +1,4 @@ -use crate::{ - commands::wrap_command, macros::wrap_functions, mem::heap_move_nonnull, -}; +use crate::{commands::wrap_command, macros::wrap_functions}; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; macro_rules! wrap_cc_only { @@ -12,8 +10,8 @@ macro_rules! wrap_cc_only { $(#[$meta])* /// #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] - fn new() -> ::core::ptr::NonNull<[< $command Command >]> { - heap_move_nonnull([< $command Command >]) + fn new() -> move ::core::ptr::NonNull<[< $command Command >]> { + [< $command Command >] }; ); } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index fb9f7d0..06f2070 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; @@ -24,16 +23,16 @@ wrap_functions!(associate CharGridCommand; grid: move NonNull, origin_x: val usize, origin_y: val usize, - ) -> NonNull { - heap_move_nonnull(CharGridCommand { + ) -> move NonNull { + CharGridCommand { grid, origin: Origin::new(origin_x, origin_y), - }) + } }; /// Moves the provided [CharGrid] into a new [CharGridCommand], /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> NonNull { - heap_move_nonnull(grid.into()) + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() }; ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 9e7e75b..d4c0e88 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; @@ -24,16 +23,16 @@ wrap_functions!(associate Cp437GridCommand; grid: move NonNull, origin_x: val usize, origin_y: val usize, - ) -> NonNull { - heap_move_nonnull(Cp437GridCommand { + ) -> move NonNull { + Cp437GridCommand { grid, origin: Origin::new(origin_x, origin_y), - }) + } }; /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> NonNull { - heap_move_nonnull(grid.into()) + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() }; ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index d4282c4..c6a1323 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -178,8 +178,8 @@ wrap_functions!(associate GenericCommand; /// The packet is dropped in the process. /// /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. - fn try_from_packet(packet: move NonNull) -> NonNull { - let result = servicepoint::TypedCommand::try_from(packet) + fn try_from_packet(packet: move NonNull) -> move NonNull { + servicepoint::TypedCommand::try_from(packet) .map(|value| match value { TypedCommand::Clear(clear) => GenericCommand { tag: CommandTag::Clear, @@ -246,8 +246,7 @@ wrap_functions!(associate GenericCommand; .unwrap_or_else(move |_| GenericCommand { tag: CommandTag::Invalid, data: CommandUnion { null: null_mut() }, - }); - heap_move_nonnull(result) + }) }; ); @@ -256,7 +255,7 @@ wrap_methods! { GenericCommand; /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - fn try_into_packet(move command) -> *mut Packet { + fn try_into_packet(move command) -> val *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 2217c43..6a883ab 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,7 +1,6 @@ use crate::{ commands::wrap_command, macros::{wrap_fields, wrap_functions}, - mem::heap_move_nonnull, }; use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; @@ -10,8 +9,8 @@ wrap_functions!(associate GlobalBrightnessCommand; /// Set the brightness of all tiles to the same value. /// /// Returns: a new [GlobalBrightnessCommand] instance. - fn new(brightness: val Brightness) -> NonNull { - heap_move_nonnull(GlobalBrightnessCommand::from(brightness)) + fn new(brightness: val Brightness) -> move NonNull { + GlobalBrightnessCommand::from(brightness) }; ); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 37f9e14..1d6e8ff 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -62,8 +62,8 @@ macro_rules! derive_command_into_packet { #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. - fn try_into_packet(move instance) -> *mut ::servicepoint::Packet { - $crate::mem::heap_move_ok(instance.try_into()) + fn try_into_packet(move instance) -> move_ok *mut ::servicepoint::Packet { + instance.try_into() }; ); } diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 6367b81..b47cfbc 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, @@ -35,15 +34,15 @@ wrap_functions!(associate Bitmap; /// sp_bitmap_set(grid, 0, 0, false); /// sp_bitmap_free(grid); /// ``` - fn new(width: val usize, height: val usize) -> *mut Bitmap { - heap_move_some(Bitmap::new(width, height)) + fn new(width: val usize, height: val usize) -> move_some *mut Bitmap { + Bitmap::new(width, height) }; /// Creates a new [Bitmap] with a size matching the screen. /// /// returns: [Bitmap] initialized to all pixels off. - fn new_max_sized() -> NonNull { - heap_move_nonnull(Bitmap::max_sized()) + fn new_max_sized() -> move NonNull { + Bitmap::max_sized() }; /// Loads a [Bitmap] with the specified dimensions from the provided data. @@ -58,9 +57,9 @@ wrap_functions!(associate Bitmap; width: val usize, height: val usize, data: val ByteSlice, - ) -> *mut Bitmap { + ) -> move_ok *mut Bitmap { let data = unsafe { data.as_slice() }; - heap_move_ok(Bitmap::load(width, height, data)) + Bitmap::load(width, height, data) }; /// Tries to convert the BitVec to a Bitmap. @@ -71,15 +70,15 @@ wrap_functions!(associate Bitmap; fn from_bitvec( width: val usize, bitvec: move NonNull, - ) -> *mut Bitmap { - heap_move_ok(Bitmap::from_bitvec(width, bitvec)) + ) -> move_ok *mut Bitmap { + Bitmap::from_bitvec(width, bitvec) }; ); wrap_methods!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - fn into_bitvec(move bitmap) -> NonNull { - heap_move_nonnull(bitmap.into()) + fn into_bitvec(move bitmap) -> move NonNull { + bitmap.into() }; /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. @@ -87,18 +86,18 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> *mut Packet { - heap_move_ok(Packet::try_from(BitmapCommand { + fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet { + Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), compression, - })) + }) }; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - fn data_ref_mut(mut instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> val ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 76297b0..50a4eee 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_container, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -22,16 +21,16 @@ wrap_functions!(associate DisplayBitVec; /// # Panics /// /// - when `size` is not divisible by 8. - fn new(size: val usize) -> NonNull { - heap_move_nonnull(DisplayBitVec::repeat(false, size)) + fn new(size: val usize) -> move NonNull { + DisplayBitVec::repeat(false, size) }; /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// /// returns: [DisplayBitVec] instance containing data. - fn load(data: val ByteSlice) -> NonNull { + fn load(data: val ByteSlice) -> move NonNull { let data = unsafe { data.as_slice() }; - heap_move_nonnull(DisplayBitVec::from_slice(data)) + DisplayBitVec::from_slice(data) }; ); @@ -46,13 +45,13 @@ wrap_methods!(DisplayBitVec; offset: val usize, operation: val BinaryOperation, compression: val CompressionCode - ) -> *mut Packet { - heap_move_ok(Packet::try_from(BitVecCommand { + ) -> move_ok *mut Packet { + Packet::try_from(BitVecCommand { bitvec, offset, operation, compression, - })) + }) }; /// Gets the value of a bit. @@ -67,7 +66,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn get(ref instance, index: val usize) -> bool { + fn get(ref instance, index: val usize) -> val bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -91,15 +90,15 @@ wrap_methods!(DisplayBitVec; fn fill(mut instance, value: val bool); /// Gets the length in bits. - fn len(ref instance) -> usize; + fn len(ref instance) -> val usize; /// Returns true if length is 0. - fn is_empty(ref instance) -> bool; + fn is_empty(ref instance) -> val bool; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - fn as_raw_mut_slice(mut instance) -> ByteSlice { + fn as_raw_mut_slice(mut instance) -> val ByteSlice { unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } }; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index a9bcad4..f7cd099 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -12,7 +11,6 @@ use std::{mem::transmute, ptr::NonNull}; wrap_grid!(BrightnessGrid, Brightness); wrap_functions!(associate BrightnessGrid; - /// Creates a new [BrightnessGrid] with the specified dimensions. /// /// returns: [BrightnessGrid] initialized to 0. @@ -30,8 +28,8 @@ wrap_functions!(associate BrightnessGrid; /// TypedCommand *command = sp_command_char_brightness(grid); /// sp_udp_free(connection); /// ``` - fn new(width: val usize, height: val usize) -> NonNull { - heap_move_nonnull(BrightnessGrid::new(width, height)) + fn new(width: val usize, height: val usize) -> move NonNull { + BrightnessGrid::new(width, height) }; /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -43,14 +41,11 @@ wrap_functions!(associate BrightnessGrid; width: val usize, height: val usize, data: val ByteSlice, - ) -> *mut BrightnessGrid { + ) -> move_some *mut BrightnessGrid { let data = unsafe { data.as_slice() }; - heap_move_some( - ByteGrid::load(width, height, data) - .map(move |grid| grid.map(Brightness::saturating_from)), - ) + ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)) }; - ); wrap_methods!(BrightnessGrid; @@ -59,17 +54,17 @@ wrap_methods!(BrightnessGrid; /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { - heap_move_ok(Packet::try_from(BrightnessGridCommand { + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), - })) + }) }; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - fn data_ref_mut(mut instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> val ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 9001d2c..a43f93d 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,7 +1,6 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -22,16 +21,16 @@ wrap_functions!(associate CharGrid; /// sp_char_grid_set(grid, 0, 0, '!'); /// sp_char_grid_free(grid); /// ``` - fn new(width: val usize, height: val usize) -> NonNull { - heap_move_nonnull(CharGrid::new(width, height)) + fn new(width: val usize, height: val usize) -> move NonNull { + CharGrid::new(width, height) }; /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut CharGrid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_ok *mut CharGrid { let data = unsafe { data.as_slice() }; - heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec())) + CharGrid::load_utf8(width, height, data.to_vec()) }; ); @@ -46,7 +45,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: val usize, y: val usize) -> u32 { + fn get(ref instance, x: val usize, y: val usize) -> val u32 { instance.get(x, y) as u32 }; @@ -82,10 +81,10 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { - heap_move_ok(Packet::try_from(CharGridCommand { + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), - })) + }) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 48f62fc..e19ff72 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,7 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, macros::{wrap_functions, wrap_methods}, - mem::{heap_move_nonnull, heap_move_ok, heap_move_some}, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -14,14 +13,14 @@ wrap_functions!(associate Cp437Grid; /// Creates a new [Cp437Grid] with the specified dimensions. /// /// returns: [Cp437Grid] initialized to 0. - fn new(width: val usize, height: val usize) -> NonNull { - heap_move_nonnull(Cp437Grid::new(width, height)) + fn new(width: val usize, height: val usize) -> move NonNull { + Cp437Grid::new(width, height) }; /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: val usize, height: val usize, data: val ByteSlice) -> *mut Cp437Grid { + fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_some *mut Cp437Grid { let data = unsafe { data.as_slice() }; - heap_move_some(Cp437Grid::load(width, height, data)) + Cp437Grid::load(width, height, data) }; ); @@ -31,17 +30,17 @@ wrap_methods!(Cp437Grid; /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> *mut Packet { - heap_move_ok(Packet::try_from(Cp437GridCommand { + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), - })) + }) }; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - fn data_ref_mut(mut instance) -> ByteSlice { + fn data_ref_mut(mut instance) -> val ByteSlice { unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } }; ); diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 35bbe3b..608d038 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -23,10 +23,10 @@ macro_rules! derive_get_width_height { ($object_type:ident) => { $crate::macros::wrap_methods! {$object_type; /// Gets the width. - fn width(ref instance) -> usize; + fn width(ref instance) -> val usize; /// Gets the height. - fn height(ref instance) -> usize; + fn height(ref instance) -> val usize; } }; } @@ -45,7 +45,7 @@ macro_rules! wrap_grid { /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: val usize, y: val usize) -> $value_type; + fn get(ref instance, x: val usize, y: val usize) -> val $value_type; /// Sets the value of the specified position. /// diff --git a/src/macros.rs b/src/macros.rs index bfbd4b3..086c901 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -17,8 +17,8 @@ macro_rules! derive_clone { ::paste::paste! { $crate::macros::wrap_method!($object_type; #[doc = "Clones a [`" $object_type "`] instance."] - fn clone(ref instance) -> ::core::ptr::NonNull<$object_type> { - $crate::mem::heap_move_nonnull(instance.clone()) + fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type> { + instance.clone() }; ); } @@ -30,14 +30,14 @@ macro_rules! wrap_method { $object_type:ident; $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)?; + $(-> $return_modifier:ident $return_type:ty)?; ) => { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; $(#[$meta])+ fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) - $(-> $return_type)? { + $(-> $return_modifier $return_type)? { $instance.$function($($($param_name),*)?) }; ); @@ -46,7 +46,7 @@ macro_rules! wrap_method { ($object_type:ident; $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $impl:block; ) => { paste::paste! { @@ -57,7 +57,7 @@ macro_rules! wrap_method { fn $function( $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> $(,$($param_name: $param_modifier $param_type),*)? - ) $(-> $return_type)? + ) $(-> $return_modifier $return_type)? $impl; ); } @@ -70,7 +70,7 @@ macro_rules! wrap_methods { $( $(#[$meta:meta])+ fn $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $($impl:block)?; )+ ) => { @@ -79,7 +79,7 @@ macro_rules! wrap_methods { $crate::macros::wrap_method!($object_type; $(#[$meta])* fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) - $(-> $return_type)? + $(-> $return_modifier $return_type)? $($impl)?; ); )+ @@ -93,8 +93,8 @@ macro_rules! wrap_fields_accessor { $crate::macros::wrap_method! {$object_type; #[doc = " Gets the value of field `" $prop_name "` of the [`servicepoint::" $object_type "`]."] - fn [](ref instance) -> $prop_type { - return instance.$prop_name; + fn [](ref instance) -> val $prop_type { + instance.$prop_name }; } } @@ -107,8 +107,8 @@ macro_rules! wrap_fields_accessor { /// /// - The returned reference inherits the lifetime of object in which it is contained. /// - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - fn [](mut instance) -> ::core::ptr::NonNull<$prop_type> { - return ::core::ptr::NonNull::from(&mut instance.$prop_name); + fn [](mut instance) -> val ::core::ptr::NonNull<$prop_type> { + ::core::ptr::NonNull::from(&mut instance.$prop_name) }; } } @@ -171,16 +171,31 @@ macro_rules! apply_param_modifier { }; } +macro_rules! apply_return_modifier { + (val, $result:ident) => { + $result + }; + (move, $result:ident) => { + $crate::mem::heap_move_nonnull($result) + }; + (move_ok, $result:ident) => { + $crate::mem::heap_move_ok($result) + }; + (move_some, $result:ident) => { + $crate::mem::heap_move_some($result) + }; +} + macro_rules! wrap_function { ( $module:ident; $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $block:block; ) => { ::paste::paste! { - $(#[$meta])* + $(#[$meta])+ #[doc = ""] #[doc = " This function is part of the `" $module "` module."] #[no_mangle] @@ -191,10 +206,14 @@ macro_rules! wrap_function { $( let $param_name = $crate::macros::apply_param_modifier!($param_modifier, $param_name); )* - $block + let result = $block; + $( + let result = $crate::macros::apply_return_modifier!($return_modifier, result); + )? + result } } - } + }; } macro_rules! wrap_functions { @@ -203,7 +222,7 @@ macro_rules! wrap_functions { $( $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $block:block; )+ ) => { @@ -211,7 +230,7 @@ macro_rules! wrap_functions { $( $crate::macros::wrap_function!($module; $(#[$meta])+ - fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)? $block; ); )+ @@ -222,7 +241,7 @@ macro_rules! wrap_functions { $( $(#[$meta:meta])+ fn $function:ident($($param_name:ident: $param_modifier:ident $param_type:ty),*$(,)?) - $(-> $return_type:ty)? + $(-> $return_modifier:ident $return_type:ty)? $block:block; )+ ) => { @@ -230,7 +249,7 @@ macro_rules! wrap_functions { $crate::macros::wrap_functions!{[< $object_type:lower >]; $( $(#[$meta])+ - fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_type)? + fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)? $block; )+ } @@ -239,7 +258,7 @@ macro_rules! wrap_functions { } pub(crate) use { - apply_param_modifier, derive_clone, derive_free, - wrap_fields, wrap_fields_accessor, wrap_function, - wrap_functions, wrap_method, wrap_methods, + apply_param_modifier, apply_return_modifier, derive_clone, derive_free, + wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions, + wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index 4f1d38f..abfa562 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -3,7 +3,6 @@ use crate::{ macros::{ derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods, }, - mem::{heap_move_nonnull, heap_move_ok}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; @@ -12,22 +11,22 @@ wrap_functions!(associate Packet; /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: val ByteSlice) -> *mut Packet { + fn try_load(data: val ByteSlice) -> move_ok *mut Packet { let data = unsafe { data.as_slice() }; - heap_move_ok(servicepoint::Packet::try_from(data)) + servicepoint::Packet::try_from(data) }; /// Creates a raw [Packet] from parts. /// /// returns: new instance. Will never return null. - fn from_parts(header: val Header, payload: val ByteSlice) -> NonNull { + fn from_parts(header: val Header, payload: val ByteSlice) -> move NonNull { let payload = if payload == ByteSlice::INVALID { None } else { Some(Vec::from(unsafe { payload.as_slice() })) }; - heap_move_nonnull(Packet { header, payload }) + Packet { header, payload } }; ); @@ -44,7 +43,7 @@ wrap_methods! { Packet; /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. /// /// The returned memory can be changed and will be valid until a new payload is set. - fn get_payload(mut packet) -> ByteSlice { + fn get_payload(mut packet) -> val ByteSlice { match &mut packet.payload { None => ByteSlice::INVALID, Some(payload) => unsafe { ByteSlice::from_slice(payload) }, @@ -67,7 +66,7 @@ wrap_methods! { Packet; /// # Panics /// /// - if the buffer is not big enough to hold header+payload. - fn serialize_to(mut packet, buffer: val ByteSlice) -> usize { + fn serialize_to(mut packet, buffer: val ByteSlice) -> val usize { unsafe { packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) } @@ -79,7 +78,7 @@ wrap_functions!(sp; /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed. - fn u16_to_command_code(code: val u16, result: mut NonNull) -> bool { + fn u16_to_command_code(code: val u16, result: mut NonNull) -> val bool { match CommandCode::try_from(code) { Ok(code) => { *result = code; diff --git a/src/udp.rs b/src/udp.rs index a12281f..ba9b336 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,7 +1,7 @@ use crate::{ commands::{CommandTag, GenericCommand}, macros::{derive_free, wrap_functions, wrap_methods}, - mem::{heap_move_ok, heap_remove}, + mem::heap_remove, }; use servicepoint::{Header, Packet, UdpSocketExt}; use std::{ @@ -13,7 +13,6 @@ use std::{ derive_free!(UdpSocket); wrap_functions!(associate UdpSocket; - /// Creates a new instance of [UdpSocket]. /// /// returns: NULL if connection fails, or connected instance @@ -25,11 +24,11 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open(host: val NonNull) -> *mut UdpSocket { + fn open(host: val NonNull) -> move_ok *mut UdpSocket { let host = unsafe { CStr::from_ptr(host.as_ptr()) } .to_str() .expect("Bad encoding"); - heap_move_ok(UdpSocket::bind_connect(host)) + UdpSocket::bind_connect(host) }; /// Creates a new instance of [UdpSocket]. @@ -43,11 +42,10 @@ wrap_functions!(associate UdpSocket; /// if (connection != NULL) /// sp_udp_send_command(connection, sp_command_clear()); /// ``` - fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> *mut UdpSocket { + fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> move_ok *mut UdpSocket { let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); - heap_move_ok(UdpSocket::bind_connect(addr)) + UdpSocket::bind_connect(addr) }; - ); wrap_methods! {UdpSocket; @@ -56,7 +54,7 @@ wrap_methods! {UdpSocket; /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(ref connection, packet: move NonNull) -> bool { + fn send_packet(ref connection, packet: move NonNull) -> val bool { connection.send(&Vec::from(packet)).is_ok() }; @@ -71,7 +69,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(ref connection, command: mut NonNull) -> bool { + fn send_command(ref connection, command: mut NonNull) -> val bool { unsafe { let result = match command.tag { CommandTag::Invalid => return false, @@ -100,7 +98,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(ref udp_connection, header: val Header) -> bool { + fn send_header(ref udp_connection, header: val Header) -> val bool { let packet = Packet { header, payload: None, From 82696b2d1ae49042dab5944f5fef67a7159fd437 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 19:26:41 +0200 Subject: [PATCH 66/76] add slice modifier --- src/containers/bitmap.rs | 7 ++----- src/containers/bitvec.rs | 7 ++----- src/containers/brightness_grid.rs | 7 +++---- src/containers/byte_slice.rs | 1 + src/containers/char_grid.rs | 3 +-- src/containers/cp437_grid.rs | 7 ++----- src/macros.rs | 6 ++++++ src/packet.rs | 3 +-- 8 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index b47cfbc..b6ed4f3 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -56,9 +56,8 @@ wrap_functions!(associate Bitmap; fn load( width: val usize, height: val usize, - data: val ByteSlice, + data: slice ByteSlice, ) -> move_ok *mut Bitmap { - let data = unsafe { data.as_slice() }; Bitmap::load(width, height, data) }; @@ -97,7 +96,5 @@ wrap_methods!(Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - fn data_ref_mut(mut instance) -> val ByteSlice { - unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } - }; + fn data_ref_mut(mut instance) -> slice ByteSlice; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 50a4eee..b16dd92 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -28,8 +28,7 @@ wrap_functions!(associate DisplayBitVec; /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. /// /// returns: [DisplayBitVec] instance containing data. - fn load(data: val ByteSlice) -> move NonNull { - let data = unsafe { data.as_slice() }; + fn load(data: slice ByteSlice) -> move NonNull { DisplayBitVec::from_slice(data) }; ); @@ -98,7 +97,5 @@ wrap_methods!(DisplayBitVec; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - fn as_raw_mut_slice(mut instance) -> val ByteSlice { - unsafe { ByteSlice::from_slice(instance.as_raw_mut_slice()) } - }; + fn as_raw_mut_slice(mut instance) -> slice ByteSlice; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index f7cd099..19f6b7a 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -40,9 +40,8 @@ wrap_functions!(associate BrightnessGrid; fn load( width: val usize, height: val usize, - data: val ByteSlice, + data: slice ByteSlice, ) -> move_some *mut BrightnessGrid { - let data = unsafe { data.as_slice() }; ByteGrid::load(width, height, data) .map(move |grid| grid.map(Brightness::saturating_from)) }; @@ -64,13 +63,13 @@ wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - fn data_ref_mut(mut instance) -> val ByteSlice { + fn data_ref_mut(mut instance) -> slice ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); let br_slice = instance.data_ref_mut(); unsafe { - ByteSlice::from_slice(transmute::<&mut [Brightness], &mut [u8]>(br_slice)) + transmute::<&mut [Brightness], &mut [u8]>(br_slice) } }; ); diff --git a/src/containers/byte_slice.rs b/src/containers/byte_slice.rs index d12cd88..81380e9 100644 --- a/src/containers/byte_slice.rs +++ b/src/containers/byte_slice.rs @@ -33,6 +33,7 @@ impl ByteSlice { }; pub(crate) unsafe fn as_slice(&self) -> &[u8] { + assert!(!self.start.is_null()); unsafe { std::slice::from_raw_parts(self.start, self.length) } } diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index a43f93d..98a2c5b 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -28,8 +28,7 @@ wrap_functions!(associate CharGrid; /// Loads a [CharGrid] with the specified dimensions from the provided data. /// /// returns: new CharGrid or NULL in case of an error - fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_ok *mut CharGrid { - let data = unsafe { data.as_slice() }; + fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid { CharGrid::load_utf8(width, height, data.to_vec()) }; ); diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index e19ff72..62f0eb2 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -18,8 +18,7 @@ wrap_functions!(associate Cp437Grid; }; /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: val usize, height: val usize, data: val ByteSlice) -> move_some *mut Cp437Grid { - let data = unsafe { data.as_slice() }; + fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_some *mut Cp437Grid { Cp437Grid::load(width, height, data) }; ); @@ -40,7 +39,5 @@ wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - fn data_ref_mut(mut instance) -> val ByteSlice { - unsafe { ByteSlice::from_slice(instance.data_ref_mut()) } - }; + fn data_ref_mut(mut instance) -> slice ByteSlice; ); diff --git a/src/macros.rs b/src/macros.rs index 086c901..79cd3af 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -169,6 +169,9 @@ macro_rules! apply_param_modifier { (ref, $param_name:ident) => { unsafe { $param_name.as_ref() } }; + (slice, $param_name:ident) => { + unsafe { $param_name.as_slice() } + }; } macro_rules! apply_return_modifier { @@ -184,6 +187,9 @@ macro_rules! apply_return_modifier { (move_some, $result:ident) => { $crate::mem::heap_move_some($result) }; + (slice, $result:ident) => { + unsafe { ByteSlice::from_slice($result) } + }; } macro_rules! wrap_function { diff --git a/src/packet.rs b/src/packet.rs index abfa562..bec75e5 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -11,8 +11,7 @@ wrap_functions!(associate Packet; /// Tries to load a [Packet] from the passed array with the specified length. /// /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: val ByteSlice) -> move_ok *mut Packet { - let data = unsafe { data.as_slice() }; + fn try_load(data: slice ByteSlice) -> move_ok *mut Packet { servicepoint::Packet::try_from(data) }; From 5beea6151a9fc135306c72865cb57f04b4bbfeca Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 20:42:31 +0200 Subject: [PATCH 67/76] wip generic wrap --- src/commands/bitmap_command.rs | 4 +-- src/commands/bitvec_command.rs | 4 +-- src/commands/brightness_grid_command.rs | 4 +-- src/commands/char_grid_command.rs | 4 +-- src/commands/cp437_grid_command.rs | 4 +-- src/commands/generic_command.rs | 6 ++--- src/commands/global_brightness_command.rs | 4 +-- src/containers/bitmap.rs | 10 ++++---- src/containers/bitvec.rs | 18 ++++++------- src/containers/brightness_grid.rs | 8 +++--- src/containers/char_grid.rs | 13 +++++----- src/containers/cp437_grid.rs | 8 +++--- src/macros.rs | 31 ++++++++++++++++++++++- src/packet.rs | 14 ++++------ src/udp.rs | 10 ++++---- 15 files changed, 83 insertions(+), 59 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 40fbc1c..5b74924 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,13 +1,13 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap_fields, wrap_functions}, + macros::{wrap, wrap_functions}, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; wrap_command!(Bitmap); -wrap_fields!(BitmapCommand; +wrap!(BitmapCommand; prop bitmap: Bitmap { get mut; set move; }; prop compression: CompressionCode { get; set; }; ); diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index f62b492..440850f 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,6 +1,6 @@ use crate::{ commands::wrap_command, - macros::{wrap_fields, wrap_functions}, + macros::{wrap, wrap_functions}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, @@ -9,7 +9,7 @@ use std::ptr::NonNull; wrap_command!(BitVec); -wrap_fields!(BitVecCommand; +wrap!(BitVecCommand; prop bitvec: DisplayBitVec { get mut; set move; }; prop offset: Offset { get; set; }; prop operation: BinaryOperation { get; set; }; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 007d645..23f5982 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,13 +1,13 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap_fields, wrap_functions}, + macros::{wrap, wrap_functions}, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(BrightnessGrid); -wrap_fields!(BrightnessGridCommand; +wrap!(BrightnessGridCommand; prop grid: BrightnessGrid { get mut; set move; }; ); diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index 06f2070..dcd5bc7 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,13 +1,13 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap_fields, wrap_functions}, + macros::{wrap, wrap_functions}, }; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(CharGrid); -wrap_fields!(CharGridCommand; +wrap!(CharGridCommand; prop grid: CharGrid { get mut; set move; }; ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index d4c0e88..90b2d3d 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,13 +1,13 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap_fields, wrap_functions}, + macros::{wrap, wrap_functions}, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; wrap_command!(Cp437Grid); -wrap_fields!(Cp437GridCommand; +wrap!(Cp437GridCommand; prop grid: Cp437Grid { get mut; set move; }; ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index c6a1323..4f71dc4 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{derive_clone, derive_free, wrap_functions, wrap_methods}, + macros::{derive_clone, derive_free, wrap, wrap_functions}, mem::{ heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, heap_remove, @@ -250,12 +250,12 @@ wrap_functions!(associate GenericCommand; }; ); -wrap_methods! { GenericCommand; +wrap! { GenericCommand; /// Tries to turn a [GenericCommand] into a [Packet]. /// The [GenericCommand] gets consumed. /// /// Returns tag [CommandTag::Invalid] in case of an error. - fn try_into_packet(move command) -> val *mut Packet { + method try_into_packet(move command) -> val *mut Packet { match command.tag { CommandTag::Invalid => null_mut(), CommandTag::Bitmap => { diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 6a883ab..06377ae 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,6 +1,6 @@ use crate::{ commands::wrap_command, - macros::{wrap_fields, wrap_functions}, + macros::{wrap, wrap_functions}, }; use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; @@ -16,6 +16,6 @@ wrap_functions!(associate GlobalBrightnessCommand; wrap_command!(GlobalBrightness); -wrap_fields!(GlobalBrightnessCommand; +wrap!(GlobalBrightnessCommand; prop brightness: Brightness { get; set; }; ); diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index b6ed4f3..acc959a 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{wrap_functions, wrap_methods}, + macros::{wrap, wrap_functions}, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, @@ -74,9 +74,9 @@ wrap_functions!(associate Bitmap; }; ); -wrap_methods!(Bitmap; +wrap!(Bitmap; /// Consumes the Bitmap and returns the contained BitVec. - fn into_bitvec(move bitmap) -> move NonNull { + method into_bitvec(move bitmap) -> move NonNull { bitmap.into() }; @@ -85,7 +85,7 @@ wrap_methods!(Bitmap; /// The provided [Bitmap] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet { + method try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet { Packet::try_from(BitmapCommand { bitmap, origin: Origin::new(x, y), @@ -96,5 +96,5 @@ wrap_methods!(Bitmap; /// Gets an unsafe reference to the data of the [Bitmap] instance. /// /// The returned memory is valid for the lifetime of the bitmap. - fn data_ref_mut(mut instance) -> slice ByteSlice; + method data_ref_mut(mut instance) -> slice ByteSlice; ); diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index b16dd92..c0627da 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_container, ByteSlice}, - macros::{wrap_functions, wrap_methods}, + macros::{wrap, wrap_functions}, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -33,13 +33,13 @@ wrap_functions!(associate DisplayBitVec; }; ); -wrap_methods!(DisplayBitVec; +wrap!(DisplayBitVec; /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. /// /// The provided [DisplayBitVec] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet( + method try_into_packet( move bitvec, offset: val usize, operation: val BinaryOperation, @@ -65,7 +65,7 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn get(ref instance, index: val usize) -> val bool { + method get(ref instance, index: val usize) -> val bool { instance.get(index).map(|x| *x).unwrap_or(false) }; @@ -79,23 +79,23 @@ wrap_methods!(DisplayBitVec; /// # Panics /// /// - when accessing `index` out of bounds - fn set(mut instance, index: val usize, value: val bool); + method set(mut instance, index: val usize, value: val bool); /// Sets the value of all bits. /// /// # Arguments /// /// - `value`: the value to set all bits to - fn fill(mut instance, value: val bool); + method fill(mut instance, value: val bool); /// Gets the length in bits. - fn len(ref instance) -> val usize; + method len(ref instance) -> val usize; /// Returns true if length is 0. - fn is_empty(ref instance) -> val bool; + method is_empty(ref instance) -> val bool; /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. /// /// The returned memory is valid for the lifetime of the bitvec. - fn as_raw_mut_slice(mut instance) -> slice ByteSlice; + method as_raw_mut_slice(mut instance) -> slice ByteSlice; ); diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 19f6b7a..c6b7f13 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{wrap_functions, wrap_methods}, + macros::{wrap, wrap_functions}, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -47,13 +47,13 @@ wrap_functions!(associate BrightnessGrid; }; ); -wrap_methods!(BrightnessGrid; +wrap!(BrightnessGrid; /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. /// /// The provided [BrightnessGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + method try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { Packet::try_from(BrightnessGridCommand { grid, origin: Origin::new(x, y), @@ -63,7 +63,7 @@ wrap_methods!(BrightnessGrid; /// Gets an unsafe reference to the data of the instance. /// /// The returned memory is valid for the lifetime of the grid. - fn data_ref_mut(mut instance) -> slice ByteSlice { + method data_ref_mut(mut instance) -> slice ByteSlice { //noinspection RsAssertEqual const _: () = assert!(size_of::() == 1); diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 98a2c5b..4323021 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, - macros::{wrap_functions, wrap_methods}, + macros::{wrap, wrap_functions}, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -33,8 +33,7 @@ wrap_functions!(associate CharGrid; }; ); -wrap_methods!(CharGrid; - +wrap!(CharGrid; /// Returns the current value at the specified position. /// /// # Arguments @@ -44,7 +43,7 @@ wrap_methods!(CharGrid; /// # Panics /// /// - when accessing `x` or `y` out of bounds - fn get(ref instance, x: val usize, y: val usize) -> val u32 { + method get(ref instance, x: val usize, y: val usize) -> val u32 { instance.get(x, y) as u32 }; @@ -61,7 +60,7 @@ wrap_methods!(CharGrid; /// /// - when accessing `x` or `y` out of bounds /// - when providing values that cannot be converted to Rust's `char`. - fn set(mut instance, x: val usize, y: val usize, value: val u32) { + method set(mut instance, x: val usize, y: val usize, value: val u32) { instance.set(x, y, char::from_u32(value).unwrap()) }; @@ -71,7 +70,7 @@ wrap_methods!(CharGrid; /// /// - `value`: the value to set all cells to /// - when providing values that cannot be converted to Rust's `char`. - fn fill(mut instance, value: val u32) { + method fill(mut instance, value: val u32) { instance.fill(char::from_u32(value).unwrap()) }; @@ -80,7 +79,7 @@ wrap_methods!(CharGrid; /// The provided [CharGrid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + method try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { Packet::try_from(CharGridCommand { grid, origin: Origin::new(x, y), diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 62f0eb2..2a157c2 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{wrap_functions, wrap_methods}, + macros::{wrap, wrap_functions}, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -23,13 +23,13 @@ wrap_functions!(associate Cp437Grid; }; ); -wrap_methods!(Cp437Grid; +wrap!(Cp437Grid; /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. /// /// The provided [Cp437Grid] gets consumed. /// /// Returns NULL in case of an error. - fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + method try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { Packet::try_from(Cp437GridCommand { grid, origin: Origin::new(x, y), @@ -39,5 +39,5 @@ wrap_methods!(Cp437Grid; /// Gets an unsafe reference to the data of the grid. /// /// The returned memory is valid for the lifetime of the instance. - fn data_ref_mut(mut instance) -> slice ByteSlice; + method data_ref_mut(mut instance) -> slice ByteSlice; ); diff --git a/src/macros.rs b/src/macros.rs index 79cd3af..ebcad75 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -263,8 +263,37 @@ macro_rules! wrap_functions { }; } +macro_rules! wrap { + ( + $object_type:ident; + $( + prop $prop_name:ident : $prop_type:ty { $($accessor:ident $($modifier:ident)?;)+ }; + )* + $( + $(#[$meta:meta])+ + method $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) + $(-> $return_modifier:ident $return_type:ty)? + $($impl:block)?; + )* + ) => { + $( + $crate::macros::wrap_fields!($object_type; + prop $prop_name : $prop_type { $($accessor $($modifier)?;)+ }; + ); + )* + $( + $crate::macros::wrap_method!($object_type; + $(#[$meta])+ + fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) + $(-> $return_modifier $return_type)? + $($impl)?; + ); + )* + }; +} + pub(crate) use { apply_param_modifier, apply_return_modifier, derive_clone, derive_free, - wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions, + wrap, wrap_fields, wrap_fields_accessor, wrap_function, wrap_functions, wrap_method, wrap_methods, }; diff --git a/src/packet.rs b/src/packet.rs index bec75e5..71c746d 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,8 +1,6 @@ use crate::{ containers::ByteSlice, - macros::{ - derive_clone, derive_free, wrap_fields, wrap_functions, wrap_methods, - }, + macros::{derive_clone, derive_free, wrap, wrap_functions}, }; use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; @@ -32,17 +30,15 @@ wrap_functions!(associate Packet; derive_clone!(Packet); derive_free!(Packet); -wrap_fields!(Packet; +wrap! {Packet; prop header: Header { get; get mut; set; }; -); -wrap_methods! { Packet; /// Returns a pointer to the current payload of the provided packet. /// /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. /// /// The returned memory can be changed and will be valid until a new payload is set. - fn get_payload(mut packet) -> val ByteSlice { + method get_payload(mut packet) -> val ByteSlice { match &mut packet.payload { None => ByteSlice::INVALID, Some(payload) => unsafe { ByteSlice::from_slice(payload) }, @@ -52,7 +48,7 @@ wrap_methods! { Packet; /// Sets the payload of the provided packet to the provided data. /// /// This makes previous payload pointers invalid. - fn set_payload(mut packet, data: val ByteSlice) { + method set_payload(mut packet, data: val ByteSlice) { packet.payload = if data == ByteSlice::INVALID { None } else { @@ -65,7 +61,7 @@ wrap_methods! { Packet; /// # Panics /// /// - if the buffer is not big enough to hold header+payload. - fn serialize_to(mut packet, buffer: val ByteSlice) -> val usize { + method serialize_to(mut packet, buffer: val ByteSlice) -> val usize { unsafe { packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) } diff --git a/src/udp.rs b/src/udp.rs index ba9b336..4fce7c0 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::{ commands::{CommandTag, GenericCommand}, - macros::{derive_free, wrap_functions, wrap_methods}, + macros::{derive_free, wrap, wrap_functions}, mem::heap_remove, }; use servicepoint::{Header, Packet, UdpSocketExt}; @@ -48,13 +48,13 @@ wrap_functions!(associate UdpSocket; }; ); -wrap_methods! {UdpSocket; +wrap! {UdpSocket; /// Sends a [Packet] to the display using the [UdpSocket]. /// /// The passed `packet` gets consumed. /// /// returns: true in case of success - fn send_packet(ref connection, packet: move NonNull) -> val bool { + method send_packet(ref connection, packet: move NonNull) -> val bool { connection.send(&Vec::from(packet)).is_ok() }; @@ -69,7 +69,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_command(connection, sp_command_brightness(5)); /// ``` - fn send_command(ref connection, command: mut NonNull) -> val bool { + method send_command(ref connection, command: mut NonNull) -> val bool { unsafe { let result = match command.tag { CommandTag::Invalid => return false, @@ -98,7 +98,7 @@ wrap_methods! {UdpSocket; /// ```C /// sp_udp_send_header(connection, sp_command_brightness(5)); /// ``` - fn send_header(ref udp_connection, header: val Header) -> val bool { + method send_header(ref udp_connection, header: val Header) -> val bool { let packet = Packet { header, payload: None, From c65b735f5743f31a30e92f062062ec3c9e861c02 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 22:23:53 +0200 Subject: [PATCH 68/76] generic wrap --- src/commands/bitmap_command.rs | 66 +++--- src/commands/bitvec_command.rs | 72 ++++--- src/commands/brightness_grid_command.rs | 54 ++--- src/commands/cc_only_commands.rs | 21 +- src/commands/char_grid_command.rs | 54 ++--- src/commands/cp437_grid_command.rs | 54 ++--- src/commands/generic_command.rs | 242 +++++++++++----------- src/commands/global_brightness_command.rs | 28 ++- src/containers/bitmap.rs | 168 +++++++-------- src/containers/bitvec.rs | 168 +++++++-------- src/containers/brightness_grid.rs | 122 +++++------ src/containers/char_grid.rs | 151 +++++++------- src/containers/cp437_grid.rs | 62 +++--- src/macros.rs | 62 ++++-- src/packet.rs | 112 +++++----- src/udp.rs | 187 ++++++++--------- 16 files changed, 828 insertions(+), 795 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 5b74924..2cb86f0 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,43 +1,43 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; wrap_command!(Bitmap); - -wrap!(BitmapCommand; - prop bitmap: Bitmap { get mut; set move; }; - prop compression: CompressionCode { get; set; }; -); - wrap_origin_accessors!(BitmapCommand); -wrap_functions!(associate BitmapCommand; - /// Sets a window of pixels to the specified values. - /// - /// The passed [Bitmap] gets consumed. - /// - /// Returns: a new [BitmapCommand] instance. - fn new( - bitmap: move NonNull, - origin_x: val usize, - origin_y: val usize, - compression: val CompressionCode, - ) -> move NonNull { - BitmapCommand { - bitmap, - origin: Origin::new(origin_x, origin_y), - compression, - } - }; +wrap! { + BitmapCommand { + properties: + prop bitmap: Bitmap { get mut; set move; }; + prop compression: CompressionCode { get; set; }; + functions: + /// Sets a window of pixels to the specified values. + /// + /// The passed [Bitmap] gets consumed. + /// + /// Returns: a new [BitmapCommand] instance. + fn new( + bitmap: move NonNull, + origin_x: val usize, + origin_y: val usize, + compression: val CompressionCode, + ) -> move NonNull { + BitmapCommand { + bitmap, + origin: Origin::new(origin_x, origin_y), + compression, + } + }; - /// Move the provided [Bitmap] into a new [BitmapCommand], - /// leaving other fields as their default values. - /// - /// Rust equivalent: `BitmapCommand::from(bitmap)` - fn from_bitmap(bitmap: move NonNull) -> move NonNull { - bitmap.into() - }; -); + /// Move the provided [Bitmap] into a new [BitmapCommand], + /// leaving other fields as their default values. + /// + /// Rust equivalent: `BitmapCommand::from(bitmap)` + fn from_bitmap(bitmap: move NonNull) -> move NonNull { + bitmap.into() + }; + } +} diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 440850f..3b239aa 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,7 +1,4 @@ -use crate::{ - commands::wrap_command, - macros::{wrap, wrap_functions}, -}; +use crate::{commands::wrap_command, macros::wrap}; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, }; @@ -9,37 +6,38 @@ use std::ptr::NonNull; wrap_command!(BitVec); -wrap!(BitVecCommand; - prop bitvec: DisplayBitVec { get mut; set move; }; - prop offset: Offset { get; set; }; - prop operation: BinaryOperation { get; set; }; - prop compression: CompressionCode { get; set; }; -); - -wrap_functions!(associate BitVecCommand; - /// Set pixel data starting at the pixel offset on screen. - /// - /// The screen will continuously overwrite more pixel data without regarding the offset, meaning - /// once the starting row is full, overwriting will continue on column 0. - /// - /// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. - /// - /// `new_bit = old_bit op sent_bit` - /// - /// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. - /// - /// The contained [`DisplayBitVec`] is always uncompressed. - fn new( - bitvec: move NonNull, - offset: val usize, - operation: val BinaryOperation, - compression: val CompressionCode, - ) -> move NonNull { - BitVecCommand { - bitvec, - offset, - operation, - compression, - } - }; +wrap!( + BitVecCommand { + properties: + prop bitvec: DisplayBitVec { get mut; set move; }; + prop offset: Offset { get; set; }; + prop operation: BinaryOperation { get; set; }; + prop compression: CompressionCode { get; set; }; + functions: + /// Set pixel data starting at the pixel offset on screen. + /// + /// The screen will continuously overwrite more pixel data without regarding the offset, meaning + /// once the starting row is full, overwriting will continue on column 0. + /// + /// The [`BinaryOperation`] will be applied on the display comparing old and sent bit. + /// + /// `new_bit = old_bit op sent_bit` + /// + /// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. + /// + /// The contained [`DisplayBitVec`] is always uncompressed. + fn new( + bitvec: move NonNull, + offset: val usize, + operation: val BinaryOperation, + compression: val CompressionCode, + ) -> move NonNull { + BitVecCommand { + bitvec, + offset, + operation, + compression, + } + }; + } ); diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index 23f5982..cbf22bb 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,38 +1,38 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(BrightnessGrid); - -wrap!(BrightnessGridCommand; - prop grid: BrightnessGrid { get mut; set move; }; -); - wrap_origin_accessors!(BrightnessGridCommand); -wrap_functions!(associate BrightnessGridCommand; - /// Set the brightness of individual tiles in a rectangular area of the display. - /// - /// The passed [BrightnessGrid] gets consumed. - /// - /// Returns: a new [BrightnessGridCommand] instance. - fn new( - grid: move NonNull, - origin_x: val usize, - origin_y: val usize - ) -> move NonNull { - BrightnessGridCommand { - grid, - origin: Origin::new(origin_x, origin_y), - } - }; +wrap!( + BrightnessGridCommand { + properties: + prop grid: BrightnessGrid { get mut; set move; }; + functions: + /// Set the brightness of individual tiles in a rectangular area of the display. + /// + /// The passed [BrightnessGrid] gets consumed. + /// + /// Returns: a new [BrightnessGridCommand] instance. + fn new( + grid: move NonNull, + origin_x: val usize, + origin_y: val usize + ) -> move NonNull { + BrightnessGridCommand { + grid, + origin: Origin::new(origin_x, origin_y), + } + }; - /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], - /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> move NonNull { - grid.into() - }; + /// Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + /// leaving other fields as their default values. + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() + }; + } ); diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index ef49fa1..3fa97c7 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,4 +1,4 @@ -use crate::{commands::wrap_command, macros::wrap_functions}; +use crate::commands::wrap_command; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; macro_rules! wrap_cc_only { @@ -6,14 +6,17 @@ macro_rules! wrap_cc_only { ::paste::paste!{ wrap_command!($command); - wrap_functions!(associate [< $command Command >]; - $(#[$meta])* - /// - #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] - fn new() -> move ::core::ptr::NonNull<[< $command Command >]> { - [< $command Command >] - }; - ); + $crate::macros::wrap!{ + [< $command Command >] { + functions: + $(#[$meta])* + /// + #[doc = " Returns: a new [`" [< $command Command >] "`] instance."] + fn new() -> move ::core::ptr::NonNull<[< $command Command >]> { + [< $command Command >] + }; + } + } } }; } diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index dcd5bc7..cbb91e6 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,38 +1,38 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; wrap_command!(CharGrid); - -wrap!(CharGridCommand; - prop grid: CharGrid { get mut; set move; }; -); - wrap_origin_accessors!(CharGridCommand); -wrap_functions!(associate CharGridCommand; - /// Show UTF-8 encoded text on the screen. - /// - /// The passed [CharGrid] gets consumed. - /// - /// Returns: a new [CharGridCommand] instance. - fn new( - grid: move NonNull, - origin_x: val usize, - origin_y: val usize, - ) -> move NonNull { - CharGridCommand { - grid, - origin: Origin::new(origin_x, origin_y), - } - }; +wrap!( + CharGridCommand { + properties: + prop grid: CharGrid { get mut; set move; }; + functions: + /// Show UTF-8 encoded text on the screen. + /// + /// The passed [CharGrid] gets consumed. + /// + /// Returns: a new [CharGridCommand] instance. + fn new( + grid: move NonNull, + origin_x: val usize, + origin_y: val usize, + ) -> move NonNull { + CharGridCommand { + grid, + origin: Origin::new(origin_x, origin_y), + } + }; - /// Moves the provided [CharGrid] into a new [CharGridCommand], - /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> move NonNull { - grid.into() - }; + /// Moves the provided [CharGrid] into a new [CharGridCommand], + /// leaving other fields as their default values. + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() + }; + } ); diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 90b2d3d..8eeeb8e 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,38 +1,38 @@ use crate::{ commands::{wrap_command, wrap_origin_accessors}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; wrap_command!(Cp437Grid); - -wrap!(Cp437GridCommand; - prop grid: Cp437Grid { get mut; set move; }; -); - wrap_origin_accessors!(Cp437GridCommand); -wrap_functions!(associate Cp437GridCommand; - /// Show text on the screen. - /// - /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. - /// - /// The origin is relative to the top-left of the display. - fn new( - grid: move NonNull, - origin_x: val usize, - origin_y: val usize, - ) -> move NonNull { - Cp437GridCommand { - grid, - origin: Origin::new(origin_x, origin_y), - } - }; +wrap!( + Cp437GridCommand { + properties: + prop grid: Cp437Grid { get mut; set move; }; + functions: + /// Show text on the screen. + /// + /// The text is sent in the form of a 2D grid of [CP-437] encoded characters. + /// + /// The origin is relative to the top-left of the display. + fn new( + grid: move NonNull, + origin_x: val usize, + origin_y: val usize, + ) -> move NonNull { + Cp437GridCommand { + grid, + origin: Origin::new(origin_x, origin_y), + } + }; - /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], - /// leaving other fields as their default values. - fn from_grid(grid: move NonNull) -> move NonNull { - grid.into() - }; + /// Moves the provided [Cp437Grid] into a new [Cp437GridCommand], + /// leaving other fields as their default values. + fn from_grid(grid: move NonNull) -> move NonNull { + grid.into() + }; + } ); diff --git a/src/commands/generic_command.rs b/src/commands/generic_command.rs index 4f71dc4..2991352 100644 --- a/src/commands/generic_command.rs +++ b/src/commands/generic_command.rs @@ -1,5 +1,5 @@ use crate::{ - macros::{derive_clone, derive_free, wrap, wrap_functions}, + macros::{derive_clone, derive_free, wrap}, mem::{ heap_clone, heap_drop, heap_move, heap_move_nonnull, heap_move_ok, heap_remove, @@ -69,8 +69,6 @@ impl GenericCommand { }; } -derive_clone!(GenericCommand); - impl Clone for GenericCommand { fn clone(&self) -> Self { unsafe { @@ -144,8 +142,6 @@ impl Clone for GenericCommand { } } -derive_free!(GenericCommand); - impl Drop for GenericCommand { fn drop(&mut self) { unsafe { @@ -172,122 +168,126 @@ impl Drop for GenericCommand { } } -wrap_functions!(associate GenericCommand; - /// Tries to turn a [Packet] into a [GenericCommand]. - /// - /// The packet is dropped in the process. - /// - /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. - fn try_from_packet(packet: move NonNull) -> move NonNull { - servicepoint::TypedCommand::try_from(packet) - .map(|value| match value { - TypedCommand::Clear(clear) => GenericCommand { - tag: CommandTag::Clear, - data: CommandUnion { - clear: heap_move_nonnull(clear), - }, - }, - TypedCommand::CharGrid(char_grid) => GenericCommand { - tag: CommandTag::CharGrid, - data: CommandUnion { - char_grid: heap_move_nonnull(char_grid), - }, - }, - TypedCommand::Cp437Grid(cp437_grid) => GenericCommand { - tag: CommandTag::Cp437Grid, - data: CommandUnion { - cp437_grid: heap_move_nonnull(cp437_grid), - }, - }, - TypedCommand::Bitmap(bitmap) => GenericCommand { - tag: CommandTag::Bitmap, - data: CommandUnion { - bitmap: heap_move_nonnull(bitmap), - }, - }, - TypedCommand::Brightness(global_brightness) => GenericCommand { - tag: CommandTag::GlobalBrightness, - data: CommandUnion { - global_brightness: heap_move_nonnull(global_brightness), - }, - }, - TypedCommand::BrightnessGrid(brightness_grid) => GenericCommand { - tag: CommandTag::BrightnessGrid, - data: CommandUnion { - brightness_grid: heap_move_nonnull(brightness_grid), - }, - }, - TypedCommand::BitVec(bitvec) => GenericCommand { - tag: CommandTag::BitVec, - data: CommandUnion { - bit_vec: heap_move_nonnull(bitvec), - }, - }, - TypedCommand::HardReset(hard_reset) => GenericCommand { - tag: CommandTag::HardReset, - data: CommandUnion { - hard_reset: heap_move_nonnull(hard_reset), - }, - }, - TypedCommand::FadeOut(fade_out) => GenericCommand { - tag: CommandTag::FadeOut, - data: CommandUnion { - fade_out: heap_move_nonnull(fade_out), - }, - }, - #[allow(deprecated)] - TypedCommand::BitmapLegacy(bitmap_legacy) => GenericCommand { - tag: CommandTag::BitmapLegacy, - data: CommandUnion { - bitmap_legacy: heap_move_nonnull(bitmap_legacy), - }, - }, - }) - .unwrap_or_else(move |_| GenericCommand { - tag: CommandTag::Invalid, - data: CommandUnion { null: null_mut() }, - }) - }; -); +derive_clone!(GenericCommand); +derive_free!(GenericCommand); -wrap! { GenericCommand; - /// Tries to turn a [GenericCommand] into a [Packet]. - /// The [GenericCommand] gets consumed. - /// - /// Returns tag [CommandTag::Invalid] in case of an error. - method try_into_packet(move command) -> val *mut Packet { - match command.tag { - CommandTag::Invalid => null_mut(), - CommandTag::Bitmap => { - heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) +wrap! { + GenericCommand { + functions: + /// Tries to turn a [Packet] into a [GenericCommand]. + /// + /// The packet is dropped in the process. + /// + /// Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. + fn try_from_packet(packet: move NonNull) -> move NonNull { + servicepoint::TypedCommand::try_from(packet) + .map(|value| match value { + TypedCommand::Clear(clear) => GenericCommand { + tag: CommandTag::Clear, + data: CommandUnion { + clear: heap_move_nonnull(clear), + }, + }, + TypedCommand::CharGrid(char_grid) => GenericCommand { + tag: CommandTag::CharGrid, + data: CommandUnion { + char_grid: heap_move_nonnull(char_grid), + }, + }, + TypedCommand::Cp437Grid(cp437_grid) => GenericCommand { + tag: CommandTag::Cp437Grid, + data: CommandUnion { + cp437_grid: heap_move_nonnull(cp437_grid), + }, + }, + TypedCommand::Bitmap(bitmap) => GenericCommand { + tag: CommandTag::Bitmap, + data: CommandUnion { + bitmap: heap_move_nonnull(bitmap), + }, + }, + TypedCommand::Brightness(global_brightness) => GenericCommand { + tag: CommandTag::GlobalBrightness, + data: CommandUnion { + global_brightness: heap_move_nonnull(global_brightness), + }, + }, + TypedCommand::BrightnessGrid(brightness_grid) => GenericCommand { + tag: CommandTag::BrightnessGrid, + data: CommandUnion { + brightness_grid: heap_move_nonnull(brightness_grid), + }, + }, + TypedCommand::BitVec(bitvec) => GenericCommand { + tag: CommandTag::BitVec, + data: CommandUnion { + bit_vec: heap_move_nonnull(bitvec), + }, + }, + TypedCommand::HardReset(hard_reset) => GenericCommand { + tag: CommandTag::HardReset, + data: CommandUnion { + hard_reset: heap_move_nonnull(hard_reset), + }, + }, + TypedCommand::FadeOut(fade_out) => GenericCommand { + tag: CommandTag::FadeOut, + data: CommandUnion { + fade_out: heap_move_nonnull(fade_out), + }, + }, + #[allow(deprecated)] + TypedCommand::BitmapLegacy(bitmap_legacy) => GenericCommand { + tag: CommandTag::BitmapLegacy, + data: CommandUnion { + bitmap_legacy: heap_move_nonnull(bitmap_legacy), + }, + }, + }) + .unwrap_or_else(move |_| GenericCommand { + tag: CommandTag::Invalid, + data: CommandUnion { null: null_mut() }, + }) + }; + methods: + /// Tries to turn a [GenericCommand] into a [Packet]. + /// The [GenericCommand] gets consumed. + /// + /// Returns tag [CommandTag::Invalid] in case of an error. + fn try_into_packet(move command) -> val *mut Packet { + match command.tag { + CommandTag::Invalid => null_mut(), + CommandTag::Bitmap => { + heap_move_ok(unsafe { heap_remove(command.data.bitmap).try_into() }) + } + CommandTag::BitVec => { + heap_move_ok(unsafe { heap_remove(command.data.bit_vec).try_into() }) + } + CommandTag::BrightnessGrid => heap_move_ok(unsafe { + heap_remove(command.data.brightness_grid).try_into() + }), + CommandTag::CharGrid => heap_move_ok(unsafe { + heap_remove(command.data.char_grid).try_into() + }), + CommandTag::Cp437Grid => heap_move_ok(unsafe { + heap_remove(command.data.cp437_grid).try_into() + }), + CommandTag::GlobalBrightness => heap_move(unsafe { + heap_remove(command.data.global_brightness).into() + }), + CommandTag::Clear => { + heap_move(unsafe { heap_remove(command.data.clear).into() }) + } + CommandTag::HardReset => { + heap_move(unsafe { heap_remove(command.data.hard_reset).into() }) + } + CommandTag::FadeOut => { + heap_move(unsafe { heap_remove(command.data.fade_out).into() }) + } + CommandTag::BitmapLegacy => { + heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) + } } - CommandTag::BitVec => { - heap_move_ok(unsafe { heap_remove(command.data.bit_vec).try_into() }) - } - CommandTag::BrightnessGrid => heap_move_ok(unsafe { - heap_remove(command.data.brightness_grid).try_into() - }), - CommandTag::CharGrid => heap_move_ok(unsafe { - heap_remove(command.data.char_grid).try_into() - }), - CommandTag::Cp437Grid => heap_move_ok(unsafe { - heap_remove(command.data.cp437_grid).try_into() - }), - CommandTag::GlobalBrightness => heap_move(unsafe { - heap_remove(command.data.global_brightness).into() - }), - CommandTag::Clear => { - heap_move(unsafe { heap_remove(command.data.clear).into() }) - } - CommandTag::HardReset => { - heap_move(unsafe { heap_remove(command.data.hard_reset).into() }) - } - CommandTag::FadeOut => { - heap_move(unsafe { heap_remove(command.data.fade_out).into() }) - } - CommandTag::BitmapLegacy => { - heap_move(unsafe { heap_remove(command.data.bitmap_legacy).into() }) - } - } - }; + }; + } } diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 06377ae..8e988c1 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,21 +1,19 @@ -use crate::{ - commands::wrap_command, - macros::{wrap, wrap_functions}, -}; +use crate::{commands::wrap_command, macros::wrap}; use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; -wrap_functions!(associate GlobalBrightnessCommand; - /// Set the brightness of all tiles to the same value. - /// - /// Returns: a new [GlobalBrightnessCommand] instance. - fn new(brightness: val Brightness) -> move NonNull { - GlobalBrightnessCommand::from(brightness) - }; -); - wrap_command!(GlobalBrightness); -wrap!(GlobalBrightnessCommand; - prop brightness: Brightness { get; set; }; +wrap!( + GlobalBrightnessCommand { + properties: + prop brightness: Brightness { get; set; }; + functions: + /// Set the brightness of all tiles to the same value. + /// + /// Returns: a new [GlobalBrightnessCommand] instance. + fn new(brightness: val Brightness) -> move NonNull { + GlobalBrightnessCommand::from(brightness) + }; + } ); diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index acc959a..4de29db 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, @@ -10,91 +10,93 @@ use std::ptr::NonNull; wrap_grid!(Bitmap, bool); -wrap_functions!(associate Bitmap; - /// Creates a new [Bitmap] with the specified dimensions. - /// - /// # Arguments - /// - /// - `width`: size in pixels in x-direction - /// - `height`: size in pixels in y-direction - /// - /// returns: [Bitmap] initialized to all pixels off, or NULL in case of an error. - /// - /// # Errors - /// - /// In the following cases, this function will return NULL: - /// - /// - when the width is not dividable by 8 - /// - /// # Examples - /// - /// ```C - /// Cp437Grid grid = sp_bitmap_new(8, 3); - /// sp_bitmap_fill(grid, true); - /// sp_bitmap_set(grid, 0, 0, false); - /// sp_bitmap_free(grid); - /// ``` - fn new(width: val usize, height: val usize) -> move_some *mut Bitmap { - Bitmap::new(width, height) - }; +wrap! { + Bitmap { + functions: + /// Creates a new [Bitmap] with the specified dimensions. + /// + /// # Arguments + /// + /// - `width`: size in pixels in x-direction + /// - `height`: size in pixels in y-direction + /// + /// returns: [Bitmap] initialized to all pixels off, or NULL in case of an error. + /// + /// # Errors + /// + /// In the following cases, this function will return NULL: + /// + /// - when the width is not dividable by 8 + /// + /// # Examples + /// + /// ```C + /// Cp437Grid grid = sp_bitmap_new(8, 3); + /// sp_bitmap_fill(grid, true); + /// sp_bitmap_set(grid, 0, 0, false); + /// sp_bitmap_free(grid); + /// ``` + fn new(width: val usize, height: val usize) -> move_some *mut Bitmap { + Bitmap::new(width, height) + }; - /// Creates a new [Bitmap] with a size matching the screen. - /// - /// returns: [Bitmap] initialized to all pixels off. - fn new_max_sized() -> move NonNull { - Bitmap::max_sized() - }; + /// Creates a new [Bitmap] with a size matching the screen. + /// + /// returns: [Bitmap] initialized to all pixels off. + fn new_max_sized() -> move NonNull { + Bitmap::max_sized() + }; - /// Loads a [Bitmap] with the specified dimensions from the provided data. - /// - /// # Arguments - /// - /// - `width`: size in pixels in x-direction - /// - `height`: size in pixels in y-direction - /// - /// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. - fn load( - width: val usize, - height: val usize, - data: slice ByteSlice, - ) -> move_ok *mut Bitmap { - Bitmap::load(width, height, data) - }; + /// Loads a [Bitmap] with the specified dimensions from the provided data. + /// + /// # Arguments + /// + /// - `width`: size in pixels in x-direction + /// - `height`: size in pixels in y-direction + /// + /// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error. + fn load( + width: val usize, + height: val usize, + data: slice ByteSlice, + ) -> move_ok *mut Bitmap { + Bitmap::load(width, height, data) + }; - /// Tries to convert the BitVec to a Bitmap. - /// - /// The provided BitVec gets consumed. - /// - /// Returns NULL in case of error. - fn from_bitvec( - width: val usize, - bitvec: move NonNull, - ) -> move_ok *mut Bitmap { - Bitmap::from_bitvec(width, bitvec) - }; -); + /// Tries to convert the BitVec to a Bitmap. + /// + /// The provided BitVec gets consumed. + /// + /// Returns NULL in case of error. + fn from_bitvec( + width: val usize, + bitvec: move NonNull, + ) -> move_ok *mut Bitmap { + Bitmap::from_bitvec(width, bitvec) + }; -wrap!(Bitmap; - /// Consumes the Bitmap and returns the contained BitVec. - method into_bitvec(move bitmap) -> move NonNull { - bitmap.into() - }; + methods: + /// Consumes the Bitmap and returns the contained BitVec. + fn into_bitvec(move bitmap) -> move NonNull { + bitmap.into() + }; - /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. - /// - /// The provided [Bitmap] gets consumed. - /// - /// Returns NULL in case of an error. - method try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet { - Packet::try_from(BitmapCommand { - bitmap, - origin: Origin::new(x, y), - compression, - }) - }; + /// Creates a [BitmapCommand] and immediately turns that into a [Packet]. + /// + /// The provided [Bitmap] gets consumed. + /// + /// Returns NULL in case of an error. + fn try_into_packet(move bitmap, x: val usize, y: val usize, compression: val CompressionCode) -> move_ok *mut Packet { + Packet::try_from(BitmapCommand { + bitmap, + origin: Origin::new(x, y), + compression, + }) + }; - /// Gets an unsafe reference to the data of the [Bitmap] instance. - /// - /// The returned memory is valid for the lifetime of the bitmap. - method data_ref_mut(mut instance) -> slice ByteSlice; -); + /// Gets an unsafe reference to the data of the [Bitmap] instance. + /// + /// The returned memory is valid for the lifetime of the bitmap. + fn data_ref_mut(mut instance) -> slice ByteSlice; + } +} diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index c0627da..369501b 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_container, ByteSlice}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, @@ -9,93 +9,95 @@ use std::ptr::NonNull; wrap_container!(DisplayBitVec); -wrap_functions!(associate DisplayBitVec; - /// Creates a new [DisplayBitVec] instance. - /// - /// # Arguments - /// - /// - `size`: size in bits. - /// - /// returns: [DisplayBitVec] with all bits set to false. - /// - /// # Panics - /// - /// - when `size` is not divisible by 8. - fn new(size: val usize) -> move NonNull { - DisplayBitVec::repeat(false, size) - }; +wrap! { + DisplayBitVec { + functions: + /// Creates a new [DisplayBitVec] instance. + /// + /// # Arguments + /// + /// - `size`: size in bits. + /// + /// returns: [DisplayBitVec] with all bits set to false. + /// + /// # Panics + /// + /// - when `size` is not divisible by 8. + fn new(size: val usize) -> move NonNull { + DisplayBitVec::repeat(false, size) + }; - /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. - /// - /// returns: [DisplayBitVec] instance containing data. - fn load(data: slice ByteSlice) -> move NonNull { - DisplayBitVec::from_slice(data) - }; -); + /// Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. + /// + /// returns: [DisplayBitVec] instance containing data. + fn load(data: slice ByteSlice) -> move NonNull { + DisplayBitVec::from_slice(data) + }; -wrap!(DisplayBitVec; - /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. - /// - /// The provided [DisplayBitVec] gets consumed. - /// - /// Returns NULL in case of an error. - method try_into_packet( - move bitvec, - offset: val usize, - operation: val BinaryOperation, - compression: val CompressionCode - ) -> move_ok *mut Packet { - Packet::try_from(BitVecCommand { - bitvec, - offset, - operation, - compression, - }) - }; + methods: + /// Creates a [BitVecCommand] and immediately turns that into a [Packet]. + /// + /// The provided [DisplayBitVec] gets consumed. + /// + /// Returns NULL in case of an error. + fn try_into_packet( + move bitvec, + offset: val usize, + operation: val BinaryOperation, + compression: val CompressionCode + ) -> move_ok *mut Packet { + Packet::try_from(BitVecCommand { + bitvec, + offset, + operation, + compression, + }) + }; - /// Gets the value of a bit. - /// - /// # Arguments - /// - /// - `bit_vec`: instance to read from - /// - `index`: the bit index to read - /// - /// returns: value of the bit - /// - /// # Panics - /// - /// - when accessing `index` out of bounds - method get(ref instance, index: val usize) -> val bool { - instance.get(index).map(|x| *x).unwrap_or(false) - }; + /// Gets the value of a bit. + /// + /// # Arguments + /// + /// - `bit_vec`: instance to read from + /// - `index`: the bit index to read + /// + /// returns: value of the bit + /// + /// # Panics + /// + /// - when accessing `index` out of bounds + fn get(ref instance, index: val usize) -> val bool { + instance.get(index).map(|x| *x).unwrap_or(false) + }; - /// Sets the value of a bit. - /// - /// # Arguments - /// - /// - `index`: the bit index to edit - /// - `value`: the value to set the bit to - /// - /// # Panics - /// - /// - when accessing `index` out of bounds - method set(mut instance, index: val usize, value: val bool); + /// Sets the value of a bit. + /// + /// # Arguments + /// + /// - `index`: the bit index to edit + /// - `value`: the value to set the bit to + /// + /// # Panics + /// + /// - when accessing `index` out of bounds + fn set(mut instance, index: val usize, value: val bool); - /// Sets the value of all bits. - /// - /// # Arguments - /// - /// - `value`: the value to set all bits to - method fill(mut instance, value: val bool); + /// Sets the value of all bits. + /// + /// # Arguments + /// + /// - `value`: the value to set all bits to + fn fill(mut instance, value: val bool); - /// Gets the length in bits. - method len(ref instance) -> val usize; + /// Gets the length in bits. + fn len(ref instance) -> val usize; - /// Returns true if length is 0. - method is_empty(ref instance) -> val bool; + /// Returns true if length is 0. + fn is_empty(ref instance) -> val bool; - /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. - /// - /// The returned memory is valid for the lifetime of the bitvec. - method as_raw_mut_slice(mut instance) -> slice ByteSlice; -); + /// Gets an unsafe reference to the data of the [DisplayBitVec] instance. + /// + /// The returned memory is valid for the lifetime of the bitvec. + fn as_raw_mut_slice(mut instance) -> slice ByteSlice; + } +} diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index c6b7f13..8c74b25 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, @@ -10,66 +10,68 @@ use std::{mem::transmute, ptr::NonNull}; wrap_grid!(BrightnessGrid, Brightness); -wrap_functions!(associate BrightnessGrid; - /// Creates a new [BrightnessGrid] with the specified dimensions. - /// - /// returns: [BrightnessGrid] initialized to 0. - /// - /// # Examples - /// ```C - /// UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); - /// if (connection == NULL) - /// return 1; - /// - /// BrightnessGrid *grid = sp_brightness_grid_new(2, 2); - /// sp_brightness_grid_set(grid, 0, 0, 0); - /// sp_brightness_grid_set(grid, 1, 1, 10); - /// - /// TypedCommand *command = sp_command_char_brightness(grid); - /// sp_udp_free(connection); - /// ``` - fn new(width: val usize, height: val usize) -> move NonNull { - BrightnessGrid::new(width, height) - }; +wrap! { + BrightnessGrid { + functions: + /// Creates a new [BrightnessGrid] with the specified dimensions. + /// + /// returns: [BrightnessGrid] initialized to 0. + /// + /// # Examples + /// ```C + /// UdpSocket *connection = sp_udp_open("127.0.0.1:2342"); + /// if (connection == NULL) + /// return 1; + /// + /// BrightnessGrid *grid = sp_brightness_grid_new(2, 2); + /// sp_brightness_grid_set(grid, 0, 0, 0); + /// sp_brightness_grid_set(grid, 1, 1, 10); + /// + /// TypedCommand *command = sp_command_char_brightness(grid); + /// sp_udp_free(connection); + /// ``` + fn new(width: val usize, height: val usize) -> move NonNull { + BrightnessGrid::new(width, height) + }; - /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. - /// - /// Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN]. - /// - /// returns: new [BrightnessGrid] instance, or NULL in case of an error. - fn load( - width: val usize, - height: val usize, - data: slice ByteSlice, - ) -> move_some *mut BrightnessGrid { - ByteGrid::load(width, height, data) - .map(move |grid| grid.map(Brightness::saturating_from)) - }; -); + /// Loads a [BrightnessGrid] with the specified dimensions from the provided data. + /// + /// Any out of range values will be set to [Brightness::MAX] or [Brightness::MIN]. + /// + /// returns: new [BrightnessGrid] instance, or NULL in case of an error. + fn load( + width: val usize, + height: val usize, + data: slice ByteSlice, + ) -> move_some *mut BrightnessGrid { + ByteGrid::load(width, height, data) + .map(move |grid| grid.map(Brightness::saturating_from)) + }; -wrap!(BrightnessGrid; - /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. - /// - /// The provided [BrightnessGrid] gets consumed. - /// - /// Returns NULL in case of an error. - method try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { - Packet::try_from(BrightnessGridCommand { - grid, - origin: Origin::new(x, y), - }) - }; + methods: + /// Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [BrightnessGrid] gets consumed. + /// + /// Returns NULL in case of an error. + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(BrightnessGridCommand { + grid, + origin: Origin::new(x, y), + }) + }; - /// Gets an unsafe reference to the data of the instance. - /// - /// The returned memory is valid for the lifetime of the grid. - method data_ref_mut(mut instance) -> slice ByteSlice { - //noinspection RsAssertEqual - const _: () = assert!(size_of::() == 1); + /// Gets an unsafe reference to the data of the instance. + /// + /// The returned memory is valid for the lifetime of the grid. + fn data_ref_mut(mut instance) -> slice ByteSlice { + //noinspection RsAssertEqual + const _: () = assert!(size_of::() == 1); - let br_slice = instance.data_ref_mut(); - unsafe { - transmute::<&mut [Brightness], &mut [u8]>(br_slice) - } - }; -); + let br_slice = instance.data_ref_mut(); + unsafe { + transmute::<&mut [Brightness], &mut [u8]>(br_slice) + } + }; + } +} diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 4323021..4423071 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{derive_get_width_height, wrap_container, ByteSlice}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; @@ -8,81 +8,82 @@ use std::ptr::NonNull; wrap_container!(CharGrid); derive_get_width_height!(CharGrid); -wrap_functions!(associate CharGrid; - /// Creates a new [CharGrid] with the specified dimensions. - /// - /// returns: [CharGrid] initialized to 0. - /// - /// # Examples - /// - /// ```C - /// CharGrid grid = sp_char_grid_new(4, 3); - /// sp_char_grid_fill(grid, '?'); - /// sp_char_grid_set(grid, 0, 0, '!'); - /// sp_char_grid_free(grid); - /// ``` - fn new(width: val usize, height: val usize) -> move NonNull { - CharGrid::new(width, height) - }; +wrap! { + CharGrid { + functions: + /// Creates a new [CharGrid] with the specified dimensions. + /// + /// returns: [CharGrid] initialized to 0. + /// + /// # Examples + /// + /// ```C + /// CharGrid grid = sp_char_grid_new(4, 3); + /// sp_char_grid_fill(grid, '?'); + /// sp_char_grid_set(grid, 0, 0, '!'); + /// sp_char_grid_free(grid); + /// ``` + fn new(width: val usize, height: val usize) -> move NonNull { + CharGrid::new(width, height) + }; - /// Loads a [CharGrid] with the specified dimensions from the provided data. - /// - /// returns: new CharGrid or NULL in case of an error - fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid { - CharGrid::load_utf8(width, height, data.to_vec()) - }; -); + /// Loads a [CharGrid] with the specified dimensions from the provided data. + /// + /// returns: new CharGrid or NULL in case of an error + fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_ok *mut CharGrid { + CharGrid::load_utf8(width, height, data.to_vec()) + }; + methods: + /// Returns the current value at the specified position. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell to read + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + fn get(ref instance, x: val usize, y: val usize) -> val u32 { + instance.get(x, y) as u32 + }; -wrap!(CharGrid; - /// Returns the current value at the specified position. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell to read - /// - /// # Panics - /// - /// - when accessing `x` or `y` out of bounds - method get(ref instance, x: val usize, y: val usize) -> val u32 { - instance.get(x, y) as u32 - }; + /// Sets the value of the specified position in the grid. + /// + /// # Arguments + /// + /// - `x` and `y`: position of the cell + /// - `value`: the value to write to the cell + /// + /// returns: old value of the cell + /// + /// # Panics + /// + /// - when accessing `x` or `y` out of bounds + /// - when providing values that cannot be converted to Rust's `char`. + fn set(mut instance, x: val usize, y: val usize, value: val u32) { + instance.set(x, y, char::from_u32(value).unwrap()) + }; - /// Sets the value of the specified position in the grid. - /// - /// # Arguments - /// - /// - `x` and `y`: position of the cell - /// - `value`: the value to write to the cell - /// - /// returns: old value of the cell - /// - /// # Panics - /// - /// - when accessing `x` or `y` out of bounds - /// - when providing values that cannot be converted to Rust's `char`. - method set(mut instance, x: val usize, y: val usize, value: val u32) { - instance.set(x, y, char::from_u32(value).unwrap()) - }; + /// Sets the value of all cells in the grid. + /// + /// # Arguments + /// + /// - `value`: the value to set all cells to + /// - when providing values that cannot be converted to Rust's `char`. + fn fill(mut instance, value: val u32) { + instance.fill(char::from_u32(value).unwrap()) + }; - /// Sets the value of all cells in the grid. - /// - /// # Arguments - /// - /// - `value`: the value to set all cells to - /// - when providing values that cannot be converted to Rust's `char`. - method fill(mut instance, value: val u32) { - instance.fill(char::from_u32(value).unwrap()) - }; - - /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. - /// - /// The provided [CharGrid] gets consumed. - /// - /// Returns NULL in case of an error. - method try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { - Packet::try_from(CharGridCommand { - grid, - origin: Origin::new(x, y), - }) - }; -); + /// Creates a [CharGridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [CharGrid] gets consumed. + /// + /// Returns NULL in case of an error. + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(CharGridCommand { + grid, + origin: Origin::new(x, y), + }) + }; + } +} diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index 2a157c2..a6dbeea 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,6 +1,6 @@ use crate::{ containers::{wrap_grid, ByteSlice}, - macros::{wrap, wrap_functions}, + macros::wrap, }; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, @@ -9,35 +9,37 @@ use std::ptr::NonNull; wrap_grid!(Cp437Grid, u8); -wrap_functions!(associate Cp437Grid; - /// Creates a new [Cp437Grid] with the specified dimensions. - /// - /// returns: [Cp437Grid] initialized to 0. - fn new(width: val usize, height: val usize) -> move NonNull { - Cp437Grid::new(width, height) - }; +wrap! { + Cp437Grid { + functions: + /// Creates a new [Cp437Grid] with the specified dimensions. + /// + /// returns: [Cp437Grid] initialized to 0. + fn new(width: val usize, height: val usize) -> move NonNull { + Cp437Grid::new(width, height) + }; - /// Loads a [Cp437Grid] with the specified dimensions from the provided data. - fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_some *mut Cp437Grid { - Cp437Grid::load(width, height, data) - }; -); + /// Loads a [Cp437Grid] with the specified dimensions from the provided data. + fn load(width: val usize, height: val usize, data: slice ByteSlice) -> move_some *mut Cp437Grid { + Cp437Grid::load(width, height, data) + }; -wrap!(Cp437Grid; - /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. - /// - /// The provided [Cp437Grid] gets consumed. - /// - /// Returns NULL in case of an error. - method try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { - Packet::try_from(Cp437GridCommand { - grid, - origin: Origin::new(x, y), - }) - }; + methods: + /// Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. + /// + /// The provided [Cp437Grid] gets consumed. + /// + /// Returns NULL in case of an error. + fn try_into_packet(move grid, x: val usize, y: val usize) -> move_ok *mut Packet { + Packet::try_from(Cp437GridCommand { + grid, + origin: Origin::new(x, y), + }) + }; - /// Gets an unsafe reference to the data of the grid. - /// - /// The returned memory is valid for the lifetime of the instance. - method data_ref_mut(mut instance) -> slice ByteSlice; -); + /// Gets an unsafe reference to the data of the grid. + /// + /// The returned memory is valid for the lifetime of the instance. + fn data_ref_mut(mut instance) -> slice ByteSlice; + } +} diff --git a/src/macros.rs b/src/macros.rs index ebcad75..4149873 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -265,30 +265,54 @@ macro_rules! wrap_functions { macro_rules! wrap { ( - $object_type:ident; - $( - prop $prop_name:ident : $prop_type:ty { $($accessor:ident $($modifier:ident)?;)+ }; - )* - $( - $(#[$meta:meta])+ - method $function:ident($ref_or_mut:ident $instance:ident $(, $($param_name:ident: $param_modifier:ident $param_type:ty),*)?) - $(-> $return_modifier:ident $return_type:ty)? - $($impl:block)?; - )* + $object_type:ident { + $( + properties: + $( + prop $prop_name:ident : $prop_type:ty { $($accessor:ident $($modifier:ident)?;)+ }; + )* + )? + $( + functions: + $( + $(#[$fn_meta:meta])+ + fn $fn_name:ident($($fn_param_name:ident: $fn_param_modifier:ident $fn_param_type:ty),*$(,)?) + $(-> $fn_return_modifier:ident $fn_return_type:ty)? + $fn_block:block; + )* + )? + $( + methods: + $( + $(#[$method_meta:meta])+ + fn $method_name:ident($method_instance_modifier:ident $method_instance:ident $(, $($method_param_name:ident: $method_param_modifier:ident $method_param_type:ty),*)?) + $(-> $method_return_modifier:ident $method_return_type:ty)? + $($method_impl:block)?; + )* + )? + } ) => { - $( + $($( $crate::macros::wrap_fields!($object_type; prop $prop_name : $prop_type { $($accessor $($modifier)?;)+ }; ); - )* - $( - $crate::macros::wrap_method!($object_type; - $(#[$meta])+ - fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) - $(-> $return_modifier $return_type)? - $($impl)?; + )*)? + $($( + $crate::macros::wrap_functions!(associate $object_type; + $(#[$fn_meta])+ + fn $fn_name($($fn_param_name: $fn_param_modifier $fn_param_type),*) + $(-> $fn_return_modifier $fn_return_type)? + $fn_block; ); - )* + )*)? + $($( + $crate::macros::wrap_method!($object_type; + $(#[$method_meta])+ + fn $method_name($method_instance_modifier $method_instance $(, $($method_param_name: $method_param_modifier $method_param_type),*)?) + $(-> $method_return_modifier $method_return_type)? + $($method_impl)?; + ); + )*)? }; } diff --git a/src/packet.rs b/src/packet.rs index 71c746d..00a34a9 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -5,71 +5,71 @@ use crate::{ use servicepoint::{CommandCode, Header, Packet}; use std::ptr::NonNull; -wrap_functions!(associate Packet; - /// Tries to load a [Packet] from the passed array with the specified length. - /// - /// returns: NULL in case of an error, pointer to the allocated packet otherwise - fn try_load(data: slice ByteSlice) -> move_ok *mut Packet { - servicepoint::Packet::try_from(data) - }; - - /// Creates a raw [Packet] from parts. - /// - /// returns: new instance. Will never return null. - fn from_parts(header: val Header, payload: val ByteSlice) -> move NonNull { - let payload = if payload == ByteSlice::INVALID { - None - } else { - Some(Vec::from(unsafe { payload.as_slice() })) - }; - - Packet { header, payload } - }; -); - derive_clone!(Packet); derive_free!(Packet); -wrap! {Packet; - prop header: Header { get; get mut; set; }; +wrap! { + Packet { + properties: + prop header: Header { get; get mut; set; }; + functions: + /// Tries to load a [Packet] from the passed array with the specified length. + /// + /// returns: NULL in case of an error, pointer to the allocated packet otherwise + fn try_load(data: slice ByteSlice) -> move_ok *mut Packet { + servicepoint::Packet::try_from(data) + }; - /// Returns a pointer to the current payload of the provided packet. - /// - /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. - /// - /// The returned memory can be changed and will be valid until a new payload is set. - method get_payload(mut packet) -> val ByteSlice { - match &mut packet.payload { - None => ByteSlice::INVALID, - Some(payload) => unsafe { ByteSlice::from_slice(payload) }, - } - }; + /// Creates a raw [Packet] from parts. + /// + /// returns: new instance. Will never return null. + fn from_parts(header: val Header, payload: val ByteSlice) -> move NonNull { + let payload = if payload == ByteSlice::INVALID { + None + } else { + Some(Vec::from(unsafe { payload.as_slice() })) + }; - /// Sets the payload of the provided packet to the provided data. - /// - /// This makes previous payload pointers invalid. - method set_payload(mut packet, data: val ByteSlice) { - packet.payload = if data == ByteSlice::INVALID { - None - } else { - Some(unsafe { data.as_slice().to_vec() }) - } - }; + Packet { header, payload } + }; + methods: + /// Returns a pointer to the current payload of the provided packet. + /// + /// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. + /// + /// The returned memory can be changed and will be valid until a new payload is set. + fn get_payload(mut packet) -> val ByteSlice { + match &mut packet.payload { + None => ByteSlice::INVALID, + Some(payload) => unsafe { ByteSlice::from_slice(payload) }, + } + }; - /// Serialize the packet into the provided buffer. - /// - /// # Panics - /// - /// - if the buffer is not big enough to hold header+payload. - method serialize_to(mut packet, buffer: val ByteSlice) -> val usize { - unsafe { - packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) - } - }; + /// Sets the payload of the provided packet to the provided data. + /// + /// This makes previous payload pointers invalid. + fn set_payload(mut packet, data: val ByteSlice) { + packet.payload = if data == ByteSlice::INVALID { + None + } else { + Some(unsafe { data.as_slice().to_vec() }) + } + }; + + /// Serialize the packet into the provided buffer. + /// + /// # Panics + /// + /// - if the buffer is not big enough to hold header+payload. + fn serialize_to(mut packet, buffer: val ByteSlice) -> val usize { + unsafe { + packet.serialize_to(buffer.as_slice_mut()).unwrap_or(0) + } + }; + } } wrap_functions!(sp; - /// Converts u16 into [CommandCode]. /// /// If the provided value is not valid, false is returned and result is not changed. diff --git a/src/udp.rs b/src/udp.rs index 4fce7c0..22a7f7c 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::{ commands::{CommandTag, GenericCommand}, - macros::{derive_free, wrap, wrap_functions}, + macros::{derive_free, wrap}, mem::heap_remove, }; use servicepoint::{Header, Packet, UdpSocketExt}; @@ -12,99 +12,100 @@ use std::{ derive_free!(UdpSocket); -wrap_functions!(associate UdpSocket; - /// Creates a new instance of [UdpSocket]. - /// - /// returns: NULL if connection fails, or connected instance - /// - /// # Examples - /// - /// ```C - /// UdpSocket connection = sp_udp_open("172.23.42.29:2342"); - /// if (connection != NULL) - /// sp_udp_send_command(connection, sp_command_clear()); - /// ``` - fn open(host: val NonNull) -> move_ok *mut UdpSocket { - let host = unsafe { CStr::from_ptr(host.as_ptr()) } - .to_str() - .expect("Bad encoding"); - UdpSocket::bind_connect(host) - }; - - /// Creates a new instance of [UdpSocket]. - /// - /// returns: NULL if connection fails, or connected instance - /// - /// # Examples - /// - /// ```C - /// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); - /// if (connection != NULL) - /// sp_udp_send_command(connection, sp_command_clear()); - /// ``` - fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> move_ok *mut UdpSocket { - let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); - UdpSocket::bind_connect(addr) - }; -); - -wrap! {UdpSocket; - /// Sends a [Packet] to the display using the [UdpSocket]. - /// - /// The passed `packet` gets consumed. - /// - /// returns: true in case of success - method send_packet(ref connection, packet: move NonNull) -> val bool { - connection.send(&Vec::from(packet)).is_ok() - }; - - /// Sends a [GenericCommand] to the display using the [UdpSocket]. - /// - /// The passed `command` gets consumed. - /// - /// returns: true in case of success - /// - /// # Examples - /// - /// ```C - /// sp_udp_send_command(connection, sp_command_brightness(5)); - /// ``` - method send_command(ref connection, command: mut NonNull) -> val bool { - unsafe { - let result = match command.tag { - CommandTag::Invalid => return false, - CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), - CommandTag::BitVec => connection.send_command(heap_remove(command.data.bit_vec)), - CommandTag::BrightnessGrid => connection.send_command(heap_remove(command.data.brightness_grid)), - CommandTag::CharGrid => connection.send_command(heap_remove(command.data.char_grid)), - CommandTag::Cp437Grid => connection.send_command(heap_remove(command.data.cp437_grid)), - CommandTag::GlobalBrightness => connection.send_command(heap_remove(command.data.global_brightness)), - CommandTag::Clear => connection.send_command(heap_remove(command.data.clear)), - CommandTag::HardReset => connection.send_command(heap_remove(command.data.hard_reset)), - CommandTag::FadeOut => connection.send_command(heap_remove(command.data.fade_out)), - CommandTag::BitmapLegacy => connection.send_command(heap_remove(command.data.bitmap_legacy)), - }.is_some(); - *command = GenericCommand::INVALID; - result - } - }; - - /// Sends a [Header] to the display using the [UdpSocket]. - /// - /// returns: true in case of success - /// - /// # Examples - /// - /// ```C - /// sp_udp_send_header(connection, sp_command_brightness(5)); - /// ``` - method send_header(ref udp_connection, header: val Header) -> val bool { - let packet = Packet { - header, - payload: None, +wrap! { + UdpSocket { + functions: + /// Creates a new instance of [UdpSocket]. + /// + /// returns: NULL if connection fails, or connected instance + /// + /// # Examples + /// + /// ```C + /// UdpSocket connection = sp_udp_open("172.23.42.29:2342"); + /// if (connection != NULL) + /// sp_udp_send_command(connection, sp_command_clear()); + /// ``` + fn open(host: val NonNull) -> move_ok *mut UdpSocket { + let host = unsafe { CStr::from_ptr(host.as_ptr()) } + .to_str() + .expect("Bad encoding"); + UdpSocket::bind_connect(host) }; - udp_connection.send(&Vec::from(packet)).is_ok() - }; + + /// Creates a new instance of [UdpSocket]. + /// + /// returns: NULL if connection fails, or connected instance + /// + /// # Examples + /// + /// ```C + /// UdpSocket connection = sp_udp_open_ipv4(172, 23, 42, 29, 2342); + /// if (connection != NULL) + /// sp_udp_send_command(connection, sp_command_clear()); + /// ``` + fn open_ipv4(ip1: val u8, ip2: val u8, ip3: val u8, ip4: val u8, port: val u16) -> move_ok *mut UdpSocket { + let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port); + UdpSocket::bind_connect(addr) + }; + methods: + /// Sends a [Packet] to the display using the [UdpSocket]. + /// + /// The passed `packet` gets consumed. + /// + /// returns: true in case of success + fn send_packet(ref connection, packet: move NonNull) -> val bool { + connection.send(&Vec::from(packet)).is_ok() + }; + + /// Sends a [GenericCommand] to the display using the [UdpSocket]. + /// + /// The passed `command` gets consumed. + /// + /// returns: true in case of success + /// + /// # Examples + /// + /// ```C + /// sp_udp_send_command(connection, sp_command_brightness(5)); + /// ``` + fn send_command(ref connection, command: mut NonNull) -> val bool { + unsafe { + let result = match command.tag { + CommandTag::Invalid => return false, + CommandTag::Bitmap => connection.send_command(heap_remove(command.data.bitmap)), + CommandTag::BitVec => connection.send_command(heap_remove(command.data.bit_vec)), + CommandTag::BrightnessGrid => connection.send_command(heap_remove(command.data.brightness_grid)), + CommandTag::CharGrid => connection.send_command(heap_remove(command.data.char_grid)), + CommandTag::Cp437Grid => connection.send_command(heap_remove(command.data.cp437_grid)), + CommandTag::GlobalBrightness => connection.send_command(heap_remove(command.data.global_brightness)), + CommandTag::Clear => connection.send_command(heap_remove(command.data.clear)), + CommandTag::HardReset => connection.send_command(heap_remove(command.data.hard_reset)), + CommandTag::FadeOut => connection.send_command(heap_remove(command.data.fade_out)), + CommandTag::BitmapLegacy => connection.send_command(heap_remove(command.data.bitmap_legacy)), + }.is_some(); + *command = GenericCommand::INVALID; + result + } + }; + + /// Sends a [Header] to the display using the [UdpSocket]. + /// + /// returns: true in case of success + /// + /// # Examples + /// + /// ```C + /// sp_udp_send_header(connection, sp_command_brightness(5)); + /// ``` + fn send_header(ref udp_connection, header: val Header) -> val bool { + let packet = Packet { + header, + payload: None, + }; + udp_connection.send(&Vec::from(packet)).is_ok() + }; + } } mod _hidden { From 363609c663cbdb45a9c10e1a8b60f7e40a32af97 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Thu, 26 Jun 2025 23:22:18 +0200 Subject: [PATCH 69/76] add derives --- src/commands/bitmap_command.rs | 9 ++--- src/commands/bitvec_command.rs | 5 ++- src/commands/brightness_grid_command.rs | 9 ++--- src/commands/cc_only_commands.rs | 4 +-- src/commands/char_grid_command.rs | 9 ++--- src/commands/cp437_grid_command.rs | 9 ++--- src/commands/global_brightness_command.rs | 5 ++- src/commands/mod.rs | 17 ++++------ src/containers/bitmap.rs | 8 ++--- src/containers/bitvec.rs | 8 ++--- src/containers/brightness_grid.rs | 8 ++--- src/containers/char_grid.rs | 9 ++--- src/containers/cp437_grid.rs | 8 ++--- src/containers/mod.rs | 7 ++-- src/macros.rs | 41 +++++++++++++---------- 15 files changed, 55 insertions(+), 101 deletions(-) diff --git a/src/commands/bitmap_command.rs b/src/commands/bitmap_command.rs index 2cb86f0..d325bce 100644 --- a/src/commands/bitmap_command.rs +++ b/src/commands/bitmap_command.rs @@ -1,15 +1,10 @@ -use crate::{ - commands::{wrap_command, wrap_origin_accessors}, - macros::wrap, -}; +use crate::macros::wrap; use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin}; use std::ptr::NonNull; -wrap_command!(Bitmap); -wrap_origin_accessors!(BitmapCommand); - wrap! { BitmapCommand { + derives: crate::commands::derive_command[Bitmap], crate::commands::derive_origin_accessors; properties: prop bitmap: Bitmap { get mut; set move; }; prop compression: CompressionCode { get; set; }; diff --git a/src/commands/bitvec_command.rs b/src/commands/bitvec_command.rs index 3b239aa..82b92fb 100644 --- a/src/commands/bitvec_command.rs +++ b/src/commands/bitvec_command.rs @@ -1,13 +1,12 @@ -use crate::{commands::wrap_command, macros::wrap}; +use crate::macros::wrap; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Offset, }; use std::ptr::NonNull; -wrap_command!(BitVec); - wrap!( BitVecCommand { + derives: crate::commands::derive_command[BitVec]; properties: prop bitvec: DisplayBitVec { get mut; set move; }; prop offset: Offset { get; set; }; diff --git a/src/commands/brightness_grid_command.rs b/src/commands/brightness_grid_command.rs index cbf22bb..ef568bd 100644 --- a/src/commands/brightness_grid_command.rs +++ b/src/commands/brightness_grid_command.rs @@ -1,15 +1,10 @@ -use crate::{ - commands::{wrap_command, wrap_origin_accessors}, - macros::wrap, -}; +use crate::macros::wrap; use servicepoint::{BrightnessGrid, BrightnessGridCommand, Origin}; use std::ptr::NonNull; -wrap_command!(BrightnessGrid); -wrap_origin_accessors!(BrightnessGridCommand); - wrap!( BrightnessGridCommand { + derives: crate::commands::derive_command[BrightnessGrid], crate::commands::derive_origin_accessors; properties: prop grid: BrightnessGrid { get mut; set move; }; functions: diff --git a/src/commands/cc_only_commands.rs b/src/commands/cc_only_commands.rs index 3fa97c7..22a4a3b 100644 --- a/src/commands/cc_only_commands.rs +++ b/src/commands/cc_only_commands.rs @@ -1,13 +1,11 @@ -use crate::commands::wrap_command; use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; macro_rules! wrap_cc_only { ($(#[$meta:meta])* $command:ident) => { ::paste::paste!{ - wrap_command!($command); - $crate::macros::wrap!{ [< $command Command >] { + derives: $crate::commands::derive_command[$command]; functions: $(#[$meta])* /// diff --git a/src/commands/char_grid_command.rs b/src/commands/char_grid_command.rs index cbb91e6..96d8b90 100644 --- a/src/commands/char_grid_command.rs +++ b/src/commands/char_grid_command.rs @@ -1,15 +1,10 @@ -use crate::{ - commands::{wrap_command, wrap_origin_accessors}, - macros::wrap, -}; +use crate::macros::wrap; use servicepoint::{CharGrid, CharGridCommand, Origin}; use std::ptr::NonNull; -wrap_command!(CharGrid); -wrap_origin_accessors!(CharGridCommand); - wrap!( CharGridCommand { + derives: crate::commands::derive_command[CharGrid], crate::commands::derive_origin_accessors; properties: prop grid: CharGrid { get mut; set move; }; functions: diff --git a/src/commands/cp437_grid_command.rs b/src/commands/cp437_grid_command.rs index 8eeeb8e..19b35f6 100644 --- a/src/commands/cp437_grid_command.rs +++ b/src/commands/cp437_grid_command.rs @@ -1,15 +1,10 @@ -use crate::{ - commands::{wrap_command, wrap_origin_accessors}, - macros::wrap, -}; +use crate::macros::wrap; use servicepoint::{Cp437Grid, Cp437GridCommand, Origin}; use std::ptr::NonNull; -wrap_command!(Cp437Grid); -wrap_origin_accessors!(Cp437GridCommand); - wrap!( Cp437GridCommand { + derives: crate::commands::derive_command[Cp437Grid], crate::commands::derive_origin_accessors; properties: prop grid: Cp437Grid { get mut; set move; }; functions: diff --git a/src/commands/global_brightness_command.rs b/src/commands/global_brightness_command.rs index 8e988c1..1ab9a1b 100644 --- a/src/commands/global_brightness_command.rs +++ b/src/commands/global_brightness_command.rs @@ -1,11 +1,10 @@ -use crate::{commands::wrap_command, macros::wrap}; +use crate::macros::wrap; use servicepoint::{Brightness, GlobalBrightnessCommand}; use std::ptr::NonNull; -wrap_command!(GlobalBrightness); - wrap!( GlobalBrightnessCommand { + derives: crate::commands::derive_command[GlobalBrightness]; properties: prop brightness: Brightness { get; set; }; functions: diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 1d6e8ff..e3e3715 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -14,8 +14,8 @@ pub use char_grid_command::*; pub use cp437_grid_command::*; pub use generic_command::*; -macro_rules! wrap_origin_accessors { - ( $object_type:ident ) => { +macro_rules! derive_origin_accessors { + ($object_type:ident) => { ::paste::paste! { $crate::macros::wrap_methods!($object_type; #[doc = " Reads the origin field of the [`" $object_type "`]."] @@ -70,21 +70,16 @@ macro_rules! derive_command_into_packet { } } -macro_rules! wrap_command { - ($command:ident, $object_type:ident) => { +macro_rules! derive_command { + ($object_type:ident, $command:ident) => { $crate::macros::derive_clone!($object_type); $crate::macros::derive_free!($object_type); $crate::commands::derive_command_from!($command); $crate::commands::derive_command_into_packet!($object_type); }; - ($command:ident) => { - ::paste::paste! { - $crate::commands::wrap_command!($command, [< $command Command >]); - } - }; } pub(crate) use { - derive_command_from, derive_command_into_packet, wrap_command, - wrap_origin_accessors, + derive_command, derive_command_from, derive_command_into_packet, + derive_origin_accessors, }; diff --git a/src/containers/bitmap.rs b/src/containers/bitmap.rs index 4de29db..5974016 100644 --- a/src/containers/bitmap.rs +++ b/src/containers/bitmap.rs @@ -1,17 +1,13 @@ -use crate::{ - containers::{wrap_grid, ByteSlice}, - macros::wrap, -}; +use crate::{containers::ByteSlice, macros::wrap}; use servicepoint::{ Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, Origin, Packet, }; use std::ptr::NonNull; -wrap_grid!(Bitmap, bool); - wrap! { Bitmap { + derives: crate::containers::derive_container, crate::containers::derive_grid[bool]; functions: /// Creates a new [Bitmap] with the specified dimensions. /// diff --git a/src/containers/bitvec.rs b/src/containers/bitvec.rs index 369501b..57e4fd8 100644 --- a/src/containers/bitvec.rs +++ b/src/containers/bitvec.rs @@ -1,16 +1,12 @@ -use crate::{ - containers::{wrap_container, ByteSlice}, - macros::wrap, -}; +use crate::{containers::ByteSlice, macros::wrap}; use servicepoint::{ BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, }; use std::ptr::NonNull; -wrap_container!(DisplayBitVec); - wrap! { DisplayBitVec { + derives: crate::containers::derive_container; functions: /// Creates a new [DisplayBitVec] instance. /// diff --git a/src/containers/brightness_grid.rs b/src/containers/brightness_grid.rs index 8c74b25..3b3fabb 100644 --- a/src/containers/brightness_grid.rs +++ b/src/containers/brightness_grid.rs @@ -1,17 +1,13 @@ -use crate::{ - containers::{wrap_grid, ByteSlice}, - macros::wrap, -}; +use crate::{containers::ByteSlice, macros::wrap}; use servicepoint::{ Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, Origin, Packet, }; use std::{mem::transmute, ptr::NonNull}; -wrap_grid!(BrightnessGrid, Brightness); - wrap! { BrightnessGrid { + derives: crate::containers::derive_container, crate::containers::derive_grid[Brightness]; functions: /// Creates a new [BrightnessGrid] with the specified dimensions. /// diff --git a/src/containers/char_grid.rs b/src/containers/char_grid.rs index 4423071..8946b50 100644 --- a/src/containers/char_grid.rs +++ b/src/containers/char_grid.rs @@ -1,15 +1,10 @@ -use crate::{ - containers::{derive_get_width_height, wrap_container, ByteSlice}, - macros::wrap, -}; +use crate::{containers::ByteSlice, macros::wrap}; use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; use std::ptr::NonNull; -wrap_container!(CharGrid); -derive_get_width_height!(CharGrid); - wrap! { CharGrid { + derives: crate::containers::derive_container, crate::containers::derive_get_width_height; functions: /// Creates a new [CharGrid] with the specified dimensions. /// diff --git a/src/containers/cp437_grid.rs b/src/containers/cp437_grid.rs index a6dbeea..b67d1ae 100644 --- a/src/containers/cp437_grid.rs +++ b/src/containers/cp437_grid.rs @@ -1,16 +1,12 @@ -use crate::{ - containers::{wrap_grid, ByteSlice}, - macros::wrap, -}; +use crate::{containers::ByteSlice, macros::wrap}; use servicepoint::{ Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, }; use std::ptr::NonNull; -wrap_grid!(Cp437Grid, u8); - wrap! { Cp437Grid { + derives: crate::containers::derive_container, crate::containers::derive_grid[u8]; functions: /// Creates a new [Cp437Grid] with the specified dimensions. /// diff --git a/src/containers/mod.rs b/src/containers/mod.rs index 608d038..5e0b48d 100644 --- a/src/containers/mod.rs +++ b/src/containers/mod.rs @@ -12,7 +12,7 @@ pub use byte_slice::*; pub use char_grid::*; pub use cp437_grid::*; -macro_rules! wrap_container { +macro_rules! derive_container { ($object_type:ident) => { $crate::macros::derive_clone!($object_type); $crate::macros::derive_free!($object_type); @@ -31,9 +31,8 @@ macro_rules! derive_get_width_height { }; } -macro_rules! wrap_grid { +macro_rules! derive_grid { ($object_type:ident, $value_type:ident) => { - $crate::containers::wrap_container!($object_type); $crate::containers::derive_get_width_height!($object_type); $crate::macros::wrap_methods! {$object_type; /// Gets the current value at the specified position. @@ -69,7 +68,7 @@ macro_rules! wrap_grid { }; } -pub(crate) use {derive_get_width_height, wrap_container, wrap_grid}; +pub(crate) use {derive_container, derive_get_width_height, derive_grid}; mod _hidden { /// This is a type only used by cbindgen to have a type for pointers. diff --git a/src/macros.rs b/src/macros.rs index 4149873..5ddb7f9 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,27 +1,23 @@ macro_rules! derive_free { - ($typ:ident) => { - ::paste::paste! { - $crate::macros::wrap_method!($typ; - #[doc = "Deallocates a [`" $typ "`] instance."] - #[allow(dropping_copy_types)] - fn free(move instance) { - ::std::mem::drop(instance) - }; - ); - } + ($object_type:ident) => { + $crate::macros::wrap_method!($object_type; + #[doc = concat!("Deallocates a [`", stringify!($object_type), "`] instance.")] + #[allow(dropping_copy_types)] + fn free(move instance) { + ::std::mem::drop(instance) + }; + ); }; } macro_rules! derive_clone { ($object_type:ident) => { - ::paste::paste! { - $crate::macros::wrap_method!($object_type; - #[doc = "Clones a [`" $object_type "`] instance."] - fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type> { - instance.clone() - }; - ); - } + $crate::macros::wrap_method!($object_type; + #[doc = concat!("Clones a [`", stringify!($object_type), "`] instance.")] + fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type> { + instance.clone() + }; + ); }; } @@ -266,6 +262,12 @@ macro_rules! wrap_functions { macro_rules! wrap { ( $object_type:ident { + $( + derives: + $( + $derive:path $( [ $( $derive_arg:tt ),+ ] )? + ),+; + )? $( properties: $( @@ -292,6 +294,9 @@ macro_rules! wrap { )? } ) => { + $($( + $derive!($object_type $(, $($derive_arg),+)?); + )+)? $($( $crate::macros::wrap_fields!($object_type; prop $prop_name : $prop_type { $($accessor $($modifier)?;)+ }; From 5b26929ffae100bd5d432ed8603c3e4b98290d36 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 27 Jun 2025 18:06:03 +0200 Subject: [PATCH 70/76] remove now unnecesary build dep, update servicepoint --- Cargo.lock | 218 +++-------------------------------------------------- Cargo.toml | 5 +- 2 files changed, 10 insertions(+), 213 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4f755c..e18fa74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,25 +104,6 @@ dependencies = [ "pkg-config", ] -[[package]] -name = "cbindgen" -version = "0.29.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "975982cdb7ad6a142be15bdf84aea7ec6a9e5d4d797c004d43185b24cfe4e684" -dependencies = [ - "clap", - "heck", - "indexmap", - "log", - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn", - "tempfile", - "toml", -] - [[package]] name = "cc" version = "1.2.24" @@ -140,33 +121,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "clap" -version = "4.5.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed93b9805f8ba930df42c2590f05453d5ec36cbb85d018868a5b24d31f6ac000" -dependencies = [ - "clap_builder", -] - -[[package]] -name = "clap_builder" -version = "4.5.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "379026ff283facf611b0ea629334361c4211d1b12ee01024eec1591133b04120" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_lex" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" - [[package]] name = "colorchoice" version = "1.0.3" @@ -205,28 +159,6 @@ dependencies = [ "log", ] -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - -[[package]] -name = "errno" -version = "0.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - [[package]] name = "flate2" version = "1.1.1" @@ -256,25 +188,14 @@ dependencies = [ ] [[package]] -name = "hashbrown" -version = "0.15.3" +name = "inherent" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "indexmap" -version = "2.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" +checksum = "6c38228f24186d9cc68c729accb4d413be9eaed6ad07ff79e0270d9e56f3de13" dependencies = [ - "equivalent", - "hashbrown", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -283,12 +204,6 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" -[[package]] -name = "itoa" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" - [[package]] name = "jiff" version = "0.2.14" @@ -329,12 +244,6 @@ version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" -[[package]] -name = "linux-raw-sys" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" - [[package]] name = "log" version = "0.4.27" @@ -464,25 +373,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "rustix" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - [[package]] name = "serde" version = "1.0.219" @@ -503,36 +393,16 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_json" -version = "1.0.140" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "serde_spanned" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" -dependencies = [ - "serde", -] - [[package]] name = "servicepoint" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91a33bff7f9db5008748b23ca0c906c276fe00694390b681f004a55968a42cfe" +checksum = "2800caad491cb44f67e5dd5b8c61ece368eecfe588155d03c7d9864acbad6919" dependencies = [ "bitvec", "bzip2", "flate2", + "inherent", "log", "once_cell", "rust-lzma", @@ -544,7 +414,6 @@ dependencies = [ name = "servicepoint_binding_c" version = "0.15.0" dependencies = [ - "cbindgen", "env_logger", "paste", "servicepoint", @@ -556,12 +425,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - [[package]] name = "syn" version = "2.0.101" @@ -579,19 +442,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" -[[package]] -name = "tempfile" -version = "3.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" -dependencies = [ - "fastrand", - "getrandom", - "once_cell", - "rustix", - "windows-sys", -] - [[package]] name = "thiserror" version = "2.0.12" @@ -612,47 +462,6 @@ dependencies = [ "syn", ] -[[package]] -name = "toml" -version = "0.8.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ae329d1f08c4d17a59bed7ff5b5a769d062e64a62d34a3261b219e62cd5aae" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - -[[package]] -name = "toml_datetime" -version = "0.6.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3da5db5a963e24bc68be8b17b6fa82814bb22ee8660f192bb182771d498f09a3" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.22.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "310068873db2c5b3e7659d2cc35d21855dbafa50d1ce336397c666e3cb08137e" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "toml_write", - "winnow", -] - -[[package]] -name = "toml_write" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfb942dfe1d8e29a7ee7fcbde5bd2b9a25fb89aa70caea2eba3bee836ff41076" - [[package]] name = "unicode-ident" version = "1.0.18" @@ -753,15 +562,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "winnow" -version = "0.7.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06928c8748d81b05c9be96aad92e1b6ff01833332f281e8cfca3be4b35fc9ec" -dependencies = [ - "memchr", -] - [[package]] name = "wit-bindgen-rt" version = "0.39.0" diff --git a/Cargo.toml b/Cargo.toml index d9a1a3e..f6bef14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,11 +13,8 @@ keywords = ["cccb", "cccb-servicepoint", "cbindgen"] [lib] crate-type = ["staticlib", "cdylib", "rlib"] -[build-dependencies] -cbindgen = "0.29.0" - [dependencies.servicepoint] -version = "0.15.0" +version = "0.15.1" default-features = false [dependencies.env_logger] From aba7d49458c1e5d2e9efc932e3292774317d2873 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 27 Jun 2025 18:09:08 +0200 Subject: [PATCH 71/76] cleanup alias, flake --- cbindgen.toml | 2 -- devShells.nix | 57 ++++++++++++++++++++++++++++++--------------------- packages.nix | 1 - 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/cbindgen.toml b/cbindgen.toml index 2341192..6825722 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -38,8 +38,6 @@ include = [] exclude = ["BitVec"] [export.rename] -"SPCommand" = "Command" -"DisplayBitVec" = "BitVec" [enum] rename_variants = "QualifiedScreamingSnakeCase" diff --git a/devShells.nix b/devShells.nix index 7942618..de4f709 100644 --- a/devShells.nix +++ b/devShells.nix @@ -5,33 +5,41 @@ ... }: let - defaultAdditionalPkgs = with pkgs; [ - rustfmt - clippy - cargo-expand - cargo-tarpaulin - gdb - nix-output-monitor - ]; -in -(builtins.mapAttrs ( - packageName: package: - pkgs.mkShell { - inputsFrom = [ package ]; - packages = defaultAdditionalPkgs; + common = { RUST_BACKTRACE = 1; RUST_LOG = "all"; - } -) selfPkgs) -// { - default = pkgs.mkShell { + packages = with pkgs; [ + gdb + nix-output-monitor + ]; + }; +in rec { + nightly = pkgs.mkShell ( common // { + inputsFrom = [ + selfPkgs.servicepoint-binding-c-nightly-release + ]; + packages = with pkgs; [ + cargo-expand + cargo-tarpaulin + nix-output-monitor + gcc + gnumake + rustfmt + xe + libgcc + libunwind + pkgsStatic.gcc + pkgsStatic.libgcc + pkgsStatic.musl + rust-cbindgen + ]; + }); + stable = pkgs.mkShell (common // { inputsFrom = [ selfPkgs.servicepoint-binding-c selfPkgs.announce ]; - packages = - defaultAdditionalPkgs - ++ (with pkgs; [ + packages = with pkgs; [ (pkgs.symlinkJoin { name = "rust-toolchain"; paths = with pkgs; [ @@ -45,6 +53,8 @@ in cargo-tarpaulin ]; }) + cargo-expand + cargo-tarpaulin gcc gnumake xe @@ -53,8 +63,9 @@ in pkgsStatic.gcc pkgsStatic.libgcc pkgsStatic.musl - ]); + ]; RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; - }; + }); + default = stable; } diff --git a/packages.nix b/packages.nix index acae2a5..d47ef6f 100644 --- a/packages.nix +++ b/packages.nix @@ -16,7 +16,6 @@ let cargoBuildFlags ? [ ], nativeBuildInputs ? [], stdlib ? false, - }: rustPlatform.buildRustPackage (finalAttrs: { inherit version buildType cargoBuildFlags stdlib; From a7d4b287e4a673a2eef3ad997473ec40aa11b525 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 27 Jun 2025 18:11:00 +0200 Subject: [PATCH 72/76] update dependencies and flake --- Cargo.lock | 72 +++++++++++++++++++++++++++--------------------------- flake.lock | 18 +++++++------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e18fa74..63012a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 4 [[package]] name = "adler2" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "aho-corasick" @@ -19,9 +19,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.18" +version = "0.6.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933" dependencies = [ "anstyle", "anstyle-parse", @@ -34,33 +34,33 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" +checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" [[package]] name = "anstyle-parse" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9" dependencies = [ "windows-sys", ] [[package]] name = "anstyle-wincon" -version = "3.0.8" +version = "3.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6680de5231bd6ee4c6191b8a1325daa282b415391ec9d3a37bd34f2060dc73fa" +checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882" dependencies = [ "anstyle", "once_cell_polyfill", @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.24" +version = "1.2.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16595d3be041c03b09d08d0858631facccee9221e579704070e6e9e4915d3bc7" +checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc" dependencies = [ "jobserver", "libc", @@ -117,15 +117,15 @@ dependencies = [ [[package]] name = "cfg-if" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" [[package]] name = "colorchoice" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" [[package]] name = "crc32fast" @@ -161,9 +161,9 @@ dependencies = [ [[package]] name = "flate2" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ced92e76e966ca2fd84c8f7aa01a4aea65b0eb6648d72f7c8f3e2764a67fece" +checksum = "4a3d7db9596fecd151c5f638c0ee5d5bd487b6e0ea232e5dc96d5250f6f94b1d" dependencies = [ "crc32fast", "miniz_oxide", @@ -206,9 +206,9 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "jiff" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a194df1107f33c79f4f93d02c80798520551949d59dfad22b6157048a88cca93" +checksum = "be1f93b8b1eb69c77f24bbb0afdf66f54b632ee39af40ca21c4365a1d7347e49" dependencies = [ "jiff-static", "log", @@ -219,9 +219,9 @@ dependencies = [ [[package]] name = "jiff-static" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c6e1db7ed32c6c71b759497fae34bf7933636f75a251b9e736555da426f6442" +checksum = "03343451ff899767262ec32146f6d559dd759fdadf42ff0e227c7c48f72594b4" dependencies = [ "proc-macro2", "quote", @@ -240,9 +240,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.172" +version = "0.2.174" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" [[package]] name = "log" @@ -252,15 +252,15 @@ checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" [[package]] name = "memchr" -version = "2.7.4" +version = "2.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" [[package]] name = "miniz_oxide" -version = "0.8.8" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" dependencies = [ "adler2", ] @@ -291,9 +291,9 @@ checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" [[package]] name = "portable-atomic" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" +checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" [[package]] name = "portable-atomic-util" @@ -324,9 +324,9 @@ dependencies = [ [[package]] name = "r-efi" -version = "5.2.0" +version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "radium" @@ -427,9 +427,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "syn" -version = "2.0.101" +version = "2.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" dependencies = [ "proc-macro2", "quote", diff --git a/flake.lock b/flake.lock index 873e7c3..4db936e 100644 --- a/flake.lock +++ b/flake.lock @@ -8,11 +8,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1748759782, - "narHash": "sha256-MJNhEBsAbxRp/53qsXv6/eaWkGS8zMGX9LuCz1BLeck=", + "lastModified": 1751006353, + "narHash": "sha256-icKFXb83uv2ezRCfuq5G8QSwCuaoLywLljSL+UGmPPI=", "owner": "nix-community", "repo": "fenix", - "rev": "9be40ad995bac282160ff374a47eed67c74f9c2a", + "rev": "b37f026b49ecb295a448c96bcbb0c174c14fc91b", "type": "github" }, "original": { @@ -38,11 +38,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1748302896, - "narHash": "sha256-ixMT0a8mM091vSswlTORZj93WQAJsRNmEvqLL+qwTFM=", + "lastModified": 1750838302, + "narHash": "sha256-aVkL3/yu50oQzi2YuKo0ceiCypVZpZXYd2P2p1FMJM4=", "owner": "nixos", "repo": "nixpkgs", - "rev": "7848cd8c982f7740edf76ddb3b43d234cb80fc4d", + "rev": "7284e2decc982b81a296ab35aa46e804baaa1cfe", "type": "github" }, "original": { @@ -62,11 +62,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1748695646, - "narHash": "sha256-VwSuuRF4NvAoeHZJRRlX8zAFZ+nZyuiIvmVqBAX0Bcg=", + "lastModified": 1750871759, + "narHash": "sha256-hMNZXMtlhfjQdu1F4Fa/UFiMoXdZag4cider2R9a648=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "2a388d1103450d814a84eda98efe89c01b158343", + "rev": "317542c1e4a3ec3467d21d1c25f6a43b80d83e7d", "type": "github" }, "original": { From 9101259ecf4a69fe724eeecc91336d4371c8fbdc Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 27 Jun 2025 18:22:16 +0200 Subject: [PATCH 73/76] remove unsupported configurations from flake --- nix-build-all.sh | 14 ---------- packages.nix | 66 ------------------------------------------------ 2 files changed, 80 deletions(-) diff --git a/nix-build-all.sh b/nix-build-all.sh index 2d247e7..9472cc5 100755 --- a/nix-build-all.sh +++ b/nix-build-all.sh @@ -13,17 +13,3 @@ $BUILD .#servicepoint-binding-c-nightly-release -o result-nightly-release $BUILD .#all-examples -o result-examples $BUILD .#all-examples-size -o result-examples-size - -# works, but needs to bootstrap a C and Rust compiler -# $BUILD .#servicepoint-binding-c-musl-stable-release -o result-musl-release -# $BUILD .#servicepoint-binding-c-musl-stable-size -o result-musl-size -# $BUILD .#all-examples-musl-static -o result-examples-musl-static -# $BUILD .#all-examples-musl-static-size -o result-examples-musl-static-size -# $BUILD .#all-examples-musl -o result-examples-musl - -# do not work yet: -# $BUILD .#servicepoint-binding-c-nightly-size -o result-nightly-size -# $BUILD .#servicepoint-binding-c-musl-nightly-release -# $BUILD .#servicepoint-binding-c-musl-nightly-size -# $BUILD .#all-examples-nightly-size -o result-examples-nightly-size - diff --git a/packages.nix b/packages.nix index d47ef6f..e01882e 100644 --- a/packages.nix +++ b/packages.nix @@ -153,31 +153,12 @@ in rec { servicepoint-binding-c-stable-release = mkServicepoint stable-release-args; servicepoint-binding-c-nightly-release = mkServicepoint nightly-release-args; - servicepoint-binding-c-musl-stable-release = mkServicepoint musl-stable-release-args; - servicepoint-binding-c-musl-nightly-release = mkServicepoint musl-nightly-release-args; - servicepoint-binding-c-stable-size = mkServicepoint (stable-release-args // stable-size-args); - servicepoint-binding-c-nightly-size = mkServicepoint ( - nightly-release-args // stable-size-args // nightly-size-args - ); - servicepoint-binding-c-musl-stable-size = mkServicepoint ( - musl-stable-release-args // stable-size-args - ); - servicepoint-binding-c-musl-nightly-size = mkServicepoint ( - musl-nightly-release-args // stable-size-args // nightly-size-args - ); - # default variants servicepoint-binding-c = servicepoint-binding-c-stable-release; - servicepoint-binding-c-musl = servicepoint-binding-c-musl-stable-release; all-examples = mkAllExamples ""; all-examples-size = mkAllExamples "-size"; - all-examples-nightly-size = mkAllExamples "-nightly-size"; - # TODO: musl targets do not work on darwin - all-examples-musl = mkAllExamples "-musl"; - all-examples-musl-static = mkAllExamples "-musl-static"; - all-examples-musl-static-size = mkAllExamples "-musl-static-size"; } # construct one package per example // (lib.genAttrs examples ( @@ -198,50 +179,3 @@ rec { } ) )) -# construct another pakage per example, but optimized for size with unstable rust -// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-unstable-size") value) ( - lib.genAttrs examples ( - name: - mkExample { - inherit name pkgs; - servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; - EXTRA_CFLAGS = builtins.toString size-cflags; - } - ) -)) -# construct another pakage per example, but with musl -// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl") value) ( - lib.genAttrs examples ( - name: - mkExample { - inherit name; - pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-release; - } - ) -)) -# construct another pakage per example, but statically linked with musl -// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl-static") value) ( - lib.genAttrs examples ( - name: - mkExample { - inherit name; - pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-release; - static = true; - } - ) -)) -# construct another pakage per example, but statically linked with musl and size optimized -// (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl-static-size") value) ( - lib.genAttrs examples ( - name: - mkExample { - inherit name; - pkgs = pkgs.pkgsMusl; - servicepointBinding = selfPkgs.servicepoint-binding-c-musl-stable-size; - static = true; - EXTRA_CFLAGS = builtins.toString size-cflags; - } - ) -)) From 284c005e4e8cb35dd8a18e3302fed1cc239428a4 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 28 Jun 2025 14:38:31 +0200 Subject: [PATCH 74/76] remove broken csbindgen in ci --- .github/workflows/rust.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 75139b5..2d347ba 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -47,14 +47,6 @@ jobs: - name: install rust targets run: rustup toolchain install nightly -t aarch64-unknown-linux-gnu -c rust-src --no-self-update - - name: install csbindgen - run: rustup run nightly cargo install cbindgen@0.29.0 - - - name: generate bindings - run: ./generate-binding.sh - - name: check that generated files did not change - run: output=$(git status --porcelain) && [ -z "$output" ] - - name: build example -- glibc size_optimized run: cd example && make clean-all -r && make -r LIBC=gnu LINK=dynamic PROFILE=size_optimized CARGO="rustup run nightly cargo" LTO=1 From c702d832f4e51715b880cb0eaa104e4b78206191 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 28 Jun 2025 17:21:00 +0200 Subject: [PATCH 75/76] do not link non existant functions in the base library --- include/servicepoint.h | 150 ----------------------------------------- src/commands/mod.rs | 2 + src/macros.rs | 8 +-- 3 files changed, 5 insertions(+), 155 deletions(-) diff --git a/include/servicepoint.h b/include/servicepoint.h index 703ab37..bb825c7 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -669,8 +669,6 @@ struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); void sp_bitmap_fill(struct Bitmap */*notnull*/ instance, bool value); /** - * Calls method [`servicepoint::Bitmap::free`]. - * *Deallocates a [`Bitmap`] instance. * * This function is part of the `bitmap` module. @@ -716,8 +714,6 @@ bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); size_t sp_bitmap_height(struct Bitmap */*notnull*/ instance); /** - * Calls method [`servicepoint::Bitmap::into_bitvec`]. - * * Consumes the Bitmap and returns the contained BitVec. * * This function is part of the `bitmap` module. @@ -800,8 +796,6 @@ void sp_bitmap_set(struct Bitmap */*notnull*/ instance, bool value); /** - * Calls method [`servicepoint::Bitmap::try_into_packet`]. - * * Creates a [BitmapCommand] and immediately turns that into a [Packet]. * * The provided [Bitmap] gets consumed. @@ -834,8 +828,6 @@ size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_bitmapcommand_clone(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitmapCommand::free`]. - * *Deallocates a [`BitmapCommand`] instance. * * This function is part of the `bitmapcommand` module. @@ -853,8 +845,6 @@ void sp_bitmapcommand_free(struct BitmapCommand */*notnull*/ instance); struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */*notnull*/ bitmap); /** - * Calls method [`servicepoint::BitmapCommand::get_bitmap_mut`]. - * * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -865,8 +855,6 @@ struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */* struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitmapCommand::get_compression`]. - * * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -874,8 +862,6 @@ struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand CompressionCode sp_bitmapcommand_get_compression(struct BitmapCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitmapCommand::get_origin`]. - * * Reads the origin field of the [`BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -899,8 +885,6 @@ struct BitmapCommand */*notnull*/ sp_bitmapcommand_new(struct Bitmap */*notnull* CompressionCode compression); /** - * Calls method [`servicepoint::BitmapCommand::set_bitmap`]. - * * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -910,8 +894,6 @@ void sp_bitmapcommand_set_bitmap(struct BitmapCommand */*notnull*/ instance, struct Bitmap */*notnull*/ value); /** - * Calls method [`servicepoint::BitmapCommand::set_compression`]. - * * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -920,8 +902,6 @@ void sp_bitmapcommand_set_compression(struct BitmapCommand */*notnull*/ instance CompressionCode value); /** - * Calls method [`servicepoint::BitmapCommand::set_origin`]. - * * Overwrites the origin field of the [`BitmapCommand`]. * * This function is part of the `bitmapcommand` module. @@ -931,8 +911,6 @@ void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, size_t origin_y); /** - * Calls method [`servicepoint::BitmapCommand::try_into_packet`]. - * *Tries to turn a [`BitmapCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -951,8 +929,6 @@ struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull* struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitVecCommand::free`]. - * *Deallocates a [`BitVecCommand`] instance. * * This function is part of the `bitveccommand` module. @@ -960,8 +936,6 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitVecCommand::get_bitvec_mut`]. - * * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -972,8 +946,6 @@ void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitVecCommand::get_compression`]. - * * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -981,8 +953,6 @@ DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitVecCommand::get_offset`]. - * * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -990,8 +960,6 @@ CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull Offset sp_bitveccommand_get_offset(struct BitVecCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BitVecCommand::get_operation`]. - * * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1020,8 +988,6 @@ struct BitVecCommand */*notnull*/ sp_bitveccommand_new(DisplayBitVec */*notnull* CompressionCode compression); /** - * Calls method [`servicepoint::BitVecCommand::set_bitvec`]. - * * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1031,8 +997,6 @@ void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, DisplayBitVec */*notnull*/ value); /** - * Calls method [`servicepoint::BitVecCommand::set_compression`]. - * * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1041,8 +1005,6 @@ void sp_bitveccommand_set_compression(struct BitVecCommand */*notnull*/ instance CompressionCode value); /** - * Calls method [`servicepoint::BitVecCommand::set_offset`]. - * * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1051,8 +1013,6 @@ void sp_bitveccommand_set_offset(struct BitVecCommand */*notnull*/ instance, Offset value); /** - * Calls method [`servicepoint::BitVecCommand::set_operation`]. - * * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. * * This function is part of the `bitveccommand` module. @@ -1061,8 +1021,6 @@ void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, BinaryOperation value); /** - * Calls method [`servicepoint::BitVecCommand::try_into_packet`]. - * *Tries to turn a [`BitVecCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -1081,8 +1039,6 @@ struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull* BrightnessGrid */*notnull*/ sp_brightnessgrid_clone(BrightnessGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::BrightnessGrid::data_ref_mut`]. - * * Gets an unsafe reference to the data of the instance. * * The returned memory is valid for the lifetime of the grid. @@ -1106,8 +1062,6 @@ void sp_brightnessgrid_fill(BrightnessGrid */*notnull*/ instance, Brightness value); /** - * Calls method [`servicepoint::BrightnessGrid::free`]. - * *Deallocates a [`BrightnessGrid`] instance. * * This function is part of the `brightnessgrid` module. @@ -1200,8 +1154,6 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, Brightness value); /** - * Calls method [`servicepoint::BrightnessGrid::try_into_packet`]. - * * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. * * The provided [BrightnessGrid] gets consumed. @@ -1233,8 +1185,6 @@ size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_clone(struct BrightnessGridCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BrightnessGridCommand::free`]. - * *Deallocates a [`BrightnessGridCommand`] instance. * * This function is part of the `brightnessgridcommand` module. @@ -1250,8 +1200,6 @@ void sp_brightnessgridcommand_free(struct BrightnessGridCommand */*notnull*/ ins struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(BrightnessGrid */*notnull*/ grid); /** - * Calls method [`servicepoint::BrightnessGridCommand::get_grid_mut`]. - * * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1262,8 +1210,6 @@ struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(Bri BrightnessGrid */*notnull*/ sp_brightnessgridcommand_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::BrightnessGridCommand::get_origin`]. - * * Reads the origin field of the [`BrightnessGridCommand`]. * * This function is part of the `brightnessgridcommand` module. @@ -1286,8 +1232,6 @@ struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_new(Brightnes size_t origin_y); /** - * Calls method [`servicepoint::BrightnessGridCommand::set_grid`]. - * * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1297,8 +1241,6 @@ void sp_brightnessgridcommand_set_grid(struct BrightnessGridCommand */*notnull*/ BrightnessGrid */*notnull*/ value); /** - * Calls method [`servicepoint::BrightnessGridCommand::set_origin`]. - * * Overwrites the origin field of the [`BrightnessGridCommand`]. * * This function is part of the `brightnessgridcommand` module. @@ -1308,8 +1250,6 @@ void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull size_t origin_y); /** - * Calls method [`servicepoint::BrightnessGridCommand::try_into_packet`]. - * *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -1328,8 +1268,6 @@ struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCom CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::CharGrid::fill`]. - * * Sets the value of all cells in the grid. * * # Arguments @@ -1342,8 +1280,6 @@ CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** - * Calls method [`servicepoint::CharGrid::free`]. - * *Deallocates a [`CharGrid`] instance. * * This function is part of the `chargrid` module. @@ -1351,8 +1287,6 @@ void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); void sp_chargrid_free(CharGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::CharGrid::get`]. - * * Returns the current value at the specified position. * * # Arguments @@ -1404,8 +1338,6 @@ CharGrid *sp_chargrid_load(size_t width, size_t height, struct ByteSlice data); CharGrid */*notnull*/ sp_chargrid_new(size_t width, size_t height); /** - * Calls method [`servicepoint::CharGrid::set`]. - * * Sets the value of the specified position in the grid. * * # Arguments @@ -1428,8 +1360,6 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, uint32_t value); /** - * Calls method [`servicepoint::CharGrid::try_into_packet`]. - * * Creates a [CharGridCommand] and immediately turns that into a [Packet]. * * The provided [CharGrid] gets consumed. @@ -1461,8 +1391,6 @@ size_t sp_chargrid_width(CharGrid */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::CharGridCommand::free`]. - * *Deallocates a [`CharGridCommand`] instance. * * This function is part of the `chargridcommand` module. @@ -1478,8 +1406,6 @@ void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*notnull*/ grid); /** - * Calls method [`servicepoint::CharGridCommand::get_grid_mut`]. - * * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1490,8 +1416,6 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*not CharGrid */*notnull*/ sp_chargridcommand_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::CharGridCommand::get_origin`]. - * * Reads the origin field of the [`CharGridCommand`]. * * This function is part of the `chargridcommand` module. @@ -1514,8 +1438,6 @@ struct CharGridCommand */*notnull*/ sp_chargridcommand_new(CharGrid */*notnull*/ size_t origin_y); /** - * Calls method [`servicepoint::CharGridCommand::set_grid`]. - * * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1525,8 +1447,6 @@ void sp_chargridcommand_set_grid(struct CharGridCommand */*notnull*/ instance, CharGrid */*notnull*/ value); /** - * Calls method [`servicepoint::CharGridCommand::set_origin`]. - * * Overwrites the origin field of the [`CharGridCommand`]. * * This function is part of the `chargridcommand` module. @@ -1536,8 +1456,6 @@ void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, size_t origin_y); /** - * Calls method [`servicepoint::CharGridCommand::try_into_packet`]. - * *Tries to turn a [`CharGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -1556,8 +1474,6 @@ struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notn struct ClearCommand */*notnull*/ sp_clearcommand_clone(struct ClearCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::ClearCommand::free`]. - * *Deallocates a [`ClearCommand`] instance. * * This function is part of the `clearcommand` module. @@ -1576,8 +1492,6 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); struct ClearCommand */*notnull*/ sp_clearcommand_new(void); /** - * Calls method [`servicepoint::ClearCommand::try_into_packet`]. - * *Tries to turn a [`ClearCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -1620,8 +1534,6 @@ struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); void sp_cp437grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** - * Calls method [`servicepoint::Cp437Grid::free`]. - * *Deallocates a [`Cp437Grid`] instance. * * This function is part of the `cp437grid` module. @@ -1694,8 +1606,6 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, uint8_t value); /** - * Calls method [`servicepoint::Cp437Grid::try_into_packet`]. - * * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. * * The provided [Cp437Grid] gets consumed. @@ -1727,8 +1637,6 @@ size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437GridCommand::free`]. - * *Deallocates a [`Cp437GridCommand`] instance. * * This function is part of the `cp437gridcommand` module. @@ -1744,8 +1652,6 @@ void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */*notnull*/ grid); /** - * Calls method [`servicepoint::Cp437GridCommand::get_grid_mut`]. - * * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -1756,8 +1662,6 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */* Cp437Grid */*notnull*/ sp_cp437gridcommand_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437GridCommand::get_origin`]. - * * Reads the origin field of the [`Cp437GridCommand`]. * * This function is part of the `cp437gridcommand` module. @@ -1780,8 +1684,6 @@ struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_new(Cp437Grid */*notnul size_t origin_y); /** - * Calls method [`servicepoint::Cp437GridCommand::set_grid`]. - * * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * @@ -1791,8 +1693,6 @@ void sp_cp437gridcommand_set_grid(struct Cp437GridCommand */*notnull*/ instance, Cp437Grid */*notnull*/ value); /** - * Calls method [`servicepoint::Cp437GridCommand::set_origin`]. - * * Overwrites the origin field of the [`Cp437GridCommand`]. * * This function is part of the `cp437gridcommand` module. @@ -1802,8 +1702,6 @@ void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command size_t origin_y); /** - * Calls method [`servicepoint::Cp437GridCommand::try_into_packet`]. - * *Tries to turn a [`Cp437GridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -1846,8 +1744,6 @@ DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ ins void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); /** - * Calls method [`servicepoint::DisplayBitVec::free`]. - * *Deallocates a [`DisplayBitVec`] instance. * * This function is part of the `displaybitvec` module. @@ -1855,8 +1751,6 @@ void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); void sp_displaybitvec_free(DisplayBitVec */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::get`]. - * * Gets the value of a bit. * * # Arguments @@ -1939,8 +1833,6 @@ void sp_displaybitvec_set(DisplayBitVec */*notnull*/ instance, bool value); /** - * Calls method [`servicepoint::DisplayBitVec::try_into_packet`]. - * * Creates a [BitVecCommand] and immediately turns that into a [Packet]. * * The provided [DisplayBitVec] gets consumed. @@ -1972,8 +1864,6 @@ void sp_envlogger_init(void); struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_clone(struct FadeOutCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::FadeOutCommand::free`]. - * *Deallocates a [`FadeOutCommand`] instance. * * This function is part of the `fadeoutcommand` module. @@ -1990,8 +1880,6 @@ void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); /** - * Calls method [`servicepoint::FadeOutCommand::try_into_packet`]. - * *Tries to turn a [`FadeOutCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -2010,8 +1898,6 @@ struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnul struct GenericCommand */*notnull*/ sp_genericcommand_clone(struct GenericCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::GenericCommand::free`]. - * *Deallocates a [`GenericCommand`] instance. * * This function is part of the `genericcommand` module. @@ -2030,8 +1916,6 @@ void sp_genericcommand_free(struct GenericCommand */*notnull*/ instance); struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Packet */*notnull*/ packet); /** - * Calls method [`servicepoint::GenericCommand::try_into_packet`]. - * * Tries to turn a [GenericCommand] into a [Packet]. * The [GenericCommand] gets consumed. * @@ -2051,8 +1935,6 @@ struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnul struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::GlobalBrightnessCommand::free`]. - * *Deallocates a [`GlobalBrightnessCommand`] instance. * * This function is part of the `globalbrightnesscommand` module. @@ -2060,8 +1942,6 @@ struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(str void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::GlobalBrightnessCommand::get_brightness`]. - * * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * * This function is part of the `globalbrightnesscommand` module. @@ -2078,8 +1958,6 @@ Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessComm struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brightness brightness); /** - * Calls method [`servicepoint::GlobalBrightnessCommand::set_brightness`]. - * * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * * This function is part of the `globalbrightnesscommand` module. @@ -2088,8 +1966,6 @@ void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */ Brightness value); /** - * Calls method [`servicepoint::GlobalBrightnessCommand::try_into_packet`]. - * *Tries to turn a [`GlobalBrightnessCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -2108,8 +1984,6 @@ struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnes struct HardResetCommand */*notnull*/ sp_hardresetcommand_clone(struct HardResetCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::HardResetCommand::free`]. - * *Deallocates a [`HardResetCommand`] instance. * * This function is part of the `hardresetcommand` module. @@ -2128,8 +2002,6 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); /** - * Calls method [`servicepoint::HardResetCommand::try_into_packet`]. - * *Tries to turn a [`HardResetCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. @@ -2148,8 +2020,6 @@ struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*no struct Packet */*notnull*/ sp_packet_clone(struct Packet */*notnull*/ instance); /** - * Calls method [`servicepoint::Packet::free`]. - * *Deallocates a [`Packet`] instance. * * This function is part of the `packet` module. @@ -2167,8 +2037,6 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct ByteSlice payload); /** - * Calls method [`servicepoint::Packet::get_header`]. - * * Gets the value of field `header` of the [`servicepoint::Packet`]. * * This function is part of the `packet` module. @@ -2176,8 +2044,6 @@ struct Packet */*notnull*/ sp_packet_from_parts(struct Header header, struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); /** - * Calls method [`servicepoint::Packet::get_header_mut`]. - * * Gets a reference to the field `header` of the [`servicepoint::Packet`]. * * - The returned reference inherits the lifetime of object in which it is contained. @@ -2188,8 +2054,6 @@ struct Header sp_packet_get_header(struct Packet */*notnull*/ instance); struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ instance); /** - * Calls method [`servicepoint::Packet::get_payload`]. - * * Returns a pointer to the current payload of the provided packet. * * Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload. @@ -2201,8 +2065,6 @@ struct Header */*notnull*/ sp_packet_get_header_mut(struct Packet */*notnull*/ i struct ByteSlice sp_packet_get_payload(struct Packet */*notnull*/ packet); /** - * Calls method [`servicepoint::Packet::serialize_to`]. - * * Serialize the packet into the provided buffer. * * # Panics @@ -2215,8 +2077,6 @@ size_t sp_packet_serialize_to(struct Packet */*notnull*/ packet, struct ByteSlice buffer); /** - * Calls method [`servicepoint::Packet::set_header`]. - * * Sets the value of field `header` of the [`servicepoint::Packet`]. * * This function is part of the `packet` module. @@ -2225,8 +2085,6 @@ void sp_packet_set_header(struct Packet */*notnull*/ instance, struct Header value); /** - * Calls method [`servicepoint::Packet::set_payload`]. - * * Sets the payload of the provided packet to the provided data. * * This makes previous payload pointers invalid. @@ -2256,8 +2114,6 @@ bool sp_sp_u16_to_command_code(uint16_t code, CommandCode */*notnull*/ result); /** - * Calls method [`servicepoint::UdpSocket::free`]. - * *Deallocates a [`UdpSocket`] instance. * * This function is part of the `udpsocket` module. @@ -2303,8 +2159,6 @@ struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, uint16_t port); /** - * Calls method [`servicepoint::UdpSocket::send_command`]. - * * Sends a [GenericCommand] to the display using the [UdpSocket]. * * The passed `command` gets consumed. @@ -2323,8 +2177,6 @@ bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, struct GenericCommand */*notnull*/ command); /** - * Calls method [`servicepoint::UdpSocket::send_header`]. - * * Sends a [Header] to the display using the [UdpSocket]. * * returns: true in case of success @@ -2341,8 +2193,6 @@ bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, struct Header header); /** - * Calls method [`servicepoint::UdpSocket::send_packet`]. - * * Sends a [Packet] to the display using the [UdpSocket]. * * The passed `packet` gets consumed. diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e3e3715..c8e9dfc 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -62,6 +62,8 @@ macro_rules! derive_command_into_packet { #[doc = "Tries to turn a [`" $command_type "`] into a [Packet]."] /// /// Returns: NULL or a [Packet] containing the command. + /// + /// [Packet]: [`servicepoint::Packet`] fn try_into_packet(move instance) -> move_ok *mut ::servicepoint::Packet { instance.try_into() }; diff --git a/src/macros.rs b/src/macros.rs index 5ddb7f9..34e94e8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -14,9 +14,7 @@ macro_rules! derive_clone { ($object_type:ident) => { $crate::macros::wrap_method!($object_type; #[doc = concat!("Clones a [`", stringify!($object_type), "`] instance.")] - fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type> { - instance.clone() - }; + fn clone(ref instance) -> move ::core::ptr::NonNull<$object_type>; ); }; } @@ -31,6 +29,8 @@ macro_rules! wrap_method { ::paste::paste!{ $crate::macros::wrap_method!( $object_type; + #[doc = " Calls method [`" $object_type "::" $function "`]."] + /// $(#[$meta])+ fn $function($ref_or_mut $instance $(, $($param_name: $param_modifier $param_type),*)?) $(-> $return_modifier $return_type)? { @@ -47,8 +47,6 @@ macro_rules! wrap_method { ) => { paste::paste! { $crate::macros::wrap_functions!([< $object_type:lower >]; - #[doc = " Calls method [`servicepoint::" $object_type "::" $function "`]."] - /// $(#[$meta])* fn $function( $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> From 41fbdbf3a3d41e974989558c50e71c8aae77769d Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 28 Jun 2025 18:38:20 +0200 Subject: [PATCH 76/76] object name to snake case --- example/src/announce.c | 28 +- example/src/brightness_tester.c | 14 +- example/src/header_logger.c | 46 +- example/src/helpers.h | 4 +- example/src/moving_line.c | 6 +- example/src/random_stuff.c | 2 +- example/src/undefined.c | 6 +- example/src/wiping_clear.c | 6 +- include/servicepoint.h | 1290 ++++++++++++++++--------------- src/macros.rs | 4 +- 10 files changed, 712 insertions(+), 694 deletions(-) diff --git a/example/src/announce.c b/example/src/announce.c index a0bdf9a..dda1f67 100644 --- a/example/src/announce.c +++ b/example/src/announce.c @@ -3,27 +3,27 @@ int main(void) { sock_init(); - sp_udpsocket_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); + sp_udp_socket_send_header(sock, (Header) {.command_code = COMMAND_CODE_CLEAR}); - CharGrid *grid = sp_chargrid_new(5, 2); + CharGrid *grid = sp_char_grid_new(5, 2); if (grid == NULL) return 1; - sp_chargrid_set(grid, 0, 0, 'H'); - sp_chargrid_set(grid, 1, 0, 'e'); - sp_chargrid_set(grid, 2, 0, 'l'); - sp_chargrid_set(grid, 3, 0, 'l'); - sp_chargrid_set(grid, 4, 0, 'o'); - sp_chargrid_set(grid, 0, 1, 'W'); - sp_chargrid_set(grid, 1, 1, 'o'); - sp_chargrid_set(grid, 2, 1, 'r'); - sp_chargrid_set(grid, 3, 1, 'l'); - sp_chargrid_set(grid, 4, 1, 'd'); + sp_char_grid_set(grid, 0, 0, 'H'); + sp_char_grid_set(grid, 1, 0, 'e'); + sp_char_grid_set(grid, 2, 0, 'l'); + sp_char_grid_set(grid, 3, 0, 'l'); + sp_char_grid_set(grid, 4, 0, 'o'); + sp_char_grid_set(grid, 0, 1, 'W'); + sp_char_grid_set(grid, 1, 1, 'o'); + sp_char_grid_set(grid, 2, 1, 'r'); + sp_char_grid_set(grid, 3, 1, 'l'); + sp_char_grid_set(grid, 4, 1, 'd'); - Packet *packet = sp_chargrid_try_into_packet(grid, 0, 0); + Packet *packet = sp_char_grid_try_into_packet(grid, 0, 0); if (packet == NULL) return 1; - sp_udpsocket_send_packet(sock, packet); + sp_udp_socket_send_packet(sock, packet); return 0; } diff --git a/example/src/brightness_tester.c b/example/src/brightness_tester.c index 921bbd0..cdd2a05 100644 --- a/example/src/brightness_tester.c +++ b/example/src/brightness_tester.c @@ -5,14 +5,14 @@ void enable_all_pixels(void) { Bitmap *all_on = sp_bitmap_new_max_sized(); sp_bitmap_fill(all_on, true); - BitmapCommand *bitmapCommand = sp_bitmapcommand_from_bitmap(all_on); - Packet *packet = sp_bitmapcommand_try_into_packet(bitmapCommand); + BitmapCommand *bitmapCommand = sp_bitmap_command_from_bitmap(all_on); + Packet *packet = sp_bitmap_command_try_into_packet(bitmapCommand); if (packet != NULL) - sp_udpsocket_send_packet(sock, packet); + sp_udp_socket_send_packet(sock, packet); } void make_brightness_pattern(BrightnessGrid *grid) { - ByteSlice slice = sp_brightnessgrid_data_ref_mut(grid); + ByteSlice slice = sp_brightness_grid_data_ref_mut(grid); for (size_t index = 0; index < slice.length; index++) { slice.start[index] = (uint8_t)(index % ((size_t) Brightness_MAX)); } @@ -23,13 +23,13 @@ int main(void) { enable_all_pixels(); - BrightnessGrid *grid = sp_brightnessgrid_new(TILE_WIDTH, TILE_HEIGHT); + BrightnessGrid *grid = sp_brightness_grid_new(TILE_WIDTH, TILE_HEIGHT); make_brightness_pattern(grid); - Packet *packet = sp_brightnessgridcommand_try_into_packet(sp_brightnessgridcommand_from_grid(grid)); + Packet *packet = sp_brightness_grid_command_try_into_packet(sp_brightness_grid_command_from_grid(grid)); if (packet == NULL) return -2; - sp_udpsocket_send_packet(sock, packet); + sp_udp_socket_send_packet(sock, packet); return 0; } diff --git a/example/src/header_logger.c b/example/src/header_logger.c index 5793da9..82b8566 100644 --- a/example/src/header_logger.c +++ b/example/src/header_logger.c @@ -28,12 +28,12 @@ bool log_command(struct GenericCommand *command) { case COMMAND_TAG_BITMAP: { BitmapCommand *bitmapCommand = command->data.bitmap; - CompressionCode compression = sp_bitmapcommand_get_compression(bitmapCommand); + CompressionCode compression = sp_bitmap_command_get_compression(bitmapCommand); size_t x, y; - sp_bitmapcommand_get_origin(bitmapCommand, &x, &y); + sp_bitmap_command_get_origin(bitmapCommand, &x, &y); - Bitmap *bitmap = sp_bitmapcommand_get_bitmap_mut(bitmapCommand); + Bitmap *bitmap = sp_bitmap_command_get_bitmap_mut(bitmapCommand); size_t w = sp_bitmap_width(bitmap); size_t h = sp_bitmap_height(bitmap); @@ -45,11 +45,11 @@ bool log_command(struct GenericCommand *command) { BrightnessGridCommand *gridCommand = command->data.brightness_grid; size_t x, y; - sp_brightnessgridcommand_get_origin(gridCommand, &x, &y); + sp_brightness_grid_command_get_origin(gridCommand, &x, &y); - BrightnessGrid *grid = sp_brightnessgridcommand_get_grid_mut(gridCommand); - size_t w = sp_brightnessgrid_width(grid); - size_t h = sp_brightnessgrid_height(grid); + BrightnessGrid *grid = sp_brightness_grid_command_get_grid_mut(gridCommand); + size_t w = sp_brightness_grid_width(grid); + size_t h = sp_brightness_grid_height(grid); printf("-> BrightnessGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -59,11 +59,11 @@ bool log_command(struct GenericCommand *command) { CharGridCommand *gridCommand = command->data.char_grid; size_t x, y; - sp_chargridcommand_get_origin(gridCommand, &x, &y); + sp_char_grid_command_get_origin(gridCommand, &x, &y); - CharGrid *grid = sp_chargridcommand_get_grid_mut(gridCommand); - size_t w = sp_chargrid_width(grid); - size_t h = sp_chargrid_height(grid); + CharGrid *grid = sp_char_grid_command_get_grid_mut(gridCommand); + size_t w = sp_char_grid_width(grid); + size_t h = sp_char_grid_height(grid); printf("-> CharGridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -73,11 +73,11 @@ bool log_command(struct GenericCommand *command) { Cp437GridCommand *gridCommand = command->data.cp437_grid; size_t x, y; - sp_cp437gridcommand_get_origin(gridCommand, &x, &y); + sp_cp437_grid_command_get_origin(gridCommand, &x, &y); - Cp437Grid *grid = sp_cp437gridcommand_get_grid_mut(gridCommand); - size_t w = sp_cp437grid_width(grid); - size_t h = sp_cp437grid_height(grid); + Cp437Grid *grid = sp_cp437_grid_command_get_grid_mut(gridCommand); + size_t w = sp_cp437_grid_width(grid); + size_t h = sp_cp437_grid_height(grid); printf("-> Cp437GridCommand with params: x=%zu, y=%zu, w=%zu, h=%zu\n", x, y, w, h); @@ -86,10 +86,10 @@ bool log_command(struct GenericCommand *command) { case COMMAND_TAG_BIT_VEC: { BitVecCommand *bitvecCommand = command->data.bit_vec; - size_t offset = sp_bitveccommand_get_offset(bitvecCommand); - CompressionCode compression = sp_bitveccommand_get_compression(bitvecCommand); + size_t offset = sp_bit_vec_command_get_offset(bitvecCommand); + CompressionCode compression = sp_bit_vec_command_get_compression(bitvecCommand); - BinaryOperation operation = sp_bitveccommand_get_operation(bitvecCommand); + BinaryOperation operation = sp_bit_vec_command_get_operation(bitvecCommand); char *operationText; switch (operation) { case BINARY_OPERATION_AND: @@ -109,8 +109,8 @@ bool log_command(struct GenericCommand *command) { break; } - DisplayBitVec *bitvec = sp_bitveccommand_get_bitvec_mut(bitvecCommand); - size_t len = sp_displaybitvec_len(bitvec); + DisplayBitVec *bitvec = sp_bit_vec_command_get_bitvec_mut(bitvecCommand); + size_t len = sp_display_bit_vec_len(bitvec); printf("-> BitVecCommand with params: offset=%zu, length=%zu, compression=%hu, operation=%s\n", offset, len, compression, operationText); @@ -129,7 +129,7 @@ bool log_command(struct GenericCommand *command) { break; } case COMMAND_TAG_GLOBAL_BRIGHTNESS: { - Brightness brightness = sp_globalbrightnesscommand_get_brightness(command->data.global_brightness); + Brightness brightness = sp_global_brightness_command_get_brightness(command->data.global_brightness); printf("-> GlobalBrightnessCommand with params: brightness=%hu\n", brightness); break; } @@ -198,10 +198,10 @@ int main(int argc, char **argv) { header->command_code, header->a, header->b, header->c, header->d, payload.start, payload.length); - struct GenericCommand *command = sp_genericcommand_try_from_packet(packet); + struct GenericCommand *command = sp_generic_command_try_from_packet(packet); done = log_command(command); - sp_genericcommand_free(command); + sp_generic_command_free(command); } close(udp_socket); diff --git a/example/src/helpers.h b/example/src/helpers.h index ee415f2..96a0b0a 100644 --- a/example/src/helpers.h +++ b/example/src/helpers.h @@ -10,11 +10,11 @@ static UdpSocket *sock = NULL; void sock_free() { - sp_udpsocket_free(sock); + sp_udp_socket_free(sock); } void sock_init() { - sock = sp_udpsocket_open_ipv4(127, 0, 0, 1, 2342); + sock = sp_udp_socket_open_ipv4(127, 0, 0, 1, 2342); //sock = sp_udp_open_ipv4(172, 23, 42, 29, 2342); if (sock == NULL) exit(-1); diff --git a/example/src/moving_line.c b/example/src/moving_line.c index 12a1a9b..1aacceb 100644 --- a/example/src/moving_line.c +++ b/example/src/moving_line.c @@ -13,14 +13,14 @@ int main(void) { sp_bitmap_set(bitmap, (y + x) % PIXEL_WIDTH, y, true); } - BitmapCommand *command = sp_bitmapcommand_from_bitmap(sp_bitmap_clone(bitmap)); - Packet *packet = sp_bitmapcommand_try_into_packet(command); + BitmapCommand *command = sp_bitmap_command_from_bitmap(sp_bitmap_clone(bitmap)); + Packet *packet = sp_bitmap_command_try_into_packet(command); if (packet == NULL) { result = -2; goto exit; } - if (!sp_udpsocket_send_packet(sock, packet)) { + if (!sp_udp_socket_send_packet(sock, packet)) { result = -3; goto exit; } diff --git a/example/src/random_stuff.c b/example/src/random_stuff.c index 7a58c3a..417899a 100644 --- a/example/src/random_stuff.c +++ b/example/src/random_stuff.c @@ -18,6 +18,6 @@ int main(void) { Header *header = sp_packet_get_header_mut(packet); printf("[%d, %d, %d, %d, %d]\n", header->command_code, header->a, header->b, header->c, header->d); - sp_udpsocket_send_packet(sock, packet); + sp_udp_socket_send_packet(sock, packet); return 0; } diff --git a/example/src/undefined.c b/example/src/undefined.c index b869d71..dfa44d8 100644 --- a/example/src/undefined.c +++ b/example/src/undefined.c @@ -4,9 +4,9 @@ /// DO NOT DO ANY OF THIS! int main(void) { - BitmapCommand *bmcmd = sp_bitmapcommand_new(sp_bitmap_new_max_sized(), 0, 0, COMPRESSION_CODE_UNCOMPRESSED); + BitmapCommand *bmcmd = sp_bitmap_command_new(sp_bitmap_new_max_sized(), 0, 0, COMPRESSION_CODE_UNCOMPRESSED); BitVecCommand *bvcmd = (BitVecCommand *) bmcmd; - sp_bitveccommand_free(bvcmd); + sp_bit_vec_command_free(bvcmd); uint8_t *data = calloc(1024, 1); struct GenericCommand generic = { @@ -16,7 +16,7 @@ int main(void) { sock_init(); - sp_udpsocket_send_command(sock, &generic); + sp_udp_socket_send_command(sock, &generic); return 0; } diff --git a/example/src/wiping_clear.c b/example/src/wiping_clear.c index 1d0440b..dc76604 100644 --- a/example/src/wiping_clear.c +++ b/example/src/wiping_clear.c @@ -14,13 +14,13 @@ int main() { } DisplayBitVec *bitvec = sp_bitmap_into_bitvec(sp_bitmap_clone(enabled_pixels)); - BitVecCommand *command = sp_bitveccommand_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); - Packet *packet = sp_bitveccommand_try_into_packet(command); + BitVecCommand *command = sp_bit_vec_command_new(bitvec, 0, BINARY_OPERATION_AND, COMPRESSION_CODE_LZMA); + Packet *packet = sp_bit_vec_command_try_into_packet(command); if (packet == NULL) { result = -2; goto exit; } - if (!sp_udpsocket_send_packet(sock, packet)) { + if (!sp_udp_socket_send_packet(sock, packet)) { result = -3; goto exit; } diff --git a/include/servicepoint.h b/include/servicepoint.h index bb825c7..e2beb27 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -447,6 +447,11 @@ typedef struct ValueGrid_char ValueGrid_char; */ typedef struct ValueGrid_u8 ValueGrid_u8; +/** + * Type alias for documenting the meaning of the u16 in enum values + */ +typedef size_t Offset; + /** * Represents a span of memory (`&mut [u8]` ) as a struct. * @@ -476,11 +481,6 @@ typedef struct ByteSlice { size_t length; } ByteSlice; -/** - * Type alias for documenting the meaning of the u16 in enum values - */ -typedef size_t Offset; - /** * A grid containing brightness values. * @@ -636,7 +636,119 @@ extern "C" { #endif // __cplusplus /** - * Calls method [`servicepoint::Bitmap::clone`]. + * Calls method [`BitVecCommand::clone`]. + * + *Clones a [`BitVecCommand`] instance. + * + * This function is part of the `bit_vec_command` module. + */ +struct BitVecCommand */*notnull*/ sp_bit_vec_command_clone(struct BitVecCommand */*notnull*/ instance); + +/** + *Deallocates a [`BitVecCommand`] instance. + * + * This function is part of the `bit_vec_command` module. + */ +void sp_bit_vec_command_free(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `bit_vec_command` module. + */ +DisplayBitVec */*notnull*/ sp_bit_vec_command_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bit_vec_command` module. + */ +CompressionCode sp_bit_vec_command_get_compression(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bit_vec_command` module. + */ +Offset sp_bit_vec_command_get_offset(struct BitVecCommand */*notnull*/ instance); + +/** + * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bit_vec_command` module. + */ +BinaryOperation sp_bit_vec_command_get_operation(struct BitVecCommand */*notnull*/ instance); + +/** + * Set pixel data starting at the pixel offset on screen. + * + * The screen will continuously overwrite more pixel data without regarding the offset, meaning + * once the starting row is full, overwriting will continue on column 0. + * + * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. + * + * `new_bit = old_bit op sent_bit` + * + * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. + * + * The contained [`DisplayBitVec`] is always uncompressed. + * + * This function is part of the `bit_vec_command` module. + */ +struct BitVecCommand */*notnull*/ sp_bit_vec_command_new(DisplayBitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); + +/** + * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `bit_vec_command` module. + */ +void sp_bit_vec_command_set_bitvec(struct BitVecCommand */*notnull*/ instance, + DisplayBitVec */*notnull*/ value); + +/** + * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bit_vec_command` module. + */ +void sp_bit_vec_command_set_compression(struct BitVecCommand */*notnull*/ instance, + CompressionCode value); + +/** + * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bit_vec_command` module. + */ +void sp_bit_vec_command_set_offset(struct BitVecCommand */*notnull*/ instance, + Offset value); + +/** + * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. + * + * This function is part of the `bit_vec_command` module. + */ +void sp_bit_vec_command_set_operation(struct BitVecCommand */*notnull*/ instance, + BinaryOperation value); + +/** + *Tries to turn a [`BitVecCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `bit_vec_command` module. + */ +struct Packet *sp_bit_vec_command_try_into_packet(struct BitVecCommand */*notnull*/ instance); + +/** + * Calls method [`Bitmap::clone`]. * *Clones a [`Bitmap`] instance. * @@ -645,7 +757,110 @@ extern "C" { struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); /** - * Calls method [`servicepoint::Bitmap::data_ref_mut`]. + * Calls method [`BitmapCommand::clone`]. + * + *Clones a [`BitmapCommand`] instance. + * + * This function is part of the `bitmap_command` module. + */ +struct BitmapCommand */*notnull*/ sp_bitmap_command_clone(struct BitmapCommand */*notnull*/ instance); + +/** + *Deallocates a [`BitmapCommand`] instance. + * + * This function is part of the `bitmap_command` module. + */ +void sp_bitmap_command_free(struct BitmapCommand */*notnull*/ instance); + +/** + * Move the provided [Bitmap] into a new [BitmapCommand], + * leaving other fields as their default values. + * + * Rust equivalent: `BitmapCommand::from(bitmap)` + * + * This function is part of the `bitmap_command` module. + */ +struct BitmapCommand */*notnull*/ sp_bitmap_command_from_bitmap(struct Bitmap */*notnull*/ bitmap); + +/** + * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `bitmap_command` module. + */ +struct Bitmap */*notnull*/ sp_bitmap_command_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); + +/** + * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * + * This function is part of the `bitmap_command` module. + */ +CompressionCode sp_bitmap_command_get_compression(struct BitmapCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`BitmapCommand`]. + * + * This function is part of the `bitmap_command` module. + */ +void sp_bitmap_command_get_origin(struct BitmapCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Sets a window of pixels to the specified values. + * + * The passed [Bitmap] gets consumed. + * + * Returns: a new [BitmapCommand] instance. + * + * This function is part of the `bitmap_command` module. + */ +struct BitmapCommand */*notnull*/ sp_bitmap_command_new(struct Bitmap */*notnull*/ bitmap, + size_t origin_x, + size_t origin_y, + CompressionCode compression); + +/** + * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `bitmap_command` module. + */ +void sp_bitmap_command_set_bitmap(struct BitmapCommand */*notnull*/ instance, + struct Bitmap */*notnull*/ value); + +/** + * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. + * + * This function is part of the `bitmap_command` module. + */ +void sp_bitmap_command_set_compression(struct BitmapCommand */*notnull*/ instance, + CompressionCode value); + +/** + * Overwrites the origin field of the [`BitmapCommand`]. + * + * This function is part of the `bitmap_command` module. + */ +void sp_bitmap_command_set_origin(struct BitmapCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + *Tries to turn a [`BitmapCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `bitmap_command` module. + */ +struct Packet *sp_bitmap_command_try_into_packet(struct BitmapCommand */*notnull*/ instance); + +/** + * Calls method [`Bitmap::data_ref_mut`]. * * Gets an unsafe reference to the data of the [Bitmap] instance. * @@ -656,7 +871,7 @@ struct Bitmap */*notnull*/ sp_bitmap_clone(struct Bitmap */*notnull*/ instance); struct ByteSlice sp_bitmap_data_ref_mut(struct Bitmap */*notnull*/ instance); /** - * Calls method [`servicepoint::Bitmap::fill`]. + * Calls method [`Bitmap::fill`]. * * Sets the state of all cells in the grid. * @@ -688,7 +903,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, DisplayBitVec */*notnull*/ bitvec); /** - * Calls method [`servicepoint::Bitmap::get`]. + * Calls method [`Bitmap::get`]. * * Gets the current value at the specified position. * @@ -705,7 +920,7 @@ struct Bitmap *sp_bitmap_from_bitvec(size_t width, bool sp_bitmap_get(struct Bitmap */*notnull*/ instance, size_t x, size_t y); /** - * Calls method [`servicepoint::Bitmap::height`]. + * Calls method [`Bitmap::height`]. * * Gets the height. * @@ -775,7 +990,7 @@ struct Bitmap *sp_bitmap_new(size_t width, size_t height); struct Bitmap */*notnull*/ sp_bitmap_new_max_sized(void); /** - * Calls method [`servicepoint::Bitmap::set`]. + * Calls method [`Bitmap::set`]. * * Sets the value of the specified position. * @@ -810,7 +1025,7 @@ struct Packet *sp_bitmap_try_into_packet(struct Bitmap */*notnull*/ bitmap, CompressionCode compression); /** - * Calls method [`servicepoint::Bitmap::width`]. + * Calls method [`Bitmap::width`]. * * Gets the width. * @@ -819,236 +1034,110 @@ struct Packet *sp_bitmap_try_into_packet(struct Bitmap */*notnull*/ bitmap, size_t sp_bitmap_width(struct Bitmap */*notnull*/ instance); /** - * Calls method [`servicepoint::BitmapCommand::clone`]. - * - *Clones a [`BitmapCommand`] instance. - * - * This function is part of the `bitmapcommand` module. - */ -struct BitmapCommand */*notnull*/ sp_bitmapcommand_clone(struct BitmapCommand */*notnull*/ instance); - -/** - *Deallocates a [`BitmapCommand`] instance. - * - * This function is part of the `bitmapcommand` module. - */ -void sp_bitmapcommand_free(struct BitmapCommand */*notnull*/ instance); - -/** - * Move the provided [Bitmap] into a new [BitmapCommand], - * leaving other fields as their default values. - * - * Rust equivalent: `BitmapCommand::from(bitmap)` - * - * This function is part of the `bitmapcommand` module. - */ -struct BitmapCommand */*notnull*/ sp_bitmapcommand_from_bitmap(struct Bitmap */*notnull*/ bitmap); - -/** - * Gets a reference to the field `bitmap` of the [`servicepoint::BitmapCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `bitmapcommand` module. - */ -struct Bitmap */*notnull*/ sp_bitmapcommand_get_bitmap_mut(struct BitmapCommand */*notnull*/ instance); - -/** - * Gets the value of field `compression` of the [`servicepoint::BitmapCommand`]. - * - * This function is part of the `bitmapcommand` module. - */ -CompressionCode sp_bitmapcommand_get_compression(struct BitmapCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`BitmapCommand`]. - * - * This function is part of the `bitmapcommand` module. - */ -void sp_bitmapcommand_get_origin(struct BitmapCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Sets a window of pixels to the specified values. - * - * The passed [Bitmap] gets consumed. - * - * Returns: a new [BitmapCommand] instance. - * - * This function is part of the `bitmapcommand` module. - */ -struct BitmapCommand */*notnull*/ sp_bitmapcommand_new(struct Bitmap */*notnull*/ bitmap, - size_t origin_x, - size_t origin_y, - CompressionCode compression); - -/** - * Sets the value of field `bitmap` of the [`servicepoint::BitmapCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `bitmapcommand` module. - */ -void sp_bitmapcommand_set_bitmap(struct BitmapCommand */*notnull*/ instance, - struct Bitmap */*notnull*/ value); - -/** - * Sets the value of field `compression` of the [`servicepoint::BitmapCommand`]. - * - * This function is part of the `bitmapcommand` module. - */ -void sp_bitmapcommand_set_compression(struct BitmapCommand */*notnull*/ instance, - CompressionCode value); - -/** - * Overwrites the origin field of the [`BitmapCommand`]. - * - * This function is part of the `bitmapcommand` module. - */ -void sp_bitmapcommand_set_origin(struct BitmapCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - -/** - *Tries to turn a [`BitmapCommand`] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `bitmapcommand` module. - */ -struct Packet *sp_bitmapcommand_try_into_packet(struct BitmapCommand */*notnull*/ instance); - -/** - * Calls method [`servicepoint::BitVecCommand::clone`]. - * - *Clones a [`BitVecCommand`] instance. - * - * This function is part of the `bitveccommand` module. - */ -struct BitVecCommand */*notnull*/ sp_bitveccommand_clone(struct BitVecCommand */*notnull*/ instance); - -/** - *Deallocates a [`BitVecCommand`] instance. - * - * This function is part of the `bitveccommand` module. - */ -void sp_bitveccommand_free(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets a reference to the field `bitvec` of the [`servicepoint::BitVecCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `bitveccommand` module. - */ -DisplayBitVec */*notnull*/ sp_bitveccommand_get_bitvec_mut(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `compression` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `bitveccommand` module. - */ -CompressionCode sp_bitveccommand_get_compression(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `offset` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `bitveccommand` module. - */ -Offset sp_bitveccommand_get_offset(struct BitVecCommand */*notnull*/ instance); - -/** - * Gets the value of field `operation` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `bitveccommand` module. - */ -BinaryOperation sp_bitveccommand_get_operation(struct BitVecCommand */*notnull*/ instance); - -/** - * Set pixel data starting at the pixel offset on screen. - * - * The screen will continuously overwrite more pixel data without regarding the offset, meaning - * once the starting row is full, overwriting will continue on column 0. - * - * The [`BinaryOperation`] will be applied on the display comparing old and sent bit. - * - * `new_bit = old_bit op sent_bit` - * - * For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels. - * - * The contained [`DisplayBitVec`] is always uncompressed. - * - * This function is part of the `bitveccommand` module. - */ -struct BitVecCommand */*notnull*/ sp_bitveccommand_new(DisplayBitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); - -/** - * Sets the value of field `bitvec` of the [`servicepoint::BitVecCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `bitveccommand` module. - */ -void sp_bitveccommand_set_bitvec(struct BitVecCommand */*notnull*/ instance, - DisplayBitVec */*notnull*/ value); - -/** - * Sets the value of field `compression` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `bitveccommand` module. - */ -void sp_bitveccommand_set_compression(struct BitVecCommand */*notnull*/ instance, - CompressionCode value); - -/** - * Sets the value of field `offset` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `bitveccommand` module. - */ -void sp_bitveccommand_set_offset(struct BitVecCommand */*notnull*/ instance, - Offset value); - -/** - * Sets the value of field `operation` of the [`servicepoint::BitVecCommand`]. - * - * This function is part of the `bitveccommand` module. - */ -void sp_bitveccommand_set_operation(struct BitVecCommand */*notnull*/ instance, - BinaryOperation value); - -/** - *Tries to turn a [`BitVecCommand`] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `bitveccommand` module. - */ -struct Packet *sp_bitveccommand_try_into_packet(struct BitVecCommand */*notnull*/ instance); - -/** - * Calls method [`servicepoint::BrightnessGrid::clone`]. + * Calls method [`BrightnessGrid::clone`]. * *Clones a [`BrightnessGrid`] instance. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -BrightnessGrid */*notnull*/ sp_brightnessgrid_clone(BrightnessGrid */*notnull*/ instance); +BrightnessGrid */*notnull*/ sp_brightness_grid_clone(BrightnessGrid */*notnull*/ instance); + +/** + * Calls method [`BrightnessGridCommand::clone`]. + * + *Clones a [`BrightnessGridCommand`] instance. + * + * This function is part of the `brightness_grid_command` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightness_grid_command_clone(struct BrightnessGridCommand */*notnull*/ instance); + +/** + *Deallocates a [`BrightnessGridCommand`] instance. + * + * This function is part of the `brightness_grid_command` module. + */ +void sp_brightness_grid_command_free(struct BrightnessGridCommand */*notnull*/ instance); + +/** + * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + * leaving other fields as their default values. + * + * This function is part of the `brightness_grid_command` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightness_grid_command_from_grid(BrightnessGrid */*notnull*/ grid); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `brightness_grid_command` module. + */ +BrightnessGrid */*notnull*/ sp_brightness_grid_command_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the `brightness_grid_command` module. + */ +void sp_brightness_grid_command_get_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Set the brightness of individual tiles in a rectangular area of the display. + * + * The passed [BrightnessGrid] gets consumed. + * + * Returns: a new [BrightnessGridCommand] instance. + * + * This function is part of the `brightness_grid_command` module. + */ +struct BrightnessGridCommand */*notnull*/ sp_brightness_grid_command_new(BrightnessGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `brightness_grid_command` module. + */ +void sp_brightness_grid_command_set_grid(struct BrightnessGridCommand */*notnull*/ instance, + BrightnessGrid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`BrightnessGridCommand`]. + * + * This function is part of the `brightness_grid_command` module. + */ +void sp_brightness_grid_command_set_origin(struct BrightnessGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `brightness_grid_command` module. + */ +struct Packet *sp_brightness_grid_command_try_into_packet(struct BrightnessGridCommand */*notnull*/ instance); /** * Gets an unsafe reference to the data of the instance. * * The returned memory is valid for the lifetime of the grid. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -struct ByteSlice sp_brightnessgrid_data_ref_mut(BrightnessGrid */*notnull*/ instance); +struct ByteSlice sp_brightness_grid_data_ref_mut(BrightnessGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::BrightnessGrid::fill`]. + * Calls method [`BrightnessGrid::fill`]. * * Sets the state of all cells in the grid. * @@ -1056,20 +1145,20 @@ struct ByteSlice sp_brightnessgrid_data_ref_mut(BrightnessGrid */*notnull*/ inst * * - `value`: the value to set all cells to * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -void sp_brightnessgrid_fill(BrightnessGrid */*notnull*/ instance, - Brightness value); +void sp_brightness_grid_fill(BrightnessGrid */*notnull*/ instance, + Brightness value); /** *Deallocates a [`BrightnessGrid`] instance. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -void sp_brightnessgrid_free(BrightnessGrid */*notnull*/ instance); +void sp_brightness_grid_free(BrightnessGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::BrightnessGrid::get`]. + * Calls method [`BrightnessGrid::get`]. * * Gets the current value at the specified position. * @@ -1081,20 +1170,20 @@ void sp_brightnessgrid_free(BrightnessGrid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -Brightness sp_brightnessgrid_get(BrightnessGrid */*notnull*/ instance, - size_t x, - size_t y); +Brightness sp_brightness_grid_get(BrightnessGrid */*notnull*/ instance, + size_t x, + size_t y); /** - * Calls method [`servicepoint::BrightnessGrid::height`]. + * Calls method [`BrightnessGrid::height`]. * * Gets the height. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -size_t sp_brightnessgrid_height(BrightnessGrid */*notnull*/ instance); +size_t sp_brightness_grid_height(BrightnessGrid */*notnull*/ instance); /** * Loads a [BrightnessGrid] with the specified dimensions from the provided data. @@ -1103,11 +1192,11 @@ size_t sp_brightnessgrid_height(BrightnessGrid */*notnull*/ instance); * * returns: new [BrightnessGrid] instance, or NULL in case of an error. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -BrightnessGrid *sp_brightnessgrid_load(size_t width, - size_t height, - struct ByteSlice data); +BrightnessGrid *sp_brightness_grid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [BrightnessGrid] with the specified dimensions. @@ -1128,12 +1217,12 @@ BrightnessGrid *sp_brightnessgrid_load(size_t width, * sp_udp_free(connection); * ``` * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -BrightnessGrid */*notnull*/ sp_brightnessgrid_new(size_t width, size_t height); +BrightnessGrid */*notnull*/ sp_brightness_grid_new(size_t width, size_t height); /** - * Calls method [`servicepoint::BrightnessGrid::set`]. + * Calls method [`BrightnessGrid::set`]. * * Sets the value of the specified position. * @@ -1146,12 +1235,12 @@ BrightnessGrid */*notnull*/ sp_brightnessgrid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, - size_t x, - size_t y, - Brightness value); +void sp_brightness_grid_set(BrightnessGrid */*notnull*/ instance, + size_t x, + size_t y, + Brightness value); /** * Creates a [BrightnessGridCommand] and immediately turns that into a [Packet]. @@ -1160,112 +1249,114 @@ void sp_brightnessgrid_set(BrightnessGrid */*notnull*/ instance, * * Returns NULL in case of an error. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -struct Packet *sp_brightnessgrid_try_into_packet(BrightnessGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_brightness_grid_try_into_packet(BrightnessGrid */*notnull*/ grid, + size_t x, + size_t y); /** - * Calls method [`servicepoint::BrightnessGrid::width`]. + * Calls method [`BrightnessGrid::width`]. * * Gets the width. * - * This function is part of the `brightnessgrid` module. + * This function is part of the `brightness_grid` module. */ -size_t sp_brightnessgrid_width(BrightnessGrid */*notnull*/ instance); +size_t sp_brightness_grid_width(BrightnessGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::BrightnessGridCommand::clone`]. + * Calls method [`CharGrid::clone`]. * - *Clones a [`BrightnessGridCommand`] instance. + *Clones a [`CharGrid`] instance. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid` module. */ -struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_clone(struct BrightnessGridCommand */*notnull*/ instance); +CharGrid */*notnull*/ sp_char_grid_clone(CharGrid */*notnull*/ instance); /** - *Deallocates a [`BrightnessGridCommand`] instance. + * Calls method [`CharGridCommand::clone`]. * - * This function is part of the `brightnessgridcommand` module. + *Clones a [`CharGridCommand`] instance. + * + * This function is part of the `char_grid_command` module. */ -void sp_brightnessgridcommand_free(struct BrightnessGridCommand */*notnull*/ instance); +struct CharGridCommand */*notnull*/ sp_char_grid_command_clone(struct CharGridCommand */*notnull*/ instance); /** - * Moves the provided [BrightnessGrid] into a new [BrightnessGridCommand], + *Deallocates a [`CharGridCommand`] instance. + * + * This function is part of the `char_grid_command` module. + */ +void sp_char_grid_command_free(struct CharGridCommand */*notnull*/ instance); + +/** + * Moves the provided [CharGrid] into a new [CharGridCommand], * leaving other fields as their default values. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid_command` module. */ -struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_from_grid(BrightnessGrid */*notnull*/ grid); +struct CharGridCommand */*notnull*/ sp_char_grid_command_from_grid(CharGrid */*notnull*/ grid); /** - * Gets a reference to the field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. * * - The returned reference inherits the lifetime of object in which it is contained. * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid_command` module. */ -BrightnessGrid */*notnull*/ sp_brightnessgridcommand_get_grid_mut(struct BrightnessGridCommand */*notnull*/ instance); +CharGrid */*notnull*/ sp_char_grid_command_get_grid_mut(struct CharGridCommand */*notnull*/ instance); /** - * Reads the origin field of the [`BrightnessGridCommand`]. + * Reads the origin field of the [`CharGridCommand`]. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid_command` module. */ -void sp_brightnessgridcommand_get_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); +void sp_char_grid_command_get_origin(struct CharGridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); /** - * Set the brightness of individual tiles in a rectangular area of the display. + * Show UTF-8 encoded text on the screen. * - * The passed [BrightnessGrid] gets consumed. + * The passed [CharGrid] gets consumed. * - * Returns: a new [BrightnessGridCommand] instance. + * Returns: a new [CharGridCommand] instance. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid_command` module. */ -struct BrightnessGridCommand */*notnull*/ sp_brightnessgridcommand_new(BrightnessGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); +struct CharGridCommand */*notnull*/ sp_char_grid_command_new(CharGrid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); /** - * Sets the value of field `grid` of the [`servicepoint::BrightnessGridCommand`]. + * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. * The provided value is moved into the instance, potentially invalidating previously taken references. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid_command` module. */ -void sp_brightnessgridcommand_set_grid(struct BrightnessGridCommand */*notnull*/ instance, - BrightnessGrid */*notnull*/ value); +void sp_char_grid_command_set_grid(struct CharGridCommand */*notnull*/ instance, + CharGrid */*notnull*/ value); /** - * Overwrites the origin field of the [`BrightnessGridCommand`]. + * Overwrites the origin field of the [`CharGridCommand`]. * - * This function is part of the `brightnessgridcommand` module. + * This function is part of the `char_grid_command` module. */ -void sp_brightnessgridcommand_set_origin(struct BrightnessGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); +void sp_char_grid_command_set_origin(struct CharGridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); /** - *Tries to turn a [`BrightnessGridCommand`] into a [Packet]. + *Tries to turn a [`CharGridCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the `brightnessgridcommand` module. - */ -struct Packet *sp_brightnessgridcommand_try_into_packet(struct BrightnessGridCommand */*notnull*/ instance); - -/** - * Calls method [`servicepoint::CharGrid::clone`]. + * [Packet]: [`servicepoint::Packet`] * - *Clones a [`CharGrid`] instance. - * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid_command` module. */ -CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); +struct Packet *sp_char_grid_command_try_into_packet(struct CharGridCommand */*notnull*/ instance); /** * Sets the value of all cells in the grid. @@ -1275,16 +1366,16 @@ CharGrid */*notnull*/ sp_chargrid_clone(CharGrid */*notnull*/ instance); * - `value`: the value to set all cells to * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -void sp_chargrid_fill(CharGrid */*notnull*/ instance, uint32_t value); +void sp_char_grid_fill(CharGrid */*notnull*/ instance, uint32_t value); /** *Deallocates a [`CharGrid`] instance. * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -void sp_chargrid_free(CharGrid */*notnull*/ instance); +void sp_char_grid_free(CharGrid */*notnull*/ instance); /** * Returns the current value at the specified position. @@ -1297,27 +1388,27 @@ void sp_chargrid_free(CharGrid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -uint32_t sp_chargrid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); +uint32_t sp_char_grid_get(CharGrid */*notnull*/ instance, size_t x, size_t y); /** - * Calls method [`servicepoint::CharGrid::height`]. + * Calls method [`CharGrid::height`]. * * Gets the height. * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -size_t sp_chargrid_height(CharGrid */*notnull*/ instance); +size_t sp_char_grid_height(CharGrid */*notnull*/ instance); /** * Loads a [CharGrid] with the specified dimensions from the provided data. * * returns: new CharGrid or NULL in case of an error * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -CharGrid *sp_chargrid_load(size_t width, size_t height, struct ByteSlice data); +CharGrid *sp_char_grid_load(size_t width, size_t height, struct ByteSlice data); /** * Creates a new [CharGrid] with the specified dimensions. @@ -1333,9 +1424,9 @@ CharGrid *sp_chargrid_load(size_t width, size_t height, struct ByteSlice data); * sp_char_grid_free(grid); * ``` * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -CharGrid */*notnull*/ sp_chargrid_new(size_t width, size_t height); +CharGrid */*notnull*/ sp_char_grid_new(size_t width, size_t height); /** * Sets the value of the specified position in the grid. @@ -1352,12 +1443,12 @@ CharGrid */*notnull*/ sp_chargrid_new(size_t width, size_t height); * - when accessing `x` or `y` out of bounds * - when providing values that cannot be converted to Rust's `char`. * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -void sp_chargrid_set(CharGrid */*notnull*/ instance, - size_t x, - size_t y, - uint32_t value); +void sp_char_grid_set(CharGrid */*notnull*/ instance, + size_t x, + size_t y, + uint32_t value); /** * Creates a [CharGridCommand] and immediately turns that into a [Packet]. @@ -1366,119 +1457,36 @@ void sp_chargrid_set(CharGrid */*notnull*/ instance, * * Returns NULL in case of an error. * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -struct Packet *sp_chargrid_try_into_packet(CharGrid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_char_grid_try_into_packet(CharGrid */*notnull*/ grid, + size_t x, + size_t y); /** - * Calls method [`servicepoint::CharGrid::width`]. + * Calls method [`CharGrid::width`]. * * Gets the width. * - * This function is part of the `chargrid` module. + * This function is part of the `char_grid` module. */ -size_t sp_chargrid_width(CharGrid */*notnull*/ instance); +size_t sp_char_grid_width(CharGrid */*notnull*/ instance); /** - * Calls method [`servicepoint::CharGridCommand::clone`]. - * - *Clones a [`CharGridCommand`] instance. - * - * This function is part of the `chargridcommand` module. - */ -struct CharGridCommand */*notnull*/ sp_chargridcommand_clone(struct CharGridCommand */*notnull*/ instance); - -/** - *Deallocates a [`CharGridCommand`] instance. - * - * This function is part of the `chargridcommand` module. - */ -void sp_chargridcommand_free(struct CharGridCommand */*notnull*/ instance); - -/** - * Moves the provided [CharGrid] into a new [CharGridCommand], - * leaving other fields as their default values. - * - * This function is part of the `chargridcommand` module. - */ -struct CharGridCommand */*notnull*/ sp_chargridcommand_from_grid(CharGrid */*notnull*/ grid); - -/** - * Gets a reference to the field `grid` of the [`servicepoint::CharGridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `chargridcommand` module. - */ -CharGrid */*notnull*/ sp_chargridcommand_get_grid_mut(struct CharGridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`CharGridCommand`]. - * - * This function is part of the `chargridcommand` module. - */ -void sp_chargridcommand_get_origin(struct CharGridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Show UTF-8 encoded text on the screen. - * - * The passed [CharGrid] gets consumed. - * - * Returns: a new [CharGridCommand] instance. - * - * This function is part of the `chargridcommand` module. - */ -struct CharGridCommand */*notnull*/ sp_chargridcommand_new(CharGrid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - -/** - * Sets the value of field `grid` of the [`servicepoint::CharGridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `chargridcommand` module. - */ -void sp_chargridcommand_set_grid(struct CharGridCommand */*notnull*/ instance, - CharGrid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`CharGridCommand`]. - * - * This function is part of the `chargridcommand` module. - */ -void sp_chargridcommand_set_origin(struct CharGridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - -/** - *Tries to turn a [`CharGridCommand`] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `chargridcommand` module. - */ -struct Packet *sp_chargridcommand_try_into_packet(struct CharGridCommand */*notnull*/ instance); - -/** - * Calls method [`servicepoint::ClearCommand::clone`]. + * Calls method [`ClearCommand::clone`]. * *Clones a [`ClearCommand`] instance. * - * This function is part of the `clearcommand` module. + * This function is part of the `clear_command` module. */ -struct ClearCommand */*notnull*/ sp_clearcommand_clone(struct ClearCommand */*notnull*/ instance); +struct ClearCommand */*notnull*/ sp_clear_command_clone(struct ClearCommand */*notnull*/ instance); /** *Deallocates a [`ClearCommand`] instance. * - * This function is part of the `clearcommand` module. + * This function is part of the `clear_command` module. */ -void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); +void sp_clear_command_free(struct ClearCommand */*notnull*/ instance); /** * Set all pixels to the off state. @@ -1487,41 +1495,128 @@ void sp_clearcommand_free(struct ClearCommand */*notnull*/ instance); * * Returns: a new [`ClearCommand`] instance. * - * This function is part of the `clearcommand` module. + * This function is part of the `clear_command` module. */ -struct ClearCommand */*notnull*/ sp_clearcommand_new(void); +struct ClearCommand */*notnull*/ sp_clear_command_new(void); /** *Tries to turn a [`ClearCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the `clearcommand` module. + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `clear_command` module. */ -struct Packet *sp_clearcommand_try_into_packet(struct ClearCommand */*notnull*/ instance); +struct Packet *sp_clear_command_try_into_packet(struct ClearCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437Grid::clone`]. + * Calls method [`Cp437Grid::clone`]. * *Clones a [`Cp437Grid`] instance. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -Cp437Grid */*notnull*/ sp_cp437grid_clone(Cp437Grid */*notnull*/ instance); +Cp437Grid */*notnull*/ sp_cp437_grid_clone(Cp437Grid */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437Grid::data_ref_mut`]. + * Calls method [`Cp437GridCommand::clone`]. + * + *Clones a [`Cp437GridCommand`] instance. + * + * This function is part of the `cp437_grid_command` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437_grid_command_clone(struct Cp437GridCommand */*notnull*/ instance); + +/** + *Deallocates a [`Cp437GridCommand`] instance. + * + * This function is part of the `cp437_grid_command` module. + */ +void sp_cp437_grid_command_free(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], + * leaving other fields as their default values. + * + * This function is part of the `cp437_grid_command` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437_grid_command_from_grid(Cp437Grid */*notnull*/ grid); + +/** + * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. + * + * - The returned reference inherits the lifetime of object in which it is contained. + * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. + * + * This function is part of the `cp437_grid_command` module. + */ +Cp437Grid */*notnull*/ sp_cp437_grid_command_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Reads the origin field of the [`Cp437GridCommand`]. + * + * This function is part of the `cp437_grid_command` module. + */ +void sp_cp437_grid_command_get_origin(struct Cp437GridCommand */*notnull*/ command, + size_t */*notnull*/ origin_x, + size_t */*notnull*/ origin_y); + +/** + * Show text on the screen. + * + * The text is sent in the form of a 2D grid of [CP-437] encoded characters. + * + * The origin is relative to the top-left of the display. + * + * This function is part of the `cp437_grid_command` module. + */ +struct Cp437GridCommand */*notnull*/ sp_cp437_grid_command_new(Cp437Grid */*notnull*/ grid, + size_t origin_x, + size_t origin_y); + +/** + * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. + * The provided value is moved into the instance, potentially invalidating previously taken references. + * + * This function is part of the `cp437_grid_command` module. + */ +void sp_cp437_grid_command_set_grid(struct Cp437GridCommand */*notnull*/ instance, + Cp437Grid */*notnull*/ value); + +/** + * Overwrites the origin field of the [`Cp437GridCommand`]. + * + * This function is part of the `cp437_grid_command` module. + */ +void sp_cp437_grid_command_set_origin(struct Cp437GridCommand */*notnull*/ command, + size_t origin_x, + size_t origin_y); + +/** + *Tries to turn a [`Cp437GridCommand`] into a [Packet]. + * + * Returns: NULL or a [Packet] containing the command. + * + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `cp437_grid_command` module. + */ +struct Packet *sp_cp437_grid_command_try_into_packet(struct Cp437GridCommand */*notnull*/ instance); + +/** + * Calls method [`Cp437Grid::data_ref_mut`]. * * Gets an unsafe reference to the data of the grid. * * The returned memory is valid for the lifetime of the instance. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); +struct ByteSlice sp_cp437_grid_data_ref_mut(Cp437Grid */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437Grid::fill`]. + * Calls method [`Cp437Grid::fill`]. * * Sets the state of all cells in the grid. * @@ -1529,19 +1624,19 @@ struct ByteSlice sp_cp437grid_data_ref_mut(Cp437Grid */*notnull*/ instance); * * - `value`: the value to set all cells to * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -void sp_cp437grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); +void sp_cp437_grid_fill(Cp437Grid */*notnull*/ instance, uint8_t value); /** *Deallocates a [`Cp437Grid`] instance. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -void sp_cp437grid_free(Cp437Grid */*notnull*/ instance); +void sp_cp437_grid_free(Cp437Grid */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437Grid::get`]. + * Calls method [`Cp437Grid::get`]. * * Gets the current value at the specified position. * @@ -1553,39 +1648,39 @@ void sp_cp437grid_free(Cp437Grid */*notnull*/ instance); * * - when accessing `x` or `y` out of bounds * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -uint8_t sp_cp437grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); +uint8_t sp_cp437_grid_get(Cp437Grid */*notnull*/ instance, size_t x, size_t y); /** - * Calls method [`servicepoint::Cp437Grid::height`]. + * Calls method [`Cp437Grid::height`]. * * Gets the height. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -size_t sp_cp437grid_height(Cp437Grid */*notnull*/ instance); +size_t sp_cp437_grid_height(Cp437Grid */*notnull*/ instance); /** * Loads a [Cp437Grid] with the specified dimensions from the provided data. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -Cp437Grid *sp_cp437grid_load(size_t width, - size_t height, - struct ByteSlice data); +Cp437Grid *sp_cp437_grid_load(size_t width, + size_t height, + struct ByteSlice data); /** * Creates a new [Cp437Grid] with the specified dimensions. * * returns: [Cp437Grid] initialized to 0. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -Cp437Grid */*notnull*/ sp_cp437grid_new(size_t width, size_t height); +Cp437Grid */*notnull*/ sp_cp437_grid_new(size_t width, size_t height); /** - * Calls method [`servicepoint::Cp437Grid::set`]. + * Calls method [`Cp437Grid::set`]. * * Sets the value of the specified position. * @@ -1598,12 +1693,12 @@ Cp437Grid */*notnull*/ sp_cp437grid_new(size_t width, size_t height); * * - when accessing `x` or `y` out of bounds * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, - size_t x, - size_t y, - uint8_t value); +void sp_cp437_grid_set(Cp437Grid */*notnull*/ instance, + size_t x, + size_t y, + uint8_t value); /** * Creates a [Cp437GridCommand] and immediately turns that into a [Packet]. @@ -1612,126 +1707,43 @@ void sp_cp437grid_set(Cp437Grid */*notnull*/ instance, * * Returns NULL in case of an error. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -struct Packet *sp_cp437grid_try_into_packet(Cp437Grid */*notnull*/ grid, - size_t x, - size_t y); +struct Packet *sp_cp437_grid_try_into_packet(Cp437Grid */*notnull*/ grid, + size_t x, + size_t y); /** - * Calls method [`servicepoint::Cp437Grid::width`]. + * Calls method [`Cp437Grid::width`]. * * Gets the width. * - * This function is part of the `cp437grid` module. + * This function is part of the `cp437_grid` module. */ -size_t sp_cp437grid_width(Cp437Grid */*notnull*/ instance); +size_t sp_cp437_grid_width(Cp437Grid */*notnull*/ instance); /** - * Calls method [`servicepoint::Cp437GridCommand::clone`]. - * - *Clones a [`Cp437GridCommand`] instance. - * - * This function is part of the `cp437gridcommand` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_clone(struct Cp437GridCommand */*notnull*/ instance); - -/** - *Deallocates a [`Cp437GridCommand`] instance. - * - * This function is part of the `cp437gridcommand` module. - */ -void sp_cp437gridcommand_free(struct Cp437GridCommand */*notnull*/ instance); - -/** - * Moves the provided [Cp437Grid] into a new [Cp437GridCommand], - * leaving other fields as their default values. - * - * This function is part of the `cp437gridcommand` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_from_grid(Cp437Grid */*notnull*/ grid); - -/** - * Gets a reference to the field `grid` of the [`servicepoint::Cp437GridCommand`]. - * - * - The returned reference inherits the lifetime of object in which it is contained. - * - The returned pointer may not be used in a function that consumes the instance, e.g. to create a command. - * - * This function is part of the `cp437gridcommand` module. - */ -Cp437Grid */*notnull*/ sp_cp437gridcommand_get_grid_mut(struct Cp437GridCommand */*notnull*/ instance); - -/** - * Reads the origin field of the [`Cp437GridCommand`]. - * - * This function is part of the `cp437gridcommand` module. - */ -void sp_cp437gridcommand_get_origin(struct Cp437GridCommand */*notnull*/ command, - size_t */*notnull*/ origin_x, - size_t */*notnull*/ origin_y); - -/** - * Show text on the screen. - * - * The text is sent in the form of a 2D grid of [CP-437] encoded characters. - * - * The origin is relative to the top-left of the display. - * - * This function is part of the `cp437gridcommand` module. - */ -struct Cp437GridCommand */*notnull*/ sp_cp437gridcommand_new(Cp437Grid */*notnull*/ grid, - size_t origin_x, - size_t origin_y); - -/** - * Sets the value of field `grid` of the [`servicepoint::Cp437GridCommand`]. - * The provided value is moved into the instance, potentially invalidating previously taken references. - * - * This function is part of the `cp437gridcommand` module. - */ -void sp_cp437gridcommand_set_grid(struct Cp437GridCommand */*notnull*/ instance, - Cp437Grid */*notnull*/ value); - -/** - * Overwrites the origin field of the [`Cp437GridCommand`]. - * - * This function is part of the `cp437gridcommand` module. - */ -void sp_cp437gridcommand_set_origin(struct Cp437GridCommand */*notnull*/ command, - size_t origin_x, - size_t origin_y); - -/** - *Tries to turn a [`Cp437GridCommand`] into a [Packet]. - * - * Returns: NULL or a [Packet] containing the command. - * - * This function is part of the `cp437gridcommand` module. - */ -struct Packet *sp_cp437gridcommand_try_into_packet(struct Cp437GridCommand */*notnull*/ instance); - -/** - * Calls method [`servicepoint::DisplayBitVec::as_raw_mut_slice`]. + * Calls method [`DisplayBitVec::as_raw_mut_slice`]. * * Gets an unsafe reference to the data of the [DisplayBitVec] instance. * * The returned memory is valid for the lifetime of the bitvec. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -struct ByteSlice sp_displaybitvec_as_raw_mut_slice(DisplayBitVec */*notnull*/ instance); +struct ByteSlice sp_display_bit_vec_as_raw_mut_slice(DisplayBitVec */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::clone`]. + * Calls method [`DisplayBitVec::clone`]. * *Clones a [`DisplayBitVec`] instance. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ instance); +DisplayBitVec */*notnull*/ sp_display_bit_vec_clone(DisplayBitVec */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::fill`]. + * Calls method [`DisplayBitVec::fill`]. * * Sets the value of all bits. * @@ -1739,16 +1751,16 @@ DisplayBitVec */*notnull*/ sp_displaybitvec_clone(DisplayBitVec */*notnull*/ ins * * - `value`: the value to set all bits to * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -void sp_displaybitvec_fill(DisplayBitVec */*notnull*/ instance, bool value); +void sp_display_bit_vec_fill(DisplayBitVec */*notnull*/ instance, bool value); /** *Deallocates a [`DisplayBitVec`] instance. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -void sp_displaybitvec_free(DisplayBitVec */*notnull*/ instance); +void sp_display_bit_vec_free(DisplayBitVec */*notnull*/ instance); /** * Gets the value of a bit. @@ -1764,36 +1776,36 @@ void sp_displaybitvec_free(DisplayBitVec */*notnull*/ instance); * * - when accessing `index` out of bounds * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -bool sp_displaybitvec_get(DisplayBitVec */*notnull*/ instance, size_t index); +bool sp_display_bit_vec_get(DisplayBitVec */*notnull*/ instance, size_t index); /** - * Calls method [`servicepoint::DisplayBitVec::is_empty`]. + * Calls method [`DisplayBitVec::is_empty`]. * * Returns true if length is 0. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -bool sp_displaybitvec_is_empty(DisplayBitVec */*notnull*/ instance); +bool sp_display_bit_vec_is_empty(DisplayBitVec */*notnull*/ instance); /** - * Calls method [`servicepoint::DisplayBitVec::len`]. + * Calls method [`DisplayBitVec::len`]. * * Gets the length in bits. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -size_t sp_displaybitvec_len(DisplayBitVec */*notnull*/ instance); +size_t sp_display_bit_vec_len(DisplayBitVec */*notnull*/ instance); /** * Interpret the data as a series of bits and load then into a new [DisplayBitVec] instance. * * returns: [DisplayBitVec] instance containing data. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -DisplayBitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); +DisplayBitVec */*notnull*/ sp_display_bit_vec_load(struct ByteSlice data); /** * Creates a new [DisplayBitVec] instance. @@ -1808,12 +1820,12 @@ DisplayBitVec */*notnull*/ sp_displaybitvec_load(struct ByteSlice data); * * - when `size` is not divisible by 8. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -DisplayBitVec */*notnull*/ sp_displaybitvec_new(size_t size); +DisplayBitVec */*notnull*/ sp_display_bit_vec_new(size_t size); /** - * Calls method [`servicepoint::DisplayBitVec::set`]. + * Calls method [`DisplayBitVec::set`]. * * Sets the value of a bit. * @@ -1826,11 +1838,11 @@ DisplayBitVec */*notnull*/ sp_displaybitvec_new(size_t size); * * - when accessing `index` out of bounds * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -void sp_displaybitvec_set(DisplayBitVec */*notnull*/ instance, - size_t index, - bool value); +void sp_display_bit_vec_set(DisplayBitVec */*notnull*/ instance, + size_t index, + bool value); /** * Creates a [BitVecCommand] and immediately turns that into a [Packet]. @@ -1839,12 +1851,12 @@ void sp_displaybitvec_set(DisplayBitVec */*notnull*/ instance, * * Returns NULL in case of an error. * - * This function is part of the `displaybitvec` module. + * This function is part of the `display_bit_vec` module. */ -struct Packet *sp_displaybitvec_try_into_packet(DisplayBitVec */*notnull*/ bitvec, - size_t offset, - BinaryOperation operation, - CompressionCode compression); +struct Packet *sp_display_bit_vec_try_into_packet(DisplayBitVec */*notnull*/ bitvec, + size_t offset, + BinaryOperation operation, + CompressionCode compression); /** * Call this function at the beginning of main to enable rust logging controlled by the @@ -1855,54 +1867,56 @@ struct Packet *sp_displaybitvec_try_into_packet(DisplayBitVec */*notnull*/ bitve void sp_envlogger_init(void); /** - * Calls method [`servicepoint::FadeOutCommand::clone`]. + * Calls method [`FadeOutCommand::clone`]. * *Clones a [`FadeOutCommand`] instance. * - * This function is part of the `fadeoutcommand` module. + * This function is part of the `fade_out_command` module. */ -struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_clone(struct FadeOutCommand */*notnull*/ instance); +struct FadeOutCommand */*notnull*/ sp_fade_out_command_clone(struct FadeOutCommand */*notnull*/ instance); /** *Deallocates a [`FadeOutCommand`] instance. * - * This function is part of the `fadeoutcommand` module. + * This function is part of the `fade_out_command` module. */ -void sp_fadeoutcommand_free(struct FadeOutCommand */*notnull*/ instance); +void sp_fade_out_command_free(struct FadeOutCommand */*notnull*/ instance); /** * A yet-to-be-tested command. * * Returns: a new [`FadeOutCommand`] instance. * - * This function is part of the `fadeoutcommand` module. + * This function is part of the `fade_out_command` module. */ -struct FadeOutCommand */*notnull*/ sp_fadeoutcommand_new(void); +struct FadeOutCommand */*notnull*/ sp_fade_out_command_new(void); /** *Tries to turn a [`FadeOutCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the `fadeoutcommand` module. + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `fade_out_command` module. */ -struct Packet *sp_fadeoutcommand_try_into_packet(struct FadeOutCommand */*notnull*/ instance); +struct Packet *sp_fade_out_command_try_into_packet(struct FadeOutCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::GenericCommand::clone`]. + * Calls method [`GenericCommand::clone`]. * *Clones a [`GenericCommand`] instance. * - * This function is part of the `genericcommand` module. + * This function is part of the `generic_command` module. */ -struct GenericCommand */*notnull*/ sp_genericcommand_clone(struct GenericCommand */*notnull*/ instance); +struct GenericCommand */*notnull*/ sp_generic_command_clone(struct GenericCommand */*notnull*/ instance); /** *Deallocates a [`GenericCommand`] instance. * - * This function is part of the `genericcommand` module. + * This function is part of the `generic_command` module. */ -void sp_genericcommand_free(struct GenericCommand */*notnull*/ instance); +void sp_generic_command_free(struct GenericCommand */*notnull*/ instance); /** * Tries to turn a [Packet] into a [GenericCommand]. @@ -1911,9 +1925,9 @@ void sp_genericcommand_free(struct GenericCommand */*notnull*/ instance); * * Returns: pointer to new [GenericCommand] instance or NULL if parsing failed. * - * This function is part of the `genericcommand` module. + * This function is part of the `generic_command` module. */ -struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Packet */*notnull*/ packet); +struct GenericCommand */*notnull*/ sp_generic_command_try_from_packet(struct Packet */*notnull*/ packet); /** * Tries to turn a [GenericCommand] into a [Packet]. @@ -1921,74 +1935,76 @@ struct GenericCommand */*notnull*/ sp_genericcommand_try_from_packet(struct Pack * * Returns tag [CommandTag::Invalid] in case of an error. * - * This function is part of the `genericcommand` module. + * This function is part of the `generic_command` module. */ -struct Packet *sp_genericcommand_try_into_packet(struct GenericCommand */*notnull*/ command); +struct Packet *sp_generic_command_try_into_packet(struct GenericCommand */*notnull*/ command); /** - * Calls method [`servicepoint::GlobalBrightnessCommand::clone`]. + * Calls method [`GlobalBrightnessCommand::clone`]. * *Clones a [`GlobalBrightnessCommand`] instance. * - * This function is part of the `globalbrightnesscommand` module. + * This function is part of the `global_brightness_command` module. */ -struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_clone(struct GlobalBrightnessCommand */*notnull*/ instance); +struct GlobalBrightnessCommand */*notnull*/ sp_global_brightness_command_clone(struct GlobalBrightnessCommand */*notnull*/ instance); /** *Deallocates a [`GlobalBrightnessCommand`] instance. * - * This function is part of the `globalbrightnesscommand` module. + * This function is part of the `global_brightness_command` module. */ -void sp_globalbrightnesscommand_free(struct GlobalBrightnessCommand */*notnull*/ instance); +void sp_global_brightness_command_free(struct GlobalBrightnessCommand */*notnull*/ instance); /** * Gets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * - * This function is part of the `globalbrightnesscommand` module. + * This function is part of the `global_brightness_command` module. */ -Brightness sp_globalbrightnesscommand_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); +Brightness sp_global_brightness_command_get_brightness(struct GlobalBrightnessCommand */*notnull*/ instance); /** * Set the brightness of all tiles to the same value. * * Returns: a new [GlobalBrightnessCommand] instance. * - * This function is part of the `globalbrightnesscommand` module. + * This function is part of the `global_brightness_command` module. */ -struct GlobalBrightnessCommand */*notnull*/ sp_globalbrightnesscommand_new(Brightness brightness); +struct GlobalBrightnessCommand */*notnull*/ sp_global_brightness_command_new(Brightness brightness); /** * Sets the value of field `brightness` of the [`servicepoint::GlobalBrightnessCommand`]. * - * This function is part of the `globalbrightnesscommand` module. + * This function is part of the `global_brightness_command` module. */ -void sp_globalbrightnesscommand_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, - Brightness value); +void sp_global_brightness_command_set_brightness(struct GlobalBrightnessCommand */*notnull*/ instance, + Brightness value); /** *Tries to turn a [`GlobalBrightnessCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the `globalbrightnesscommand` module. + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `global_brightness_command` module. */ -struct Packet *sp_globalbrightnesscommand_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ instance); +struct Packet *sp_global_brightness_command_try_into_packet(struct GlobalBrightnessCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::HardResetCommand::clone`]. + * Calls method [`HardResetCommand::clone`]. * *Clones a [`HardResetCommand`] instance. * - * This function is part of the `hardresetcommand` module. + * This function is part of the `hard_reset_command` module. */ -struct HardResetCommand */*notnull*/ sp_hardresetcommand_clone(struct HardResetCommand */*notnull*/ instance); +struct HardResetCommand */*notnull*/ sp_hard_reset_command_clone(struct HardResetCommand */*notnull*/ instance); /** *Deallocates a [`HardResetCommand`] instance. * - * This function is part of the `hardresetcommand` module. + * This function is part of the `hard_reset_command` module. */ -void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); +void sp_hard_reset_command_free(struct HardResetCommand */*notnull*/ instance); /** * Kills the udp daemon on the display, which usually results in a restart. @@ -1997,21 +2013,23 @@ void sp_hardresetcommand_free(struct HardResetCommand */*notnull*/ instance); * * Returns: a new [`HardResetCommand`] instance. * - * This function is part of the `hardresetcommand` module. + * This function is part of the `hard_reset_command` module. */ -struct HardResetCommand */*notnull*/ sp_hardresetcommand_new(void); +struct HardResetCommand */*notnull*/ sp_hard_reset_command_new(void); /** *Tries to turn a [`HardResetCommand`] into a [Packet]. * * Returns: NULL or a [Packet] containing the command. * - * This function is part of the `hardresetcommand` module. + * [Packet]: [`servicepoint::Packet`] + * + * This function is part of the `hard_reset_command` module. */ -struct Packet *sp_hardresetcommand_try_into_packet(struct HardResetCommand */*notnull*/ instance); +struct Packet *sp_hard_reset_command_try_into_packet(struct HardResetCommand */*notnull*/ instance); /** - * Calls method [`servicepoint::Packet::clone`]. + * Calls method [`Packet::clone`]. * *Clones a [`Packet`] instance. * @@ -2116,9 +2134,9 @@ bool sp_sp_u16_to_command_code(uint16_t code, /** *Deallocates a [`UdpSocket`] instance. * - * This function is part of the `udpsocket` module. + * This function is part of the `udp_socket` module. */ -void sp_udpsocket_free(struct UdpSocket */*notnull*/ instance); +void sp_udp_socket_free(struct UdpSocket */*notnull*/ instance); /** * Creates a new instance of [UdpSocket]. @@ -2133,9 +2151,9 @@ void sp_udpsocket_free(struct UdpSocket */*notnull*/ instance); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the `udpsocket` module. + * This function is part of the `udp_socket` module. */ -struct UdpSocket *sp_udpsocket_open(char */*notnull*/ host); +struct UdpSocket *sp_udp_socket_open(char */*notnull*/ host); /** * Creates a new instance of [UdpSocket]. @@ -2150,13 +2168,13 @@ struct UdpSocket *sp_udpsocket_open(char */*notnull*/ host); * sp_udp_send_command(connection, sp_command_clear()); * ``` * - * This function is part of the `udpsocket` module. + * This function is part of the `udp_socket` module. */ -struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, - uint8_t ip2, - uint8_t ip3, - uint8_t ip4, - uint16_t port); +struct UdpSocket *sp_udp_socket_open_ipv4(uint8_t ip1, + uint8_t ip2, + uint8_t ip3, + uint8_t ip4, + uint16_t port); /** * Sends a [GenericCommand] to the display using the [UdpSocket]. @@ -2171,10 +2189,10 @@ struct UdpSocket *sp_udpsocket_open_ipv4(uint8_t ip1, * sp_udp_send_command(connection, sp_command_brightness(5)); * ``` * - * This function is part of the `udpsocket` module. + * This function is part of the `udp_socket` module. */ -bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, - struct GenericCommand */*notnull*/ command); +bool sp_udp_socket_send_command(struct UdpSocket */*notnull*/ connection, + struct GenericCommand */*notnull*/ command); /** * Sends a [Header] to the display using the [UdpSocket]. @@ -2187,10 +2205,10 @@ bool sp_udpsocket_send_command(struct UdpSocket */*notnull*/ connection, * sp_udp_send_header(connection, sp_command_brightness(5)); * ``` * - * This function is part of the `udpsocket` module. + * This function is part of the `udp_socket` module. */ -bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, - struct Header header); +bool sp_udp_socket_send_header(struct UdpSocket */*notnull*/ udp_connection, + struct Header header); /** * Sends a [Packet] to the display using the [UdpSocket]. @@ -2199,10 +2217,10 @@ bool sp_udpsocket_send_header(struct UdpSocket */*notnull*/ udp_connection, * * returns: true in case of success * - * This function is part of the `udpsocket` module. + * This function is part of the `udp_socket` module. */ -bool sp_udpsocket_send_packet(struct UdpSocket */*notnull*/ connection, - struct Packet */*notnull*/ packet); +bool sp_udp_socket_send_packet(struct UdpSocket */*notnull*/ connection, + struct Packet */*notnull*/ packet); #ifdef __cplusplus } // extern "C" diff --git a/src/macros.rs b/src/macros.rs index 34e94e8..89d3369 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -46,7 +46,7 @@ macro_rules! wrap_method { $impl:block; ) => { paste::paste! { - $crate::macros::wrap_functions!([< $object_type:lower >]; + $crate::macros::wrap_functions!(associate $object_type; $(#[$meta])* fn $function( $instance: $ref_or_mut ::core::ptr::NonNull<$object_type> @@ -246,7 +246,7 @@ macro_rules! wrap_functions { )+ ) => { ::paste::paste! { - $crate::macros::wrap_functions!{[< $object_type:lower >]; + $crate::macros::wrap_functions!{[< $object_type:snake >]; $( $(#[$meta])+ fn $function($($param_name: $param_modifier $param_type),*) $(-> $return_modifier $return_type)?