use crate::{data::views::SliceRange, private::Sealed};
pub trait Len {
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
}
pub trait AssocOwnedType: Sealed {
type Owned: AssocOwnedType<Owned = Self::Owned>;
}
pub trait AssocViewType: Sealed {
type View<'a>: DataView<'a> + for<'b> AssocViewType<View<'b> = Self::View<'b>>;
}
pub trait AssocViewMutType: Sealed {
type ViewMut<'a>: DataViewMut<'a> + for<'b> AssocViewMutType<ViewMut<'b> = Self::ViewMut<'b>>;
}
pub trait ToOwnedData: AssocOwnedType {
#[must_use]
fn to_owned_data(&self) -> Self::Owned;
}
pub trait AsView: AssocViewType {
#[must_use]
fn as_view(&self) -> Self::View<'_>;
}
pub trait AsViewMut: AssocViewMutType {
#[must_use]
fn as_view_mut(&mut self) -> Self::ViewMut<'_>;
}
pub trait ToView<'a>: AssocViewType {
fn to_view(self) -> Self::View<'a>;
}
pub trait DataView<'a>: AssocViewType {
#[must_use]
fn reborrow_view<'b>(&'b self) -> Self::View<'b>
where
'a: 'b;
}
pub trait DataViewMut<'a>: AssocViewMutType {
fn reborrow_view_mut<'b>(&'b mut self) -> Self::ViewMut<'b>
where
'a: 'b;
}
pub trait Restrict {
fn restrict<R: SliceRange>(&mut self, range: R);
fn clear(&mut self);
}
pub trait Slice: AssocViewType {
fn slice<R: SliceRange>(&self, range: R) -> Self::View<'_>;
fn get_slice<R: SliceRange>(&self, range: R) -> Option<Self::View<'_>>;
}
pub trait SliceCopy: Copy {
#[must_use]
fn slice<R: SliceRange>(self, range: R) -> Self;
#[must_use]
fn get_slice<R: SliceRange>(self, range: R) -> Option<Self>;
}
pub trait SliceMut: AssocViewMutType {
fn slice_mut<R: SliceRange>(&mut self, range: R) -> Self::ViewMut<'_>;
fn get_slice_mut<R: SliceRange>(&mut self, range: R) -> Option<Self::ViewMut<'_>>;
}
impl Len for Vec<u8> {
fn is_empty(&self) -> bool {
self.is_empty()
}
fn len(&self) -> usize {
self.len()
}
}
impl Len for &[u8] {
fn is_empty(&self) -> bool {
(*self).is_empty()
}
fn len(&self) -> usize {
(*self).len()
}
}
impl Len for &mut [u8] {
fn is_empty(&self) -> bool {
(**self).is_empty()
}
fn len(&self) -> usize {
(**self).len()
}
}
impl AssocOwnedType for Vec<u8> {
type Owned = Vec<u8>;
}
impl AssocViewType for Vec<u8> {
type View<'a> = &'a [u8];
}
impl AssocViewMutType for Vec<u8> {
type ViewMut<'a> = &'a mut [u8];
}
impl AssocOwnedType for &[u8] {
type Owned = Vec<u8>;
}
impl AssocViewType for &[u8] {
type View<'a> = &'a [u8];
}
impl AssocViewMutType for &[u8] {
type ViewMut<'a> = &'a mut [u8];
}
impl AssocOwnedType for &mut [u8] {
type Owned = Vec<u8>;
}
impl AssocViewType for &mut [u8] {
type View<'a> = &'a [u8];
}
impl AssocViewMutType for &mut [u8] {
type ViewMut<'a> = &'a mut [u8];
}
impl AsView for Vec<u8> {
fn as_view(&self) -> Self::View<'_> {
self
}
}
impl AsView for &mut [u8] {
fn as_view(&self) -> Self::View<'_> {
self
}
}
impl AsViewMut for Vec<u8> {
fn as_view_mut(&mut self) -> Self::ViewMut<'_> {
self
}
}
impl ToOwnedData for &[u8] {
fn to_owned_data(&self) -> Self::Owned {
self.to_vec()
}
}
impl ToOwnedData for &mut [u8] {
fn to_owned_data(&self) -> Self::Owned {
self.to_vec()
}
}
impl<'a> ToView<'a> for &'a mut [u8] {
fn to_view(self) -> Self::View<'a> {
self
}
}
impl<'a> DataView<'a> for &'a [u8] {
#[inline]
fn reborrow_view<'b>(&'b self) -> Self::View<'b>
where
'a: 'b, {
self
}
}
impl<'a> DataViewMut<'a> for &'a mut [u8] {
#[inline]
fn reborrow_view_mut<'b>(&'b mut self) -> Self::ViewMut<'b>
where
'a: 'b, {
self
}
}
impl Restrict for &[u8] {
#[inline]
fn restrict<R: SliceRange>(&mut self, range: R) {
*self = &self[range];
}
#[inline]
fn clear(&mut self) {
*self = &[];
}
}
impl Restrict for &mut [u8] {
#[inline]
fn restrict<R: SliceRange>(&mut self, range: R) {
let data = std::mem::take(self);
*self = &mut data[range];
}
#[inline]
fn clear(&mut self) {
*self = &mut [];
}
}
impl Slice for Vec<u8> {
#[inline]
fn slice<R: SliceRange>(&self, range: R) -> Self::View<'_> {
&self[range]
}
#[inline]
fn get_slice<R: SliceRange>(&self, range: R) -> Option<Self::View<'_>> {
self.get(range)
}
}
impl Slice for &[u8] {
#[inline]
fn slice<R: SliceRange>(&self, range: R) -> Self::View<'_> {
&self[range]
}
#[inline]
fn get_slice<R: SliceRange>(&self, range: R) -> Option<Self::View<'_>> {
self.get(range)
}
}
impl SliceCopy for &[u8] {
#[inline]
fn slice<R: SliceRange>(self, range: R) -> Self {
&self[range]
}
#[inline]
fn get_slice<R: SliceRange>(self, range: R) -> Option<Self> {
self.get(range)
}
}
impl Slice for &mut [u8] {
#[inline]
fn slice<R: SliceRange>(&self, range: R) -> Self::View<'_> {
&self[range]
}
#[inline]
fn get_slice<R: SliceRange>(&self, range: R) -> Option<Self::View<'_>> {
self.get(range)
}
}
impl SliceMut for Vec<u8> {
#[inline]
fn slice_mut<R: SliceRange>(&mut self, range: R) -> &mut [u8] {
&mut self[range]
}
#[inline]
fn get_slice_mut<R: SliceRange>(&mut self, range: R) -> Option<&mut [u8]> {
self.get_mut(range)
}
}
impl SliceMut for &mut [u8] {
#[inline]
fn slice_mut<R: SliceRange>(&mut self, range: R) -> &mut [u8] {
&mut self[range]
}
#[inline]
fn get_slice_mut<R: SliceRange>(&mut self, range: R) -> Option<&mut [u8]> {
self.get_mut(range)
}
}