From 0b946f0a9099c8a116d2f51473a48d0e16b43037 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 18 Jul 2014 16:02:39 -0700 Subject: [PATCH] std: Stabilize task module. Most stable. deschedule/failing experimental because of concerns about naming and desirability. Adds task::name() to replace deprecated task::with_name(). --- src/libstd/task.rs | 59 +++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/libstd/task.rs b/src/libstd/task.rs index d7af92024eb5e..4b8c15a0152a3 100644 --- a/src/libstd/task.rs +++ b/src/libstd/task.rs @@ -91,7 +91,7 @@ //! # } //! ``` -#![experimental] +#![stable] use any::Any; use comm::channel; @@ -104,7 +104,9 @@ use rt::local::Local; use rt::task; use rt::task::Task; use str::{Str, SendStr, IntoMaybeOwned}; +use string::String; use sync::Future; +use to_str::ToString; /// A means of spawning a task pub trait Spawner { @@ -172,6 +174,7 @@ impl TaskBuilder { impl TaskBuilder { /// Name the task-to-be. Currently the name is used for identification /// only in failure messages. + #[unstable = "IntoMaybeOwned will probably change."] pub fn named>(mut self, name: T) -> TaskBuilder { self.name = Some(name.into_maybe_owned()); self @@ -184,12 +187,14 @@ impl TaskBuilder { } /// Redirect task-local stdout. + #[experimental = "May not want to make stdio overridable here."] pub fn stdout(mut self, stdout: Box) -> TaskBuilder { self.stdout = Some(stdout); self } /// Redirect task-local stderr. + #[experimental = "May not want to make stdio overridable here."] pub fn stderr(mut self, stderr: Box) -> TaskBuilder { self.stderr = Some(stderr); self @@ -288,6 +293,7 @@ impl TaskBuilder { /// future returns `result::Ok` containing the value returned by the /// function. If the child task fails then the future returns `result::Err` /// containing the argument to `fail!(...)` as an `Any` trait object. + #[experimental = "Futures are experimental."] pub fn try_future(self, f: proc():Send -> T) -> Future>> { // currently, the on_exit proc provided by librustrt only works for unit @@ -308,6 +314,7 @@ impl TaskBuilder { /// Execute a function in a newly-spawnedtask and block until the task /// completes or fails. Equivalent to `.try_future(f).unwrap()`. + #[unstable = "Error type may change."] pub fn try(self, f: proc():Send -> T) -> Result> { self.try_future(f).unwrap() } @@ -329,6 +336,7 @@ pub fn spawn(f: proc(): Send) { /// value of the function or an error if the task failed. /// /// This is equivalent to `TaskBuilder::new().try`. +#[unstable = "Error type may change."] pub fn try(f: proc(): Send -> T) -> Result> { TaskBuilder::new().try(f) } @@ -337,6 +345,7 @@ pub fn try(f: proc(): Send -> T) -> Result> { /// task's result. /// /// This is equivalent to `TaskBuilder::new().try_future`. +#[experimental = "Futures are experimental."] pub fn try_future(f: proc():Send -> T) -> Future>> { TaskBuilder::new().try_future(f) } @@ -345,6 +354,7 @@ pub fn try_future(f: proc():Send -> T) -> Future(blk: |Option<&str>| -> U) -> U { use rt::task::Task; @@ -355,7 +365,20 @@ pub fn with_task_name(blk: |Option<&str>| -> U) -> U { } } +/// Read the name of the current task. +#[stable] +pub fn name() -> Option { + use rt::task::Task; + + let task = Local::borrow(None::); + match task.name { + Some(ref name) => Some(name.as_slice().to_string()), + None => None + } +} + /// Yield control to the task scheduler. +#[unstable = "Name will change."] pub fn deschedule() { use rt::local::Local; @@ -366,6 +389,7 @@ pub fn deschedule() { /// True if the running task is currently failing (e.g. will return `true` inside a /// destructor that is run while unwinding the stack after a call to `fail!()`). +#[unstable = "May move to a different module."] pub fn failing() -> bool { use rt::task::Task; Local::borrow(None::).unwinder.unwinding() @@ -377,7 +401,6 @@ mod test { use boxed::BoxAny; use result; use result::{Ok, Err}; - use str::StrAllocating; use string::String; use std::io::{ChanReader, ChanWriter}; use prelude::*; @@ -388,38 +411,30 @@ mod test { #[test] fn test_unnamed_task() { - spawn(proc() { - with_task_name(|name| { - assert!(name.is_none()); - }) - }) + try(proc() { + assert!(name().is_none()); + }).map_err(|_| ()).unwrap(); } #[test] fn test_owned_named_task() { - TaskBuilder::new().named("ada lovelace".to_string()).spawn(proc() { - with_task_name(|name| { - assert!(name.unwrap() == "ada lovelace"); - }) - }) + TaskBuilder::new().named("ada lovelace".to_string()).try(proc() { + assert!(name().unwrap() == "ada lovelace".to_string()); + }).map_err(|_| ()).unwrap(); } #[test] fn test_static_named_task() { - TaskBuilder::new().named("ada lovelace").spawn(proc() { - with_task_name(|name| { - assert!(name.unwrap() == "ada lovelace"); - }) - }) + TaskBuilder::new().named("ada lovelace").try(proc() { + assert!(name().unwrap() == "ada lovelace".to_string()); + }).map_err(|_| ()).unwrap(); } #[test] fn test_send_named_task() { - TaskBuilder::new().named("ada lovelace".into_maybe_owned()).spawn(proc() { - with_task_name(|name| { - assert!(name.unwrap() == "ada lovelace"); - }) - }) + TaskBuilder::new().named("ada lovelace".into_maybe_owned()).try(proc() { + assert!(name().unwrap() == "ada lovelace".to_string()); + }).map_err(|_| ()).unwrap(); } #[test]