Last active
August 29, 2015 13:56
-
-
Save arielb1/8978329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::RefCell; | |
struct Context { | |
data: RefCell<i32>; | |
} | |
struct A<'a> { | |
context: &'a Context, | |
} | |
struct BiggerContext<'a> { | |
priv context: &'a Context, | |
priv a: A<'a>, | |
} | |
impl Drop for BiggerContext { | |
fn drop(self) { | |
unsafe { | |
let ctx: ~Context = cast::transmute(self.context) | |
} | |
} | |
} | |
fn create_bigger_context<'a>() -> ~BiggerContext<'a> { | |
unsafe { | |
let ctx : &'a Context = cast::transmute(~Context { RefCell::new(0) }); | |
BiggerContext { ctx, A { ctx }} | |
} | |
} | |
impl BiggerContext<'a> { | |
fn context(ctx: &'a self) -> &'a Context<'a> { | |
ctx.context | |
} | |
fn A(ctx: &'a self) -> &'a A<'a> { | |
unsafe { | |
cast::transmute(&ctx.a) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::RefCell; | |
struct Context { | |
data: RefCell<i32>; | |
} | |
struct A<'a> { | |
context: &'a Context, | |
} | |
struct BiggerContext<'a> { | |
context: &'a Context, | |
a: A<'a>, | |
} | |
macro_rules! define_bigger_context( | |
($id: ident) => ( | |
let ctx = ~Context { RefCell::new(0) }; | |
let $id = BiggerContext { ctx, A { ctx }} | |
); | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment