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

@ -28,10 +28,9 @@ use std::ptr::NonNull;
/// by explicitly calling `sp_packet_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_command(
command: *mut TypedCommand,
command: NonNull<TypedCommand>,
) -> *mut Packet {
assert!(!command.is_null());
let command = unsafe { *Box::from_raw(command) };
let command = unsafe { *Box::from_raw(command.as_ptr()) };
if let Ok(packet) = command.try_into() {
Box::leak(Box::new(packet))
} else {
@ -56,12 +55,8 @@ pub unsafe extern "C" fn sp_packet_from_command(
/// - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_packet_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_try_load(
data: *const u8,
length: usize,
) -> *mut Packet {
assert!(!data.is_null());
let data = unsafe { std::slice::from_raw_parts(data, length) };
pub unsafe extern "C" fn sp_packet_try_load(data: SPByteSlice) -> *mut Packet {
let data = unsafe { data.as_slice() };
match servicepoint::Packet::try_from(data) {
Err(_) => std::ptr::null_mut(),
Ok(packet) => Box::into_raw(Box::new(packet)),
@ -95,16 +90,12 @@ pub unsafe extern "C" fn sp_packet_try_load(
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_parts(
header: Header,
payload: *const u8,
payload_len: usize,
payload: *const SPByteSlice,
) -> NonNull<Packet> {
assert_eq!(payload.is_null(), payload_len == 0);
let payload = if payload.is_null() {
vec![]
} else {
let payload =
unsafe { std::slice::from_raw_parts(payload, payload_len) };
let payload = unsafe { (*payload).as_slice() };
Vec::from(payload)
};
@ -113,34 +104,34 @@ pub unsafe extern "C" fn sp_packet_from_parts(
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_get_header(packet: *mut Packet) -> *mut Header {
assert!(!packet.is_null());
&mut unsafe { (*packet).header }
pub unsafe extern "C" fn sp_packet_get_header(
packet: NonNull<Packet>,
) -> NonNull<Header> {
NonNull::from(&mut unsafe { (*packet.as_ptr()).header })
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_get_payload(packet: *mut Packet) -> SPByteSlice {
assert!(!packet.is_null());
unsafe { SPByteSlice::from_slice(&mut *(*packet).payload) }
pub unsafe extern "C" fn sp_packet_get_payload(
packet: NonNull<Packet>,
) -> SPByteSlice {
unsafe { SPByteSlice::from_slice(&mut *(*packet.as_ptr()).payload) }
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_set_payload(packet: *mut Packet, data: SPByteSlice) {
assert!(!packet.is_null());
unsafe {
(*packet).payload = data.as_slice().to_vec()
}
pub unsafe extern "C" fn sp_packet_set_payload(
packet: NonNull<Packet>,
data: SPByteSlice,
) {
unsafe { (*packet.as_ptr()).payload = data.as_slice().to_vec() }
}
#[no_mangle]
pub unsafe extern "C" fn sp_packet_write_to(
packet: *const Packet,
mut buffer: SPByteSlice,
packet: NonNull<Packet>,
buffer: SPByteSlice,
) {
assert!(!packet.is_null());
unsafe {
(*packet).serialize_to(buffer.as_slice_mut());
packet.as_ref().serialize_to(buffer.as_slice_mut());
}
}
@ -162,10 +153,9 @@ pub unsafe extern "C" fn sp_packet_write_to(
/// by explicitly calling `sp_packet_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_packet_clone(
packet: *const Packet,
packet: NonNull<Packet>,
) -> NonNull<Packet> {
assert!(!packet.is_null());
let result = Box::new(unsafe { (*packet).clone() });
let result = Box::new(unsafe { packet.as_ref().clone() });
NonNull::from(Box::leak(result))
}
@ -182,7 +172,6 @@ pub unsafe extern "C" fn sp_packet_clone(
/// - `packet` points to a valid [SPPacket]
/// - `packet` is not used concurrently or after this call
#[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: *mut Packet) {
assert!(!packet.is_null());
_ = unsafe { Box::from_raw(packet) }
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
_ = unsafe { Box::from_raw(packet.as_ptr()) }
}