#![allow(unsafe_code)]
#![warn(missing_docs)]
use core::{cmp::PartialEq, fmt::Debug, marker::PhantomData, ptr::NonNull};
#[repr(C)]
#[derive(PartialEq)]
pub struct Slice<'a, T> {
ptr: NonNull<T>,
len: usize,
phantom: PhantomData<&'a [T]>,
}
impl<'a, T: Debug> Debug for Slice<'a, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_slice().fmt(f)
}
}
impl<'a, T> Copy for Slice<'a, T> {}
impl<'a, T> Clone for Slice<'a, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> Slice<'a, T> {
pub fn as_slice(self) -> &'a [T] {
unsafe { core::slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
}
pub const fn from_slice(slice: &'a [T]) -> Self {
Slice {
ptr: unsafe { NonNull::new_unchecked(slice.as_ptr() as *mut T) },
len: slice.len(),
phantom: PhantomData,
}
}
}
impl<'a, T> From<&'a [T]> for Slice<'a, T> {
fn from(slice: &'a [T]) -> Self {
Self::from_slice(slice)
}
}
impl<'a, T> core::ops::Deref for Slice<'a, T> {
type Target = [T];
fn deref(&self) -> &[T] {
self.as_slice()
}
}
impl<'a, T> Default for Slice<'a, T> {
fn default() -> Self {
Self::from_slice(&[])
}
}
unsafe impl<T: Sync> Sync for Slice<'_, T> {}
unsafe impl<T: Sync> Send for Slice<'_, T> {}