use super::*;
impl<'a, T, N> GetIndex<'a, &'a [T]> for StaticRange<N>
where
N: Unsigned + Array<T>,
<N as Array<T>>::Array: 'a,
{
type Output = &'a N::Array;
fn get(self, set: &&'a [T]) -> Option<Self::Output> {
if self.end() <= set.len() {
let slice = *set;
Some(unsafe { &*(slice.as_ptr().add(self.start()) as *const N::Array) })
} else {
None
}
}
}
impl<'a, T, N> IsolateIndex<&'a [T]> for StaticRange<N>
where
N: Unsigned + Array<T>,
<N as Array<T>>::Array: 'a,
{
type Output = &'a N::Array;
fn try_isolate(self, set: &'a [T]) -> Option<Self::Output> {
if self.end() <= set.len() {
Some(unsafe { &*(set.as_ptr().add(self.start()) as *const N::Array) })
} else {
None
}
}
}
impl<'a, T, N> IsolateIndex<&'a mut [T]> for StaticRange<N>
where
N: Unsigned + Array<T>,
<N as Array<T>>::Array: 'a,
{
type Output = &'a mut N::Array;
fn try_isolate(self, set: &'a mut [T]) -> Option<Self::Output> {
if self.end() <= set.len() {
Some(unsafe { &mut *(set.as_mut_ptr().add(self.start()) as *mut N::Array) })
} else {
None
}
}
}
impl<'a, T, I> GetIndex<'a, &'a [T]> for I
where
I: std::slice::SliceIndex<[T]>,
<[T] as std::ops::Index<I>>::Output: 'a,
{
type Output = &'a <[T] as std::ops::Index<I>>::Output;
fn get(self, set: &&'a [T]) -> Option<Self::Output> {
Some(std::ops::Index::<I>::index(*set, self))
}
}
impl<'a, T, I> IsolateIndex<&'a [T]> for I
where
I: std::slice::SliceIndex<[T]>,
<I as std::slice::SliceIndex<[T]>>::Output: 'a,
{
type Output = &'a <[T] as std::ops::Index<I>>::Output;
fn try_isolate(self, set: &'a [T]) -> Option<&'a <[T] as std::ops::Index<I>>::Output> {
Some(std::ops::Index::<I>::index(set, self))
}
}
impl<'a, T, I> IsolateIndex<&'a mut [T]> for I
where
I: std::slice::SliceIndex<[T]>,
<I as std::slice::SliceIndex<[T]>>::Output: 'a,
{
type Output = &'a mut <[T] as std::ops::Index<I>>::Output;
fn try_isolate(self, set: &'a mut [T]) -> Option<&'a mut <[T] as std::ops::Index<I>>::Output> {
let slice = unsafe { std::slice::from_raw_parts_mut(set.as_mut_ptr(), set.len()) };
Some(std::ops::IndexMut::<I>::index_mut(slice, self))
}
}
impl<T> Set for [T] {
type Elem = T;
type Atom = T;
fn len(&self) -> usize {
<[T]>::len(self)
}
}
impl<'a, T: 'a> View<'a> for [T] {
type Type = &'a [T];
fn view(&'a self) -> Self::Type {
self
}
}
impl<'a, T: 'a> ViewMut<'a> for [T] {
type Type = &'a mut [T];
fn view_mut(&'a mut self) -> Self::Type {
self
}
}
impl<'a, T: 'a> ViewIterator<'a> for [T] {
type Item = &'a T;
type Iter = std::slice::Iter<'a, T>;
fn view_iter(&'a self) -> Self::Iter {
self.iter()
}
}
impl<'a, T: 'a> ViewMutIterator<'a> for [T] {
type Item = &'a mut T;
type Iter = std::slice::IterMut<'a, T>;
fn view_mut_iter(&'a mut self) -> Self::Iter {
self.iter_mut()
}
}
impl<'a, T: 'a> AtomIterator<'a> for [T] {
type Item = &'a T;
type Iter = std::slice::Iter<'a, T>;
fn atom_iter(&'a self) -> Self::Iter {
self.iter()
}
}
impl<'a, T: 'a> AtomMutIterator<'a> for [T] {
type Item = &'a mut T;
type Iter = std::slice::IterMut<'a, T>;
fn atom_mut_iter(&'a mut self) -> Self::Iter {
self.iter_mut()
}
}
impl<'a, T, N> SplitPrefix<N> for &'a [T]
where
N: Unsigned + Array<T>,
<N as Array<T>>::Array: 'a,
{
type Prefix = &'a N::Array;
fn split_prefix(self) -> Option<(Self::Prefix, Self)> {
if self.len() < N::to_usize() {
return None;
}
let (prefix, rest) = unsafe {
let prefix = self.as_ptr() as *const N::Array;
(&*prefix, self.get_unchecked(N::to_usize()..))
};
Some((prefix, rest))
}
}
impl<'a, T, N> SplitPrefix<N> for &'a mut [T]
where
N: Unsigned + Array<T>,
<N as Array<T>>::Array: 'a,
{
type Prefix = &'a mut N::Array;
fn split_prefix(self) -> Option<(Self::Prefix, Self)> {
if self.len() < N::to_usize() {
return None;
}
let (prefix, rest) = unsafe {
let prefix = self.as_mut_ptr() as *mut N::Array;
(&mut *prefix, self.get_unchecked_mut(N::to_usize()..))
};
Some((prefix, rest))
}
}
impl<T, N: Array<T>> UniChunkable<N> for [T] {
type Chunk = N::Array;
}
impl<'a, T, N: Array<T>> UniChunkable<N> for &'a [T]
where
<N as Array<T>>::Array: 'a,
{
type Chunk = &'a N::Array;
}
impl<'a, T, N: Array<T>> UniChunkable<N> for &'a mut [T]
where
<N as Array<T>>::Array: 'a,
{
type Chunk = &'a mut N::Array;
}
impl<'a, T, N> IntoStaticChunkIterator<N> for &'a [T]
where
Self: SplitPrefix<N>,
N: Unsigned,
{
type Item = <Self as SplitPrefix<N>>::Prefix;
type IterType = UniChunkedIter<Self, N>;
fn into_static_chunk_iter(self) -> Self::IterType {
self.into_generic_static_chunk_iter()
}
}
impl<'a, T, N> IntoStaticChunkIterator<N> for &'a mut [T]
where
Self: SplitPrefix<N>,
N: Unsigned,
{
type Item = <Self as SplitPrefix<N>>::Prefix;
type IterType = UniChunkedIter<Self, N>;
fn into_static_chunk_iter(self) -> Self::IterType {
self.into_generic_static_chunk_iter()
}
}
impl<'a, T: Send + Sync> IntoParChunkIterator for &'a [T] {
type Item = &'a [T];
type IterType = rayon::slice::Chunks<'a, T>;
fn into_par_chunk_iter(self, chunk_size: usize) -> Self::IterType {
use rayon::slice::ParallelSlice;
assert_eq!(self.len() % chunk_size, 0);
self.par_chunks(chunk_size)
}
}
impl<'a, T> SplitFirst for &'a [T] {
type First = &'a T;
fn split_first(self) -> Option<(Self::First, Self)> {
self.split_first()
}
}
impl<'a, T> SplitFirst for &'a mut [T] {
type First = &'a mut T;
fn split_first(self) -> Option<(Self::First, Self)> {
self.split_first_mut()
}
}
impl<'a, T> IntoFlat for &'a [T] {
type FlatType = &'a [T];
fn into_flat(self) -> Self::FlatType {
self
}
}
impl<'a, T> IntoFlat for &'a mut [T] {
type FlatType = &'a mut [T];
fn into_flat(self) -> Self::FlatType {
self
}
}
impl<'a, T> StorageView<'a> for &'a [T] {
type StorageView = &'a [T];
fn storage_view(&'a self) -> Self::StorageView {
self
}
}
impl<'a, T> Storage for &'a [T] {
type Storage = [T];
fn storage(&self) -> &Self::Storage {
self
}
}
impl<'a, T> Storage for &'a mut [T] {
type Storage = [T];
fn storage(&self) -> &Self::Storage {
self
}
}
impl<'a, T> StorageMut for &'a mut [T] {
fn storage_mut(&mut self) -> &mut Self::Storage {
self
}
}
impl<T> Storage for [T] {
type Storage = [T];
fn storage(&self) -> &Self::Storage {
self
}
}
impl<T> StorageMut for [T] {
fn storage_mut(&mut self) -> &mut Self::Storage {
self
}
}
impl<'a, T: 'a> CloneWithStorage<Vec<T>> for &'a [T] {
type CloneType = Vec<T>;
fn clone_with_storage(&self, storage: Vec<T>) -> Self::CloneType {
assert_eq!(self.len(), storage.len());
storage
}
}
impl<'a, T: 'a> CloneWithStorage<&'a [T]> for &'a [T] {
type CloneType = &'a [T];
fn clone_with_storage(&self, storage: &'a [T]) -> Self::CloneType {
assert_eq!(self.len(), storage.len());
storage
}
}
impl<'a, T: 'a> CloneWithStorage<&'a mut [T]> for &'a mut [T] {
type CloneType = &'a mut [T];
fn clone_with_storage(&self, storage: &'a mut [T]) -> Self::CloneType {
assert_eq!(self.len(), storage.len());
storage
}
}
impl<'a, T> SplitAt for &mut [T] {
fn split_at(self, mid: usize) -> (Self, Self) {
self.split_at_mut(mid)
}
}
impl<'a, T> SplitAt for &[T] {
fn split_at(self, mid: usize) -> (Self, Self) {
self.split_at(mid)
}
}
impl<T> Dummy for &[T] {
unsafe fn dummy() -> Self {
&[]
}
}
impl<T> Dummy for &mut [T] {
unsafe fn dummy() -> Self {
&mut []
}
}
impl<T> RemovePrefix for &[T] {
fn remove_prefix(&mut self, n: usize) {
let (_, r) = self.split_at(n);
*self = r;
}
}
impl<T> RemovePrefix for &mut [T] {
fn remove_prefix(&mut self, n: usize) {
let data = std::mem::replace(self, &mut []);
let (_, r) = data.split_at_mut(n);
*self = r;
}
}
impl<'a, T, N> ReinterpretAsGrouped<N> for &'a [T]
where
N: Array<T>,
<N as Array<T>>::Array: 'a,
{
type Output = &'a [N::Array];
#[inline]
fn reinterpret_as_grouped(self) -> Self::Output {
unsafe { reinterpret::reinterpret_slice(self) }
}
}
impl<'a, T, N> ReinterpretAsGrouped<N> for &'a mut [T]
where
N: Array<T>,
<N as Array<T>>::Array: 'a,
{
type Output = &'a mut [N::Array];
#[inline]
fn reinterpret_as_grouped(self) -> Self::Output {
unsafe { reinterpret::reinterpret_mut_slice(self) }
}
}
impl<T> Viewed for [T] {}
impl<T> Truncate for &[T] {
fn truncate(&mut self, new_len: usize) {
*self = self.split_at(new_len).0;
}
}
impl<T> Truncate for &mut [T] {
fn truncate(&mut self, new_len: usize) {
let data = std::mem::replace(self, &mut []);
*self = data.split_at_mut(new_len).0;
}
}
impl<'a, T: Clone> StorageInto<Vec<T>> for &'a [T] {
type Output = Vec<T>;
fn storage_into(self) -> Self::Output {
self.to_vec()
}
}
impl<'a, T: Clone> StorageInto<Vec<T>> for &'a mut [T] {
type Output = Vec<T>;
fn storage_into(self) -> Self::Output {
self.to_vec()
}
}
impl<'a, T: 'a> StorageInto<&'a [T]> for &'a mut [T] {
type Output = &'a [T];
fn storage_into(self) -> Self::Output {
&*self
}
}
impl<T> SwapChunks for &mut [T] {
fn swap_chunks(&mut self, i: usize, j: usize, chunk_size: usize) {
assert!(i + chunk_size <= j || j + chunk_size <= i);
let (lower, upper) = if i < j { (i, j) } else { (j, i) };
let (l, r) = self.split_at_mut(upper);
l[lower..lower + chunk_size].swap_with_slice(&mut r[..chunk_size]);
}
}
impl<T: PartialOrd + Clone> Sort for [T] {
fn sort_indices(&self, indices: &mut [usize]) {
indices.sort_by(|&a, &b| {
self[a]
.partial_cmp(&self[b])
.unwrap_or(std::cmp::Ordering::Less)
});
}
}
impl<T> PermuteInPlace for &mut [T] {
fn permute_in_place(&mut self, permutation: &[usize], seen: &mut [bool]) {
let data = std::mem::replace(self, &mut []);
UniChunked {
chunk_size: 1,
data,
}
.permute_in_place(permutation, seen);
}
}
impl<T: Clone> CloneIntoOther<Vec<T>> for [T] {
fn clone_into_other(&self, other: &mut Vec<T>) {
other.clear();
other.extend_from_slice(self);
}
}
impl<T: Clone> CloneIntoOther for [T] {
fn clone_into_other(&self, other: &mut [T]) {
assert_eq!(self.len(), other.len());
other.clone_from_slice(self);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clone_into_other() {
let a = vec![1, 2, 3, 4];
let mut b = vec![5, 6, 7, 8];
a.as_slice().clone_into_other(b.as_mut_slice());
assert_eq!(b, a);
}
}