1
Fork 0

libstd: refactor future, remove with(), remove ~ indirection.

Conflicts:

	src/libstd/future.rs
This commit is contained in:
Graydon Hoare 2012-12-11 15:19:43 -08:00
parent 3ee1adb7ec
commit a55ea48d2b
5 changed files with 35 additions and 93 deletions

View file

@ -283,7 +283,7 @@ pub fn future_writer_factory(
do task::spawn |move writer_ch| { do task::spawn |move writer_ch| {
let (writer, future) = future_writer(); let (writer, future) = future_writer();
writer_ch.send(move writer); writer_ch.send(move writer);
let s = future::get(&future); let s = future.get();
comm::send(markdown_ch, (page, s)); comm::send(markdown_ch, (page, s));
} }
writer_po.recv() writer_po.recv()

View file

@ -8,11 +8,6 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// NB: transitionary, de-mode-ing.
// tjc: allowing deprecated modes due to function issue.
// can re-forbid them after snapshot
#[forbid(deprecated_pattern)];
/*! /*!
* A type representing values that may be computed concurrently and * A type representing values that may be computed concurrently and
* operations for working with them. * operations for working with them.
@ -44,27 +39,42 @@ impl<A> Future<A> : Drop {
priv enum FutureState<A> { priv enum FutureState<A> {
Pending(fn~() -> A), Pending(fn~() -> A),
Evaluating, Evaluating,
Forced(~A) Forced(A)
} }
/// Methods on the `future` type /// Methods on the `future` type
impl<A:Copy> Future<A> { impl<A:Copy> Future<A> {
fn get() -> A { fn get() -> A {
//! Get the value of the future //! Get the value of the future
*(self.get_ref())
get(&self)
} }
} }
impl<A> Future<A> { impl<A> Future<A> {
fn get_ref(&self) -> &self/A {
get_ref(self)
}
fn with<B>(blk: fn(&A) -> B) -> B { pure fn get_ref(&self) -> &self/A {
//! Work with the value without copying it /*!
* Executes the future's closure and then returns a borrowed
* pointer to the result. The borrowed pointer lasts as long as
* the future.
*/
unsafe {
match self.state {
Forced(ref mut v) => { return cast::transmute(v); }
Evaluating => fail ~"Recursive forcing of future!",
Pending(_) => {}
}
with(&self, blk) let mut state = Evaluating;
self.state <-> state;
match move state {
Forced(_) | Evaluating => fail ~"Logic error.",
Pending(move f) => {
self.state = Forced(move f());
self.get_ref()
}
}
}
} }
} }
@ -76,7 +86,7 @@ pub fn from_value<A>(val: A) -> Future<A> {
* not block. * not block.
*/ */
Future {state: Forced(~(move val))} Future {state: Forced(move val)}
} }
pub fn from_port<A:Send>(port: PortOne<A>) -> pub fn from_port<A:Send>(port: PortOne<A>) ->
@ -130,62 +140,12 @@ pub fn spawn<A:Send>(blk: fn~() -> A) -> Future<A> {
return from_port(move port); return from_port(move port);
} }
pub fn get_ref<A>(future: &r/Future<A>) -> &r/A {
/*!
* Executes the future's closure and then returns a borrowed
* pointer to the result. The borrowed pointer lasts as long as
* the future.
*/
// The unsafety here is to hide the aliases from borrowck, which
// would otherwise be concerned that someone might reassign
// `future.state` and cause the value of the future to be freed.
// But *we* know that once `future.state` is `Forced()` it will
// never become "unforced"---so we can safely return a pointer
// into the interior of the Forced() variant which will last as
// long as the future itself.
match future.state {
Forced(ref v) => { // v here has type &A, but with a shorter lifetime.
return unsafe{ copy_lifetime(future, &**v) }; // ...extend it.
}
Evaluating => {
fail ~"Recursive forcing of future!";
}
Pending(_) => {}
}
let mut state = Evaluating;
state <-> future.state;
match move state {
Forced(_) | Evaluating => {
fail ~"Logic error.";
}
Pending(move f) => {
future.state = Forced(~f());
return get_ref(future);
}
}
}
pub fn get<A:Copy>(future: &Future<A>) -> A {
//! Get the value of the future
*get_ref(future)
}
pub fn with<A,B>(future: &Future<A>, blk: fn(&A) -> B) -> B {
//! Work with the value without copying it
blk(get_ref(future))
}
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
pub mod test { pub mod test {
#[test] #[test]
pub fn test_from_value() { pub fn test_from_value() {
let f = from_value(~"snail"); let f = from_value(~"snail");
assert get(&f) == ~"snail"; assert f.get() == ~"snail";
} }
#[test] #[test]
@ -193,13 +153,13 @@ pub mod test {
let (ch, po) = oneshot::init(); let (ch, po) = oneshot::init();
send_one(move ch, ~"whale"); send_one(move ch, ~"whale");
let f = from_port(move po); let f = from_port(move po);
assert get(&f) == ~"whale"; assert f.get() == ~"whale";
} }
#[test] #[test]
pub fn test_from_fn() { pub fn test_from_fn() {
let f = from_fn(|| ~"brail"); let f = from_fn(|| ~"brail");
assert get(&f) == ~"brail"; assert f.get() == ~"brail";
} }
#[test] #[test]
@ -208,34 +168,16 @@ pub mod test {
assert f.get() == ~"fail"; assert f.get() == ~"fail";
} }
#[test]
pub fn test_with() {
let f = from_value(~"nail");
assert with(&f, |v| copy *v) == ~"nail";
}
#[test] #[test]
pub fn test_get_ref_method() { pub fn test_get_ref_method() {
let f = from_value(22); let f = from_value(22);
assert *f.get_ref() == 22; assert *f.get_ref() == 22;
} }
#[test]
pub fn test_get_ref_fn() {
let f = from_value(22);
assert *get_ref(&f) == 22;
}
#[test]
pub fn test_interface_with() {
let f = from_value(~"kale");
assert f.with(|v| copy *v) == ~"kale";
}
#[test] #[test]
pub fn test_spawn() { pub fn test_spawn() {
let f = spawn(|| ~"bale"); let f = spawn(|| ~"bale");
assert get(&f) == ~"bale"; assert f.get() == ~"bale";
} }
#[test] #[test]
@ -243,7 +185,7 @@ pub mod test {
#[ignore(cfg(target_os = "win32"))] #[ignore(cfg(target_os = "win32"))]
pub fn test_futurefail() { pub fn test_futurefail() {
let f = spawn(|| fail); let f = spawn(|| fail);
let _x: ~str = get(&f); let _x: ~str = f.get();
} }
#[test] #[test]
@ -251,7 +193,7 @@ pub mod test {
let expected = ~"schlorf"; let expected = ~"schlorf";
let f = do spawn |copy expected| { copy expected }; let f = do spawn |copy expected| { copy expected };
do task::spawn |move f, move expected| { do task::spawn |move f, move expected| {
let actual = get(&f); let actual = f.get();
assert actual == expected; assert actual == expected;
} }
} }

View file

@ -107,7 +107,7 @@ fn main() {
thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port); thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port);
// synchronize // synchronize
for futures.each |f| { future::get(f) }; for futures.each |f| { f.get() };
let stop = time::precise_time_s(); let stop = time::precise_time_s();

View file

@ -101,7 +101,7 @@ fn main() {
thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port); thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port);
// synchronize // synchronize
for futures.each |f| { future::get(f) }; for futures.each |f| { f.get() };
let stop = time::precise_time_s(); let stop = time::precise_time_s();

View file

@ -108,7 +108,7 @@ fn main() {
thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port); thread_ring(0, msg_per_task, option::unwrap(move num_chan), move num_port);
// synchronize // synchronize
for futures.each |f| { future::get(f) }; for futures.each |f| { f.get() };
let stop = time::precise_time_s(); let stop = time::precise_time_s();