add NULL checks - command

This commit is contained in:
Vinzenz Schroeter 2024-10-13 18:56:29 +02:00
parent bcb73999da
commit 2e1cb6f681
5 changed files with 186 additions and 105 deletions

View file

@ -4,7 +4,7 @@
use std::ptr::null_mut; use std::ptr::null_mut;
use servicepoint::{Brightness, Origin}; use servicepoint::{Brightness, Command, Origin};
use crate::{ use crate::{
SPBitVec, SPBrightnessGrid, SPCompressionCode, SPCp437Grid, SPPacket, SPBitVec, SPBrightnessGrid, SPCompressionCode, SPCp437Grid, SPPacket,
@ -15,7 +15,7 @@ use crate::{
/// ///
/// This struct and associated functions implement the UDP protocol for the display. /// This struct and associated functions implement the UDP protocol for the display.
/// ///
/// To send a [SPCommand], use a `SPConnection`. /// To send a [SPCommand], use a [SPConnection].
/// ///
/// # Examples /// # Examples
/// ///
@ -31,18 +31,22 @@ impl Clone for SPCommand {
} }
} }
/// Tries to turn a `SPPacket` into a [SPCommand]. /// Tries to turn a [SPPacket] into a [SPCommand].
/// ///
/// The packet is deallocated in the process. /// The packet is deallocated in the process.
/// ///
/// Returns: pointer to new [SPCommand] instance or NULL /// Returns: pointer to new [SPCommand] instance or NULL if parsing failed.
///
/// # Panics
///
/// - when `packet` is NULL
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `SPPacket` points to a valid instance of `SPPacket` /// - [SPPacket] points to a valid instance of [SPPacket]
/// - `SPPacket` is not used concurrently or after this call /// - [SPPacket] is not used concurrently or after this call
/// - the result is checked for NULL /// - the result is checked for NULL
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
@ -59,6 +63,12 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
/// Clones a [SPCommand] instance. /// Clones a [SPCommand] instance.
/// ///
/// returns: new [SPCommand] instance. Will never return NULL.
///
/// # Panics
///
/// - when `command` is NULL
///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
@ -71,14 +81,17 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
pub unsafe extern "C" fn sp_command_clone( pub unsafe extern "C" fn sp_command_clone(
command: *const SPCommand, command: *const SPCommand,
) -> *mut SPCommand { ) -> *mut SPCommand {
Box::into_raw(Box::new((*command).clone())) assert!(!command.is_null());
let result = Box::into_raw(Box::new((*command).clone()));
assert!(!result.is_null());
result
} }
/// Set all pixels to the off state. /// Set all pixels to the off state.
/// ///
/// Does not affect brightness. /// Does not affect brightness.
/// ///
/// Returns: a new `Command::Clear` instance. Will never return NULL. /// Returns: a new [Command::Clear] instance. Will never return NULL.
/// ///
/// # Examples /// # Examples
/// ///
@ -94,14 +107,16 @@ pub unsafe extern "C" fn sp_command_clone(
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_clear() -> *mut SPCommand { pub unsafe extern "C" fn sp_command_clear() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Clear))) let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::Clear)));
assert!(!result.is_null());
result
} }
/// Kills the udp daemon on the display, which usually results in a restart. /// Kills the udp daemon on the display, which usually results in a restart.
/// ///
/// Please do not send this in your normal program flow. /// Please do not send this in your normal program flow.
/// ///
/// Returns: a new `Command::HardReset` instance. Will never return NULL. /// Returns: a new [Command::HardReset] instance. Will never return NULL.
/// ///
/// # Safety /// # Safety
/// ///
@ -111,7 +126,9 @@ pub unsafe extern "C" fn sp_command_clear() -> *mut SPCommand {
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_hard_reset() -> *mut SPCommand { pub unsafe extern "C" fn sp_command_hard_reset() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::HardReset))) let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::HardReset)));
assert!(!result.is_null());
result
} }
/// A yet-to-be-tested command. /// A yet-to-be-tested command.
@ -126,12 +143,14 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> *mut SPCommand {
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_fade_out() -> *mut SPCommand { pub unsafe extern "C" fn sp_command_fade_out() -> *mut SPCommand {
Box::into_raw(Box::new(SPCommand(servicepoint::Command::FadeOut))) let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::FadeOut)));
assert!(!result.is_null());
result
} }
/// Set the brightness of all tiles to the same value. /// Set the brightness of all tiles to the same value.
/// ///
/// Returns: a new `Command::Brightness` instance. Will never return NULL. /// Returns: a new [Command::Brightness] instance. Will never return NULL.
/// ///
/// # Panics /// # Panics
/// ///
@ -149,16 +168,22 @@ pub unsafe extern "C" fn sp_command_brightness(
) -> *mut SPCommand { ) -> *mut SPCommand {
let brightness = let brightness =
Brightness::try_from(brightness).expect("invalid brightness"); Brightness::try_from(brightness).expect("invalid brightness");
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Brightness( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::Brightness(
brightness, brightness,
)))) ))));
assert!(!result.is_null());
result
} }
/// Set the brightness of individual tiles in a rectangular area of the display. /// Set the brightness of individual tiles in a rectangular area of the display.
/// ///
/// The passed [SPBrightnessGrid] gets consumed. /// The passed [SPBrightnessGrid] gets consumed.
/// ///
/// Returns: a new `Command::CharBrightness` instance. Will never return NULL. /// Returns: a new [Command::CharBrightness] instance. Will never return NULL.
///
/// # Panics
///
/// - when `grid` is NULL
/// ///
/// # Safety /// # Safety
/// ///
@ -174,11 +199,14 @@ pub unsafe extern "C" fn sp_command_char_brightness(
y: usize, y: usize,
grid: *mut SPBrightnessGrid, grid: *mut SPBrightnessGrid,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!grid.is_null());
let byte_grid = *Box::from_raw(grid); let byte_grid = *Box::from_raw(grid);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::CharBrightness( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::CharBrightness(
Origin::new(x, y), Origin::new(x, y),
byte_grid.0, byte_grid.0,
)))) ))));
assert!(!result.is_null());
result
} }
/// Set pixel data starting at the pixel offset on screen. /// Set pixel data starting at the pixel offset on screen.
@ -190,7 +218,12 @@ pub unsafe extern "C" fn sp_command_char_brightness(
/// ///
/// The passed [SPBitVec] gets consumed. /// The passed [SPBitVec] gets consumed.
/// ///
/// Returns: a new `Command::BitmapLinear` instance. Will never return NULL. /// Returns: a new [Command::BitmapLinear] instance. Will never return NULL.
///
/// # Panics
///
/// - when `bit_vec` is null
/// - when `compression_code` is not a valid value
/// ///
/// # Safety /// # Safety
/// ///
@ -207,12 +240,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!bit_vec.is_null());
let bit_vec = *Box::from_raw(bit_vec); let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinear( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinear(
offset, offset,
bit_vec.into(), bit_vec.into(),
compression.try_into().expect("invalid compression code"), compression.try_into().expect("invalid compression code"),
)))) ))));
assert!(!result.is_null());
result
} }
/// Set pixel data according to an and-mask starting at the offset. /// Set pixel data according to an and-mask starting at the offset.
@ -224,7 +260,12 @@ pub unsafe extern "C" fn sp_command_bitmap_linear(
/// ///
/// The passed [SPBitVec] gets consumed. /// The passed [SPBitVec] gets consumed.
/// ///
/// Returns: a new `Command::BitmapLinearAnd` instance. Will never return NULL. /// Returns: a new [Command::BitmapLinearAnd] instance. Will never return NULL.
///
/// # Panics
///
/// - when `bit_vec` is null
/// - when `compression_code` is not a valid value
/// ///
/// # Safety /// # Safety
/// ///
@ -241,12 +282,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!bit_vec.is_null());
let bit_vec = *Box::from_raw(bit_vec); let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearAnd( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearAnd(
offset, offset,
bit_vec.into(), bit_vec.into(),
compression.try_into().expect("invalid compression code"), compression.try_into().expect("invalid compression code"),
)))) ))));
assert!(!result.is_null());
result
} }
/// Set pixel data according to an or-mask starting at the offset. /// Set pixel data according to an or-mask starting at the offset.
@ -258,7 +302,12 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_and(
/// ///
/// The passed [SPBitVec] gets consumed. /// The passed [SPBitVec] gets consumed.
/// ///
/// Returns: a new `Command::BitmapLinearOr` instance. Will never return NULL. /// Returns: a new [Command::BitmapLinearOr] instance. Will never return NULL.
///
/// # Panics
///
/// - when `bit_vec` is null
/// - when `compression_code` is not a valid value
/// ///
/// # Safety /// # Safety
/// ///
@ -275,12 +324,15 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!bit_vec.is_null());
let bit_vec = *Box::from_raw(bit_vec); let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearOr( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearOr(
offset, offset,
bit_vec.into(), bit_vec.into(),
compression.try_into().expect("invalid compression code"), compression.try_into().expect("invalid compression code"),
)))) ))));
assert!(!result.is_null());
result
} }
/// Set pixel data according to a xor-mask starting at the offset. /// Set pixel data according to a xor-mask starting at the offset.
@ -292,7 +344,12 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_or(
/// ///
/// The passed [SPBitVec] gets consumed. /// The passed [SPBitVec] gets consumed.
/// ///
/// Returns: a new `Command::BitmapLinearXor` instance. Will never return NULL. /// Returns: a new [Command::BitmapLinearXor] instance. Will never return NULL.
///
/// # Panics
///
/// - when `bit_vec` is null
/// - when `compression_code` is not a valid value
/// ///
/// # Safety /// # Safety
/// ///
@ -309,30 +366,32 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_xor(
bit_vec: *mut SPBitVec, bit_vec: *mut SPBitVec,
compression: SPCompressionCode, compression: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!bit_vec.is_null());
let bit_vec = *Box::from_raw(bit_vec); let bit_vec = *Box::from_raw(bit_vec);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearXor( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearXor(
offset, offset,
bit_vec.into(), bit_vec.into(),
compression.try_into().expect("invalid compression code"), compression.try_into().expect("invalid compression code"),
)))) ))));
assert!(!result.is_null());
result
} }
/// Show text on the screen. /// Show text on the screen.
/// ///
/// <div class="warning"> /// The passed [SPCp437Grid] gets consumed.
/// The library does not currently convert between UTF-8 and CP-437.
/// Because Rust expects UTF-8 strings, it might be necessary to only send ASCII for now.
/// </div>
/// ///
/// The passed `SPCp437Grid` gets consumed./// /// Returns: a new [Command::Cp437Data] instance. Will never return NULL.
/// ///
/// Returns: a new `Command::Cp437Data` instance. Will never return NULL. /// # Panics
///
/// - when `grid` is null
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `grid` points to a valid instance of `SPCp437Grid` /// - `grid` points to a valid instance of [SPCp437Grid]
/// - `grid` is not used concurrently or after this call /// - `grid` is not used concurrently or after this call
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`. /// by explicitly calling `sp_command_free`.
@ -342,24 +401,32 @@ pub unsafe extern "C" fn sp_command_cp437_data(
y: usize, y: usize,
grid: *mut SPCp437Grid, grid: *mut SPCp437Grid,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!grid.is_null());
let grid = *Box::from_raw(grid); let grid = *Box::from_raw(grid);
Box::into_raw(Box::new(SPCommand(servicepoint::Command::Cp437Data( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::Cp437Data(
Origin::new(x, y), Origin::new(x, y),
grid.0, grid.0,
)))) ))));
assert!(!result.is_null());
result
} }
/// Sets a window of pixels to the specified values. /// Sets a window of pixels to the specified values.
/// ///
/// The passed `SPPixelGrid` gets consumed. /// The passed [SPPixelGrid] gets consumed.
/// ///
/// Returns: a new `Command::BitmapLinearWin` instance. Will never return NULL. /// Returns: a new [Command::BitmapLinearWin] instance. Will never return NULL.
///
/// # Panics
///
/// - when `pixel_grid` is null
/// - when `compression_code` is not a valid value
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid instance of `SPPixelGrid` /// - `pixel_grid` points to a valid instance of [SPPixelGrid]
/// - `pixel_grid` is not used concurrently or after this call /// - `pixel_grid` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values /// - `compression` matches one of the allowed enum values
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or /// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
@ -371,14 +438,17 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
pixel_grid: *mut SPPixelGrid, pixel_grid: *mut SPPixelGrid,
compression_code: SPCompressionCode, compression_code: SPCompressionCode,
) -> *mut SPCommand { ) -> *mut SPCommand {
assert!(!pixel_grid.is_null());
let byte_grid = (*Box::from_raw(pixel_grid)).0; let byte_grid = (*Box::from_raw(pixel_grid)).0;
Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearWin( let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearWin(
Origin::new(x, y), Origin::new(x, y),
byte_grid, byte_grid,
compression_code compression_code
.try_into() .try_into()
.expect("invalid compression code"), .expect("invalid compression code"),
)))) ))));
assert!(!result.is_null());
result
} }
/// Deallocates a [SPCommand]. /// Deallocates a [SPCommand].
@ -390,14 +460,19 @@ pub unsafe extern "C" fn sp_command_bitmap_linear_win(
/// sp_command_free(c); /// sp_command_free(c);
/// ``` /// ```
/// ///
/// # Panics
///
/// - when `command` is NULL
///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `command` points to a valid [SPCommand] /// - `command` points to a valid [SPCommand]
/// - `command` is not used concurrently or after this call /// - `command` is not used concurrently or after this call
/// - `command` was not passed to another consuming function, e.g. to create a `SPPacket` /// - `command` was not passed to another consuming function, e.g. to create a [SPPacket]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_free(command: *mut SPCommand) { pub unsafe extern "C" fn sp_command_free(command: *mut SPCommand) {
assert!(!command.is_null());
_ = Box::from_raw(command); _ = Box::from_raw(command);
} }

