add example helper, translate more examples

update to wip servicepoint lib
This commit is contained in:
Vinzenz Schroeter 2025-05-10 14:58:50 +02:00
parent b8a55d0433
commit 4ab5305377
15 changed files with 195 additions and 61 deletions

View file

@ -1,7 +1,5 @@
//! FFI slice helper
use std::ptr::NonNull;
/// Represents a span of memory (`&mut [u8]` ) as a struct.
///
/// # Safety
@ -11,28 +9,39 @@ use std::ptr::NonNull;
/// - accesses to the memory pointed to with `start` is never accessed outside `length`
/// - the lifetime of the `CByteSlice` does not outlive the memory it points to, as described in
/// the function returning this type.
/// - if `start` is NULL or `length` is 0, do not dereference `start`.
///
/// # Examples
///
/// ```c
/// ByteSlice empty = {.start: NULL, .length = 0};
/// ```
#[repr(C)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ByteSlice {
/// The start address of the memory.
pub start: NonNull<u8>,
pub start: *mut u8,
/// The amount of memory in bytes.
pub length: usize,
}
impl ByteSlice {
pub(crate) const INVALID: ByteSlice = ByteSlice {
start: std::ptr::null_mut(),
length: 0,
};
pub(crate) unsafe fn as_slice(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.start.as_ptr(), self.length) }
unsafe { std::slice::from_raw_parts(self.start, self.length) }
}
pub(crate) unsafe fn as_slice_mut(&mut self) -> &mut [u8] {
unsafe {
std::slice::from_raw_parts_mut(self.start.as_ptr(), self.length)
}
unsafe { std::slice::from_raw_parts_mut(self.start, self.length) }
}
pub(crate) unsafe fn from_slice(slice: &mut [u8]) -> Self {
Self {
start: NonNull::new(slice.as_mut_ptr()).unwrap(),
start: slice.as_mut_ptr(),
length: slice.len(),
}
}