from-scratch/lang/tests/shared/shared.rs
janis 86bbab90c3
modularise test with shared rust structs
add expect/unwrap token methods to tokeniser to aid with parsing
2025-10-29 14:00:17 +01:00

41 lines
812 B
Rust

#[unsafe(no_mangle)]
extern "C" fn panic() -> ! {
panic!("Called panic from external code.");
}
#[repr(C)]
#[derive(Debug, PartialEq, Eq)]
pub struct FFISlice {
pub ptr: *const u8,
pub len: usize,
}
#[repr(transparent)]
#[derive(Debug, PartialEq, Eq)]
pub struct MaybeFFISlice {
inner: FFISlice,
}
impl MaybeFFISlice {
pub fn is_none(&self) -> bool {
self.inner.ptr.is_null()
}
pub fn into_option(self) -> Option<FFISlice> {
if self.is_none() {
None
} else {
Some(self.inner)
}
}
}
impl FFISlice {
pub fn as_slice(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts(self.ptr, self.len) }
}
pub fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(self.as_slice()) }
}
}