View file

@ -18,7 +18,7 @@ use crate::{SPCommand, SPPacket};
/// ``` /// ```
pub struct SPConnection(pub(crate) servicepoint::Connection); pub struct SPConnection(pub(crate) servicepoint::Connection);
/// Creates a new instance of `SPConnection`. /// Creates a new instance of [SPConnection].
/// ///
/// returns: NULL if connection fails, or connected instance /// returns: NULL if connection fails, or connected instance
/// ///
@ -45,7 +45,7 @@ pub unsafe extern "C" fn sp_connection_open(
Box::into_raw(Box::new(SPConnection(connection))) Box::into_raw(Box::new(SPConnection(connection)))
} }
/// Sends a `SPPacket` to the display using the `SPConnection`. /// Sends a [SPPacket] to the display using the [SPConnection].
/// ///
/// The passed `packet` gets consumed. /// The passed `packet` gets consumed.
/// ///
@ -55,8 +55,8 @@ pub unsafe extern "C" fn sp_connection_open(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `connection` points to a valid instance of `SPConnection` /// - `connection` points to a valid instance of [SPConnection]
/// - `packet` points to a valid instance of `SPPacket` /// - `packet` points to a valid instance of [SPPacket]
/// - `packet` is not used concurrently or after this call /// - `packet` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_send_packet( pub unsafe extern "C" fn sp_connection_send_packet(
@ -77,8 +77,8 @@ pub unsafe extern "C" fn sp_connection_send_packet(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `connection` points to a valid instance of `SPConnection` /// - `connection` points to a valid instance of [SPConnection]
/// - `command` points to a valid instance of `SPPacket` /// - `command` points to a valid instance of [SPPacket]
/// - `command` is not used concurrently or after this call /// - `command` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_send_command( pub unsafe extern "C" fn sp_connection_send_command(
@ -89,13 +89,19 @@ pub unsafe extern "C" fn sp_connection_send_command(
(*connection).0.send(command).is_ok() (*connection).0.send(command).is_ok()
} }
/// Closes and deallocates a `SPConnection`. /// Closes and deallocates a [SPConnection].
///
/// # Panics
///
/// # Panics
///
/// - when `connection` is NULL
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `connection` points to a valid `SPConnection` /// - `connection` points to a valid [SPConnection]
/// - `connection` is not used concurrently or after this call /// - `connection` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) { pub unsafe extern "C" fn sp_connection_free(connection: *mut SPConnection) {

View file

@ -1,4 +1,4 @@
//! C functions for interacting with `SPCp437Grid`s //! C functions for interacting with [SPCp437Grid]s
//! //!
//! prefix `sp_cp437_grid_` //! prefix `sp_cp437_grid_`
@ -25,9 +25,9 @@ impl Clone for SPCp437Grid {
} }
} }
/// Creates a new `SPCp437Grid` with the specified dimensions. /// Creates a new [SPCp437Grid] with the specified dimensions.
/// ///
/// returns: `SPCp437Grid` initialized to 0. /// returns: [SPCp437Grid] initialized to 0.
/// ///
/// # Safety /// # Safety
/// ///
@ -45,7 +45,7 @@ pub unsafe extern "C" fn sp_cp437_grid_new(
)))) ))))
} }
/// Loads a `SPCp437Grid` with the specified dimensions from the provided data. /// Loads a [SPCp437Grid] with the specified dimensions from the provided data.
/// ///
/// Will never return NULL. /// Will never return NULL.
/// ///
@ -74,7 +74,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
)))) ))))
} }
/// Clones a `SPCp437Grid`. /// Clones a [SPCp437Grid].
/// ///
/// Will never return NULL. /// Will never return NULL.
/// ///
@ -82,7 +82,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
/// - `cp437_grid` is not written to concurrently /// - `cp437_grid` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or /// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_cp437_grid_free`. /// by explicitly calling `sp_cp437_grid_free`.
@ -93,13 +93,13 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
Box::into_raw(Box::new((*cp437_grid).clone())) Box::into_raw(Box::new((*cp437_grid).clone()))
} }
/// Deallocates a `SPCp437Grid`. /// Deallocates a [SPCp437Grid].
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
/// - `cp437_grid` is not used concurrently or after cp437_grid call /// - `cp437_grid` is not used concurrently or after cp437_grid call
/// - `cp437_grid` was not passed to another consuming function, e.g. to create a [SPCommand] /// - `cp437_grid` was not passed to another consuming function, e.g. to create a [SPCommand]
#[no_mangle] #[no_mangle]
@ -122,7 +122,7 @@ pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) {
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
/// - `cp437_grid` is not written to concurrently /// - `cp437_grid` is not written to concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_get( pub unsafe extern "C" fn sp_cp437_grid_get(
@ -133,7 +133,7 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
(*cp437_grid).0.get(x, y) (*cp437_grid).0.get(x, y)
} }
/// Sets the value of the specified position in the `SPCp437Grid`. /// Sets the value of the specified position in the [SPCp437Grid].
/// ///
/// # Arguments /// # Arguments
/// ///
@ -163,7 +163,7 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
(*cp437_grid).0.set(x, y, value); (*cp437_grid).0.set(x, y, value);
} }
/// Sets the value of all cells in the `SPCp437Grid`. /// Sets the value of all cells in the [SPCp437Grid].
/// ///
/// # Arguments /// # Arguments
/// ///
@ -174,7 +174,7 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
/// - `cp437_grid` is not written to or read from concurrently /// - `cp437_grid` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_fill( pub unsafe extern "C" fn sp_cp437_grid_fill(
@ -184,7 +184,7 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
(*cp437_grid).0.fill(value); (*cp437_grid).0.fill(value);
} }
/// Gets the width of the `SPCp437Grid` instance. /// Gets the width of the [SPCp437Grid] instance.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -194,7 +194,7 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_width( pub unsafe extern "C" fn sp_cp437_grid_width(
cp437_grid: *const SPCp437Grid, cp437_grid: *const SPCp437Grid,
@ -202,7 +202,7 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
(*cp437_grid).0.width() (*cp437_grid).0.width()
} }
/// Gets the height of the `SPCp437Grid` instance. /// Gets the height of the [SPCp437Grid] instance.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -212,7 +212,7 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_height( pub unsafe extern "C" fn sp_cp437_grid_height(
cp437_grid: *const SPCp437Grid, cp437_grid: *const SPCp437Grid,
@ -220,7 +220,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
(*cp437_grid).0.height() (*cp437_grid).0.height()
} }
/// Gets an unsafe reference to the data of the `SPCp437Grid` instance. /// Gets an unsafe reference to the data of the [SPCp437Grid] instance.
/// ///
/// Will never return NULL. /// Will never return NULL.
/// ///
@ -228,9 +228,9 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `cp437_grid` points to a valid `SPCp437Grid` /// - `cp437_grid` points to a valid [SPCp437Grid]
/// - the returned memory range is never accessed after the passed `SPCp437Grid` has been freed /// - the returned memory range is never accessed after the passed [SPCp437Grid] has been freed
/// - the returned memory range is never accessed concurrently, either via the `SPCp437Grid` or directly /// - the returned memory range is never accessed concurrently, either via the [SPCp437Grid] or directly
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref( pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
cp437_grid: *mut SPCp437Grid, cp437_grid: *mut SPCp437Grid,

View file

@ -1,4 +1,4 @@
//! C functions for interacting with `SPPacket`s //! C functions for interacting with [SPPacket]s
//! //!
//! prefix `sp_packet_` //! prefix `sp_packet_`
@ -9,7 +9,7 @@ use crate::SPCommand;
/// The raw packet /// The raw packet
pub struct SPPacket(pub(crate) servicepoint::packet::Packet); pub struct SPPacket(pub(crate) servicepoint::packet::Packet);
/// Turns a [SPCommand] into a `SPPacket`. /// Turns a [SPCommand] into a [SPPacket].
/// The [SPCommand] gets consumed. /// The [SPCommand] gets consumed.
/// ///
/// Will never return NULL. /// Will never return NULL.
@ -20,7 +20,7 @@ pub struct SPPacket(pub(crate) servicepoint::packet::Packet);
/// ///
/// - [SPCommand] points to a valid instance of [SPCommand] /// - [SPCommand] points to a valid instance of [SPCommand]
/// - [SPCommand] is not used concurrently or after this call /// - [SPCommand] is not used concurrently or after this call
/// - the returned `SPPacket` instance is freed in some way, either by using a consuming function or /// - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_packet_free`. /// by explicitly calling `sp_packet_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_from_command( pub unsafe extern "C" fn sp_packet_from_command(
@ -31,7 +31,7 @@ pub unsafe extern "C" fn sp_packet_from_command(
Box::into_raw(Box::new(packet)) Box::into_raw(Box::new(packet))
} }
/// Tries to load a `SPPacket` from the passed array with the specified length. /// Tries to load a [SPPacket] from the passed array with the specified length.
/// ///
/// returns: NULL in case of an error, pointer to the allocated packet otherwise /// returns: NULL in case of an error, pointer to the allocated packet otherwise
/// ///
@ -41,7 +41,7 @@ pub unsafe extern "C" fn sp_packet_from_command(
/// ///
/// - `data` points to a valid memory region of at least `length` bytes /// - `data` points to a valid memory region of at least `length` bytes
/// - `data` is not written to concurrently /// - `data` is not written to concurrently
/// - the returned `SPPacket` instance is freed in some way, either by using a consuming function or /// - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_packet_free`. /// by explicitly calling `sp_packet_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_try_load( pub unsafe extern "C" fn sp_packet_try_load(
@ -55,7 +55,7 @@ pub unsafe extern "C" fn sp_packet_try_load(
} }
} }
/// Clones a `SPPacket`. /// Clones a [SPPacket].
/// ///
/// Will never return NULL. /// Will never return NULL.
/// ///
@ -63,7 +63,7 @@ pub unsafe extern "C" fn sp_packet_try_load(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `packet` points to a valid `SPPacket` /// - `packet` points to a valid [SPPacket]
/// - `packet` is not written to concurrently /// - `packet` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or /// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_packet_free`. /// by explicitly calling `sp_packet_free`.
@ -74,13 +74,13 @@ pub unsafe extern "C" fn sp_packet_clone(
Box::into_raw(Box::new(SPPacket((*packet).0.clone()))) Box::into_raw(Box::new(SPPacket((*packet).0.clone())))
} }
/// Deallocates a `SPPacket`. /// Deallocates a [SPPacket].
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `packet` points to a valid `SPPacket` /// - `packet` points to a valid [SPPacket]
/// - `packet` is not used concurrently or after this call /// - `packet` is not used concurrently or after this call
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) { pub unsafe extern "C" fn sp_packet_free(packet: *mut SPPacket) {

View file

@ -1,4 +1,4 @@
//! C functions for interacting with `SPPixelGrid`s //! C functions for interacting with [SPPixelGrid]s
//! //!
//! prefix `sp_pixel_grid_` //! prefix `sp_pixel_grid_`
@ -18,14 +18,14 @@ use crate::byte_slice::SPByteSlice;
/// ``` /// ```
pub struct SPPixelGrid(pub(crate) servicepoint::PixelGrid); pub struct SPPixelGrid(pub(crate) servicepoint::PixelGrid);
/// Creates a new `SPPixelGrid` with the specified dimensions. /// Creates a new [SPPixelGrid] with the specified dimensions.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `width`: size in pixels in x-direction /// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction /// - `height`: size in pixels in y-direction
/// ///
/// returns: `SPPixelGrid` initialized to all pixels off. Will never return NULL. /// returns: [SPPixelGrid] initialized to all pixels off. Will never return NULL.
/// ///
/// # Panics /// # Panics
/// ///
@ -47,14 +47,14 @@ pub unsafe extern "C" fn sp_pixel_grid_new(
)))) ))))
} }
/// Loads a `SPPixelGrid` with the specified dimensions from the provided data. /// Loads a [SPPixelGrid] with the specified dimensions from the provided data.
/// ///
/// # Arguments /// # Arguments
/// ///
/// - `width`: size in pixels in x-direction /// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction /// - `height`: size in pixels in y-direction
/// ///
/// returns: `SPPixelGrid` that contains a copy of the provided data. Will never return NULL. /// returns: [SPPixelGrid] that contains a copy of the provided data. Will never return NULL.
/// ///
/// # Panics /// # Panics
/// ///
@ -81,7 +81,7 @@ pub unsafe extern "C" fn sp_pixel_grid_load(
)))) ))))
} }
/// Clones a `SPPixelGrid`. /// Clones a [SPPixelGrid].
/// ///
/// Will never return NULL. /// Will never return NULL.
/// ///
@ -89,7 +89,7 @@ pub unsafe extern "C" fn sp_pixel_grid_load(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to concurrently /// - `pixel_grid` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or /// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_pixel_grid_free`. /// by explicitly calling `sp_pixel_grid_free`.
@ -100,13 +100,13 @@ pub unsafe extern "C" fn sp_pixel_grid_clone(
Box::into_raw(Box::new(SPPixelGrid((*pixel_grid).0.clone()))) Box::into_raw(Box::new(SPPixelGrid((*pixel_grid).0.clone())))
} }
/// Deallocates a `SPPixelGrid`. /// Deallocates a [SPPixelGrid].
/// ///
/// # Safety /// # Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not used concurrently or after pixel_grid call /// - `pixel_grid` is not used concurrently or after pixel_grid call
/// - `pixel_grid` was not passed to another consuming function, e.g. to create a [SPCommand] /// - `pixel_grid` was not passed to another consuming function, e.g. to create a [SPCommand]
#[no_mangle] #[no_mangle]
@ -114,7 +114,7 @@ pub unsafe extern "C" fn sp_pixel_grid_free(pixel_grid: *mut SPPixelGrid) {
_ = Box::from_raw(pixel_grid); _ = Box::from_raw(pixel_grid);
} }
/// Gets the current value at the specified position in the `SPPixelGrid`. /// Gets the current value at the specified position in the [SPPixelGrid].
/// ///
/// # Arguments /// # Arguments
/// ///
@ -129,7 +129,7 @@ pub unsafe extern "C" fn sp_pixel_grid_free(pixel_grid: *mut SPPixelGrid) {
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to concurrently /// - `pixel_grid` is not written to concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_get( pub unsafe extern "C" fn sp_pixel_grid_get(
@ -140,7 +140,7 @@ pub unsafe extern "C" fn sp_pixel_grid_get(
(*pixel_grid).0.get(x, y) (*pixel_grid).0.get(x, y)
} }
/// Sets the value of the specified position in the `SPPixelGrid`. /// Sets the value of the specified position in the [SPPixelGrid].
/// ///
/// # Arguments /// # Arguments
/// ///
@ -158,7 +158,7 @@ pub unsafe extern "C" fn sp_pixel_grid_get(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to or read from concurrently /// - `pixel_grid` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_set( pub unsafe extern "C" fn sp_pixel_grid_set(
@ -170,7 +170,7 @@ pub unsafe extern "C" fn sp_pixel_grid_set(
(*pixel_grid).0.set(x, y, value); (*pixel_grid).0.set(x, y, value);
} }
/// Sets the state of all pixels in the `SPPixelGrid`. /// Sets the state of all pixels in the [SPPixelGrid].
/// ///
/// # Arguments /// # Arguments
/// ///
@ -181,7 +181,7 @@ pub unsafe extern "C" fn sp_pixel_grid_set(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to or read from concurrently /// - `pixel_grid` is not written to or read from concurrently
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_fill( pub unsafe extern "C" fn sp_pixel_grid_fill(
@ -191,7 +191,7 @@ pub unsafe extern "C" fn sp_pixel_grid_fill(
(*pixel_grid).0.fill(value); (*pixel_grid).0.fill(value);
} }
/// Gets the width in pixels of the `SPPixelGrid` instance. /// Gets the width in pixels of the [SPPixelGrid] instance.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -201,7 +201,7 @@ pub unsafe extern "C" fn sp_pixel_grid_fill(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_width( pub unsafe extern "C" fn sp_pixel_grid_width(
pixel_grid: *const SPPixelGrid, pixel_grid: *const SPPixelGrid,
@ -209,7 +209,7 @@ pub unsafe extern "C" fn sp_pixel_grid_width(
(*pixel_grid).0.width() (*pixel_grid).0.width()
} }
/// Gets the height in pixels of the `SPPixelGrid` instance. /// Gets the height in pixels of the [SPPixelGrid] instance.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -219,7 +219,7 @@ pub unsafe extern "C" fn sp_pixel_grid_width(
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_height( pub unsafe extern "C" fn sp_pixel_grid_height(
pixel_grid: *const SPPixelGrid, pixel_grid: *const SPPixelGrid,
@ -227,15 +227,15 @@ pub unsafe extern "C" fn sp_pixel_grid_height(
(*pixel_grid).0.height() (*pixel_grid).0.height()
} }
/// Gets an unsafe reference to the data of the `SPPixelGrid` instance. /// Gets an unsafe reference to the data of the [SPPixelGrid] instance.
/// ///
/// ## Safety /// ## Safety
/// ///
/// The caller has to make sure that: /// The caller has to make sure that:
/// ///
/// - `pixel_grid` points to a valid `SPPixelGrid` /// - `pixel_grid` points to a valid [SPPixelGrid]
/// - the returned memory range is never accessed after the passed `SPPixelGrid` has been freed /// - the returned memory range is never accessed after the passed [SPPixelGrid] has been freed
/// - the returned memory range is never accessed concurrently, either via the `SPPixelGrid` or directly /// - the returned memory range is never accessed concurrently, either via the [SPPixelGrid] or directly
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_unsafe_data_ref( pub unsafe extern "C" fn sp_pixel_grid_unsafe_data_ref(
pixel_grid: *mut SPPixelGrid, pixel_grid: *mut SPPixelGrid,