use std::{
clone::Clone,
convert::AsRef,
ops::{Deref, Index},
rc::Rc,
};
pub struct Source<T> {
inner: Rc<T>,
}
impl<T> Source<T>
where
T: AsRef<[u8]>,
{
pub(crate) fn new(inner: T) -> Self {
Self {
inner: Rc::new(inner),
}
}
}
impl<T> Clone for Source<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T: AsRef<[u8]>> Deref for Source<T> {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
self.as_ref()
}
}
impl<T: AsRef<[u8]>> AsRef<[u8]> for Source<T> {
fn as_ref(&self) -> &[u8] {
self.inner.as_ref().as_ref()
}
}
impl<T> Index<usize> for Source<T>
where
T: AsRef<[u8]>,
{
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
&self.as_ref()[index]
}
}
impl<T> Index<std::ops::Range<usize>> for Source<T>
where
T: AsRef<[u8]>,
{
type Output = [u8];
fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
&self.as_ref()[index]
}
}
impl<T> Index<std::ops::RangeFrom<usize>> for Source<T>
where
T: AsRef<[u8]>,
{
type Output = [u8];
fn index(&self, index: std::ops::RangeFrom<usize>) -> &Self::Output {
&self.as_ref()[index]
}
}