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

@ -35,7 +35,7 @@ pub unsafe extern "C" fn sp_cp437_grid_new(
width: usize,
height: usize,
) -> NonNull<Cp437Grid> {
let result = Box::new(Cp437Grid::new(width, height));
let result = Box::new(Cp437Grid::new(width, height));
NonNull::from(Box::leak(result))
}
@ -60,11 +60,9 @@ pub unsafe extern "C" fn sp_cp437_grid_new(
pub unsafe extern "C" fn sp_cp437_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
data: SPByteSlice,
) -> *mut Cp437Grid {
assert!(data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, data_length) };
let data = unsafe { data.as_slice() };
let grid = Cp437Grid::load(width, height, data);
if let Some(grid) = grid {
Box::leak(Box::new(grid))
@ -91,10 +89,9 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
/// by explicitly calling `sp_cp437_grid_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_clone(
cp437_grid: *const Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
) -> NonNull<Cp437Grid> {
assert!(!cp437_grid.is_null());
let result = Box::new(unsafe { (*cp437_grid).clone() });
let result = Box::new(unsafe { cp437_grid.as_ref().clone() });
NonNull::from(Box::leak(result))
}
@ -114,9 +111,8 @@ pub unsafe extern "C" fn sp_cp437_grid_clone(
///
/// [SPCommand]: [crate::SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut Cp437Grid) {
assert!(!cp437_grid.is_null());
_ = unsafe { Box::from_raw(cp437_grid) };
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull<Cp437Grid>) {
_ = unsafe { Box::from_raw(cp437_grid.as_ptr()) };
}
/// Gets the current value at the specified position.
@ -139,12 +135,11 @@ pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut Cp437Grid) {
/// - `cp437_grid` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_get(
cp437_grid: *const Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
x: usize,
y: usize,
) -> u8 {
assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).get(x, y) }
unsafe { cp437_grid.as_ref().get(x, y) }
}
/// Sets the value of the specified position in the [SPCp437Grid].
@ -172,13 +167,12 @@ pub unsafe extern "C" fn sp_cp437_grid_get(
/// [SPBitVec]: [crate::SPBitVec]
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_set(
cp437_grid: *mut Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
x: usize,
y: usize,
value: u8,
) {
assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).set(x, y, value) };
unsafe { (*cp437_grid.as_ptr()).set(x, y, value) };
}
/// Sets the value of all cells in the [SPCp437Grid].
@ -200,11 +194,10 @@ pub unsafe extern "C" fn sp_cp437_grid_set(
/// - `cp437_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_fill(
cp437_grid: *mut Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
value: u8,
) {
assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).fill(value) };
unsafe { (*cp437_grid.as_ptr()).fill(value) };
}
/// Gets the width of the [SPCp437Grid] instance.
@ -224,10 +217,9 @@ pub unsafe extern "C" fn sp_cp437_grid_fill(
/// - `cp437_grid` points to a valid [SPCp437Grid]
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_width(
cp437_grid: *const Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
) -> usize {
assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).width() }
unsafe { cp437_grid.as_ref().width() }
}
/// Gets the height of the [SPCp437Grid] instance.
@ -247,10 +239,9 @@ pub unsafe extern "C" fn sp_cp437_grid_width(
/// - `cp437_grid` points to a valid [SPCp437Grid]
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_height(
cp437_grid: *const Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
) -> usize {
assert!(!cp437_grid.is_null());
unsafe { (*cp437_grid).height() }
unsafe { cp437_grid.as_ref().height() }
}
/// Gets an unsafe reference to the data of the [SPCp437Grid] instance.
@ -270,7 +261,7 @@ pub unsafe extern "C" fn sp_cp437_grid_height(
/// - the returned memory range is never accessed concurrently, either via the [SPCp437Grid] or directly
#[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
cp437_grid: *mut Cp437Grid,
cp437_grid: NonNull<Cp437Grid>,
) -> SPByteSlice {
unsafe {SPByteSlice::from_slice((*cp437_grid).data_ref_mut()) }
unsafe { SPByteSlice::from_slice((*cp437_grid.as_ptr()).data_ref_mut()) }
}