-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from EspressoSystems/abort-on-drop
abort task on drop
- Loading branch information
Showing
10 changed files
with
54 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::{ | ||
future::Future, | ||
hash::{Hash, Hasher}, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use tokio::task::{JoinError, JoinHandle}; | ||
|
||
/// A function for generating a cute little user mnemonic from a hash | ||
#[must_use] | ||
pub fn mnemonic<H: Hash>(bytes: H) -> String { | ||
let mut state = std::collections::hash_map::DefaultHasher::new(); | ||
bytes.hash(&mut state); | ||
mnemonic::to_string(state.finish().to_le_bytes()) | ||
} | ||
|
||
/// A wrapper for a `JoinHandle` that will abort the task if dropped | ||
pub struct AbortOnDropHandle<T>(pub JoinHandle<T>); | ||
|
||
impl<T> Drop for AbortOnDropHandle<T> { | ||
fn drop(&mut self) { | ||
self.0.abort(); | ||
} | ||
} | ||
|
||
impl<T> Future for AbortOnDropHandle<T> { | ||
type Output = Result<T, JoinError>; | ||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Pin::new(&mut self.0).poll(cx) | ||
} | ||
} |