-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//! Service Utilities | ||
|
||
use std::{ | ||
future::Future, | ||
io::{self, ErrorKind}, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use futures::ready; | ||
use tokio::task::JoinHandle; | ||
|
||
/// Wrapper of `tokio::task::JoinHandle`, which links to a server instance. | ||
/// | ||
/// `ServerHandle` implements `Future` which will join the `JoinHandle` and get the result. | ||
/// When `ServerHandle` drops, it will abort the task. | ||
pub struct ServerHandle(pub JoinHandle<io::Result<()>>); | ||
|
||
impl Drop for ServerHandle { | ||
#[inline] | ||
fn drop(&mut self) { | ||
self.0.abort(); | ||
} | ||
} | ||
|
||
impl Future for ServerHandle { | ||
type Output = io::Result<()>; | ||
|
||
#[inline] | ||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
match ready!(Pin::new(&mut self.0).poll(cx)) { | ||
Ok(res) => res.into(), | ||
Err(err) => Err(io::Error::new(ErrorKind::Other, err)).into(), | ||
} | ||
} | ||
} |