pub struct SignalHandler { /* private fields */ }
Expand description
A handler for listening to multiple signals, and providing a future for receiving them.
This is useful for applications that need to listen for multiple signals, and want to react to them in a non-blocking way. Typically you would need to use a tokio::select{} to listen for multiple signals, but this provides a more ergonomic interface for doing so.
After a signal is received you can poll the handler again to wait for another signal. Dropping the handle will cancel the signal subscription
§Example
use scuffle_signal::SignalHandler;
use tokio::signal::unix::SignalKind;
let mut handler = SignalHandler::new()
.with_signal(SignalKind::interrupt())
.with_signal(SignalKind::terminate());
// Wait for a signal to be received
let signal = handler.await;
// Handle the signal
let interrupt = SignalKind::interrupt();
let terminate = SignalKind::terminate();
match signal {
interrupt => {
// Handle SIGINT
println!("received SIGINT");
},
terminate => {
// Handle SIGTERM
println!("received SIGTERM");
},
}
Implementations§
Source§impl SignalHandler
impl SignalHandler
Sourcepub fn with_signals<T: Into<SignalKind>>(
signals: impl IntoIterator<Item = T>,
) -> Self
pub fn with_signals<T: Into<SignalKind>>( signals: impl IntoIterator<Item = T>, ) -> Self
Create a new SignalHandler
with the given signals.
Sourcepub fn with_signal(self, kind: impl Into<SignalKind>) -> Self
pub fn with_signal(self, kind: impl Into<SignalKind>) -> Self
Add a signal to the handler.
If the signal is already in the handler, it will not be added again.
Sourcepub fn add_signal(&mut self, kind: impl Into<SignalKind>) -> &mut Self
pub fn add_signal(&mut self, kind: impl Into<SignalKind>) -> &mut Self
Add a signal to the handler.
If the signal is already in the handler, it will not be added again.
Sourcepub async fn recv(&mut self) -> SignalKind
pub async fn recv(&mut self) -> SignalKind
Wait for a signal to be received. This is equivilant to calling (&mut handler).await, but is more ergonomic if you want to not take ownership of the handler.
Sourcepub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<SignalKind>
pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<SignalKind>
Poll for a signal to be received. Does not require pinning the handler.
Trait Implementations§
Source§impl Debug for SignalHandler
impl Debug for SignalHandler
Source§impl Default for SignalHandler
impl Default for SignalHandler
Source§impl Future for SignalHandler
impl Future for SignalHandler
Auto Trait Implementations§
impl Freeze for SignalHandler
impl !RefUnwindSafe for SignalHandler
impl Send for SignalHandler
impl Sync for SignalHandler
impl Unpin for SignalHandler
impl !UnwindSafe for SignalHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<F> ContextFutExt<<F as IntoFuture>::IntoFuture> for Fwhere
F: IntoFuture,
impl<F> ContextFutExt<<F as IntoFuture>::IntoFuture> for Fwhere
F: IntoFuture,
§fn with_context<'a>(
self,
ctx: impl Into<ContextRef<'a>>,
) -> FutureWithContext<'a, <F as IntoFuture>::IntoFuture>where
F: IntoFuture,
fn with_context<'a>(
self,
ctx: impl Into<ContextRef<'a>>,
) -> FutureWithContext<'a, <F as IntoFuture>::IntoFuture>where
F: IntoFuture,
§impl<F> FutureExt for F
impl<F> FutureExt for F
§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f
. Read more§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read more§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
()
on completion and sends
its output to another future on a separate task. Read more§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.