use core::ops::{Deref, DerefMut};
use crate::{SSlice, Sentinel};
pub type InlineCStr<const N: usize> = InlineSSlice<u8, N>;
#[derive(Debug, Clone, Copy)]
pub struct InlineSSlice<T: Sentinel, const N: usize>([T; N]);
impl<T: Sentinel, const N: usize> InlineSSlice<T, N> {
#[inline(always)]
pub const unsafe fn new_unchecked(slice: [T; N]) -> Self {
Self(slice)
}
pub fn new(slice: [T; N]) -> Result<Self, [T; N]> {
if T::find_sentinel(&slice).is_some() {
Ok(Self(slice))
} else {
Err(slice)
}
}
}
impl<T: Sentinel, const N: usize> Deref for InlineSSlice<T, N> {
type Target = SSlice<T>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { SSlice::from_ptr(self.0.as_ptr()) }
}
}
impl<T: Sentinel, const N: usize> DerefMut for InlineSSlice<T, N> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { SSlice::from_mut_ptr(self.0.as_mut_ptr()) }
}
}
impl<T, U, const N: usize, const M: usize> PartialEq<InlineSSlice<U, M>> for InlineSSlice<T, N>
where
T: PartialEq<U>,
T: Sentinel,
U: Sentinel,
{
#[inline(always)]
fn eq(&self, other: &InlineSSlice<U, M>) -> bool {
**self == **other
}
}
impl<T, U, const N: usize> PartialEq<SSlice<U>> for InlineSSlice<T, N>
where
T: PartialEq<U>,
T: Sentinel,
U: Sentinel,
{
#[inline(always)]
fn eq(&self, other: &SSlice<U>) -> bool {
**self == *other
}
}
impl<T, U, const N: usize> PartialEq<[U]> for InlineSSlice<T, N>
where
T: PartialEq<U>,
T: Sentinel,
U: Sentinel,
{
#[inline(always)]
fn eq(&self, other: &[U]) -> bool {
**self == *other
}
}
impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for [T]
where
T: PartialEq<U>,
T: Sentinel,
U: Sentinel,
{
#[inline(always)]
fn eq(&self, other: &InlineSSlice<U, N>) -> bool {
*self == **other
}
}
impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for SSlice<T>
where
T: PartialEq<U>,
T: Sentinel,
U: Sentinel,
{
#[inline(always)]
fn eq(&self, other: &InlineSSlice<U, N>) -> bool {
*self == **other
}
}
impl<T, const N: usize> AsRef<[T]> for InlineSSlice<T, N>
where
T: Sentinel,
{
#[inline(always)]
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}
impl<T, const N: usize> AsMut<[T]> for InlineSSlice<T, N>
where
T: Sentinel,
{
#[inline(always)]
fn as_mut(&mut self) -> &mut [T] {
self.as_slice_mut()
}
}
impl<T, const N: usize> AsRef<SSlice<T>> for InlineSSlice<T, N>
where
T: Sentinel,
{
#[inline(always)]
fn as_ref(&self) -> &SSlice<T> {
self
}
}
impl<T, const N: usize> AsMut<SSlice<T>> for InlineSSlice<T, N>
where
T: Sentinel,
{
#[inline(always)]
fn as_mut(&mut self) -> &mut SSlice<T> {
self
}
}