wip remove newtypes

This commit is contained in:
Vinzenz Schroeter 2025-04-12 12:08:49 +02:00
parent 38633f0b6b
commit 69014dff38
3 changed files with 54 additions and 78 deletions

View file

@ -245,22 +245,6 @@ typedef ValueGrid_char CharGrid;
*/ */
typedef ValueGrid_u8 Cp437Grid; typedef ValueGrid_u8 Cp437Grid;
/**
* A C-wrapper for grid containing codepage 437 characters.
*
* The encoding is currently not enforced.
*
* # Examples
*
* ```C
* Cp437Grid grid = sp_cp437_grid_new(4, 3);
* sp_cp437_grid_fill(grid, '?');
* sp_cp437_grid_set(grid, 0, 0, '!');
* sp_cp437_grid_free(grid);
* ```
*/
typedef Cp437Grid SPCp437Grid;
/** /**
* A raw header. * A raw header.
* *
@ -1417,7 +1401,7 @@ Command *sp_command_clone(const Command *command);
*/ */
Command *sp_command_cp437_data(size_t x, Command *sp_command_cp437_data(size_t x,
size_t y, size_t y,
SPCp437Grid *grid); Cp437Grid *grid);
/** /**
* A yet-to-be-tested command. * A yet-to-be-tested command.
@ -1631,7 +1615,7 @@ bool sp_connection_send_packet(const UdpConnection *connection, Packet *packet);
* - 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`.
*/ */
SPCp437Grid *sp_cp437_grid_clone(const SPCp437Grid *cp437_grid); Cp437Grid *sp_cp437_grid_clone(const Cp437Grid *cp437_grid);
/** /**
* Sets the value of all cells in the [SPCp437Grid]. * Sets the value of all cells in the [SPCp437Grid].
@ -1652,7 +1636,7 @@ SPCp437Grid *sp_cp437_grid_clone(const SPCp437Grid *cp437_grid);
* - `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
*/ */
void sp_cp437_grid_fill(SPCp437Grid *cp437_grid, uint8_t value); void sp_cp437_grid_fill(Cp437Grid *cp437_grid, uint8_t value);
/** /**
* Deallocates a [SPCp437Grid]. * Deallocates a [SPCp437Grid].
@ -1671,7 +1655,7 @@ void sp_cp437_grid_fill(SPCp437Grid *cp437_grid, uint8_t value);
* *
* [SPCommand]: [crate::SPCommand] * [SPCommand]: [crate::SPCommand]
*/ */
void sp_cp437_grid_free(SPCp437Grid *cp437_grid); void sp_cp437_grid_free(Cp437Grid *cp437_grid);
/** /**
* Gets the current value at the specified position. * Gets the current value at the specified position.
@ -1693,7 +1677,7 @@ void sp_cp437_grid_free(SPCp437Grid *cp437_grid);
* - `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
*/ */
uint8_t sp_cp437_grid_get(const SPCp437Grid *cp437_grid, size_t x, size_t y); uint8_t sp_cp437_grid_get(const Cp437Grid *cp437_grid, size_t x, size_t y);
/** /**
* Gets the height of the [SPCp437Grid] instance. * Gets the height of the [SPCp437Grid] instance.
@ -1712,7 +1696,7 @@ uint8_t sp_cp437_grid_get(const SPCp437Grid *cp437_grid, size_t x, size_t y);
* *
* - `cp437_grid` points to a valid [SPCp437Grid] * - `cp437_grid` points to a valid [SPCp437Grid]
*/ */
size_t sp_cp437_grid_height(const SPCp437Grid *cp437_grid); size_t sp_cp437_grid_height(const Cp437Grid *cp437_grid);
/** /**
* Loads a [SPCp437Grid] with the specified dimensions from the provided data. * Loads a [SPCp437Grid] with the specified dimensions from the provided data.
@ -1733,10 +1717,10 @@ size_t sp_cp437_grid_height(const SPCp437Grid *cp437_grid);
* - 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`.
*/ */
SPCp437Grid *sp_cp437_grid_load(size_t width, Cp437Grid *sp_cp437_grid_load(size_t width,
size_t height, size_t height,
const uint8_t *data, const uint8_t *data,
size_t data_length); size_t data_length);
/** /**
* Creates a new [SPCp437Grid] with the specified dimensions. * Creates a new [SPCp437Grid] with the specified dimensions.
@ -1750,8 +1734,8 @@ SPCp437Grid *sp_cp437_grid_load(size_t width,
* - 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`.
*/ */
SPCp437Grid *sp_cp437_grid_new(size_t width, Cp437Grid *sp_cp437_grid_new(size_t width,
size_t height); size_t height);
/** /**
* Sets the value of the specified position in the [SPCp437Grid]. * Sets the value of the specified position in the [SPCp437Grid].
@ -1778,7 +1762,7 @@ SPCp437Grid *sp_cp437_grid_new(size_t width,
* *
* [SPBitVec]: [crate::SPBitVec] * [SPBitVec]: [crate::SPBitVec]
*/ */
void sp_cp437_grid_set(SPCp437Grid *cp437_grid, void sp_cp437_grid_set(Cp437Grid *cp437_grid,
size_t x, size_t x,
size_t y, size_t y,
uint8_t value); uint8_t value);
@ -1800,7 +1784,7 @@ void sp_cp437_grid_set(SPCp437Grid *cp437_grid,
* - 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
*/ */
SPByteSlice sp_cp437_grid_unsafe_data_ref(SPCp437Grid *cp437_grid); SPByteSlice sp_cp437_grid_unsafe_data_ref(Cp437Grid *cp437_grid);
/** /**
* Gets the width of the [SPCp437Grid] instance. * Gets the width of the [SPCp437Grid] instance.
@ -1819,7 +1803,7 @@ SPByteSlice sp_cp437_grid_unsafe_data_ref(SPCp437Grid *cp437_grid);
* *
* - `cp437_grid` points to a valid [SPCp437Grid] * - `cp437_grid` points to a valid [SPCp437Grid]
*/ */
size_t sp_cp437_grid_width(const SPCp437Grid *cp437_grid); size_t sp_cp437_grid_width(const Cp437Grid *cp437_grid);
/** /**
* Clones a [SPPacket]. * Clones a [SPPacket].

View file

@ -2,8 +2,8 @@
//! //!
//! prefix `sp_command_` //! prefix `sp_command_`
use crate::{SPBitVec, SPCompressionCode, SPCp437Grid}; use crate::{SPBitVec, SPCompressionCode};
use servicepoint::{BinaryOperation, BrightnessGrid, CharGrid, GlobalBrightnessCommand, Packet, TypedCommand}; use servicepoint::{BinaryOperation, BrightnessGrid, CharGrid, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand};
use std::ptr::NonNull; use std::ptr::NonNull;
/// A low-level display command. /// A low-level display command.
@ -405,14 +405,14 @@ unsafe fn sp_command_bitmap_linear_internal(
pub unsafe extern "C" fn sp_command_cp437_data( pub unsafe extern "C" fn sp_command_cp437_data(
x: usize, x: usize,
y: usize, y: usize,
grid: *mut SPCp437Grid, grid: *mut Cp437Grid,
) -> NonNull<TypedCommand> { ) -> NonNull<TypedCommand> {
assert!(!grid.is_null()); assert!(!grid.is_null());
let grid = *unsafe { Box::from_raw(grid) }; let grid = *unsafe { Box::from_raw(grid) };
let result = Box::new( let result = Box::new(
servicepoint::Cp437GridCommand { servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid: grid.0, grid,
} }
.into(), .into(),
); );

View file

@ -1,32 +1,25 @@
//! C functions for interacting with [SPCp437Grid]s //! C functions for interacting with [SPCp437Grid]s
//! //!
//! prefix `sp_cp437_grid_` //! prefix `sp_cp437_grid_`
//!
//!
//! A C-wrapper for grid containing codepage 437 characters.
//!
//! The encoding is currently not enforced.
//!
//! # Examples
//!
//! ```C
//! Cp437Grid grid = sp_cp437_grid_new(4, 3);
//! sp_cp437_grid_fill(grid, '?');
//! sp_cp437_grid_set(grid, 0, 0, '!');
//! sp_cp437_grid_free(grid);
//! ```
use crate::SPByteSlice; use crate::SPByteSlice;
use servicepoint::{DataRef, Grid}; use servicepoint::{Cp437Grid, DataRef, Grid};
use std::ptr::NonNull; use std::ptr::NonNull;
/// A C-wrapper for grid containing codepage 437 characters.
///
/// The encoding is currently not enforced.
///
/// # Examples
///
/// ```C
/// Cp437Grid grid = sp_cp437_grid_new(4, 3);
/// sp_cp437_grid_fill(grid, '?');
/// sp_cp437_grid_set(grid, 0, 0, '!');
/// sp_cp437_grid_free(grid);
/// ```
#[repr(transparent)]
pub struct SPCp437Grid(pub(crate) servicepoint::Cp437Grid);
impl Clone for SPCp437Grid {
fn clone(&self) -> Self {
SPCp437Grid(self.0.clone())
}
}
/// Creates a new [SPCp437Grid] with the specified dimensions. /// Creates a new [SPCp437Grid] with the specified dimensions.
/// ///
/// returns: [SPCp437Grid] initialized to 0. Will never return NULL. /// returns: [SPCp437Grid] initialized to 0. Will never return NULL.
@ -41,9 +34,8 @@ impl Clone for SPCp437Grid {
pub unsafe extern "C" fn sp_cp437_grid_new( pub unsafe extern "C" fn sp_cp437_grid_new(
width: usize, width: usize,
height: usize, height: usize,
) -> NonNull<SPCp437Grid> { ) -> NonNull<Cp437Grid> {
let result = let result = Box::new(Cp437Grid::new(width, height));
Box::new(SPCp437Grid(servicepoint::Cp437Grid::new(width, height)));
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
@ -70,12 +62,12 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
height: usize, height: usize,
data: *const u8, data: *const u8,
data_length: usize, data_length: usize,
) -> *mut SPCp437Grid { ) -> *mut Cp437Grid {
assert!(data.is_null()); assert!(data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) }; let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let grid = servicepoint::Cp437Grid::load(width, height, data); let grid = Cp437Grid::load(width, height, data);
if let Some(grid) = grid { if let Some(grid) = grid {
Box::leak(Box::new(SPCp437Grid(grid))) Box::leak(Box::new(grid))
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -99,8 +91,8 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
/// by explicitly calling `sp_cp437_grid_free`. /// by explicitly calling `sp_cp437_grid_free`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_clone( pub unsafe extern "C" fn sp_cp437_grid_clone(
cp437_grid: *const SPCp437Grid, cp437_grid: *const Cp437Grid,
) -> NonNull<SPCp437Grid> { ) -> NonNull<Cp437Grid> {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
let result = Box::new(unsafe { (*cp437_grid).clone() }); let result = Box::new(unsafe { (*cp437_grid).clone() });
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
@ -122,7 +114,7 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
/// ///
/// [SPCommand]: [crate::SPCommand] /// [SPCommand]: [crate::SPCommand]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) { pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut Cp437Grid) {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
_ = unsafe { Box::from_raw(cp437_grid) }; _ = unsafe { Box::from_raw(cp437_grid) };
} }
@ -147,12 +139,12 @@ pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut 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(
cp437_grid: *const SPCp437Grid, cp437_grid: *const Cp437Grid,
x: usize, x: usize,
y: usize, y: usize,
) -> u8 { ) -> u8 {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).0.get(x, y) } unsafe { (*cp437_grid).get(x, y) }
} }
/// Sets the value of the specified position in the [SPCp437Grid]. /// Sets the value of the specified position in the [SPCp437Grid].
@ -180,13 +172,13 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
/// [SPBitVec]: [crate::SPBitVec] /// [SPBitVec]: [crate::SPBitVec]
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_set( pub unsafe extern "C" fn sp_cp437_grid_set(
cp437_grid: *mut SPCp437Grid, cp437_grid: *mut Cp437Grid,
x: usize, x: usize,
y: usize, y: usize,
value: u8, value: u8,
) { ) {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).0.set(x, y, value) }; unsafe { (*cp437_grid).set(x, y, value) };
} }
/// Sets the value of all cells in the [SPCp437Grid]. /// Sets the value of all cells in the [SPCp437Grid].
@ -208,11 +200,11 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
/// - `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(
cp437_grid: *mut SPCp437Grid, cp437_grid: *mut Cp437Grid,
value: u8, value: u8,
) { ) {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).0.fill(value) }; unsafe { (*cp437_grid).fill(value) };
} }
/// Gets the width of the [SPCp437Grid] instance. /// Gets the width of the [SPCp437Grid] instance.
@ -232,10 +224,10 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
/// - `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 Cp437Grid,
) -> usize { ) -> usize {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).0.width() } unsafe { (*cp437_grid).width() }
} }
/// Gets the height of the [SPCp437Grid] instance. /// Gets the height of the [SPCp437Grid] instance.
@ -255,10 +247,10 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
/// - `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 Cp437Grid,
) -> usize { ) -> usize {
assert!(!cp437_grid.is_null()); assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).0.height() } unsafe { (*cp437_grid).height() }
} }
/// Gets an unsafe reference to the data of the [SPCp437Grid] instance. /// Gets an unsafe reference to the data of the [SPCp437Grid] instance.
@ -278,7 +270,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
/// - 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 Cp437Grid,
) -> SPByteSlice { ) -> SPByteSlice {
unsafe {SPByteSlice::from_slice((*cp437_grid).0.data_ref_mut()) } unsafe {SPByteSlice::from_slice((*cp437_grid).data_ref_mut()) }
} }