use core::marker::PhantomData;
use core::ops::Range;
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SliceView<'a> {
pub(crate) ptr: u64,
pub(crate) len: u64,
_region: PhantomData<&'a [u8]>,
}
#[repr(C)]
#[derive(PartialEq, Eq, Hash)]
pub struct Slice {
pub(crate) ptr: u64,
pub(crate) len: u64,
}
impl<'a> SliceView<'a> {
pub const NULL: SliceView<'a> = SliceView {
ptr: 0,
len: 0,
_region: PhantomData,
};
#[must_use]
#[inline(always)]
pub const unsafe fn new(ptr: u64, len: u64) -> Self {
Self {
ptr,
len,
_region: PhantomData,
}
}
#[must_use]
#[inline(always)]
pub const fn ptr(&self) -> u64 {
self.ptr
}
#[must_use]
#[inline(always)]
pub const fn len(&self) -> u64 {
self.len
}
#[must_use]
#[inline(always)]
pub const fn is_null(&self) -> bool {
self.ptr == 0
}
#[must_use]
#[inline(always)]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
#[must_use]
#[inline(always)]
pub const fn end(&self) -> u64 {
self.ptr.wrapping_add(self.len)
}
#[must_use]
#[inline(always)]
pub const fn range(&self) -> Range<u64> {
self.ptr..self.end()
}
#[must_use]
#[inline(always)]
pub const fn contains(&self, addr: u64) -> bool {
addr >= self.ptr && addr < self.end()
}
#[must_use]
#[inline(always)]
pub const fn covers(&self, other: &SliceView<'_>) -> bool {
other.ptr >= self.ptr && other.end() <= self.end()
}
#[must_use]
#[inline(always)]
pub const fn overlaps(&self, other: &SliceView<'_>) -> bool {
self.ptr < other.end() && other.ptr < self.end()
}
#[must_use]
#[inline(always)]
pub const fn is_aligned_to(&self, align: u64) -> bool {
align != 0 && self.ptr % align == 0
}
#[must_use]
#[inline(always)]
pub const fn as_ptr(&self) -> *const u8 {
self.ptr as *const u8
}
#[must_use]
#[inline(always)]
pub const fn as_mut_ptr(&self) -> *mut u8 {
self.ptr as *mut u8
}
#[must_use]
#[inline]
pub const fn subslice(&self, offset: u64, len: u64) -> Option<SliceView<'a>> {
if let Some(hi) = offset.checked_add(len) {
if hi <= self.len {
return Some(SliceView {
ptr: self.ptr + offset,
len,
_region: PhantomData,
});
}
}
None
}
#[must_use]
#[inline(always)]
pub const unsafe fn subslice_unchecked(&self, offset: u64, len: u64) -> SliceView<'a> {
SliceView {
ptr: self.ptr + offset,
len,
_region: PhantomData,
}
}
#[must_use]
#[inline]
pub const fn split_at(&self, mid: u64) -> Option<(SliceView<'a>, SliceView<'a>)> {
if mid <= self.len {
Some((
SliceView {
ptr: self.ptr,
len: mid,
_region: PhantomData,
},
SliceView {
ptr: self.ptr + mid,
len: self.len - mid,
_region: PhantomData,
},
))
} else {
None
}
}
#[must_use]
#[inline(always)]
pub const unsafe fn as_bytes(&self) -> &'a [u8] {
unsafe { core::slice::from_raw_parts(self.ptr as *const u8, self.len as usize) }
}
#[must_use]
#[inline(always)]
#[allow(clippy::mut_from_ref)] pub const unsafe fn as_bytes_mut(&self) -> &'a mut [u8] {
unsafe { core::slice::from_raw_parts_mut(self.ptr as *mut u8, self.len as usize) }
}
#[must_use]
#[inline(always)]
pub const unsafe fn to_slice(&self) -> Slice {
Slice {
ptr: self.ptr,
len: self.len,
}
}
}
impl Slice {
pub const NULL: Slice = Slice { ptr: 0, len: 0 };
#[must_use]
#[inline(always)]
pub const unsafe fn new(ptr: u64, len: u64) -> Self {
Self { ptr, len }
}
#[must_use]
#[inline(always)]
pub const fn view(&self) -> SliceView<'_> {
SliceView {
ptr: self.ptr,
len: self.len,
_region: PhantomData,
}
}
#[must_use]
#[inline(always)]
pub const fn into_view(self) -> SliceView<'static> {
SliceView {
ptr: self.ptr,
len: self.len,
_region: PhantomData,
}
}
#[must_use]
#[inline(always)]
pub const fn ptr(&self) -> u64 {
self.ptr
}
#[must_use]
#[inline(always)]
pub const fn len(&self) -> u64 {
self.len
}
#[must_use]
#[inline(always)]
pub const fn is_null(&self) -> bool {
self.ptr == 0
}
#[must_use]
#[inline(always)]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
#[must_use]
#[inline(always)]
pub const fn end(&self) -> u64 {
self.ptr.wrapping_add(self.len)
}
#[must_use]
#[inline(always)]
pub const fn range(&self) -> Range<u64> {
self.ptr..self.end()
}
#[must_use]
#[inline(always)]
pub const fn contains(&self, addr: u64) -> bool {
addr >= self.ptr && addr < self.end()
}
#[must_use]
#[inline(always)]
pub const fn as_ptr(&self) -> *const u8 {
self.ptr as *const u8
}
#[must_use]
#[inline(always)]
pub const fn as_mut_ptr(&self) -> *mut u8 {
self.ptr as *mut u8
}
#[must_use]
#[inline(always)]
pub const unsafe fn as_bytes(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts(self.ptr as *const u8, self.len as usize) }
}
#[must_use]
#[inline(always)]
pub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { core::slice::from_raw_parts_mut(self.ptr as *mut u8, self.len as usize) }
}
}
impl From<Slice> for SliceView<'static> {
#[inline(always)]
fn from(s: Slice) -> SliceView<'static> {
s.into_view()
}
}
impl core::fmt::Debug for SliceView<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SliceView")
.field("ptr", &format_args!("{:#x}", self.ptr))
.field("len", &self.len)
.finish()
}
}
impl core::fmt::Debug for Slice {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Slice")
.field("ptr", &format_args!("{:#x}", self.ptr))
.field("len", &self.len)
.finish()
}
}