]>
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 | |
20 | //! `ExprAddrOf` node taking a mutable borrow). The only reason `@T` in the | |
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}; |
1a4d82fc | 40 | use std::hash::{Hash, Hasher}; |
1a4d82fc JJ |
41 | use std::ops::Deref; |
42 | use std::ptr; | |
43 | ||
44 | use serialize::{Encodable, Decodable, Encoder, Decoder}; | |
45 | ||
46 | /// An owned smart pointer. | |
47 | pub struct P<T> { | |
48 | ptr: Box<T> | |
49 | } | |
50 | ||
51 | #[allow(non_snake_case)] | |
52 | /// Construct a `P<T>` from a `T` value. | |
53 | pub fn P<T: 'static>(value: T) -> P<T> { | |
54 | P { | |
55 | ptr: box value | |
56 | } | |
57 | } | |
58 | ||
59 | impl<T: 'static> P<T> { | |
60 | /// Move out of the pointer. | |
61 | /// Intended for chaining transformations not covered by `map`. | |
62 | pub fn and_then<U, F>(self, f: F) -> U where | |
63 | F: FnOnce(T) -> U, | |
64 | { | |
65 | f(*self.ptr) | |
66 | } | |
67 | ||
68 | /// Transform the inner value, consuming `self` and producing a new `P<T>`. | |
69 | pub fn map<F>(mut self, f: F) -> P<T> where | |
70 | F: FnOnce(T) -> T, | |
71 | { | |
72 | unsafe { | |
73 | let p = &mut *self.ptr; | |
c34b1796 AL |
74 | // FIXME(#5016) this shouldn't need to drop-fill to be safe. |
75 | ptr::write(p, f(ptr::read_and_drop(p))); | |
1a4d82fc JJ |
76 | } |
77 | self | |
78 | } | |
79 | } | |
80 | ||
81 | impl<T> Deref for P<T> { | |
82 | type Target = T; | |
83 | ||
84 | fn deref<'a>(&'a self) -> &'a T { | |
85 | &*self.ptr | |
86 | } | |
87 | } | |
88 | ||
89 | impl<T: 'static + Clone> Clone for P<T> { | |
90 | fn clone(&self) -> P<T> { | |
91 | P((**self).clone()) | |
92 | } | |
93 | } | |
94 | ||
95 | impl<T: PartialEq> PartialEq for P<T> { | |
96 | fn eq(&self, other: &P<T>) -> bool { | |
97 | **self == **other | |
98 | } | |
99 | } | |
100 | ||
101 | impl<T: Eq> Eq for P<T> {} | |
102 | ||
85aaf69f | 103 | impl<T: Debug> Debug for P<T> { |
1a4d82fc | 104 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
85aaf69f SL |
105 | Debug::fmt(&**self, f) |
106 | } | |
107 | } | |
108 | impl<T: Display> Display for P<T> { | |
109 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
110 | Display::fmt(&**self, f) | |
1a4d82fc JJ |
111 | } |
112 | } | |
113 | ||
9346a6ac AL |
114 | #[stable(feature = "rust1", since = "1.0.0")] |
115 | impl<T> fmt::Pointer for P<T> { | |
116 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
117 | fmt::Pointer::fmt(&self.ptr, f) | |
118 | } | |
119 | } | |
120 | ||
85aaf69f SL |
121 | impl<T: Hash> Hash for P<T> { |
122 | fn hash<H: Hasher>(&self, state: &mut H) { | |
1a4d82fc JJ |
123 | (**self).hash(state); |
124 | } | |
125 | } | |
126 | ||
127 | impl<T: 'static + Decodable> Decodable for P<T> { | |
128 | fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> { | |
129 | Decodable::decode(d).map(P) | |
130 | } | |
131 | } | |
132 | ||
133 | impl<T: Encodable> Encodable for P<T> { | |
134 | fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { | |
135 | (**self).encode(s) | |
136 | } | |
137 | } |