If you want that, you'll need to use a custom pointer type instead of ~. You 
can use ~ internally, and implement Deref/DerefMut to do the desired capturing.

pub struct Ptr<T> {
    value: ~T // assuming 0.10 here, master would be Box<T>
}

impl<T> Ptr<T> {
    pub fn new(val: T) -> Ptr<T> {
        Ptr { value: ~val }
    }
}

impl<T> Deref<T> for Ptr<T> {
    fn deref<'a>(&'a self) -> &'a T {
        // do any capturing of the dereference here
        &*self.value
    }
}

impl<T> DerefMut<T> for Ptr<T> {
    fn deref_mut<'a>(&'a mut self) -> &'a mut T {
        // do any capturing of the mutable dereference here
        &mut *self.value
    }
}

fn main() {
    let x = Ptr::new(3i);
    println!("x: {}", *x);
}

-Kevin

On May 13, 2014, at 9:16 PM, Noah Watkins <jayh...@cs.ucsc.edu> wrote:

> On Tue, May 13, 2014 at 2:19 PM, Alex Crichton <a...@crichton.co> wrote:
>> The ~int type has since moved to Box<int>, which will one day be a
>> library type with Deref implemented on it (currently it is implemented
>> by the compiler).
> 
> Thanks for the note. I'm using a slightly older version that doesn't
> have Box<T>, but it sounds like ~ is also implemented with the
> compiler. I was actually looking into this because I was interested in
> intercepting the deref (as well as allocate/free) for some distributed
> shared-memory experiments. Some of the safety checks rust performs
> simplifies the coherency requirements needed of the storage layer
> (e.g. expensive locking).
> 
> -Noah
> 
>> 
>> On Mon, May 12, 2014 at 11:50 AM, Noah Watkins <jayh...@cs.ucsc.edu> wrote:
>>> I am trying to capture the reference to type `~int` with the following
>>> code. I can change it to apply to bare `int` and it works fine.
>>> 
>>> #[lang="deref"]
>>> pub trait Deref<Result> {
>>>    fn deref<'a>(&'a self) -> &'a Result;
>>> }
>>> 
>>> impl Deref<~int> for ~int {
>>>    fn deref<'a>(&'a self) -> &'a ~int {
>>>        println!("deref caught");
>>>        self
>>>    }
>>> }
>>> 
>>> fn main() {
>>>  let x: ~int = 3;
>>>  *x
>>> }
>>> 
>>> Thanks,
>>> Noah
>>> _______________________________________________
>>> Rust-dev mailing list
>>> Rust-dev@mozilla.org
>>> https://mail.mozilla.org/listinfo/rust-dev
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to