diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2014-05-18 00:46:40 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2014-09-14 03:39:35 +0300 |
| commit | 1872c4c6b57a93c689c54e00d29d324ca1a10e95 (patch) | |
| tree | 804b459e32ee25a0adfe5c0279bc28b9cc18979a /src/libsyntax | |
| parent | 79a5448f41dcc6ab52663105a6b02fc5af4c503e (diff) | |
| download | rust-1872c4c6b57a93c689c54e00d29d324ca1a10e95.tar.gz rust-1872c4c6b57a93c689c54e00d29d324ca1a10e95.zip | |
syntax: add a custom owned smart pointer in ptr::P.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/ptr.rs | 81 |
3 files changed, 82 insertions, 10 deletions
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<T> in the future. -pub type P<T> = Gc<T>; - -#[allow(non_snake_case)] -/// Construct a P<T> from a T value. -pub fn P<T: 'static>(value: T) -> P<T> { - 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 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<T> { + ptr: Box<T> +} + +#[allow(non_snake_case)] +/// Construct a P<T> from a T value. +pub fn P<T: 'static>(value: T) -> P<T> { + P { + ptr: box value + } +} + +impl<T: 'static> P<T> { + pub fn and_then<U>(self, f: |T| -> U) -> U { + f(*self.ptr) + } + + pub fn map(self, f: |T| -> T) -> P<T> { + self.and_then(|x| P(f(x))) + } +} + +impl<T> Deref<T> for P<T> { + fn deref<'a>(&'a self) -> &'a T { + &*self.ptr + } +} + +impl<T: 'static + Clone> Clone for P<T> { + fn clone(&self) -> P<T> { + P((**self).clone()) + } +} + +impl<T: PartialEq> PartialEq for P<T> { + fn eq(&self, other: &P<T>) -> bool { + **self == **other + } +} + +impl<T: Eq> Eq for P<T> {} + +impl<T: Show> Show for P<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + (**self).fmt(f) + } +} + +impl<S, T: Hash<S>> Hash<S> for P<T> { + fn hash(&self, state: &mut S) { + (**self).hash(state); + } +} + +impl<E, D: Decoder<E>, T: 'static + Decodable<D, E>> Decodable<D, E> for P<T> { + fn decode(d: &mut D) -> Result<P<T>, E> { + Decodable::decode(d).map(P) + } +} + +impl<E, S: Encoder<E>, T: Encodable<S, E>> Encodable<S, E> for P<T> { + fn encode(&self, s: &mut S) -> Result<(), E> { + (**self).encode(s) + } +} |
