3 //! Provides `P<T>`, a frozen owned smart pointer.
5 //! # Motivations and benefits
7 //! * **Identity**: sharing AST nodes is problematic for the various analysis
8 //! passes (e.g., one may be able to bypass the borrow checker with a shared
9 //! `ExprKind::AddrOf` node taking a mutable borrow).
11 //! * **Immutability**: `P<T>` disallows mutating its inner `T`, unlike `Box<T>`
12 //! (unless it contains an `Unsafe` interior, but that may be denied later).
13 //! This mainly prevents mistakes, but also enforces a kind of "purity".
15 //! * **Efficiency**: folding can reuse allocation space for `P<T>` and `Vec<T>`,
16 //! the latter even when the input and output types differ (as it would be the
17 //! case with arenas or a GADT AST using type parameters to toggle features).
19 //! * **Maintainability**: `P<T>` provides a fixed interface - `Deref`,
20 //! `and_then` and `map` - which can remain fully functional even if the
21 //! implementation changes (using a special thread-local heap, for example).
22 //! Moreover, a switch to, e.g., `P<'a, T>` would be easy and mostly automated.
24 use std::fmt::{self, Debug, Display};
25 use std::ops::{Deref, DerefMut};
26 use std::{slice, vec};
28 use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
30 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
31 /// An owned smart pointer.
32 pub struct P<T: ?Sized> {
36 /// Construct a `P<T>` from a `T` value.
37 #[allow(non_snake_case)]
38 pub fn P<T: 'static>(value: T) -> P<T> {
39 P { ptr: Box::new(value) }
42 impl<T: 'static> P<T> {
43 /// Move out of the pointer.
44 /// Intended for chaining transformations not covered by `map`.
45 pub fn and_then<U, F>(self, f: F) -> U
52 /// Equivalent to `and_then(|x| x)`.
53 pub fn into_inner(self) -> T {
57 /// Produce a new `P<T>` from `self` without reallocating.
58 pub fn map<F>(mut self, f: F) -> P<T>
68 /// Optionally produce a new `P<T>` from `self` without reallocating.
69 pub fn filter_map<F>(mut self, f: F) -> Option<P<T>>
71 F: FnOnce(T) -> Option<T>,
73 *self.ptr = f(*self.ptr)?;
78 impl<T: ?Sized> Deref for P<T> {
81 fn deref(&self) -> &T {
86 impl<T: ?Sized> DerefMut for P<T> {
87 fn deref_mut(&mut self) -> &mut T {
92 impl<T: 'static + Clone> Clone for P<T> {
93 fn clone(&self) -> P<T> {
98 impl<T: ?Sized + Debug> Debug for P<T> {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 Debug::fmt(&self.ptr, f)
104 impl<T: Display> Display for P<T> {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 Display::fmt(&**self, f)
110 impl<T> fmt::Pointer for P<T> {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 fmt::Pointer::fmt(&self.ptr, f)
116 impl<D: Decoder, T: 'static + Decodable<D>> Decodable<D> for P<T> {
117 fn decode(d: &mut D) -> P<T> {
118 P(Decodable::decode(d))
122 impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<T> {
123 fn encode(&self, s: &mut S) {
129 pub const fn new() -> P<[T]> {
130 P { ptr: Box::default() }
134 pub fn from_vec(v: Vec<T>) -> P<[T]> {
135 P { ptr: v.into_boxed_slice() }
139 pub fn into_vec(self) -> Vec<T> {
144 impl<T> Default for P<[T]> {
145 /// Creates an empty `P<[T]>`.
146 fn default() -> P<[T]> {
151 impl<T: Clone> Clone for P<[T]> {
152 fn clone(&self) -> P<[T]> {
153 P::from_vec(self.to_vec())
157 impl<T> From<Vec<T>> for P<[T]> {
158 fn from(v: Vec<T>) -> Self {
163 impl<T> Into<Vec<T>> for P<[T]> {
164 fn into(self) -> Vec<T> {
169 impl<T> FromIterator<T> for P<[T]> {
170 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> P<[T]> {
171 P::from_vec(iter.into_iter().collect())
175 impl<T> IntoIterator for P<[T]> {
177 type IntoIter = vec::IntoIter<T>;
179 fn into_iter(self) -> Self::IntoIter {
180 self.into_vec().into_iter()
184 impl<'a, T> IntoIterator for &'a P<[T]> {
186 type IntoIter = slice::Iter<'a, T>;
187 fn into_iter(self) -> Self::IntoIter {
192 impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<[T]> {
193 fn encode(&self, s: &mut S) {
194 Encodable::encode(&**self, s);
198 impl<D: Decoder, T: Decodable<D>> Decodable<D> for P<[T]> {
199 fn decode(d: &mut D) -> P<[T]> {
200 P::from_vec(Decodable::decode(d))
204 impl<CTX, T> HashStable<CTX> for P<T>
206 T: ?Sized + HashStable<CTX>,
208 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
209 (**self).hash_stable(hcx, hasher);