ptr.rs 3.26 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
//use std::fmt::{self, Display, Debug};
//use std::hash::{Hash, Hasher};
//use std::ops::Deref;
42

43 44
use std::sync::Arc;

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

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

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