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::{CommandCode, Header, Packet, TypedCommand};
use std::ptr::NonNull;
@ -11,11 +13,7 @@ pub unsafe extern "C" fn sp_packet_from_command(
command: NonNull<TypedCommand>,
) -> *mut Packet {
let command = unsafe { heap_remove(command) };
if let Ok(packet) = command.try_into() {
heap_move(packet)
} else {
std::ptr::null_mut()
}
heap_move_ok(command.try_into())
}
/// Tries to load a [Packet] from the passed array with the specified length.
@ -24,10 +22,7 @@ pub unsafe extern "C" fn sp_packet_from_command(
#[no_mangle]
pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet {
let data = unsafe { data.as_slice() };
match servicepoint::Packet::try_from(data) {
Err(_) => std::ptr::null_mut(),
Ok(packet) => heap_move(packet),
}
heap_move_ok(servicepoint::Packet::try_from(data))
}
/// Creates a raw [Packet] from parts.