dex/
source.rs

1use std::{clone::Clone, convert::AsRef, ops::Index, rc::Rc};
2
3use crate::ubyte;
4
5/// Represents the source `Dex` file. This is a
6/// wrapper type that allows for shallow copies
7/// of the dex file's source.
8pub(crate) struct Source<T> {
9    inner: Rc<T>,
10}
11
12impl<T> Source<T>
13where
14    T: AsRef<[u8]>,
15{
16    pub(crate) fn new(inner: T) -> Self {
17        Self {
18            inner: Rc::new(inner),
19        }
20    }
21}
22
23impl<T> Index<usize> for Source<T>
24where
25    T: AsRef<[u8]>,
26{
27    type Output = ubyte;
28
29    fn index(&self, index: usize) -> &Self::Output {
30        &self.as_ref()[index]
31    }
32}
33
34impl<T> Index<std::ops::Range<usize>> for Source<T>
35where
36    T: AsRef<[u8]>,
37{
38    type Output = [ubyte];
39
40    fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
41        &self.as_ref()[index]
42    }
43}
44
45impl<T> Index<std::ops::RangeFrom<usize>> for Source<T>
46where
47    T: AsRef<[u8]>,
48{
49    type Output = [ubyte];
50
51    fn index(&self, index: std::ops::RangeFrom<usize>) -> &Self::Output {
52        &self.as_ref()[index]
53    }
54}
55
56impl<T> Clone for Source<T> {
57    fn clone(&self) -> Self {
58        Self {
59            inner: self.inner.clone(),
60        }
61    }
62}
63
64impl<T: AsRef<[u8]>> AsRef<[u8]> for Source<T> {
65    fn as_ref(&self) -> &[ubyte] {
66        self.inner.as_ref().as_ref()
67    }
68}