• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::mem;
2 use core::slice;
3 use std::rc::Rc;
4 use std::vec;
5 
6 pub(crate) struct RcVec<T> {
7     inner: Rc<Vec<T>>,
8 }
9 
10 pub(crate) struct RcVecBuilder<T> {
11     inner: Vec<T>,
12 }
13 
14 pub(crate) struct RcVecMut<'a, T> {
15     inner: &'a mut Vec<T>,
16 }
17 
18 #[derive(Clone)]
19 pub(crate) struct RcVecIntoIter<T> {
20     inner: vec::IntoIter<T>,
21 }
22 
23 impl<T> RcVec<T> {
is_empty(&self) -> bool24     pub fn is_empty(&self) -> bool {
25         self.inner.is_empty()
26     }
27 
len(&self) -> usize28     pub fn len(&self) -> usize {
29         self.inner.len()
30     }
31 
iter(&self) -> slice::Iter<T>32     pub fn iter(&self) -> slice::Iter<T> {
33         self.inner.iter()
34     }
35 
make_mut(&mut self) -> RcVecMut<T> where T: Clone,36     pub fn make_mut(&mut self) -> RcVecMut<T>
37     where
38         T: Clone,
39     {
40         RcVecMut {
41             inner: Rc::make_mut(&mut self.inner),
42         }
43     }
44 
get_mut(&mut self) -> Option<RcVecMut<T>>45     pub fn get_mut(&mut self) -> Option<RcVecMut<T>> {
46         let inner = Rc::get_mut(&mut self.inner)?;
47         Some(RcVecMut { inner })
48     }
49 
make_owned(mut self) -> RcVecBuilder<T> where T: Clone,50     pub fn make_owned(mut self) -> RcVecBuilder<T>
51     where
52         T: Clone,
53     {
54         let vec = if let Some(owned) = Rc::get_mut(&mut self.inner) {
55             mem::replace(owned, Vec::new())
56         } else {
57             Vec::clone(&self.inner)
58         };
59         RcVecBuilder { inner: vec }
60     }
61 }
62 
63 impl<T> RcVecBuilder<T> {
new() -> Self64     pub fn new() -> Self {
65         RcVecBuilder { inner: Vec::new() }
66     }
67 
with_capacity(cap: usize) -> Self68     pub fn with_capacity(cap: usize) -> Self {
69         RcVecBuilder {
70             inner: Vec::with_capacity(cap),
71         }
72     }
73 
push(&mut self, element: T)74     pub fn push(&mut self, element: T) {
75         self.inner.push(element);
76     }
77 
extend(&mut self, iter: impl IntoIterator<Item = T>)78     pub fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
79         self.inner.extend(iter);
80     }
81 
as_mut(&mut self) -> RcVecMut<T>82     pub fn as_mut(&mut self) -> RcVecMut<T> {
83         RcVecMut {
84             inner: &mut self.inner,
85         }
86     }
87 
build(self) -> RcVec<T>88     pub fn build(self) -> RcVec<T> {
89         RcVec {
90             inner: Rc::new(self.inner),
91         }
92     }
93 }
94 
95 impl<'a, T> RcVecMut<'a, T> {
push(&mut self, element: T)96     pub fn push(&mut self, element: T) {
97         self.inner.push(element);
98     }
99 
extend(&mut self, iter: impl IntoIterator<Item = T>)100     pub fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
101         self.inner.extend(iter);
102     }
103 
pop(&mut self) -> Option<T>104     pub fn pop(&mut self) -> Option<T> {
105         self.inner.pop()
106     }
107 
as_mut(&mut self) -> RcVecMut<T>108     pub fn as_mut(&mut self) -> RcVecMut<T> {
109         RcVecMut { inner: self.inner }
110     }
111 }
112 
113 impl<T> Clone for RcVec<T> {
clone(&self) -> Self114     fn clone(&self) -> Self {
115         RcVec {
116             inner: Rc::clone(&self.inner),
117         }
118     }
119 }
120 
121 impl<T> IntoIterator for RcVecBuilder<T> {
122     type Item = T;
123     type IntoIter = RcVecIntoIter<T>;
124 
into_iter(self) -> Self::IntoIter125     fn into_iter(self) -> Self::IntoIter {
126         RcVecIntoIter {
127             inner: self.inner.into_iter(),
128         }
129     }
130 }
131 
132 impl<T> Iterator for RcVecIntoIter<T> {
133     type Item = T;
134 
next(&mut self) -> Option<Self::Item>135     fn next(&mut self) -> Option<Self::Item> {
136         self.inner.next()
137     }
138 
size_hint(&self) -> (usize, Option<usize>)139     fn size_hint(&self) -> (usize, Option<usize>) {
140         self.inner.size_hint()
141     }
142 }
143