From 4377802202d59eb5aeb36bb6b656d376bbdf67dc Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Tue, 14 Aug 2012 19:17:59 -0400 Subject: [PATCH] De-mode task.rs and add some option::foo_ref() helpers --- src/libcore/option.rs | 54 ++++++++++++++++++++++++++++++++++++++--- src/libcore/send_map.rs | 4 +-- src/libcore/task.rs | 23 ++++++++++++------ 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index d68401fadbe9..2f86e1be4957 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -46,7 +46,13 @@ pure fn map(opt: option, f: fn(T) -> U) -> option { match opt { some(x) => some(f(x)), none => none } } -pure fn map_consume(-opt: option, f: fn(-T) -> U) -> option { +pure fn map_ref(opt: &option, f: fn(x: &T) -> U) -> option { + //! 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(+opt: option, f: fn(+T) -> U) -> option { /*! * As `map`, but consumes the option and gives `f` ownership to avoid * copying. @@ -63,6 +69,16 @@ pure fn chain(opt: option, f: fn(T) -> option) -> option { match opt { some(x) => f(x), none => none } } +pure fn chain_ref(opt: &option, + f: fn(x: &T) -> option) -> option { + /*! + * 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(+x: option, blk: fn(+T) -> option) { //! Applies a function zero or more times until the result is none. @@ -97,14 +113,28 @@ pure fn map_default(opt: option, +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(opt: &option, +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(opt: option, f: fn(T)) { //! Performs an operation on the contained value or does nothing match opt { none => (), some(t) => f(t) } } +pure fn iter_ref(opt: &option, 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(-opt: option) -> T { +pure fn unwrap(+opt: option) -> T { /*! * Moves a value out of an option type and returns it. * @@ -130,12 +160,13 @@ fn swap_unwrap(opt: &mut option) -> T { unwrap(util::replace(opt, none)) } -pure fn unwrap_expect(-opt: option, reason: &str) -> T { +pure fn unwrap_expect(+opt: option, 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, some should not. See below. impl option { /** * Update an optional value by optionally running its content through a @@ -155,6 +186,23 @@ impl option { pure fn map(f: fn(T) -> U) -> option { map(self, f) } } +impl &option { + /** + * Update an optional value by optionally running its content by reference + * through a function that returns an option. + */ + pure fn chain_ref(f: fn(x: &T) -> option) -> option { + chain_ref(self, f) + } + /// Applies a function to the contained value or returns a default + pure fn map_default_ref(+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(f: fn(x: &T) -> U) -> option { map_ref(self, f) } +} + impl option { /** * Gets the value out of an option diff --git a/src/libcore/send_map.rs b/src/libcore/send_map.rs index 6ce590c5ec98..1a130e0e273c 100644 --- a/src/libcore/send_map.rs +++ b/src/libcore/send_map.rs @@ -237,11 +237,11 @@ mod linear { } impl &const linear_map { - fn len() -> uint { + pure fn len() -> uint { self.size } - fn is_empty() -> bool { + pure fn is_empty() -> bool { self.len() == 0 } diff --git a/src/libcore/task.rs b/src/libcore/task.rs index 230e9173dcb1..62398a6b8c9f 100644 --- a/src/libcore/task.rs +++ b/src/libcore/task.rs @@ -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>; type taskgroup_inner = &mut option; // 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>, - -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)