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`.
5 use std::fmt::{self, Display, Debug};
6 use std::iter::FromIterator;
10 use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
12 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
14 /// An owned smart pointer.
15 #[derive(Hash, PartialEq, Eq)]
16 pub struct P<T: ?Sized> {
20 /// Construct a `P<T>` from a `T` value.
21 #[allow(non_snake_case)]
22 pub fn P<T: 'static>(value: T) -> P<T> {
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 {
36 impl<T: ?Sized> Deref for P<T> {
39 fn deref(&self) -> &T {
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)
50 impl<T: Display> Display for P<T> {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 Display::fmt(&**self, f)
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)
62 impl<T: Encodable> Encodable for P<T> {
63 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
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).
74 use std::ptr::NonNull;
75 std::mem::transmute(NonNull::<[T; 0]>::dangling() as NonNull<[T]>)
81 pub fn from_vec(v: Vec<T>) -> P<[T]> {
82 P { ptr: v.into_boxed_slice() }
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()
94 impl<T> Default for P<[T]> {
95 /// Creates an empty `P<[T]>`.
96 fn default() -> P<[T]> {
101 impl<T> From<Vec<T>> for P<[T]> {
102 fn from(v: Vec<T>) -> Self {
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())
113 impl<'a, T> IntoIterator for &'a P<[T]> {
115 type IntoIter = slice::Iter<'a, T>;
116 fn into_iter(self) -> Self::IntoIter {
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)
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)?))
133 impl<CTX, T> HashStable<CTX> for P<T>
134 where T: ?Sized + HashStable<CTX>
136 fn hash_stable<W: StableHasherResult>(&self,
138 hasher: &mut StableHasher<W>) {
139 (**self).hash_stable(hcx, hasher);