De-mode task.rs and add some option::foo_ref() helpers

This commit is contained in:
Ben Blum 2012-08-14 19:17:59 -04:00
parent 821fa337ff
commit 4377802202
3 changed files with 68 additions and 13 deletions

View file

@ -46,7 +46,13 @@ pure fn map<T, U>(opt: option<T>, f: fn(T) -> U) -> option<U> {
match opt { some(x) => some(f(x)), none => none }
}
pure fn map_consume<T, U>(-opt: option<T>, f: fn(-T) -> U) -> option<U> {
pure fn map_ref<T, U>(opt: &option<T>, f: fn(x: &T) -> U) -> option<U> {
//! Maps a `some` value by reference from one type to another
match *opt { some(ref x) => some(f(x)), none => none }
}
pure fn map_consume<T, U>(+opt: option<T>, f: fn(+T) -> U) -> option<U> {
/*!
* As `map`, but consumes the option and gives `f` ownership to avoid
* copying.
@ -63,6 +69,16 @@ pure fn chain<T, U>(opt: option<T>, f: fn(T) -> option<U>) -> option<U> {
match opt { some(x) => f(x), none => none }
}
pure fn chain_ref<T, U>(opt: &option<T>,
f: fn(x: &T) -> option<U>) -> option<U> {
/*!
* Update an optional value by optionally running its content by reference
* through a function that returns an option.
*/
match *opt { some(ref x) => f(x), none => none }
}
#[inline(always)]
pure fn while_some<T>(+x: option<T>, blk: fn(+T) -> option<T>) {
//! Applies a function zero or more times until the result is none.
@ -97,14 +113,28 @@ pure fn map_default<T, U>(opt: option<T>, +def: U, f: fn(T) -> U) -> U {
match opt { none => def, some(t) => f(t) }
}
// This should replace map_default.
pure fn map_default_ref<T, U>(opt: &option<T>, +def: U,
f: fn(x: &T) -> U) -> U {
//! Applies a function to the contained value or returns a default
match *opt { none => def, some(ref t) => f(t) }
}
// This should change to by-copy mode; use iter_ref below for by reference
pure fn iter<T>(opt: option<T>, f: fn(T)) {
//! Performs an operation on the contained value or does nothing
match opt { none => (), some(t) => f(t) }
}
pure fn iter_ref<T>(opt: &option<T>, f: fn(x: &T)) {
//! Performs an operation on the contained value by reference
match *opt { none => (), some(ref t) => f(t) }
}
#[inline(always)]
pure fn unwrap<T>(-opt: option<T>) -> T {
pure fn unwrap<T>(+opt: option<T>) -> T {
/*!
* Moves a value out of an option type and returns it.
*
@ -130,12 +160,13 @@ fn swap_unwrap<T>(opt: &mut option<T>) -> T {
unwrap(util::replace(opt, none))
}
pure fn unwrap_expect<T>(-opt: option<T>, reason: &str) -> T {
pure fn unwrap_expect<T>(+opt: option<T>, reason: &str) -> T {
//! As unwrap, but with a specified failure message.
if opt.is_none() { fail reason.to_unique(); }
unwrap(opt)
}
// Some of these should change to be &option<T>, some should not. See below.
impl<T> option<T> {
/**
* Update an optional value by optionally running its content through a
@ -155,6 +186,23 @@ impl<T> option<T> {
pure fn map<U>(f: fn(T) -> U) -> option<U> { map(self, f) }
}
impl<T> &option<T> {
/**
* Update an optional value by optionally running its content by reference
* through a function that returns an option.
*/
pure fn chain_ref<U>(f: fn(x: &T) -> option<U>) -> option<U> {
chain_ref(self, f)
}
/// Applies a function to the contained value or returns a default
pure fn map_default_ref<U>(+def: U, f: fn(x: &T) -> U) -> U
{ map_default_ref(self, def, f) }
/// Performs an operation on the contained value by reference
pure fn iter_ref(f: fn(x: &T)) { iter_ref(self, f) }
/// Maps a `some` value from one type to another by reference
pure fn map_ref<U>(f: fn(x: &T) -> U) -> option<U> { map_ref(self, f) }
}
impl<T: copy> option<T> {
/**
* Gets the value out of an option

View file

@ -237,11 +237,11 @@ mod linear {
}
impl<K,V> &const linear_map<K,V> {
fn len() -> uint {
pure fn len() -> uint {
self.size
}
fn is_empty() -> bool {
pure fn is_empty() -> bool {
self.len() == 0
}

View file

@ -1,3 +1,7 @@
// NB: transitionary, de-mode-ing.
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
/*!
* Task management.
*
@ -730,8 +734,8 @@ type taskgroup_arc = unsafe::Exclusive<option<taskgroup_data>>;
type taskgroup_inner = &mut option<taskgroup_data>;
// A taskgroup is 'dead' when nothing can cause it to fail; only members can.
fn taskgroup_is_dead(tg: taskgroup_data) -> bool {
(&mut tg.members).is_empty()
pure fn taskgroup_is_dead(tg: &taskgroup_data) -> bool {
(&tg.members).is_empty()
}
// A list-like structure by which taskgroups keep track of all ancestor groups
@ -841,8 +845,11 @@ fn each_ancestor(list: &mut ancestor_list,
do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
// Decide whether this group is dead. Note that the
// group being *dead* is disjoint from it *failing*.
do tg_opt.iter |tg| {
nobe_is_dead = taskgroup_is_dead(tg);
match *tg_opt {
some(ref tg) => {
nobe_is_dead = taskgroup_is_dead(tg);
},
none => { }
}
// Call iterator block. (If the group is dead, it's
// safe to skip it. This will leave our *rust_task
@ -1100,7 +1107,7 @@ fn gen_child_taskgroup(linked: bool, supervised: bool)
}
}
fn spawn_raw(opts: task_opts, +f: fn~()) {
fn spawn_raw(+opts: task_opts, +f: fn~()) {
let (child_tg, ancestors, is_main) =
gen_child_taskgroup(opts.linked, opts.supervised);
@ -1138,10 +1145,10 @@ fn spawn_raw(opts: task_opts, +f: fn~()) {
// (3a) If any of those fails, it leaves all groups, and does nothing.
// (3b) Otherwise it builds a task control structure and puts it in TLS,
// (4) ...and runs the provided body function.
fn make_child_wrapper(child: *rust_task, -child_arc: taskgroup_arc,
-ancestors: ancestor_list, is_main: bool,
fn make_child_wrapper(child: *rust_task, +child_arc: taskgroup_arc,
+ancestors: ancestor_list, is_main: bool,
notify_chan: option<comm::chan<notification>>,
-f: fn~()) -> fn~() {
+f: fn~()) -> fn~() {
let child_data = ~mut some((child_arc, ancestors));
return fn~() {
// Agh. Get move-mode items into the closure. FIXME (#2829)