1use std::{
2 clone::Clone,
3 convert::AsRef,
4 ops::{Deref, Index},
5 sync::Arc,
6};
7
8pub struct Source<T> {
10 inner: Arc<T>,
15}
16
17impl<T> Source<T>
18where
19 T: AsRef<[u8]>,
20{
21 pub(crate) fn new(inner: T) -> Self {
23 Self {
24 inner: Arc::new(inner),
26 }
27 }
28}
29
30impl<T> Clone for Source<T> {
31 fn clone(&self) -> Self {
33 Self {
34 inner: self.inner.clone(),
36 }
37 }
38}
39
40impl<T: AsRef<[u8]>> Deref for Source<T> {
43 type Target = [u8];
44
45 #[inline]
46 fn deref(&self) -> &[u8] {
47 self.as_ref()
48 }
49}
50
51impl<T: AsRef<[u8]>> AsRef<[u8]> for Source<T> {
54 fn as_ref(&self) -> &[u8] {
56 self.inner.as_ref().as_ref()
57 }
58}
59
60impl<T> Index<usize> for Source<T>
62where
63 T: AsRef<[u8]>,
64{
65 type Output = u8;
66
67 fn index(&self, index: usize) -> &Self::Output {
68 &self.as_ref()[index]
69 }
70}
71
72impl<T> Index<std::ops::Range<usize>> for Source<T>
74where
75 T: AsRef<[u8]>,
76{
77 type Output = [u8];
78
79 fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
80 &self.as_ref()[index]
81 }
82}
83
84impl<T> Index<std::ops::RangeFrom<usize>> for Source<T>
86where
87 T: AsRef<[u8]>,
88{
89 type Output = [u8];
90
91 fn index(&self, index: std::ops::RangeFrom<usize>) -> &Self::Output {
92 &self.as_ref()[index]
93 }
94}