diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 4e65082fe3a..0c367f3a9a4 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -25,16 +25,6 @@ use std::rc::Rc; use std::gc::{Gc, GC}; use serialize::{Encodable, Decodable, Encoder, Decoder}; -/// A pointer abstraction. -// FIXME(eddyb) #10676 use Rc in the future. -pub type P = Gc; - -#[allow(non_snake_case)] -/// Construct a P from a T value. -pub fn P(value: T) -> P { - box(GC) value -} - // FIXME #6993: in librustc, uses of "ident" should be replaced // by just "Name". diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 254428486f8..44cc63fbede 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -63,6 +63,7 @@ pub mod diagnostic; pub mod fold; pub mod owned_slice; pub mod parse; +pub mod ptr; pub mod visit; pub mod print { diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs new file mode 100644 index 00000000000..92315a870f2 --- /dev/null +++ b/src/libsyntax/ptr.rs @@ -0,0 +1,81 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::fmt; +use std::fmt::Show; +use std::hash::Hash; +use serialize::{Encodable, Decodable, Encoder, Decoder}; + +/// An owned smart pointer. +pub struct P { + ptr: Box +} + +#[allow(non_snake_case)] +/// Construct a P from a T value. +pub fn P(value: T) -> P { + P { + ptr: box value + } +} + +impl P { + pub fn and_then(self, f: |T| -> U) -> U { + f(*self.ptr) + } + + pub fn map(self, f: |T| -> T) -> P { + self.and_then(|x| P(f(x))) + } +} + +impl Deref for P { + fn deref<'a>(&'a self) -> &'a T { + &*self.ptr + } +} + +impl Clone for P { + fn clone(&self) -> P { + P((**self).clone()) + } +} + +impl PartialEq for P { + fn eq(&self, other: &P) -> bool { + **self == **other + } +} + +impl Eq for P {} + +impl Show for P { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + (**self).fmt(f) + } +} + +impl> Hash for P { + fn hash(&self, state: &mut S) { + (**self).hash(state); + } +} + +impl, T: 'static + Decodable> Decodable for P { + fn decode(d: &mut D) -> Result, E> { + Decodable::decode(d).map(P) + } +} + +impl, T: Encodable> Encodable for P { + fn encode(&self, s: &mut S) -> Result<(), E> { + (**self).encode(s) + } +}