use std::borrow::Cow;
use super::Bytes;
#[doc = r#"
Any type that represents some midi source type.
"#]
pub trait MidiSource<'slc> {
fn max_len(&self) -> usize;
fn get_slice(&self, start: usize, end: usize) -> Option<Bytes<'slc>>;
fn get_byte(&self, pos: usize) -> Option<u8>;
}
impl<'slc> MidiSource<'slc> for &'slc [u8] {
fn max_len(&self) -> usize {
self.len()
}
fn get_slice(&self, start: usize, end: usize) -> Option<Bytes<'slc>> {
self.get(start..end).map(Into::into)
}
fn get_byte(&self, pos: usize) -> Option<u8> {
self.get(pos).copied()
}
}
impl<'a> MidiSource<'a> for Bytes<'a> {
fn max_len(&self) -> usize {
self.len()
}
fn get_slice(&self, start: usize, end: usize) -> Option<Bytes<'a>> {
match self.as_cow() {
Cow::Borrowed(v) => v.get(start..end).map(Into::into),
Cow::Owned(v) => {
let slice = v.get(start..end)?;
Some(slice.to_vec().into())
}
}
}
fn get_byte(&self, pos: usize) -> Option<u8> {
self.get(pos).copied()
}
}