Skip to content

Commit 0c388be

Browse files
committed
auto merge of #9731 : Kimundi/rust/SendStrTaskName, r=alexcrichton
This resolves a FIXME in `std::rt::task`.
2 parents 1506dac + 517298d commit 0c388be

File tree

7 files changed

+116
-6
lines changed

7 files changed

+116
-6
lines changed

src/libstd/rt/task.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rt::context::Context;
3333
use unstable::finally::Finally;
3434
use task::spawn::Taskgroup;
3535
use cell::Cell;
36+
use send_str::SendStr;
3637

3738
// The Task struct represents all state associated with a rust
3839
// task. There are at this point two primary "subtypes" of task,
@@ -49,8 +50,7 @@ pub struct Task {
4950
taskgroup: Option<Taskgroup>,
5051
death: Death,
5152
destroyed: bool,
52-
// FIXME(#6874/#7599) use StringRef to save on allocations
53-
name: Option<~str>,
53+
name: Option<SendStr>,
5454
coroutine: Option<Coroutine>,
5555
sched: Option<~Scheduler>,
5656
task_type: TaskType,

src/libstd/send_str.rs

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ impl IntoSendStr for &'static str {
6464
fn into_send_str(self) -> SendStr { SendStrStatic(self) }
6565
}
6666

67+
impl IntoSendStr for SendStr {
68+
#[inline]
69+
fn into_send_str(self) -> SendStr { self }
70+
}
71+
6772
/*
6873
Section: String trait impls.
6974
`SendStr` should behave like a normal string, so we don't derive.

src/libstd/task/mod.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use rt::in_green_task_context;
6363
use rt::local::Local;
6464
use unstable::finally::Finally;
6565
use util;
66+
use send_str::{SendStr, IntoSendStr};
6667

6768
#[cfg(test)] use cast;
6869
#[cfg(test)] use comm::SharedChan;
@@ -148,7 +149,7 @@ pub struct TaskOpts {
148149
watched: bool,
149150
indestructible: bool,
150151
notify_chan: Option<Chan<TaskResult>>,
151-
name: Option<~str>,
152+
name: Option<SendStr>,
152153
sched: SchedOpts,
153154
stack_size: Option<uint>
154155
}
@@ -295,8 +296,8 @@ impl TaskBuilder {
295296

296297
/// Name the task-to-be. Currently the name is used for identification
297298
/// only in failure messages.
298-
pub fn name(&mut self, name: ~str) {
299-
self.opts.name = Some(name);
299+
pub fn name<S: IntoSendStr>(&mut self, name: S) {
300+
self.opts.name = Some(name.into_send_str());
300301
}
301302

302303
/// Configure a custom scheduler mode for the task.
@@ -944,7 +945,7 @@ fn test_unnamed_task() {
944945
}
945946

946947
#[test]
947-
fn test_named_task() {
948+
fn test_owned_named_task() {
948949
use rt::test::run_in_newsched_task;
949950

950951
do run_in_newsched_task {
@@ -958,6 +959,36 @@ fn test_named_task() {
958959
}
959960
}
960961

962+
#[test]
963+
fn test_static_named_task() {
964+
use rt::test::run_in_newsched_task;
965+
966+
do run_in_newsched_task {
967+
let mut t = task();
968+
t.name("ada lovelace");
969+
do t.spawn {
970+
do with_task_name |name| {
971+
assert!(name.unwrap() == "ada lovelace");
972+
}
973+
}
974+
}
975+
}
976+
977+
#[test]
978+
fn test_send_named_task() {
979+
use rt::test::run_in_newsched_task;
980+
981+
do run_in_newsched_task {
982+
let mut t = task();
983+
t.name("ada lovelace".into_send_str());
984+
do t.spawn {
985+
do with_task_name |name| {
986+
assert!(name.unwrap() == "ada lovelace");
987+
}
988+
}
989+
}
990+
}
991+
961992
#[test]
962993
fn test_run_basic() {
963994
let (po, ch) = stream::<()>();
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:task '<unnamed>' failed at 'test'
12+
13+
fn main() {
14+
do spawn {
15+
fail2!("test");
16+
}
17+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:task 'owned name' failed at 'test'
12+
13+
fn main() {
14+
let mut t = ::std::task::task();
15+
t.name(~"owned name");
16+
do t.spawn {
17+
fail2!("test");
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:task 'send name' failed at 'test'
12+
13+
fn main() {
14+
let mut t = ::std::task::task();
15+
t.name("send name".to_send_str());
16+
do t.spawn {
17+
fail2!("test");
18+
}
19+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:task 'static name' failed at 'test'
12+
13+
fn main() {
14+
let mut t = ::std::task::task();
15+
t.name("static name");
16+
do t.spawn {
17+
fail2!("test");
18+
}
19+
}

0 commit comments

Comments
 (0)