Skip to content

Instantly share code, notes, and snippets.

@arielb1
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arielb1/8978329 to your computer and use it in GitHub Desktop.
Save arielb1/8978329 to your computer and use it in GitHub Desktop.
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)
}
}
}
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