Created
May 19, 2013 05:08
-
-
Save mstewartgallus/5606751 to your computer and use it in GitHub Desktop.
A prototype for a nonmoveable type.
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
/// A data type for making a type which is not moveable by | |
/// default. This is useful for marking large types which are | |
/// expensive to copy around. | |
pub struct NonMoveable<'self> { | |
/// Never actually used but only for guaranteeing certain | |
/// properties by default | |
priv contents: &'self() | |
} | |
pub fn NonMoveable<T>(f : &fn(NonMoveable) -> T) -> T { | |
f(NonMoveable { contents: &() }) | |
} | |
pub impl<'self> NonMoveable<'self> { | |
/// Not actually that useful but it demonstrates how to manipulate | |
/// these kind of types. Also not exactly correct. It should | |
/// return a polymorphic function, and not be a polymorphic | |
/// function itself but that's impossible. | |
fn move<T>(&self) -> ~fn(&fn(NonMoveable) -> T) -> T { | |
|f| { | |
f(NonMoveable { contents: &() }) | |
} | |
} | |
} | |
priv struct Large <'self> { | |
contents: [uint, ..256], | |
nonmoveable: NonMoveable <'self> | |
} | |
priv fn Large<T>(f : &fn(Large) -> T) -> T { | |
do NonMoveable |n| { | |
f (Large { contents: [0, ..256], nonmoveable: n }) | |
} | |
} | |
pub impl<'self> Large<'self> { | |
fn move<T>(&self) -> ~fn(&fn(Large) -> T) -> T { | |
let mover = self.nonmoveable.move (); | |
let contents = self.contents; | |
|f| { | |
do mover |n| { | |
f(Large { contents: contents, nonmoveable: n }) | |
} | |
} | |
} | |
} | |
#[test] | |
priv fn test_non_moveable() { | |
do do Large |my_large_structure| { | |
my_large_structure.move() | |
} |_| { | |
// Don't actually need to use this large structure for | |
// anything | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment