-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added spawn_aborting #6224
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||||||||
use std::ops::{Deref, DerefMut}; | ||||||||||||
|
||||||||||||
use tokio::task::JoinHandle; | ||||||||||||
|
||||||||||||
use futures_core::Future; | ||||||||||||
|
||||||||||||
/// This is a wrapper type around JoinHandle that allows it to be dropped. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A
Suggested change
|
||||||||||||
#[derive(Debug)] | ||||||||||||
pub struct DropHandle<T>(JoinHandle<T>); | ||||||||||||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name is okay, but I think there is precedent for the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, we have a type called But another option is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
|
||||||||||||
impl<T> Drop for DropHandle<T> { | ||||||||||||
fn drop(&mut self) { | ||||||||||||
self.0.abort(); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
impl<T> Deref for DropHandle<T> { | ||||||||||||
type Target = JoinHandle<T>; | ||||||||||||
|
||||||||||||
fn deref(&self) -> &Self::Target { | ||||||||||||
&self.0 | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
impl<T> DerefMut for DropHandle<T> { | ||||||||||||
fn deref_mut(&mut self) -> &mut Self::Target { | ||||||||||||
&mut self.0 | ||||||||||||
} | ||||||||||||
} | ||||||||||||
Comment on lines
+17
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this, could you add all of the wrapper methods here? You can implement It would probably also be nice with a |
||||||||||||
|
||||||||||||
/// This function spawns a task that aborts on drop instead of lingering. | ||||||||||||
pub fn spawn_aborting<F>(future: F) -> DropHandle<F::Output> | ||||||||||||
where | ||||||||||||
F: Future + Send + 'static, | ||||||||||||
F::Output: Send + 'static, | ||||||||||||
{ | ||||||||||||
DropHandle(tokio::spawn(future)) | ||||||||||||
} | ||||||||||||
Comment on lines
+31
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this name. It sounds like something that would immediately make something abort. How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another possibility is to make this a constructor method. Then you would type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For API naming, how about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I don't love My personal preference would be to make the spawn function a function on the I'd probably recommend an interface that looks sort of like this: impl<T> DropHandle<T> {
pub fn new<T>(task: JoinHandle<T>) -> Self{
Self(task)
}
pub fn spawn(future: impl Future<Output = T> + Send + 'static) -> DropHandle<T> {
Self(tokio::spwan(future))
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please import
Future
from the standard library instead.