use super::Slice;
#[cfg(feature = "alloc")]
use crate::data::Vec;
trait Sealed {}
impl<T> Sealed for [T] {}
impl<T> Sealed for &[T] {}
impl<T> Sealed for &mut [T] {}
impl<T, const LEN: usize> Sealed for [T; LEN] {}
#[cfg(feature = "alloc")]
impl<T> Sealed for Vec<T> {}
#[doc = crate::_tags!(namespace)]
#[doc = crate::_doc_location!("sys/mem")]
#[doc = crate::doclink!(custom devela "[`slice!`]" "sys/mem/macro.slice.html")]
#[cfg_attr(nightly_doc, doc(notable_trait))]
#[expect(private_bounds, reason = "Sealed")]
pub trait SliceExt<T>: Sealed {
#[must_use]
fn slice_lsplit(&self, len: usize) -> &[T];
#[must_use]
fn slice_rsplit(&self, len: usize) -> &[T];
#[must_use]
fn slice_msplit_left(&self, len: usize) -> &[T];
#[must_use]
fn slice_msplit_right(&self, len: usize) -> &[T];
#[must_use]
fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where
T: Clone,
U: From<T>;
#[must_use]
#[cfg(feature = "alloc")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "alloc")))]
fn slice_into_vec<U>(&self) -> Vec<U>
where
T: Clone,
U: From<T>;
#[cfg(feature = "alloc")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "alloc")))]
fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where
T: Clone,
U: TryFrom<T, Error = E>;
}
#[doc = crate::_tags!(namespace)]
#[doc = crate::_doc_location!("sys/mem")]
#[cfg_attr(nightly_doc, doc(notable_trait))]
pub trait SliceExtMut<T>: SliceExt<T> {
#[must_use]
fn slice_lsplit_mut(&mut self, len: usize) -> &mut [T];
#[must_use]
fn slice_rsplit_mut(&mut self, len: usize) -> &mut [T];
#[must_use]
fn slice_msplit_left_mut(&mut self, len: usize) -> &mut [T];
#[must_use]
fn slice_msplit_right_mut(&mut self, len: usize) -> &mut [T];
}
macro_rules! impl_ext_slice {
($t:ty, for $for:ty, impl: $($impl:tt)*) => {
impl<$($impl)*> SliceExt<$t> for $for {
fn slice_lsplit(&self, len: usize) -> &[T] { Slice::lsplit(self, len) }
fn slice_rsplit(&self, len: usize) -> &[T] { Slice::rsplit(self, len) }
fn slice_msplit_left(&self, len: usize) -> &[T] { Slice::msplit_left(self, len) }
fn slice_msplit_right(&self, len: usize) -> &[T] { Slice::msplit_right(self, len) }
fn slice_into_array<U, const N: usize>(&self) -> [U; N] where T: Clone, U: From<T> {
if self.len() >= N {
#[cfg(any(feature = "safe_mem", not(feature = "unsafe_array")))]
{
let mut array: [U; N] = crate::array_from_fn(|i| U::from(self[i].clone()));
for (i, item) in self.iter().take(N).enumerate() {
array[i] = U::from(item.clone());
}
array
}
#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_array"))]
{
use crate::MaybeUninit;
let mut array: [MaybeUninit<U>; N] =
unsafe { MaybeUninit::uninit().assume_init() };
for i in 0..N { array[i] = MaybeUninit::new(U::from(self[i].clone())); }
array.map(|x| unsafe { x.assume_init() })
}
} else {
panic!("Slice length is less than the requested array size")
}
}
#[cfg(feature = "alloc")]
fn slice_into_vec<U>(&self) -> Vec<U> where T: Clone, U: From<T> {
self.iter().map(|t| U::from(t.clone())).collect::<Vec<_>>().into_iter().collect()
}
#[cfg(feature = "alloc")]
fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where T: Clone, U: TryFrom<T, Error = E> {
self
.iter()
.map(|t| U::try_from(t.clone()))
.collect::<Vec<_>>()
.into_iter()
.collect::<Result<Vec<_>, _>>()
}
}
};
(mut: $t:ty, for $for:ty, impl: $($impl:tt)*) => {
impl_ext_slice![$t, for $for, impl: $($impl)*];
impl<$($impl)*> SliceExtMut<$t> for $for {
fn slice_lsplit_mut(&mut self, len: usize) -> &mut [T] { Slice::lsplit_mut(self, len) }
fn slice_rsplit_mut(&mut self, len: usize) -> &mut [T] { Slice::rsplit_mut(self, len) }
fn slice_msplit_left_mut(&mut self, len: usize) -> &mut [T] {
Slice::msplit_left_mut(self, len) }
fn slice_msplit_right_mut(&mut self, len: usize) -> &mut [T] {
Slice::msplit_right_mut(self, len) }
}
};
}
impl_ext_slice![mut: T, for [T], impl: T];
impl_ext_slice![T, for &[T], impl: T];
impl_ext_slice![mut: T, for &mut [T], impl: T];
impl_ext_slice![mut: T, for [T; LEN], impl: T, const LEN: usize];
#[cfg(feature = "alloc")]
impl_ext_slice![mut: T, for Vec<T>, impl: T];