[][src]Crate addy

Addy

A library for ergonomically handling kernel interrupts.

Quick Start

use addy::SIGWINCH;
use std::io::{Read, stdin};
fn main() -> Result<(), addy::Error> {
	/* SIGWINCH is a POSIX interrupt signal for window resized */
	addy::mediate(SIGWINCH)
			.register("print", |_signal| { println!("Screen Resized!"); })?
			.enable()?;

	/* Block so the program doesn't exit immediately
	 * Try resizing your terminal window :)
	*/
	let mut buffer = [0; 1];
   	loop {
       stdin().read(&mut buffer);
  	}
	Ok(())
}

Things To Know

I love you and I wish the best for you. No matter what you choose to do, I hope you decide it is worth you time to do it well.

Addy is Thread Safe!

You can call it from anywhere, at anytime! You can store a SignalHandle (returned from addy::mediate(signal)) in a variable and pass it around.

use addy::{SIGWINCH, SIGINT};
use std::io::{Read, stdin};
static QUOTE: &'static str = "Look at you, hacker: a pathetic creature of meat \
							  and bone, panting and sweating as you run through \
							  my corridors. How can you challenge a perfect, \
							  immortal machine?";

fn main() -> Result<(), addy::Error> {
	/* When the window resizes */
    addy::mediate(SIGWINCH)
    		.register("hello", |_signal| { println!("Hello, World!"); })?
    		.register("girls", |_signal| { println!("Hello, Girls!"); })?
    		.enable()?;

    /* SIGINT is sent when the user presses Ctrl + C. The default behavior is
     * to interrupt the program's execution.
    */
    let mut ctrl_c = addy::mediate(SIGINT);
    ctrl_c.register("no_interruptions", |_signal| { println!("{}", QUOTE); })?.enable()?;

    /* Let the user use Ctrl + C to kill the program after 10 seconds */
    std::thread::spawn(move || -> Result<(), addy::Error> {
        std::thread::sleep(std::time::Duration::from_secs(10));
        ctrl_c.default()?;
        Ok(())
    });

    /* Stop saying "Hello, World!" on resize after 5 seconds */
    std::thread::spawn(move || -> Result<(), addy::Error> {
        std::thread::sleep(std::time::Duration::from_secs(5));
        addy::mediate(SIGWINCH).remove("hello")?;
        Ok(())
    });

    /* Capture the input so we don't exit the program immediately */
    let mut buffer = [0; 1];
    loop {
        stdin().read(&mut buffer);
    }

    Ok(())
}

Errors

If the MPSC channel closes, of the Event Loop thread closes, there is no way to recover and any future Addy calls will return an addy::Error.

Re-exports

pub use self::Signal::*;

Structs

SignalHandle

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).

SignalIterator

Useful if you want to set every signal to "Ignore" or "Default."

Enums

Error

Addy Error type - realistically you will never see it. As it only occurs when the MPSC channel fails. MPSC channels only fail if the receiver is dropped which can only happen if the event loop thread panics somehow.

Signal

Enum representing the different interrupt signals

Functions

mediate

Use this to get a SignalHandle representing a interrupt specified by Signal.