Re: [rust-dev] how to capture de-reference to ~int

2014-05-16 Thread Kevin Ballard
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 PtrT {
value: ~T // assuming 0.10 here, master would be BoxT
}

implT PtrT {
pub fn new(val: T) - PtrT {
Ptr { value: ~val }
}
}

implT DerefT for PtrT {
fn deref'a('a self) - 'a T {
// do any capturing of the dereference here
*self.value
}
}

implT DerefMutT for PtrT {
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 Boxint, 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 BoxT, 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 DerefResult {
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


Re: [rust-dev] how to capture de-reference to ~int

2014-05-13 Thread Alex Crichton
The ~int type has since moved to Boxint, which will one day be a
library type with Deref implemented on it (currently it is implemented
by the compiler). Regardless, there's no need to implement the Deref
trait for the type today, the compiler already takes care of it.

For example, your code will compile without the deref trait:

fn main() {
let x: Boxint = box 3;
let y = *x;
println!({}, y);
}

The language only allows for one definition of each lang item, and
most lang items now reside in libcore, so you shouldn't have to define
them manually.

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 DerefResult {
 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


Re: [rust-dev] how to capture de-reference to ~int

2014-05-13 Thread Noah Watkins
On Tue, May 13, 2014 at 2:19 PM, Alex Crichton a...@crichton.co wrote:
 The ~int type has since moved to Boxint, 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 BoxT, 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 DerefResult {
 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