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().
This commit is contained in:
Brian Anderson 2014-07-18 16:02:39 -07:00
parent cf8bfde9d3
commit 0b946f0a90

View file

@ -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<SiblingSpawner> {
impl<S: Spawner> TaskBuilder<S> {
/// Name the task-to-be. Currently the name is used for identification
/// only in failure messages.
#[unstable = "IntoMaybeOwned will probably change."]
pub fn named<T: IntoMaybeOwned<'static>>(mut self, name: T) -> TaskBuilder<S> {
self.name = Some(name.into_maybe_owned());
self
@ -184,12 +187,14 @@ impl<S: Spawner> TaskBuilder<S> {
}
/// Redirect task-local stdout.
#[experimental = "May not want to make stdio overridable here."]
pub fn stdout(mut self, stdout: Box<Writer + Send>) -> TaskBuilder<S> {
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<Writer + Send>) -> TaskBuilder<S> {
self.stderr = Some(stderr);
self
@ -288,6 +293,7 @@ impl<S: Spawner> TaskBuilder<S> {
/// 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<T:Send>(self, f: proc():Send -> T)
-> Future<Result<T, Box<Any + Send>>> {
// currently, the on_exit proc provided by librustrt only works for unit
@ -308,6 +314,7 @@ impl<S: Spawner> TaskBuilder<S> {
/// 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<T:Send>(self, f: proc():Send -> T) -> Result<T, Box<Any + Send>> {
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<T: Send>(f: proc(): Send -> T) -> Result<T, Box<Any + Send>> {
TaskBuilder::new().try(f)
}
@ -337,6 +345,7 @@ pub fn try<T: Send>(f: proc(): Send -> T) -> Result<T, Box<Any + Send>> {
/// task's result.
///
/// This is equivalent to `TaskBuilder::new().try_future`.
#[experimental = "Futures are experimental."]
pub fn try_future<T:Send>(f: proc():Send -> T) -> Future<Result<T, Box<Any + Send>>> {
TaskBuilder::new().try_future(f)
}
@ -345,6 +354,7 @@ pub fn try_future<T:Send>(f: proc():Send -> T) -> Future<Result<T, Box<Any + Sen
/* Lifecycle functions */
/// Read the name of the current task.
#[deprecated = "Use `task::name()`."]
pub fn with_task_name<U>(blk: |Option<&str>| -> U) -> U {
use rt::task::Task;
@ -355,7 +365,20 @@ pub fn with_task_name<U>(blk: |Option<&str>| -> U) -> U {
}
}
/// Read the name of the current task.
#[stable]
pub fn name() -> Option<String> {
use rt::task::Task;
let task = Local::borrow(None::<Task>);
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::<Task>).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]