add heap_move_ok and heap_move_some helpers

This commit is contained in:
Vinzenz Schroeter 2025-05-05 18:01:35 +02:00
commit 84adf166a9
9 changed files with 55 additions and 89 deletions

View file

@ -1,4 +1,6 @@
use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, ByteSlice};
use crate::{
heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, ByteSlice,
};
use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet};
use std::ptr::NonNull;
@ -32,11 +34,7 @@ pub unsafe extern "C" fn sp_char_grid_load(
data: ByteSlice,
) -> *mut CharGrid {
let data = unsafe { data.as_slice() };
if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) {
heap_move(grid)
} else {
std::ptr::null_mut()
}
heap_move_ok(CharGrid::load_utf8(width, height, data.to_vec()))
}
/// Clones a [CharGrid].
@ -145,11 +143,8 @@ pub unsafe extern "C" fn sp_char_grid_into_packet(
y: usize,
) -> *mut Packet {
let grid = unsafe { heap_remove(grid) };
match Packet::try_from(CharGridCommand {
heap_move_ok(Packet::try_from(CharGridCommand {
grid,
origin: Origin::new(x, y),
}) {
Ok(packet) => heap_move(packet),
Err(_) => std::ptr::null_mut(),
}
}))
}