use core::ops::{Deref, DerefMut};
use core::fmt;
#[cfg(feature = "std")]
use std::boxed::Box;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::vec::Vec;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::Vec;
pub enum ManagedSlice<'a, T: 'a> {
Borrowed(&'a mut [T]),
#[cfg(any(feature = "std", feature = "alloc"))]
Owned(Vec<T>)
}
impl<'a, T: 'a> fmt::Debug for ManagedSlice<'a, T>
where T: fmt::Debug {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ManagedSlice::Borrowed(ref x) => write!(f, "Borrowed({:?})", x),
#[cfg(any(feature = "std", feature = "alloc"))]
&ManagedSlice::Owned(ref x) => write!(f, "Owned({:?})", x)
}
}
}
impl<'a, T: 'a> From<&'a mut [T]> for ManagedSlice<'a, T> {
fn from(value: &'a mut [T]) -> Self {
ManagedSlice::Borrowed(value)
}
}
macro_rules! from_unboxed_slice {
($n:expr) => (
impl<'a, T> From<[T; $n]> for ManagedSlice<'a, T> {
#[inline]
fn from(value: [T; $n]) -> Self {
ManagedSlice::Owned((Box::new(value) as Box<[T]>).into_vec())
}
}
);
($n:expr, $( $r:expr ),*) => (
from_unboxed_slice!($n);
from_unboxed_slice!($( $r ),*);
)
}
#[cfg(any(feature = "std", feature = "alloc"))]
from_unboxed_slice!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31);
#[cfg(any(feature = "std", feature = "alloc"))]
impl<'a, T: 'a> From<Vec<T>> for ManagedSlice<'a, T> {
fn from(value: Vec<T>) -> Self {
ManagedSlice::Owned(value)
}
}
impl<'a, T: 'a> Deref for ManagedSlice<'a, T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
match self {
&ManagedSlice::Borrowed(ref value) => value,
#[cfg(any(feature = "std", feature = "alloc"))]
&ManagedSlice::Owned(ref value) => value
}
}
}
impl<'a, T: 'a> DerefMut for ManagedSlice<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
&mut ManagedSlice::Borrowed(ref mut value) => value,
#[cfg(any(feature = "std", feature = "alloc"))]
&mut ManagedSlice::Owned(ref mut value) => value
}
}
}