From 49ac6baa726988c7a84dff3bdc8c1f8812940224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20L=C3=B6bel?= Date: Sat, 5 Oct 2013 21:01:58 +0200 Subject: [PATCH 1/2] Make a task name use a `SendStr`, allowing for either static or owned strings --- src/libstd/rt/task.rs | 4 ++-- src/libstd/task/mod.rs | 24 ++++++++++++++++++---- src/test/run-fail/fail-task-name-none.rs | 17 +++++++++++++++ src/test/run-fail/fail-task-name-owned.rs | 19 +++++++++++++++++ src/test/run-fail/fail-task-name-static.rs | 19 +++++++++++++++++ 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/test/run-fail/fail-task-name-none.rs create mode 100644 src/test/run-fail/fail-task-name-owned.rs create mode 100644 src/test/run-fail/fail-task-name-static.rs diff --git a/src/libstd/rt/task.rs b/src/libstd/rt/task.rs index 0068d1030738e..2d1b57cebf5a9 100644 --- a/src/libstd/rt/task.rs +++ b/src/libstd/rt/task.rs @@ -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, death: Death, destroyed: bool, - // FIXME(#6874/#7599) use StringRef to save on allocations - name: Option<~str>, + name: Option, coroutine: Option, sched: Option<~Scheduler>, task_type: TaskType, diff --git a/src/libstd/task/mod.rs b/src/libstd/task/mod.rs index 74fcccc70e597..315b11cac084c 100644 --- a/src/libstd/task/mod.rs +++ b/src/libstd/task/mod.rs @@ -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>, - name: Option<~str>, + name: Option, sched: SchedOpts, stack_size: Option } @@ -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(&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::<()>(); diff --git a/src/test/run-fail/fail-task-name-none.rs b/src/test/run-fail/fail-task-name-none.rs new file mode 100644 index 0000000000000..5d4de9ea43c11 --- /dev/null +++ b/src/test/run-fail/fail-task-name-none.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// error-pattern:task '' failed at 'test' + +fn main() { + do spawn { + fail2!("test"); + } +} diff --git a/src/test/run-fail/fail-task-name-owned.rs b/src/test/run-fail/fail-task-name-owned.rs new file mode 100644 index 0000000000000..f00f18cf5c2ab --- /dev/null +++ b/src/test/run-fail/fail-task-name-owned.rs @@ -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 or the MIT license +// , 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"); + } +} diff --git a/src/test/run-fail/fail-task-name-static.rs b/src/test/run-fail/fail-task-name-static.rs new file mode 100644 index 0000000000000..0b502d8a35ee9 --- /dev/null +++ b/src/test/run-fail/fail-task-name-static.rs @@ -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 or the MIT license +// , 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"); + } +} From 517298de484b2a9c88689e8313bed5fde80acd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20L=C3=B6bel?= Date: Sat, 5 Oct 2013 21:23:24 +0200 Subject: [PATCH 2/2] Implemented `IntoSendStr` on `SendStr` to allow naming a task with a `SendStr` directly --- src/libstd/send_str.rs | 5 +++++ src/libstd/task/mod.rs | 15 +++++++++++++++ src/test/run-fail/fail-task-name-send-str.rs | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/test/run-fail/fail-task-name-send-str.rs diff --git a/src/libstd/send_str.rs b/src/libstd/send_str.rs index 075c1abd25c6b..3ef99d48a0345 100644 --- a/src/libstd/send_str.rs +++ b/src/libstd/send_str.rs @@ -64,6 +64,11 @@ impl IntoSendStr for &'static str { fn into_send_str(self) -> SendStr { SendStrStatic(self) } } +impl IntoSendStr for SendStr { + #[inline] + fn into_send_str(self) -> SendStr { self } +} + /* Section: String trait impls. `SendStr` should behave like a normal string, so we don't derive. diff --git a/src/libstd/task/mod.rs b/src/libstd/task/mod.rs index 315b11cac084c..8e5353341eac8 100644 --- a/src/libstd/task/mod.rs +++ b/src/libstd/task/mod.rs @@ -974,6 +974,21 @@ fn test_static_named_task() { } } +#[test] +fn test_send_named_task() { + use rt::test::run_in_newsched_task; + + do run_in_newsched_task { + let mut t = task(); + t.name("ada lovelace".into_send_str()); + do t.spawn { + do with_task_name |name| { + assert!(name.unwrap() == "ada lovelace"); + } + } + } +} + #[test] fn test_run_basic() { let (po, ch) = stream::<()>(); diff --git a/src/test/run-fail/fail-task-name-send-str.rs b/src/test/run-fail/fail-task-name-send-str.rs new file mode 100644 index 0000000000000..e44f78094ff48 --- /dev/null +++ b/src/test/run-fail/fail-task-name-send-str.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// error-pattern:task 'send name' failed at 'test' + +fn main() { + let mut t = ::std::task::task(); + t.name("send name".to_send_str()); + do t.spawn { + fail2!("test"); + } +}