]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/ptr.rs
cleanup: Remove `extern crate serialize as rustc_serialize`s
[rust.git] / src / librustc / hir / ptr.rs
1 // HACK(eddyb) this is a copy of `syntax::ptr`, minus the mutation (the HIR is
2 // frozen anyway). The only reason for doing this instead of replacing `P<T>`
3 // with `Box<T>` in HIR, is that `&Box<[T]>` doesn't implement `IntoIterator`.
4
5 use std::fmt::{self, Display, Debug};
6 use std::iter::FromIterator;
7 use std::ops::Deref;
8 use std::{slice, vec};
9
10 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
11
12 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
13                                            HashStable};
14 /// An owned smart pointer.
15 #[derive(Hash, PartialEq, Eq)]
16 pub struct P<T: ?Sized> {
17     ptr: Box<T>
18 }
19
20 /// Construct a `P<T>` from a `T` value.
21 #[allow(non_snake_case)]
22 pub fn P<T: 'static>(value: T) -> P<T> {
23     P {
24         ptr: box value
25     }
26 }
27
28 impl<T: 'static> P<T> {
29     // HACK(eddyb) used by HIR lowering in a few places still.
30     // NOTE: do not make this more public than `pub(super)`.
31     pub(super) fn into_inner(self) -> T {
32         *self.ptr
33     }
34 }
35
36 impl<T: ?Sized> Deref for P<T> {
37     type Target = T;
38
39     fn deref(&self) -> &T {
40         &self.ptr
41     }
42 }
43
44 impl<T: ?Sized + Debug> Debug for P<T> {
45     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46         Debug::fmt(&self.ptr, f)
47     }
48 }
49
50 impl<T: Display> Display for P<T> {
51     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52         Display::fmt(&**self, f)
53     }
54 }
55
56 impl<T: 'static + Decodable> Decodable for P<T> {
57     fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> {
58         Decodable::decode(d).map(P)
59     }
60 }
61
62 impl<T: Encodable> Encodable for P<T> {
63     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
64         (**self).encode(s)
65     }
66 }
67
68 impl<T> P<[T]> {
69     pub const fn new() -> P<[T]> {
70         // HACK(eddyb) bypass the lack of a `const fn` to create an empty `Box<[T]>`
71         // (as trait methods, `default` in this case, can't be `const fn` yet).
72         P {
73             ptr: unsafe {
74                 use std::ptr::NonNull;
75                 std::mem::transmute(NonNull::<[T; 0]>::dangling() as NonNull<[T]>)
76             },
77         }
78     }
79
80     #[inline(never)]
81     pub fn from_vec(v: Vec<T>) -> P<[T]> {
82         P { ptr: v.into_boxed_slice() }
83     }
84
85     // HACK(eddyb) used by HIR lowering in a few places still.
86     // NOTE: do not make this more public than `pub(super)`,
87     // and do not make this into an `IntoIterator` impl.
88     pub(super) fn into_iter(self) -> vec::IntoIter<T> {
89         self.ptr.into_vec().into_iter()
90     }
91 }
92
93
94 impl<T> Default for P<[T]> {
95     /// Creates an empty `P<[T]>`.
96     fn default() -> P<[T]> {
97         P::new()
98     }
99 }
100
101 impl<T> From<Vec<T>> for P<[T]> {
102     fn from(v: Vec<T>) -> Self {
103         P::from_vec(v)
104     }
105 }
106
107 impl<T> FromIterator<T> for P<[T]> {
108     fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
109         P::from_vec(iter.into_iter().collect())
110     }
111 }
112
113 impl<'a, T> IntoIterator for &'a P<[T]> {
114     type Item = &'a T;
115     type IntoIter = slice::Iter<'a, T>;
116     fn into_iter(self) -> Self::IntoIter {
117         self.ptr.into_iter()
118     }
119 }
120
121 impl<T: Encodable> Encodable for P<[T]> {
122     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
123         Encodable::encode(&**self, s)
124     }
125 }
126
127 impl<T: Decodable> Decodable for P<[T]> {
128     fn decode<D: Decoder>(d: &mut D) -> Result<P<[T]>, D::Error> {
129         Ok(P::from_vec(Decodable::decode(d)?))
130     }
131 }
132
133 impl<CTX, T> HashStable<CTX> for P<T>
134     where T: ?Sized + HashStable<CTX>
135 {
136     fn hash_stable<W: StableHasherResult>(&self,
137                                           hcx: &mut CTX,
138                                           hasher: &mut StableHasher<W>) {
139         (**self).hash_stable(hcx, hasher);
140     }
141 }