]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/rc_vec.rs
improve tests as suggested by review comments
[rust.git] / src / libsyntax / util / rc_vec.rs
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
11 use std::fmt;
12 use std::ops::{Deref, Range};
13
14 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
15 use rustc_data_structures::sync::Lrc;
16
17 #[derive(Clone)]
18 pub struct RcVec<T> {
19     data: Lrc<Vec<T>>,
20     offset: u32,
21     len: u32,
22 }
23
24 impl<T> RcVec<T> {
25     pub fn new(mut vec: Vec<T>) -> Self {
26         // By default, constructing RcVec from Vec gives it just enough capacity
27         // to hold the initial elements. Callers that anticipate needing to
28         // extend the vector may prefer RcVec::new_preserving_capacity.
29         vec.shrink_to_fit();
30         Self::new_preserving_capacity(vec)
31     }
32
33     pub fn new_preserving_capacity(vec: Vec<T>) -> Self {
34         RcVec {
35             offset: 0,
36             len: vec.len() as u32,
37             data: Lrc::new(vec),
38         }
39     }
40
41     pub fn sub_slice(&self, range: Range<usize>) -> Self {
42         RcVec {
43             data: self.data.clone(),
44             offset: self.offset + range.start as u32,
45             len: (range.end - range.start) as u32,
46         }
47     }
48
49     /// If this RcVec has exactly one strong reference, returns ownership of the
50     /// underlying vector. Otherwise returns self unmodified.
51     pub fn try_unwrap(self) -> Result<Vec<T>, Self> {
52         match Lrc::try_unwrap(self.data) {
53             // If no other RcVec shares ownership of this data.
54             Ok(mut vec) => {
55                 // Drop any elements after our view of the data.
56                 vec.truncate(self.offset as usize + self.len as usize);
57                 // Drop any elements before our view of the data. Do this after
58                 // the `truncate` so that elements past the end of our view do
59                 // not need to be copied around.
60                 vec.drain(..self.offset as usize);
61                 Ok(vec)
62             }
63
64             // If the data is shared.
65             Err(data) => Err(RcVec { data, ..self }),
66         }
67     }
68 }
69
70 impl<T> Deref for RcVec<T> {
71     type Target = [T];
72     fn deref(&self) -> &[T] {
73         &self.data[self.offset as usize..(self.offset + self.len) as usize]
74     }
75 }
76
77 impl<T: fmt::Debug> fmt::Debug for RcVec<T> {
78     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79         fmt::Debug::fmt(self.deref(), f)
80     }
81 }
82
83 impl<CTX, T> HashStable<CTX> for RcVec<T>
84 where
85     T: HashStable<CTX>,
86 {
87     fn hash_stable<W: StableHasherResult>(&self, hcx: &mut CTX, hasher: &mut StableHasher<W>) {
88         (**self).hash_stable(hcx, hasher);
89     }
90 }