wip remove newtypes
This commit is contained in:
parent
52f6f3f3fe
commit
1a58294f88
14 changed files with 488 additions and 421 deletions
|
|
@ -1,13 +1,12 @@
|
|||
//! C functions for interacting with [SPPacket]s
|
||||
//!
|
||||
//! prefix `sp_packet_`
|
||||
//!
|
||||
//!
|
||||
//! The raw packet
|
||||
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use crate::SPCommand;
|
||||
|
||||
/// The raw packet
|
||||
pub struct SPPacket(pub(crate) servicepoint::Packet);
|
||||
use servicepoint::{Header, Packet, TypedCommand};
|
||||
|
||||
/// Turns a [SPCommand] into a [SPPacket].
|
||||
/// The [SPCommand] gets consumed.
|
||||
|
|
@ -28,12 +27,12 @@ pub struct SPPacket(pub(crate) servicepoint::Packet);
|
|||
/// by explicitly calling `sp_packet_free`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_packet_from_command(
|
||||
command: *mut SPCommand,
|
||||
) -> *mut SPPacket {
|
||||
command: *mut TypedCommand,
|
||||
) -> *mut Packet {
|
||||
assert!(!command.is_null());
|
||||
let command = unsafe { *Box::from_raw(command) };
|
||||
if let Ok(packet) = command.0.try_into() {
|
||||
Box::leak(Box::new(SPPacket(packet)))
|
||||
if let Ok(packet) = command.try_into() {
|
||||
Box::leak(Box::new(packet))
|
||||
} else {
|
||||
std::ptr::null_mut()
|
||||
}
|
||||
|
|
@ -59,12 +58,12 @@ pub unsafe extern "C" fn sp_packet_from_command(
|
|||
pub unsafe extern "C" fn sp_packet_try_load(
|
||||
data: *const u8,
|
||||
length: usize,
|
||||
) -> *mut SPPacket {
|
||||
) -> *mut Packet {
|
||||
assert!(!data.is_null());
|
||||
let data = unsafe { std::slice::from_raw_parts(data, length) };
|
||||
match servicepoint::Packet::try_from(data) {
|
||||
Err(_) => std::ptr::null_mut(),
|
||||
Ok(packet) => Box::into_raw(Box::new(SPPacket(packet))),
|
||||
Ok(packet) => Box::into_raw(Box::new(packet)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -94,14 +93,10 @@ pub unsafe extern "C" fn sp_packet_try_load(
|
|||
/// by explicitly calling [sp_packet_free].
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_packet_from_parts(
|
||||
command_code: u16,
|
||||
a: u16,
|
||||
b: u16,
|
||||
c: u16,
|
||||
d: u16,
|
||||
header: Header,
|
||||
payload: *const u8,
|
||||
payload_len: usize,
|
||||
) -> NonNull<SPPacket> {
|
||||
) -> NonNull<Packet> {
|
||||
assert_eq!(payload.is_null(), payload_len == 0);
|
||||
|
||||
let payload = if payload.is_null() {
|
||||
|
|
@ -112,18 +107,19 @@ pub unsafe extern "C" fn sp_packet_from_parts(
|
|||
Vec::from(payload)
|
||||
};
|
||||
|
||||
let packet = servicepoint::Packet {
|
||||
header: servicepoint::Header {
|
||||
command_code,
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
d,
|
||||
},
|
||||
let packet = Box::new(Packet {
|
||||
header,
|
||||
payload,
|
||||
};
|
||||
let result = Box::new(SPPacket(packet));
|
||||
NonNull::from(Box::leak(result))
|
||||
});
|
||||
NonNull::from(Box::leak(packet))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_packet_get_header(
|
||||
packet: *const Packet,
|
||||
) -> Header {
|
||||
assert!(!packet.is_null());
|
||||
unsafe { (*packet).header }
|
||||
}
|
||||
|
||||
/// Clones a [SPPacket].
|
||||
|
|
@ -144,10 +140,10 @@ pub unsafe extern "C" fn sp_packet_from_parts(
|
|||
/// by explicitly calling `sp_packet_free`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sp_packet_clone(
|
||||
packet: *const SPPacket,
|
||||
) -> NonNull<SPPacket> {
|
||||
packet: *const Packet,
|
||||
) -> NonNull<Packet> {
|
||||
assert!(!packet.is_null());
|
||||
let result = Box::new(SPPacket(unsafe { (*packet).0.clone() }));
|
||||
let result = Box::new(unsafe { (*packet).clone() });
|
||||
NonNull::from(Box::leak(result))
|
||||
}
|
||||
|
||||
|
|
@ -164,7 +160,7 @@ 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 SPPacket) {
|
||||
pub unsafe extern "C" fn sp_packet_free(packet: *mut Packet) {
|
||||
assert!(!packet.is_null());
|
||||
_ = unsafe { Box::from_raw(packet) }
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue