diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-04-13 14:39:04 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-04-15 19:45:00 -0700 |
| commit | c836ff46215b743c0f681d3e4d799cde1832cde3 (patch) | |
| tree | d2d65dfca8f0683102c85ef3a287be374b1cf647 /src/libstd | |
| parent | 326f938730c1eaae48ed333907dce2cc92dc9aab (diff) | |
| download | rust-c836ff46215b743c0f681d3e4d799cde1832cde3.tar.gz rust-c836ff46215b743c0f681d3e4d799cde1832cde3.zip | |
std: Impl Deref/DerefMut for a borrowed task
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/stdio.rs | 6 | ||||
| -rw-r--r-- | src/libstd/rt/local_heap.rs | 3 | ||||
| -rw-r--r-- | src/libstd/rt/local_ptr.rs | 18 | ||||
| -rw-r--r-- | src/libstd/rt/task.rs | 10 | ||||
| -rw-r--r-- | src/libstd/task.rs | 9 |
5 files changed, 20 insertions, 26 deletions
diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 5f47e227901..34a57884398 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -160,7 +160,7 @@ fn reset_helper(w: ~Writer:Send, { let mut t = Local::borrow(None::<Task>); // Be sure to flush any pending output from the writer - match f(t.get(), w) { + match f(&mut *t, w) { Some(mut w) => { drop(t); // FIXME: is failing right here? @@ -230,9 +230,7 @@ fn with_task_stdout(f: |&mut Writer| -> IoResult<()> ) { // To protect against this, we do a little dance in which we // temporarily take the task, swap the handles, put the task in TLS, // and only then drop the previous handle. - let mut t = Local::borrow(None::<Task>); - let prev = replace(&mut t.get().stdout, my_stdout); - drop(t); + let prev = replace(&mut Local::borrow(None::<Task>).stdout, my_stdout); drop(prev); ret } diff --git a/src/libstd/rt/local_heap.rs b/src/libstd/rt/local_heap.rs index b9d0d829374..caf0d9028c5 100644 --- a/src/libstd/rt/local_heap.rs +++ b/src/libstd/rt/local_heap.rs @@ -319,8 +319,7 @@ pub unsafe fn local_free(ptr: *u8) { } pub fn live_allocs() -> *mut Box { - let mut task = Local::borrow(None::<Task>); - task.get().heap.live_allocs + Local::borrow(None::<Task>).heap.live_allocs } #[cfg(test)] diff --git a/src/libstd/rt/local_ptr.rs b/src/libstd/rt/local_ptr.rs index e3f64f40c0d..6b61af1d9a2 100644 --- a/src/libstd/rt/local_ptr.rs +++ b/src/libstd/rt/local_ptr.rs @@ -18,7 +18,7 @@ #![allow(dead_code)] use cast; -use ops::Drop; +use ops::{Drop, Deref, DerefMut}; use ptr::RawPtr; #[cfg(windows)] // mingw-w32 doesn't like thread_local things @@ -48,13 +48,15 @@ impl<T> Drop for Borrowed<T> { } } -impl<T> Borrowed<T> { - pub fn get<'a>(&'a mut self) -> &'a mut T { - unsafe { - let val_ptr: &mut ~T = cast::transmute(&mut self.val); - let val_ptr: &'a mut T = *val_ptr; - val_ptr - } +impl<T> Deref<T> for Borrowed<T> { + fn deref<'a>(&'a self) -> &'a T { + unsafe { &*(self.val as *T) } + } +} + +impl<T> DerefMut<T> for Borrowed<T> { + fn deref_mut<'a>(&'a mut self) -> &'a mut T { + unsafe { &mut *(self.val as *mut T) } } } diff --git a/src/libstd/rt/task.rs b/src/libstd/rt/task.rs index a112ed77f09..a3664b45a41 100644 --- a/src/libstd/rt/task.rs +++ b/src/libstd/rt/task.rs @@ -127,8 +127,8 @@ impl Task { #[allow(unused_must_use)] fn close_outputs() { let mut task = Local::borrow(None::<Task>); - let stderr = task.get().stderr.take(); - let stdout = task.get().stdout.take(); + let stderr = task.stderr.take(); + let stdout = task.stdout.take(); drop(task); match stdout { Some(mut w) => { w.flush(); }, None => {} } match stderr { Some(mut w) => { w.flush(); }, None => {} } @@ -159,8 +159,7 @@ impl Task { // be intertwined, and miraculously work for now... let mut task = Local::borrow(None::<Task>); let storage_map = { - let task = task.get(); - let LocalStorage(ref mut optmap) = task.storage; + let &LocalStorage(ref mut optmap) = &mut task.storage; optmap.take() }; drop(task); @@ -332,8 +331,7 @@ impl BlockedTask { } /// Converts one blocked task handle to a list of many handles to the same. - pub fn make_selectable(self, num_handles: uint) -> Take<BlockedTasks> - { + pub fn make_selectable(self, num_handles: uint) -> Take<BlockedTasks> { let arc = match self { Owned(task) => { let flag = unsafe { AtomicUint::new(cast::transmute(task)) }; diff --git a/src/libstd/task.rs b/src/libstd/task.rs index ed10f6d15cd..df627809ea0 100644 --- a/src/libstd/task.rs +++ b/src/libstd/task.rs @@ -257,8 +257,8 @@ pub fn try<T:Send>(f: proc():Send -> T) -> Result<T, ~Any:Send> { pub fn with_task_name<U>(blk: |Option<&str>| -> U) -> U { use rt::task::Task; - let mut task = Local::borrow(None::<Task>); - match task.get().name { + let task = Local::borrow(None::<Task>); + match task.name { Some(ref name) => blk(Some(name.as_slice())), None => blk(None) } @@ -276,11 +276,8 @@ pub fn deschedule() { pub fn failing() -> bool { //! True if the running task has failed - use rt::task::Task; - - let mut local = Local::borrow(None::<Task>); - local.get().unwinder.unwinding() + Local::borrow(None::<Task>).unwinder.unwinding() } // The following 8 tests test the following 2^3 combinations: |
