add /*notnull*/ comments in header, use NotNull for parameters

This commit is contained in:
Vinzenz Schroeter 2025-04-12 12:19:10 +02:00
commit 40fed5ba04
13 changed files with 299 additions and 385 deletions

View file

@ -18,6 +18,7 @@
//! sp_char_grid_free(grid);
//! ```
use crate::SPByteSlice;
use servicepoint::{CharGrid, Grid};
use std::ptr::NonNull;
@ -36,7 +37,7 @@ pub unsafe extern "C" fn sp_char_grid_new(
width: usize,
height: usize,
) -> NonNull<CharGrid> {
let result = Box::new(CharGrid::new(width, height));
let result = Box::new(CharGrid::new(width, height));
NonNull::from(Box::leak(result))
}
@ -62,16 +63,12 @@ pub unsafe extern "C" fn sp_char_grid_new(
pub unsafe extern "C" fn sp_char_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
data: SPByteSlice,
) -> NonNull<CharGrid> {
assert!(data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let data = unsafe { data.as_slice() };
// TODO remove unwrap
let result = Box::new(
CharGrid::load_utf8(width, height, data.to_vec())
.unwrap(),
);
let result =
Box::new(CharGrid::load_utf8(width, height, data.to_vec()).unwrap());
NonNull::from(Box::leak(result))
}
@ -93,10 +90,9 @@ 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 CharGrid,
char_grid: NonNull<CharGrid>,
) -> NonNull<CharGrid> {
assert!(!char_grid.is_null());
let result = Box::new(unsafe { (*char_grid).clone() });
let result = Box::new(unsafe { char_grid.as_ref().clone() });
NonNull::from(Box::leak(result))
}
@ -116,9 +112,8 @@ 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 CharGrid) {
assert!(!char_grid.is_null());
_ = unsafe { Box::from_raw(char_grid) };
pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull<CharGrid>) {
_ = unsafe { Box::from_raw(char_grid.as_ptr()) };
}
/// Gets the current value at the specified position.
@ -141,12 +136,11 @@ pub unsafe extern "C" fn sp_char_grid_free(char_grid: *mut CharGrid) {
/// - `char_grid` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_char_grid_get(
char_grid: *const CharGrid,
char_grid: NonNull<CharGrid>,
x: usize,
y: usize,
) -> u32 {
assert!(!char_grid.is_null());
unsafe { (*char_grid).get(x, y) as u32 }
unsafe { char_grid.as_ref().get(x, y) as u32 }
}
/// Sets the value of the specified position in the [SPCharGrid].
@ -174,13 +168,12 @@ 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 CharGrid,
char_grid: NonNull<CharGrid>,
x: usize,
y: usize,
value: u32,
) {
assert!(!char_grid.is_null());
unsafe { (*char_grid).set(x, y, char::from_u32(value).unwrap()) };
unsafe { (*char_grid.as_ptr()).set(x, y, char::from_u32(value).unwrap()) };
}
/// Sets the value of all cells in the [SPCharGrid].
@ -202,11 +195,10 @@ 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 CharGrid,
char_grid: NonNull<CharGrid>,
value: u32,
) {
assert!(!char_grid.is_null());
unsafe { (*char_grid).fill(char::from_u32(value).unwrap()) };
unsafe { (*char_grid.as_ptr()).fill(char::from_u32(value).unwrap()) };
}
/// Gets the width of the [SPCharGrid] instance.
@ -226,10 +218,9 @@ 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 CharGrid,
char_grid: NonNull<CharGrid>,
) -> usize {
assert!(!char_grid.is_null());
unsafe { (*char_grid).width() }
unsafe { char_grid.as_ref().width() }
}
/// Gets the height of the [SPCharGrid] instance.
@ -249,8 +240,7 @@ 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 CharGrid,
char_grid: NonNull<CharGrid>,
) -> usize {
assert!(!char_grid.is_null());
unsafe { (*char_grid).height() }
unsafe { char_grid.as_ref().height() }
}