]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/util/rc_slice.rs
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libsyntax / util / rc_slice.rs
CommitLineData
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 11use std::fmt;
041b39d2 12use std::ops::{Deref, Range};
32a655c1
SL
13use std::rc::Rc;
14
cc61c64b
XL
15use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
16 HashStable};
17
32a655c1
SL
18#[derive(Clone)]
19pub struct RcSlice<T> {
20 data: Rc<Box<[T]>>,
21 offset: u32,
22 len: u32,
23}
24
25impl<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
43impl<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
50impl<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
56impl<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}