]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax/ptr.rs
Rollup merge of #68141 - euclio:replace-bindings-with-winapi, r=alexcrichton
[rust.git] / src / libsyntax / ptr.rs
index d987dc855b6156ec8c7ae2fbdd272167a1c47540..4597624ef88b08d21f55c6d55bac50f39709494b 100644 (file)
@@ -1,17 +1,12 @@
 //! The AST pointer.
 //!
-//! Provides `P<T>`, a frozen owned smart pointer, as a replacement for `@T` in
-//! the AST.
+//! Provides `P<T>`, a frozen owned smart pointer.
 //!
 //! # Motivations and benefits
 //!
 //! * **Identity**: sharing AST nodes is problematic for the various analysis
 //!   passes (e.g., one may be able to bypass the borrow checker with a shared
-//!   `ExprKind::AddrOf` node taking a mutable borrow). The only reason `@T` in the
-//!   AST hasn't caused issues is because of inefficient folding passes which
-//!   would always deduplicate any such shared nodes. Even if the AST were to
-//!   switch to an arena, this would still hold, i.e., it couldn't use `&'a T`,
-//!   but rather a wrapper like `P<'a, T>`.
+//!   `ExprKind::AddrOf` node taking a mutable borrow).
 //!
 //! * **Immutability**: `P<T>` disallows mutating its inner `T`, unlike `Box<T>`
 //!   (unless it contains an `Unsafe` interior, but that may be denied later).
 //!   implementation changes (using a special thread-local heap, for example).
 //!   Moreover, a switch to, e.g., `P<'a, T>` would be easy and mostly automated.
 
-use std::fmt::{self, Display, Debug};
+use std::fmt::{self, Debug, Display};
 use std::iter::FromIterator;
 use std::ops::{Deref, DerefMut};
 use std::{slice, vec};
 
-use rustc_serialize::{Encodable, Decodable, Encoder, Decoder};
+use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
 
-use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
+use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
 /// An owned smart pointer.
 pub struct P<T: ?Sized> {
-    ptr: Box<T>
+    ptr: Box<T>,
 }
 
 /// Construct a `P<T>` from a `T` value.
 #[allow(non_snake_case)]
 pub fn P<T: 'static>(value: T) -> P<T> {
-    P {
-        ptr: box value
-    }
+    P { ptr: box value }
 }
 
 impl<T: 'static> P<T> {
     /// Move out of the pointer.
     /// Intended for chaining transformations not covered by `map`.
-    pub fn and_then<U, F>(self, f: F) -> U where
+    pub fn and_then<U, F>(self, f: F) -> U
+    where
         F: FnOnce(T) -> U,
     {
         f(*self.ptr)
@@ -62,7 +56,8 @@ pub fn into_inner(self) -> T {
     }
 
     /// Produce a new `P<T>` from `self` without reallocating.
-    pub fn map<F>(mut self, f: F) -> P<T> where
+    pub fn map<F>(mut self, f: F) -> P<T>
+    where
         F: FnOnce(T) -> T,
     {
         let x = f(*self.ptr);
@@ -72,7 +67,8 @@ pub fn map<F>(mut self, f: F) -> P<T> where
     }
 
     /// Optionally produce a new `P<T>` from `self` without reallocating.
-    pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> where
+    pub fn filter_map<F>(mut self, f: F) -> Option<P<T>>
+    where
         F: FnOnce(T) -> Option<T>,
     {
         *self.ptr = f(*self.ptr)?;
@@ -179,7 +175,7 @@ fn into(self) -> Vec<T> {
 }
 
 impl<T> FromIterator<T> for P<[T]> {
-    fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> P<[T]> {
+    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> P<[T]> {
         P::from_vec(iter.into_iter().collect())
     }
 }
@@ -214,7 +210,8 @@ fn decode<D: Decoder>(d: &mut D) -> Result<P<[T]>, D::Error> {
 }
 
 impl<CTX, T> HashStable<CTX> for P<T>
-    where T: ?Sized + HashStable<CTX>
+where
+    T: ?Sized + HashStable<CTX>,
 {
     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
         (**self).hash_stable(hcx, hasher);