#[repr(C)]
#[derive(Copy, Clone)]
pub struct StringView {
pub ptr: *const u8,
pub len: usize,
}
impl StringView {
pub unsafe fn as_ref<'a>(self) -> &'a [u8] {
if self.ptr.is_null() {
assert_eq!(self.len, 0, "Non-empty slice with null data pointer");
&[]
} else {
unsafe { core::slice::from_raw_parts(self.ptr, self.len) }
}
}
}
impl From<&[u8]> for StringView {
fn from(slice: &[u8]) -> Self {
Self { ptr: slice.as_ptr(), len: slice.len() }
}
}
impl<const N: usize> From<&[u8; N]> for StringView {
fn from(slice: &[u8; N]) -> Self {
Self { ptr: slice.as_ptr(), len: N }
}
}