wip remove newtypes

This commit is contained in:
Vinzenz Schroeter 2025-04-12 10:54:47 +02:00
commit 1a58294f88
14 changed files with 488 additions and 421 deletions

View file

@ -1,33 +1,26 @@
//! C functions for interacting with [SPCharGrid]s
//!
//! prefix `sp_char_grid_`
//!
//! A C-wrapper for grid containing UTF-8 characters.
//!
//! As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers.
//!
//! The encoding is enforced in most cases by the rust standard library
//! and will panic when provided with illegal characters.
//!
//! # 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);
//! ```
use servicepoint::Grid;
use servicepoint::{CharGrid, Grid};
use std::ptr::NonNull;
/// A C-wrapper for grid containing UTF-8 characters.
///
/// As the rust [char] type is not FFI-safe, characters are passed in their UTF-32 form as 32bit unsigned integers.
///
/// The encoding is enforced in most cases by the rust standard library
/// and will panic when provided with illegal characters.
///
/// # 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);
/// ```
pub struct SPCharGrid(pub(crate) servicepoint::CharGrid);
impl Clone for SPCharGrid {
fn clone(&self) -> Self {
SPCharGrid(self.0.clone())
}
}
/// Creates a new [SPCharGrid] with the specified dimensions.
///
/// returns: [SPCharGrid] initialized to 0. Will never return NULL.
@ -42,9 +35,8 @@ impl Clone for SPCharGrid {
pub unsafe extern "C" fn sp_char_grid_new(
width: usize,
height: usize,
) -> NonNull<SPCharGrid> {
let result =
Box::new(SPCharGrid(servicepoint::CharGrid::new(width, height)));
) -> NonNull<CharGrid> {
let result = Box::new(CharGrid::new(width, height));
NonNull::from(Box::leak(result))
}
@ -72,13 +64,14 @@ pub unsafe extern "C" fn sp_char_grid_load(
height: usize,
data: *const u8,
data_length: usize,
) -> NonNull<SPCharGrid> {
) -> NonNull<CharGrid> {
assert!(data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let result = Box::new(SPCharGrid(
servicepoint::CharGrid::load_utf8(width, height, data.to_vec())
// TODO remove unwrap
let result = Box::new(
CharGrid::load_utf8(width, height, data.to_vec())
.unwrap(),
));
);
NonNull::from(Box::leak(result))
}
@ -100,8 +93,8 @@ pub unsafe extern "C" fn sp_char_grid_load(
/// by explicitly calling `sp_char_grid_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_clone(
char_grid: *const SPCharGrid,
) -> NonNull<SPCharGrid> {
char_grid: *const CharGrid,
) -> NonNull<CharGrid> {
assert!(!char_grid.is_null());
let result = Box::new(unsafe { (*char_grid).clone() });
NonNull::from(Box::leak(result))
@ -123,7 +116,7 @@ pub unsafe extern "C" fn sp_char_grid_clone(
///
/// [SPCommand]: [crate::SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut CharGrid) {
assert!(!char_grid.is_null());
_ = unsafe { Box::from_raw(char_grid) };
}
@ -148,12 +141,12 @@ pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut SPCharGrid) {
/// - `char_grid` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_get(
char_grid: *const SPCharGrid,
char_grid: *const CharGrid,
x: usize,
y: usize,
) -> u32 {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.get(x, y) as u32 }
unsafe { (*char_grid).get(x, y) as u32 }
}
/// Sets the value of the specified position in the [SPCharGrid].
@ -181,13 +174,13 @@ pub unsafe extern "C" fn sp_char_grid_get(
/// [SPBitVec]: [crate::SPBitVec]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_set(
char_grid: *mut SPCharGrid,
char_grid: *mut CharGrid,
x: usize,
y: usize,
value: u32,
) {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.set(x, y, char::from_u32(value).unwrap()) };
unsafe { (*char_grid).set(x, y, char::from_u32(value).unwrap()) };
}
/// Sets the value of all cells in the [SPCharGrid].
@ -209,11 +202,11 @@ pub unsafe extern "C" fn sp_char_grid_set(
/// - `char_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_fill(
char_grid: *mut SPCharGrid,
char_grid: *mut CharGrid,
value: u32,
) {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.fill(char::from_u32(value).unwrap()) };
unsafe { (*char_grid).fill(char::from_u32(value).unwrap()) };
}
/// Gets the width of the [SPCharGrid] instance.
@ -233,10 +226,10 @@ pub unsafe extern "C" fn sp_char_grid_fill(
/// - `char_grid` points to a valid [SPCharGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_width(
char_grid: *const SPCharGrid,
char_grid: *const CharGrid,
) -> usize {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.width() }
unsafe { (*char_grid).width() }
}
/// Gets the height of the [SPCharGrid] instance.
@ -256,8 +249,8 @@ pub unsafe extern "C" fn sp_char_grid_width(
/// - `char_grid` points to a valid [SPCharGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_height(
char_grid: *const SPCharGrid,
char_grid: *const CharGrid,
) -> usize {
assert!(!char_grid.is_null());
unsafe { (*char_grid).0.height() }
unsafe { (*char_grid).height() }
}