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