Skip to content

Commit 1872c4c

Browse files
committed
syntax: add a custom owned smart pointer in ptr::P.
1 parent 79a5448 commit 1872c4c

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

src/libsyntax/ast.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ use std::rc::Rc;
2525
use std::gc::{Gc, GC};
2626
use serialize::{Encodable, Decodable, Encoder, Decoder};
2727

28-
/// A pointer abstraction.
29-
// FIXME(eddyb) #10676 use Rc<T> in the future.
30-
pub type P<T> = Gc<T>;
31-
32-
#[allow(non_snake_case)]
33-
/// Construct a P<T> from a T value.
34-
pub fn P<T: 'static>(value: T) -> P<T> {
35-
box(GC) value
36-
}
37-
3828
// FIXME #6993: in librustc, uses of "ident" should be replaced
3929
// by just "Name".
4030

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub mod diagnostic;
6363
pub mod fold;
6464
pub mod owned_slice;
6565
pub mod parse;
66+
pub mod ptr;
6667
pub mod visit;
6768

6869
pub mod print {

src/libsyntax/ptr.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt;
12+
use std::fmt::Show;
13+
use std::hash::Hash;
14+
use serialize::{Encodable, Decodable, Encoder, Decoder};
15+
16+
/// An owned smart pointer.
17+
pub struct P<T> {
18+
ptr: Box<T>
19+
}
20+
21+
#[allow(non_snake_case)]
22+
/// Construct a P<T> from a T value.
23+
pub fn P<T: 'static>(value: T) -> P<T> {
24+
P {
25+
ptr: box value
26+
}
27+
}
28+
29+
impl<T: 'static> P<T> {
30+
pub fn and_then<U>(self, f: |T| -> U) -> U {
31+
f(*self.ptr)
32+
}
33+
34+
pub fn map(self, f: |T| -> T) -> P<T> {
35+
self.and_then(|x| P(f(x)))
36+
}
37+
}
38+
39+
impl<T> Deref<T> for P<T> {
40+
fn deref<'a>(&'a self) -> &'a T {
41+
&*self.ptr
42+
}
43+
}
44+
45+
impl<T: 'static + Clone> Clone for P<T> {
46+
fn clone(&self) -> P<T> {
47+
P((**self).clone())
48+
}
49+
}
50+
51+
impl<T: PartialEq> PartialEq for P<T> {
52+
fn eq(&self, other: &P<T>) -> bool {
53+
**self == **other
54+
}
55+
}
56+
57+
impl<T: Eq> Eq for P<T> {}
58+
59+
impl<T: Show> Show for P<T> {
60+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61+
(**self).fmt(f)
62+
}
63+
}
64+
65+
impl<S, T: Hash<S>> Hash<S> for P<T> {
66+
fn hash(&self, state: &mut S) {
67+
(**self).hash(state);
68+
}
69+
}
70+
71+
impl<E, D: Decoder<E>, T: 'static + Decodable<D, E>> Decodable<D, E> for P<T> {
72+
fn decode(d: &mut D) -> Result<P<T>, E> {
73+
Decodable::decode(d).map(P)
74+
}
75+
}
76+
77+
impl<E, S: Encoder<E>, T: Encodable<S, E>> Encodable<S, E> for P<T> {
78+
fn encode(&self, s: &mut S) -> Result<(), E> {
79+
(**self).encode(s)
80+
}
81+
}

0 commit comments

Comments
 (0)