91
91
//! # }
92
92
//! ```
93
93
94
- #![ experimental ]
94
+ #![ stable ]
95
95
96
96
use any:: Any ;
97
97
use comm:: channel;
@@ -104,7 +104,9 @@ use rt::local::Local;
104
104
use rt:: task;
105
105
use rt:: task:: Task ;
106
106
use str:: { Str , SendStr , IntoMaybeOwned } ;
107
+ use string:: String ;
107
108
use sync:: Future ;
109
+ use to_str:: ToString ;
108
110
109
111
/// A means of spawning a task
110
112
pub trait Spawner {
@@ -172,6 +174,7 @@ impl TaskBuilder<SiblingSpawner> {
172
174
impl < S : Spawner > TaskBuilder < S > {
173
175
/// Name the task-to-be. Currently the name is used for identification
174
176
/// only in failure messages.
177
+ #[ unstable = "IntoMaybeOwned will probably change." ]
175
178
pub fn named < T : IntoMaybeOwned < ' static > > ( mut self , name : T ) -> TaskBuilder < S > {
176
179
self . name = Some ( name. into_maybe_owned ( ) ) ;
177
180
self
@@ -184,12 +187,14 @@ impl<S: Spawner> TaskBuilder<S> {
184
187
}
185
188
186
189
/// Redirect task-local stdout.
190
+ #[ experimental = "May not want to make stdio overridable here." ]
187
191
pub fn stdout ( mut self , stdout : Box < Writer + Send > ) -> TaskBuilder < S > {
188
192
self . stdout = Some ( stdout) ;
189
193
self
190
194
}
191
195
192
196
/// Redirect task-local stderr.
197
+ #[ experimental = "May not want to make stdio overridable here." ]
193
198
pub fn stderr ( mut self , stderr : Box < Writer + Send > ) -> TaskBuilder < S > {
194
199
self . stderr = Some ( stderr) ;
195
200
self
@@ -288,6 +293,7 @@ impl<S: Spawner> TaskBuilder<S> {
288
293
/// future returns `result::Ok` containing the value returned by the
289
294
/// function. If the child task fails then the future returns `result::Err`
290
295
/// containing the argument to `fail!(...)` as an `Any` trait object.
296
+ #[ experimental = "Futures are experimental." ]
291
297
pub fn try_future < T : Send > ( self , f : proc ( ) : Send -> T )
292
298
-> Future < Result < T , Box < Any + Send > > > {
293
299
// currently, the on_exit proc provided by librustrt only works for unit
@@ -308,6 +314,7 @@ impl<S: Spawner> TaskBuilder<S> {
308
314
309
315
/// Execute a function in a newly-spawnedtask and block until the task
310
316
/// completes or fails. Equivalent to `.try_future(f).unwrap()`.
317
+ #[ unstable = "Error type may change." ]
311
318
pub fn try < T : Send > ( self , f : proc ( ) : Send -> T ) -> Result < T , Box < Any + Send > > {
312
319
self . try_future ( f) . unwrap ( )
313
320
}
@@ -329,6 +336,7 @@ pub fn spawn(f: proc(): Send) {
329
336
/// value of the function or an error if the task failed.
330
337
///
331
338
/// This is equivalent to `TaskBuilder::new().try`.
339
+ #[ unstable = "Error type may change." ]
332
340
pub fn try < T : Send > ( f : proc ( ) : Send -> T ) -> Result < T , Box < Any + Send > > {
333
341
TaskBuilder :: new ( ) . try ( f)
334
342
}
@@ -337,6 +345,7 @@ pub fn try<T: Send>(f: proc(): Send -> T) -> Result<T, Box<Any + Send>> {
337
345
/// task's result.
338
346
///
339
347
/// This is equivalent to `TaskBuilder::new().try_future`.
348
+ #[ experimental = "Futures are experimental." ]
340
349
pub fn try_future < T : Send > ( f : proc ( ) : Send -> T ) -> Future < Result < T , Box < Any + Send > > > {
341
350
TaskBuilder :: new ( ) . try_future ( f)
342
351
}
@@ -345,6 +354,7 @@ pub fn try_future<T:Send>(f: proc():Send -> T) -> Future<Result<T, Box<Any + Sen
345
354
/* Lifecycle functions */
346
355
347
356
/// Read the name of the current task.
357
+ #[ deprecated = "Use `task::name()`." ]
348
358
pub fn with_task_name < U > ( blk: |Option < & str > | -> U ) -> U {
349
359
use rt:: task:: Task ;
350
360
@@ -355,7 +365,20 @@ pub fn with_task_name<U>(blk: |Option<&str>| -> U) -> U {
355
365
}
356
366
}
357
367
368
+ /// Read the name of the current task.
369
+ #[ stable]
370
+ pub fn name ( ) -> Option < String > {
371
+ use rt:: task:: Task ;
372
+
373
+ let task = Local :: borrow ( None :: < Task > ) ;
374
+ match task. name {
375
+ Some ( ref name) => Some ( name. as_slice ( ) . to_string ( ) ) ,
376
+ None => None
377
+ }
378
+ }
379
+
358
380
/// Yield control to the task scheduler.
381
+ #[ unstable = "Name will change." ]
359
382
pub fn deschedule ( ) {
360
383
use rt:: local:: Local ;
361
384
@@ -366,6 +389,7 @@ pub fn deschedule() {
366
389
367
390
/// True if the running task is currently failing (e.g. will return `true` inside a
368
391
/// destructor that is run while unwinding the stack after a call to `fail!()`).
392
+ #[ unstable = "May move to a different module." ]
369
393
pub fn failing ( ) -> bool {
370
394
use rt:: task:: Task ;
371
395
Local :: borrow ( None :: < Task > ) . unwinder . unwinding ( )
@@ -377,7 +401,6 @@ mod test {
377
401
use boxed:: BoxAny ;
378
402
use result;
379
403
use result:: { Ok , Err } ;
380
- use str:: StrAllocating ;
381
404
use string:: String ;
382
405
use std:: io:: { ChanReader , ChanWriter } ;
383
406
use prelude:: * ;
@@ -388,38 +411,30 @@ mod test {
388
411
389
412
#[ test]
390
413
fn test_unnamed_task ( ) {
391
- spawn ( proc ( ) {
392
- with_task_name ( |name| {
393
- assert ! ( name. is_none( ) ) ;
394
- } )
395
- } )
414
+ try( proc ( ) {
415
+ assert ! ( name( ) . is_none( ) ) ;
416
+ } ) . map_err ( |_| ( ) ) . unwrap ( ) ;
396
417
}
397
418
398
419
#[ test]
399
420
fn test_owned_named_task ( ) {
400
- TaskBuilder :: new ( ) . named ( "ada lovelace" . to_string ( ) ) . spawn ( proc ( ) {
401
- with_task_name ( |name| {
402
- assert ! ( name. unwrap( ) == "ada lovelace" ) ;
403
- } )
404
- } )
421
+ TaskBuilder :: new ( ) . named ( "ada lovelace" . to_string ( ) ) . try ( proc ( ) {
422
+ assert ! ( name( ) . unwrap( ) == "ada lovelace" . to_string( ) ) ;
423
+ } ) . map_err ( |_| ( ) ) . unwrap ( ) ;
405
424
}
406
425
407
426
#[ test]
408
427
fn test_static_named_task ( ) {
409
- TaskBuilder :: new ( ) . named ( "ada lovelace" ) . spawn ( proc ( ) {
410
- with_task_name ( |name| {
411
- assert ! ( name. unwrap( ) == "ada lovelace" ) ;
412
- } )
413
- } )
428
+ TaskBuilder :: new ( ) . named ( "ada lovelace" ) . try ( proc ( ) {
429
+ assert ! ( name( ) . unwrap( ) == "ada lovelace" . to_string( ) ) ;
430
+ } ) . map_err ( |_| ( ) ) . unwrap ( ) ;
414
431
}
415
432
416
433
#[ test]
417
434
fn test_send_named_task ( ) {
418
- TaskBuilder :: new ( ) . named ( "ada lovelace" . into_maybe_owned ( ) ) . spawn ( proc ( ) {
419
- with_task_name ( |name| {
420
- assert ! ( name. unwrap( ) == "ada lovelace" ) ;
421
- } )
422
- } )
435
+ TaskBuilder :: new ( ) . named ( "ada lovelace" . into_maybe_owned ( ) ) . try ( proc ( ) {
436
+ assert ! ( name( ) . unwrap( ) == "ada lovelace" . to_string( ) ) ;
437
+ } ) . map_err ( |_| ( ) ) . unwrap ( ) ;
423
438
}
424
439
425
440
#[ test]
0 commit comments