Struct SignalHandle

Source
pub struct SignalHandle { /* private fields */ }
Expand description

This is the struct returned from an addy::mediate(Signal) call. It allows the caller to add, remove, and clear callbacks to the provided interrupt handler. Adding closures prevents the default bevaior (if any).

You can also set the interrupt to the default behaviour, or to be ignored by the process. If you set a signal to be ignored or back to the defaults you can call .resume() to to have it handle your callbacks again.

Dropping it does not stop the signal handler. You must call .release() to have Addy stop handling this interrupt and free the associated resources. Conversely, you don’t have to keep a handle to this around once you’ve set it up.

If you register callbacks for an interrupt, you must call .enable() to have them run. If you call .release() on a SignalHandler you must call .enable() again (after re-registering new callbacks).

§Example

use addy::{Signal, SIGWINCH};

fn my_func(signal: Signal) {
	/* Does a thing */
}

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.register("my_func", my_func)?
			.enable()?;

	//-- Later --//

	// Ignore the incoming signal
	addy::mediate(SIGWINCH).ignore()?;

	//-- Later Still --//

	// Swap out one of the callbacks and re-enable capturing the interrupt
	addy::mediate(SIGWINCH)
			.remove("print")?
			.register("print", |_signal| { println!("New Output!"); })?
			.enable()?;

	Ok(())
}

Implementations§

Source§

impl SignalHandle

Source

pub fn register<'a, A, F>( &'a mut self, name: A, cb: F, ) -> Result<&'_ mut SignalHandle, Error>
where A: AsRef<str>, F: Fn(Signal) + Send + 'static,

Registers a callback with the interrupt handler for the associated Signal. If you call register with the same name it will replace the previous callback.

§Example
use addy::{Signal, SIGWINCH};

fn my_func(signal: Signal) {
	/* Does a thing */
}

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.register("my_func", my_func)?
			.enable()?;

	Ok(())
}
Source

pub fn remove<'a, A>( &'a mut self, name: A, ) -> Result<&'_ mut SignalHandle, Error>
where A: AsRef<str>,

Removes a named callback from the associated Signal. If no callback with that name exists, it does nothing.

§Example
use addy::{Signal, SIGWINCH};

fn my_func(signal: Signal) {
	/* Does a thing */
}

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.register("my_func", my_func)?
			.enable()?;

	//-- Later --//

	// Stop calling "print" when the process receives a SIGWINCH signal
	addy::mediate(SIGWINCH).remove("print")?;

	Ok(())
}
Source

pub fn clear<'a>(&'a mut self) -> Result<&'_ mut SignalHandle, Error>

Removes a all callbacks from the associated Signal. Functionally similar to calling .ignore() except you don’t need to call .enable() if you add new callbacks later.

§Example
use addy::{Signal, SIGWINCH};

fn my_func(signal: Signal) {
	/* Does a thing */
}

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.register("my_func", my_func)?
			.enable()?;

	//-- Later --//

	// Capture the signal, but stop calling anything
	addy::mediate(SIGWINCH)
			.clear()?
			.register("solo_callback", |_signal| { println!("ALONE!"); })?;

	Ok(())
}
Source

pub fn release<'a>(&'a mut self) -> Result<&'_ mut SignalHandle, Error>

Removes a all callbacks from the associated Signal and resets the interrupt handler to the default behavior. Funcationally the same as calling .clear() and .default().

You will need to call .enable() again after re-registering callbacks.

§Example
use addy::SIGWINCH;

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.enable()?;

	//-- Later --//

	// Stop capturing the signal
	addy::mediate(SIGWINCH).release()?;
  
	//-- Later Still --//

	// Start catpuring again
	addy::mediate(SIGWINCH)
			.register("new", |_signal| { println!("New callback!"); })?
			.enable()?;

	Ok(())
}
Source

pub fn ignore<'a>(&'a mut self) -> Result<&'_ mut SignalHandle, Error>

Tells the process to ignore this interrupt. Keeps all your callbacks. Calling .resume() will re-enable them.

§Example
use addy::SIGWINCH;

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.enable()?;

	//-- Later --//

	// Ignore the signal
	addy::mediate(SIGWINCH).ignore()?;

	//-- Later Still --//

	// Start catpuring again
	addy::mediate(SIGWINCH).resume()?;

	Ok(())
}
Source

pub fn default<'a>(&'a mut self) -> Result<&'_ mut SignalHandle, Error>

Restore the interrupt handler to the system default. Not all interrupts have a default, and some interrupts default is to be ignored. Keeps all your callbacks. Calling .resume() will re-enable them.

§Example
use addy::SIGINT;

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGINT)
			.register("print", |_signal| { println!("Interrupted!"); })?
			.enable()?;

	//-- Later --//

	// Set the signal to its default
	addy::mediate(SIGINT).default()?;

	//-- Later Still --//

	// Start catpuring again
	addy::mediate(SIGINT).resume()?;

	Ok(())
}
Source

pub fn resume<'a>(&'a mut self) -> Result<&'_ mut SignalHandle, Error>

Resumes capturing the interrupt and calling any associated callbacks. Most often used after a call to .ignore() and .default().

Alias of .enable()

§Example
use addy::SIGINT;

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGINT)
			.register("print", |_signal| { println!("Interrupted!"); })?
			.enable()?;

	//-- Later --//

	// Set the signal to its default
	addy::mediate(SIGINT).default()?;

	//-- Later Still --//

	// Start catpuring and printing "Interrupted!" again
	addy::mediate(SIGINT).resume()?;

	Ok(())
}
Source

pub fn enable<'a>(&'a mut self) -> Result<&'_ mut SignalHandle, Error>

Begins capturing the interrupt and calling any associated callbacks. Most often used after a calls .register()

Alias of .resume()

§Example
use addy::SIGINT;

fn main() -> Result<(), addy::Error> {
	addy::mediate(SIGINT)
			.register("print", |_signal| { println!("Interrupted!"); })?
			.enable()?;
	Ok(())
}

Trait Implementations§

Source§

impl Debug for SignalHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.