]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 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 | //! The AST pointer | |
12 | //! | |
13 | //! Provides `P<T>`, a frozen owned smart pointer, as a replacement for `@T` in | |
14 | //! the AST. | |
15 | //! | |
16 | //! # Motivations and benefits | |
17 | //! | |
18 | //! * **Identity**: sharing AST nodes is problematic for the various analysis | |
19 | //! passes (e.g. one may be able to bypass the borrow checker with a shared | |
7453a54e | 20 | //! `ExprKind::AddrOf` node taking a mutable borrow). The only reason `@T` in the |
1a4d82fc JJ |
21 | //! AST hasn't caused issues is because of inefficient folding passes which |
22 | //! would always deduplicate any such shared nodes. Even if the AST were to | |
23 | //! switch to an arena, this would still hold, i.e. it couldn't use `&'a T`, | |
24 | //! but rather a wrapper like `P<'a, T>`. | |
25 | //! | |
26 | //! * **Immutability**: `P<T>` disallows mutating its inner `T`, unlike `Box<T>` | |
27 | //! (unless it contains an `Unsafe` interior, but that may be denied later). | |
28 | //! This mainly prevents mistakes, but can also enforces a kind of "purity". | |
29 | //! | |
30 | //! * **Efficiency**: folding can reuse allocation space for `P<T>` and `Vec<T>`, | |
31 | //! the latter even when the input and output types differ (as it would be the | |
32 | //! case with arenas or a GADT AST using type parameters to toggle features). | |
33 | //! | |
34 | //! * **Maintainability**: `P<T>` provides a fixed interface - `Deref`, | |
35 | //! `and_then` and `map` - which can remain fully functional even if the | |
36 | //! implementation changes (using a special thread-local heap, for example). | |
37 | //! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated. | |
38 | ||
85aaf69f | 39 | use std::fmt::{self, Display, Debug}; |
9cc50fc6 | 40 | use std::iter::FromIterator; |
1a4d82fc | 41 | use std::ops::Deref; |
9e0c209e | 42 | use std::{mem, ptr, slice, vec}; |
1a4d82fc JJ |
43 | |
44 | use serialize::{Encodable, Decodable, Encoder, Decoder}; | |
45 | ||
cc61c64b XL |
46 | use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult, |
47 | HashStable}; | |
1a4d82fc | 48 | /// An owned smart pointer. |
9cc50fc6 SL |
49 | #[derive(Hash, PartialEq, Eq, PartialOrd, Ord)] |
50 | pub struct P<T: ?Sized> { | |
1a4d82fc JJ |
51 | ptr: Box<T> |
52 | } | |
53 | ||
54 | #[allow(non_snake_case)] | |
55 | /// Construct a `P<T>` from a `T` value. | |
56 | pub fn P<T: 'static>(value: T) -> P<T> { | |
57 | P { | |
d9579d0f | 58 | ptr: Box::new(value) |
1a4d82fc JJ |
59 | } |
60 | } | |
61 | ||
62 | impl<T: 'static> P<T> { | |
63 | /// Move out of the pointer. | |
64 | /// Intended for chaining transformations not covered by `map`. | |
65 | pub fn and_then<U, F>(self, f: F) -> U where | |
66 | F: FnOnce(T) -> U, | |
67 | { | |
68 | f(*self.ptr) | |
69 | } | |
7453a54e | 70 | /// Equivalent to and_then(|x| x) |
ff7c6d11 | 71 | pub fn into_inner(self) -> T { |
7453a54e SL |
72 | *self.ptr |
73 | } | |
1a4d82fc JJ |
74 | |
75 | /// Transform the inner value, consuming `self` and producing a new `P<T>`. | |
76 | pub fn map<F>(mut self, f: F) -> P<T> where | |
77 | F: FnOnce(T) -> T, | |
78 | { | |
9e0c209e SL |
79 | let p: *mut T = &mut *self.ptr; |
80 | ||
81 | // Leak self in case of panic. | |
82 | // FIXME(eddyb) Use some sort of "free guard" that | |
83 | // only deallocates, without dropping the pointee, | |
84 | // in case the call the `f` below ends in a panic. | |
85 | mem::forget(self); | |
86 | ||
1a4d82fc | 87 | unsafe { |
9e0c209e SL |
88 | ptr::write(p, f(ptr::read(p))); |
89 | ||
90 | // Recreate self from the raw pointer. | |
91 | P { | |
92 | ptr: Box::from_raw(p) | |
93 | } | |
1a4d82fc | 94 | } |
1a4d82fc JJ |
95 | } |
96 | } | |
97 | ||
a7813a04 | 98 | impl<T: ?Sized> Deref for P<T> { |
1a4d82fc JJ |
99 | type Target = T; |
100 | ||
a7813a04 | 101 | fn deref(&self) -> &T { |
7453a54e | 102 | &self.ptr |
1a4d82fc JJ |
103 | } |
104 | } | |
105 | ||
106 | impl<T: 'static + Clone> Clone for P<T> { | |
107 | fn clone(&self) -> P<T> { | |
108 | P((**self).clone()) | |
109 | } | |
110 | } | |
111 | ||
a7813a04 | 112 | impl<T: ?Sized + Debug> Debug for P<T> { |
1a4d82fc | 113 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
a7813a04 | 114 | Debug::fmt(&self.ptr, f) |
85aaf69f SL |
115 | } |
116 | } | |
a7813a04 | 117 | |
85aaf69f SL |
118 | impl<T: Display> Display for P<T> { |
119 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
120 | Display::fmt(&**self, f) | |
1a4d82fc JJ |
121 | } |
122 | } | |
123 | ||
9346a6ac AL |
124 | impl<T> fmt::Pointer for P<T> { |
125 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
126 | fmt::Pointer::fmt(&self.ptr, f) | |
127 | } | |
128 | } | |
129 | ||
1a4d82fc JJ |
130 | impl<T: 'static + Decodable> Decodable for P<T> { |
131 | fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> { | |
132 | Decodable::decode(d).map(P) | |
133 | } | |
134 | } | |
135 | ||
136 | impl<T: Encodable> Encodable for P<T> { | |
137 | fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { | |
138 | (**self).encode(s) | |
139 | } | |
140 | } | |
9cc50fc6 | 141 | |
9cc50fc6 SL |
142 | impl<T> P<[T]> { |
143 | pub fn new() -> P<[T]> { | |
9cc50fc6 SL |
144 | P { ptr: Default::default() } |
145 | } | |
146 | ||
147 | #[inline(never)] | |
148 | pub fn from_vec(v: Vec<T>) -> P<[T]> { | |
149 | P { ptr: v.into_boxed_slice() } | |
150 | } | |
151 | ||
152 | #[inline(never)] | |
153 | pub fn into_vec(self) -> Vec<T> { | |
154 | self.ptr.into_vec() | |
155 | } | |
9cc50fc6 SL |
156 | } |
157 | ||
158 | impl<T> Default for P<[T]> { | |
9e0c209e | 159 | /// Creates an empty `P<[T]>`. |
9cc50fc6 | 160 | fn default() -> P<[T]> { |
a7813a04 | 161 | P::new() |
9cc50fc6 SL |
162 | } |
163 | } | |
164 | ||
165 | impl<T: Clone> Clone for P<[T]> { | |
166 | fn clone(&self) -> P<[T]> { | |
167 | P::from_vec(self.to_vec()) | |
168 | } | |
169 | } | |
170 | ||
171 | impl<T> From<Vec<T>> for P<[T]> { | |
172 | fn from(v: Vec<T>) -> Self { | |
173 | P::from_vec(v) | |
174 | } | |
175 | } | |
176 | ||
177 | impl<T> Into<Vec<T>> for P<[T]> { | |
178 | fn into(self) -> Vec<T> { | |
179 | self.into_vec() | |
180 | } | |
181 | } | |
182 | ||
183 | impl<T> FromIterator<T> for P<[T]> { | |
184 | fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> { | |
185 | P::from_vec(iter.into_iter().collect()) | |
186 | } | |
187 | } | |
188 | ||
189 | impl<T> IntoIterator for P<[T]> { | |
190 | type Item = T; | |
191 | type IntoIter = vec::IntoIter<T>; | |
192 | ||
193 | fn into_iter(self) -> Self::IntoIter { | |
194 | self.into_vec().into_iter() | |
195 | } | |
196 | } | |
197 | ||
198 | impl<'a, T> IntoIterator for &'a P<[T]> { | |
199 | type Item = &'a T; | |
200 | type IntoIter = slice::Iter<'a, T>; | |
201 | fn into_iter(self) -> Self::IntoIter { | |
202 | self.ptr.into_iter() | |
203 | } | |
204 | } | |
205 | ||
206 | impl<T: Encodable> Encodable for P<[T]> { | |
207 | fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { | |
208 | Encodable::encode(&**self, s) | |
209 | } | |
210 | } | |
211 | ||
212 | impl<T: Decodable> Decodable for P<[T]> { | |
213 | fn decode<D: Decoder>(d: &mut D) -> Result<P<[T]>, D::Error> { | |
041b39d2 | 214 | Ok(P::from_vec(Decodable::decode(d)?)) |
9cc50fc6 SL |
215 | } |
216 | } | |
cc61c64b XL |
217 | |
218 | impl<CTX, T> HashStable<CTX> for P<T> | |
219 | where T: ?Sized + HashStable<CTX> | |
220 | { | |
221 | fn hash_stable<W: StableHasherResult>(&self, | |
222 | hcx: &mut CTX, | |
223 | hasher: &mut StableHasher<W>) { | |
224 | (**self).hash_stable(hcx, hasher); | |
225 | } | |
226 | } |