Make a task name use a SendStr, allowing for either

static or owned strings
This commit is contained in:
Marvin Löbel 2013-10-05 21:01:58 +02:00
parent 1506dac10f
commit 49ac6baa72
5 changed files with 77 additions and 6 deletions

@ -33,6 +33,7 @@ use rt::context::Context;
use unstable::finally::Finally;
use task::spawn::Taskgroup;
use cell::Cell;
use send_str::SendStr;
// The Task struct represents all state associated with a rust
// task. There are at this point two primary "subtypes" of task,
@ -49,8 +50,7 @@ pub struct Task {
taskgroup: Option<Taskgroup>,
death: Death,
destroyed: bool,
// FIXME(#6874/#7599) use StringRef to save on allocations
name: Option<~str>,
name: Option<SendStr>,
coroutine: Option<Coroutine>,
sched: Option<~Scheduler>,
task_type: TaskType,

@ -63,6 +63,7 @@ use rt::in_green_task_context;
use rt::local::Local;
use unstable::finally::Finally;
use util;
use send_str::{SendStr, IntoSendStr};
#[cfg(test)] use cast;
#[cfg(test)] use comm::SharedChan;
@ -148,7 +149,7 @@ pub struct TaskOpts {
watched: bool,
indestructible: bool,
notify_chan: Option<Chan<TaskResult>>,
name: Option<~str>,
name: Option<SendStr>,
sched: SchedOpts,
stack_size: Option<uint>
}
@ -295,8 +296,8 @@ impl TaskBuilder {
/// Name the task-to-be. Currently the name is used for identification
/// only in failure messages.
pub fn name(&mut self, name: ~str) {
self.opts.name = Some(name);
pub fn name<S: IntoSendStr>(&mut self, name: S) {
self.opts.name = Some(name.into_send_str());
}
/// Configure a custom scheduler mode for the task.
@ -944,7 +945,7 @@ fn test_unnamed_task() {
}
#[test]
fn test_named_task() {
fn test_owned_named_task() {
use rt::test::run_in_newsched_task;
do run_in_newsched_task {
@ -958,6 +959,21 @@ fn test_named_task() {
}
}
#[test]
fn test_static_named_task() {
use rt::test::run_in_newsched_task;
do run_in_newsched_task {
let mut t = task();
t.name("ada lovelace");
do t.spawn {
do with_task_name |name| {
assert!(name.unwrap() == "ada lovelace");
}
}
}
}
#[test]
fn test_run_basic() {
let (po, ch) = stream::<()>();

@ -0,0 +1,17 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// error-pattern:task '<unnamed>' failed at 'test'
fn main() {
do spawn {
fail2!("test");
}
}

@ -0,0 +1,19 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// error-pattern:task 'owned name' failed at 'test'
fn main() {
let mut t = ::std::task::task();
t.name(~"owned name");
do t.spawn {
fail2!("test");
}
}

@ -0,0 +1,19 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// error-pattern:task 'static name' failed at 'test'
fn main() {
let mut t = ::std::task::task();
t.name("static name");
do t.spawn {
fail2!("test");
}
}