1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::fmt;
use std::ops::{Deref, Range};
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
                                           HashStable};
#[derive(Clone)]
pub struct RcSlice<T> {
    data: Lrc<Box<[T]>>,
    offset: u32,
    len: u32,
}
impl<T> RcSlice<T> {
    pub fn new(vec: Vec<T>) -> Self {
        RcSlice {
            offset: 0,
            len: vec.len() as u32,
            data: Lrc::new(vec.into_boxed_slice()),
        }
    }
    pub fn sub_slice(&self, range: Range<usize>) -> Self {
        RcSlice {
            data: self.data.clone(),
            offset: self.offset + range.start as u32,
            len: (range.end - range.start) as u32,
        }
    }
}
impl<T> Deref for RcSlice<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        &self.data[self.offset as usize .. (self.offset + self.len) as usize]
    }
}
impl<T: fmt::Debug> fmt::Debug for RcSlice<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self.deref(), f)
    }
}
impl<CTX, T> HashStable<CTX> for RcSlice<T>
    where T: HashStable<CTX>
{
    fn hash_stable<W: StableHasherResult>(&self,
                                          hcx: &mut CTX,
                                          hasher: &mut StableHasher<W>) {
        (**self).hash_stable(hcx, hasher);
    }
}