]>
Commit | Line | Data |
---|---|---|
32a655c1 SL |
1 | // Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
32a655c1 | 11 | use std::fmt; |
041b39d2 | 12 | use std::ops::{Deref, Range}; |
32a655c1 SL |
13 | use std::rc::Rc; |
14 | ||
cc61c64b XL |
15 | use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult, |
16 | HashStable}; | |
17 | ||
32a655c1 SL |
18 | #[derive(Clone)] |
19 | pub struct RcSlice<T> { | |
20 | data: Rc<Box<[T]>>, | |
21 | offset: u32, | |
22 | len: u32, | |
23 | } | |
24 | ||
25 | impl<T> RcSlice<T> { | |
26 | pub fn new(vec: Vec<T>) -> Self { | |
27 | RcSlice { | |
28 | offset: 0, | |
29 | len: vec.len() as u32, | |
30 | data: Rc::new(vec.into_boxed_slice()), | |
31 | } | |
32 | } | |
041b39d2 XL |
33 | |
34 | pub fn sub_slice(&self, range: Range<usize>) -> Self { | |
35 | RcSlice { | |
36 | data: self.data.clone(), | |
37 | offset: self.offset + range.start as u32, | |
38 | len: (range.end - range.start) as u32, | |
39 | } | |
40 | } | |
32a655c1 SL |
41 | } |
42 | ||
43 | impl<T> Deref for RcSlice<T> { | |
44 | type Target = [T]; | |
45 | fn deref(&self) -> &[T] { | |
46 | &self.data[self.offset as usize .. (self.offset + self.len) as usize] | |
47 | } | |
48 | } | |
49 | ||
32a655c1 SL |
50 | impl<T: fmt::Debug> fmt::Debug for RcSlice<T> { |
51 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
52 | fmt::Debug::fmt(self.deref(), f) | |
53 | } | |
54 | } | |
cc61c64b XL |
55 | |
56 | impl<CTX, T> HashStable<CTX> for RcSlice<T> | |
57 | where T: HashStable<CTX> | |
58 | { | |
59 | fn hash_stable<W: StableHasherResult>(&self, | |
60 | hcx: &mut CTX, | |
61 | hasher: &mut StableHasher<W>) { | |
62 | (**self).hash_stable(hcx, hasher); | |
63 | } | |
64 | } |