diff options
author | Stjepan Glavina <stjepang@gmail.com> | 2020-09-18 16:06:39 +0200 |
---|---|---|
committer | Stjepan Glavina <stjepang@gmail.com> | 2020-09-18 16:06:39 +0200 |
commit | 2c42950de5b39fdd371d0baaef798138a391626a (patch) | |
tree | 565707b317d129c18cee87af25190bd5e8296346 | |
parent | deb709f4198c4c6d1c691a69f5cd713d040e0fed (diff) | |
download | async-task-2c42950de5b39fdd371d0baaef798138a391626a.tar.gz |
Rename R to T
-rw-r--r-- | examples/spawn-local.rs | 12 | ||||
-rw-r--r-- | examples/spawn-on-thread.rs | 6 | ||||
-rw-r--r-- | examples/spawn.rs | 6 | ||||
-rw-r--r-- | src/join_handle.rs | 40 | ||||
-rw-r--r-- | src/raw.rs | 38 | ||||
-rw-r--r-- | src/task.rs | 20 |
6 files changed, 61 insertions, 61 deletions
diff --git a/examples/spawn-local.rs b/examples/spawn-local.rs index b25c72c..1e1180c 100644 --- a/examples/spawn-local.rs +++ b/examples/spawn-local.rs @@ -12,10 +12,10 @@ thread_local! { } /// Spawns a future on the executor. -fn spawn<F, R>(future: F) -> JoinHandle<R> +fn spawn<F, T>(future: F) -> JoinHandle<T> where - F: Future<Output = R> + 'static, - R: 'static, + F: Future<Output = T> + 'static, + T: 'static, { // Create a task that is scheduled by sending itself into the channel. let schedule = |t| QUEUE.with(|(s, _)| s.send(t).unwrap()); @@ -28,10 +28,10 @@ where } /// Runs a future to completion. -fn run<F, R>(future: F) -> R +fn run<F, T>(future: F) -> T where - F: Future<Output = R> + 'static, - R: 'static, + F: Future<Output = T> + 'static, + T: 'static, { // Spawn a task that sends its result through a channel. let (s, r) = flume::unbounded(); diff --git a/examples/spawn-on-thread.rs b/examples/spawn-on-thread.rs index c921034..3b7e522 100644 --- a/examples/spawn-on-thread.rs +++ b/examples/spawn-on-thread.rs @@ -10,10 +10,10 @@ use futures_lite::future; /// Spawns a future on a new dedicated thread. /// /// The returned handle can be used to await the output of the future. -fn spawn_on_thread<F, R>(future: F) -> JoinHandle<R> +fn spawn_on_thread<F, T>(future: F) -> JoinHandle<T> where - F: Future<Output = R> + Send + 'static, - R: Send + 'static, + F: Future<Output = T> + Send + 'static, + T: Send + 'static, { // Create a channel that holds the task when it is scheduled for running. let (sender, receiver) = flume::unbounded(); diff --git a/examples/spawn.rs b/examples/spawn.rs index 6c00d78..63e84b6 100644 --- a/examples/spawn.rs +++ b/examples/spawn.rs @@ -9,10 +9,10 @@ use futures_lite::future; use once_cell::sync::Lazy; /// Spawns a future on the executor. -fn spawn<F, R>(future: F) -> JoinHandle<R> +fn spawn<F, T>(future: F) -> JoinHandle<T> where - F: Future<Output = R> + Send + 'static, - R: Send + 'static, + F: Future<Output = T> + Send + 'static, + T: Send + 'static, { // A channel that holds scheduled tasks. static QUEUE: Lazy<flume::Sender<Task>> = Lazy::new(|| { diff --git a/src/join_handle.rs b/src/join_handle.rs index 24fef59..2b7fcc0 100644 --- a/src/join_handle.rs +++ b/src/join_handle.rs @@ -12,37 +12,37 @@ use crate::state::*; /// A handle that awaits the result of a task. /// -/// This type is a future that resolves to an `Option<R>` where: +/// This type is a future that resolves to an `Option<T>` where: /// /// * `None` indicates the task has panicked or was canceled. -/// * `Some(result)` indicates the task has completed with `result` of type `R`. +/// * `Some(result)` indicates the task has completed with `result` of type `T`. #[must_use = "tasks get canceled when dropped, use `.detach()` to run them in the background"] -pub struct JoinHandle<R> { +pub struct JoinHandle<T> { /// A raw task pointer. pub(crate) raw_task: NonNull<()>, - /// A marker capturing generic type `R`. - pub(crate) _marker: PhantomData<R>, + /// A marker capturing generic type `T`. + pub(crate) _marker: PhantomData<T>, } -unsafe impl<R: Send> Send for JoinHandle<R> {} -unsafe impl<R> Sync for JoinHandle<R> {} +unsafe impl<T: Send> Send for JoinHandle<T> {} +unsafe impl<T> Sync for JoinHandle<T> {} -impl<R> Unpin for JoinHandle<R> {} +impl<T> Unpin for JoinHandle<T> {} #[cfg(feature = "std")] -impl<R> std::panic::UnwindSafe for JoinHandle<R> {} +impl<T> std::panic::UnwindSafe for JoinHandle<T> {} #[cfg(feature = "std")] -impl<R> std::panic::RefUnwindSafe for JoinHandle<R> {} +impl<T> std::panic::RefUnwindSafe for JoinHandle<T> {} -impl<R> JoinHandle<R> { +impl<T> JoinHandle<T> { pub fn detach(self) { let mut this = self; let _out = this.set_detached(); mem::forget(this); } - pub async fn cancel(self) -> Option<R> { + pub async fn cancel(self) -> Option<T> { let mut this = self; this.set_canceled(); @@ -106,7 +106,7 @@ impl<R> JoinHandle<R> { } } - fn set_detached(&mut self) -> Option<R> { + fn set_detached(&mut self) -> Option<T> { let ptr = self.raw_task.as_ptr(); let header = ptr as *const Header; @@ -137,7 +137,7 @@ impl<R> JoinHandle<R> { Ok(_) => { // Read the output. output = - Some((((*header).vtable.get_output)(ptr) as *mut R).read()); + Some((((*header).vtable.get_output)(ptr) as *mut T).read()); // Update the state variable because we're continuing the loop. state |= CLOSED; @@ -184,7 +184,7 @@ impl<R> JoinHandle<R> { } } - fn poll_result(&mut self, cx: &mut Context<'_>) -> Poll<Option<R>> { + fn poll_result(&mut self, cx: &mut Context<'_>) -> Poll<Option<T>> { let ptr = self.raw_task.as_ptr(); let header = ptr as *const Header; @@ -252,7 +252,7 @@ impl<R> JoinHandle<R> { } // Take the output from the task. - let output = ((*header).vtable.get_output)(ptr) as *mut R; + let output = ((*header).vtable.get_output)(ptr) as *mut T; return Poll::Ready(Some(output.read())); } Err(s) => state = s, @@ -262,15 +262,15 @@ impl<R> JoinHandle<R> { } } -impl<R> Drop for JoinHandle<R> { +impl<T> Drop for JoinHandle<T> { fn drop(&mut self) { self.set_canceled(); self.set_detached(); } } -impl<R> Future for JoinHandle<R> { - type Output = R; +impl<T> Future for JoinHandle<T> { + type Output = T; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match self.poll_result(cx) { @@ -280,7 +280,7 @@ impl<R> Future for JoinHandle<R> { } } -impl<R> fmt::Debug for JoinHandle<R> { +impl<T> fmt::Debug for JoinHandle<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let ptr = self.raw_task.as_ptr(); let header = ptr as *const Header; @@ -58,7 +58,7 @@ pub(crate) struct TaskLayout { } /// Raw pointers to the fields inside a task. -pub(crate) struct RawTask<F, R, S> { +pub(crate) struct RawTask<F, T, S> { /// The task header. pub(crate) header: *const Header, @@ -69,20 +69,20 @@ pub(crate) struct RawTask<F, R, S> { pub(crate) future: *mut F, /// The output of the future. - pub(crate) output: *mut R, + pub(crate) output: *mut T, } -impl<F, R, S> Copy for RawTask<F, R, S> {} +impl<F, T, S> Copy for RawTask<F, T, S> {} -impl<F, R, S> Clone for RawTask<F, R, S> { +impl<F, T, S> Clone for RawTask<F, T, S> { fn clone(&self) -> Self { *self } } -impl<F, R, S> RawTask<F, R, S> +impl<F, T, S> RawTask<F, T, S> where - F: Future<Output = R> + 'static, + F: Future<Output = T> + 'static, S: Fn(Task) + Send + Sync + 'static, { const RAW_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new( @@ -144,7 +144,7 @@ where header: p as *const Header, schedule: p.add(task_layout.offset_s) as *const S, future: p.add(task_layout.offset_f) as *mut F, - output: p.add(task_layout.offset_r) as *mut R, + output: p.add(task_layout.offset_r) as *mut T, } } } @@ -152,18 +152,18 @@ where /// Returns the memory layout for a task. #[inline] fn task_layout() -> TaskLayout { - // Compute the layouts for `Header`, `S`, `F`, and `R`. + // Compute the layouts for `Header`, `S`, `F`, and `T`. let layout_header = Layout::new::<Header>(); let layout_s = Layout::new::<S>(); let layout_f = Layout::new::<F>(); - let layout_r = Layout::new::<R>(); + let layout_r = Layout::new::<T>(); - // Compute the layout for `union { F, R }`. + // Compute the layout for `union { F, T }`. let size_union = layout_f.size().max(layout_r.size()); let align_union = layout_f.align().max(layout_r.align()); let layout_union = unsafe { Layout::from_size_align_unchecked(size_union, align_union) }; - // Compute the layout for `Header` followed `S` and `union { F, R }`. + // Compute the layout for `Header` followed `S` and `union { F, T }`. let layout = layout_header; let (layout, offset_s) = extend(layout, layout_s); let (layout, offset_union) = extend(layout, layout_union); @@ -590,14 +590,14 @@ where return false; /// A guard that closes the task if polling its future panics. - struct Guard<F, R, S>(RawTask<F, R, S>) + struct Guard<F, T, S>(RawTask<F, T, S>) where - F: Future<Output = R> + 'static, + F: Future<Output = T> + 'static, S: Fn(Task) + Send + Sync + 'static; - impl<F, R, S> Drop for Guard<F, R, S> + impl<F, T, S> Drop for Guard<F, T, S> where - F: Future<Output = R> + 'static, + F: Future<Output = T> + 'static, S: Fn(Task) + Send + Sync + 'static, { fn drop(&mut self) { @@ -613,7 +613,7 @@ where if state & CLOSED != 0 { // The thread that closed the task didn't drop the future because it // was running so now it's our responsibility to do so. - RawTask::<F, R, S>::drop_future(ptr); + RawTask::<F, T, S>::drop_future(ptr); // Mark the task as not running and not scheduled. (*raw.header) @@ -626,7 +626,7 @@ where } // Drop the task reference. - RawTask::<F, R, S>::drop_task(ptr); + RawTask::<F, T, S>::drop_task(ptr); break; } @@ -639,7 +639,7 @@ where ) { Ok(state) => { // Drop the future because the task is now closed. - RawTask::<F, R, S>::drop_future(ptr); + RawTask::<F, T, S>::drop_future(ptr); // Notify the awaiter that the future has been dropped. if state & AWAITER != 0 { @@ -647,7 +647,7 @@ where } // Drop the task reference. - RawTask::<F, R, S>::drop_task(ptr); + RawTask::<F, T, S>::drop_task(ptr); break; } Err(s) => state = s, diff --git a/src/task.rs b/src/task.rs index 4850e00..02ef126 100644 --- a/src/task.rs +++ b/src/task.rs @@ -45,18 +45,18 @@ use crate::JoinHandle; /// // Create a task with the future and the schedule function. /// let (task, handle) = async_task::spawn(future, schedule); /// ``` -pub fn spawn<F, R, S>(future: F, schedule: S) -> (Task, JoinHandle<R>) +pub fn spawn<F, T, S>(future: F, schedule: S) -> (Task, JoinHandle<T>) where - F: Future<Output = R> + Send + 'static, - R: Send + 'static, + F: Future<Output = T> + Send + 'static, + T: Send + 'static, S: Fn(Task) + Send + Sync + 'static, { // Allocate large futures on the heap. let raw_task = if mem::size_of::<F>() >= 2048 { let future = alloc::boxed::Box::pin(future); - RawTask::<_, R, S>::allocate(future, schedule) + RawTask::<_, T, S>::allocate(future, schedule) } else { - RawTask::<F, R, S>::allocate(future, schedule) + RawTask::<F, T, S>::allocate(future, schedule) }; let task = Task { raw_task }; @@ -105,10 +105,10 @@ where /// let (task, handle) = async_task::spawn_local(future, schedule); /// ``` #[cfg(feature = "std")] -pub fn spawn_local<F, R, S>(future: F, schedule: S) -> (Task, JoinHandle<R>) +pub fn spawn_local<F, T, S>(future: F, schedule: S) -> (Task, JoinHandle<T>) where - F: Future<Output = R> + 'static, - R: 'static, + F: Future<Output = T> + 'static, + T: 'static, S: Fn(Task) + Send + Sync + 'static, { use std::mem::ManuallyDrop; @@ -163,9 +163,9 @@ where // Allocate large futures on the heap. let raw_task = if mem::size_of::<F>() >= 2048 { let future = alloc::boxed::Box::pin(future); - RawTask::<_, R, S>::allocate(future, schedule) + RawTask::<_, T, S>::allocate(future, schedule) } else { - RawTask::<_, R, S>::allocate(future, schedule) + RawTask::<_, T, S>::allocate(future, schedule) }; let task = Task { raw_task }; |