pub trait RawChord {
type Elem;
private! {}
fn as_slice(&self) -> &[Self::Elem];
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
pub trait RawChordMut: RawChord {
fn as_mut_slice(&mut self) -> &mut [Self::Elem];
}
pub trait ChordRepr: RawChord + Sized {
fn from_slice(slice: &[Self::Elem]) -> Self
where
Self::Elem: Clone;
}
pub trait RawChordIter: RawChord {
type Iter<'b>: Iterator<Item = &'b Self::Elem>
where
Self::Elem: 'b,
Self: 'b;
fn iter(&self) -> Self::Iter<'_>;
}
impl<C, T> RawChord for &C
where
C: RawChord<Elem = T>,
{
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
C::as_slice(*self)
}
fn len(&self) -> usize {
C::len(*self)
}
}
impl<C, T> RawChord for &mut C
where
C: RawChord<Elem = T>,
{
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
C::as_slice(*self)
}
fn len(&self) -> usize {
C::len(*self)
}
}
impl<T> RawChord for (T, T, T) {
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
unsafe { core::slice::from_raw_parts(self as *const (T, T, T) as *const T, 3) }
}
fn len(&self) -> usize {
3
}
}
impl<T> RawChord for [T] {
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
self
}
fn len(&self) -> usize {
self.len()
}
}
impl<T> RawChordMut for [T] {
fn as_mut_slice(&mut self) -> &mut [T] {
self
}
}
impl<T> RawChord for &[T] {
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
self
}
fn len(&self) -> usize {
(*self).len()
}
}
impl<T> RawChord for &mut [T] {
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
self
}
fn len(&self) -> usize {
(**self).len()
}
}
impl<T> RawChordMut for &mut [T] {
fn as_mut_slice(&mut self) -> &mut [T] {
self
}
}
impl<const N: usize, T> RawChord for [T; N] {
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
self
}
fn len(&self) -> usize {
N
}
}
impl<const N: usize, T> RawChordMut for [T; N] {
fn as_mut_slice(&mut self) -> &mut [T] {
self
}
}
#[cfg(feature = "alloc")]
mod impl_alloc {
use super::{RawChord, RawChordMut};
use alloc::vec::Vec;
impl<T> RawChord for Vec<T> {
type Elem = T;
seal! {}
fn as_slice(&self) -> &[T] {
self.as_slice()
}
fn len(&self) -> usize {
self.len()
}
}
impl<T> RawChordMut for Vec<T> {
fn as_mut_slice(&mut self) -> &mut [T] {
self.as_mut_slice()
}
}
}