ptr.rs 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// 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.

//! The AST pointer
//!
//! Provides `P<T>`, a frozen owned smart pointer, as a replacement for `@T` in
//! the AST.
//!
//! # 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
//!   `ExprAddrOf` 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>`.
//!
//! * **Immutability**: `P<T>` disallows mutating its inner `T`, unlike `Box<T>`
//!   (unless it contains an `Unsafe` interior, but that may be denied later).
//!   This mainly prevents mistakes, but can also enforces a kind of "purity".
//!
//! * **Efficiency**: folding can reuse allocation space for `P<T>` and `Vec<T>`,
//!   the latter even when the input and output types differ (as it would be the
//!   case with arenas or a GADT AST using type parameters to toggle features).
//!
//! * **Maintainability**: `P<T>` provides a fixed interface - `Deref`,
//!   `and_then` and `map` - which can remain fully functional even if the
//!   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.

qinsoon's avatar
qinsoon committed
39 40 41 42
//use std::fmt::{self, Display, Debug};
//use std::hash::{Hash, Hasher};
//use std::ops::Deref;
//use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
43

44 45
use std::sync::Arc;

qinsoon's avatar
qinsoon committed
46 47 48
pub type P<T> = Arc<T>;
//pub struct P<T: MuEntity> {
//    ptr: Arc<T>
49
//}
50 51 52

#[allow(non_snake_case)]
/// Construct a `P<T>` from a `T` value.
qinsoon's avatar
qinsoon committed
53
pub fn P<T>(value: T) -> P<T> {
qinsoon's avatar
qinsoon committed
54
//    P {ptr: Arc::new(value)}
55
    Arc::new(value)
56 57
}

qinsoon's avatar
qinsoon committed
58
//impl<T: MuEntity> Deref for P<T> {
59 60 61 62 63 64 65
//    type Target = T;
//
//    fn deref<'a>(&'a self) -> &'a T {
//        &*self.ptr
//    }
//}
//
qinsoon's avatar
qinsoon committed
66
//impl<T: MuEntity> Clone for P<T> {
67
//    fn clone(&self) -> P<T> {
qinsoon's avatar
qinsoon committed
68
//        P {ptr: self.ptr.clone()}
69 70 71
//    }
//}
//
qinsoon's avatar
qinsoon committed
72
//impl<T: MuEntity + PartialEq> PartialEq for P<T> {
73 74 75 76 77
//    fn eq(&self, other: &P<T>) -> bool {
//        **self == **other
//    }
//}
//
qinsoon's avatar
qinsoon committed
78
//impl<T: MuEntity + Eq> Eq for P<T> {}
79
//
qinsoon's avatar
qinsoon committed
80
//impl<T: MuEntity + Debug> Debug for P<T> {
81 82 83 84
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        Debug::fmt(&**self, f)
//    }
//}
qinsoon's avatar
qinsoon committed
85
//impl<T: MuEntity + Display> Display for P<T> {
86 87 88 89 90
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        Display::fmt(&**self, f)
//    }
//}
//
qinsoon's avatar
qinsoon committed
91
//impl<T: MuEntity> fmt::Pointer for P<T> {
92 93 94 95 96
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        fmt::Pointer::fmt(&self.ptr, f)
//    }
//}
//
qinsoon's avatar
qinsoon committed
97
//impl<T: MuEntity + Hash> Hash for P<T> {
98 99 100 101
//    fn hash<H: Hasher>(&self, state: &mut H) {
//        (**self).hash(state);
//    }
//}
qinsoon's avatar
qinsoon committed
102 103 104 105 106

//impl<T: MuEntity> Encodable for P<T> {
//    fn encode<S: Encoder> (&self, s: &mut S) -> Result<(), S::Error> {
//        s.emit_usize(self.id())
//    }
107
//}