Skip to content

Async support and more #133

@ssrlive

Description

@ssrlive

Hi @Detegr ,

After some years of independent development, the CtrlC2 I maintain has fully met my expectations.

Now I hope to contribute back to the CtrlC crate.

The main improvements of Ctrlc2 to Ctrlc are,

  1. The return value of the function set_handler is a thread handle, which can be used to wait for its end.

  2. The parameter passed into the function set_handler is a closure, which returns a boolean value, which is used to represent whether the user is willing to end the program.

fn main() {
    let handle = ctrlc2::set_handler(move || {
        println!(" ");
        println!("Ctrl-C received, ready to exiting...");
        true
    })
    .unwrap();
    println!("Waiting for Ctrl-C...");
    handle.join().unwrap();
    println!("Got it! Exiting...");
}
  1. The AsyncCtrlC structure is implemented, which is a Future that users can use for any asynchronous runtime such as tokio or async-std.
async fn async_main() {
    let ctrlc = ctrlc2::AsyncCtrlC::new(move || {
        println!("Ctrl-C received!");
        true
    })
    .expect("cannot create Ctrl+C handler");
    println!("Waiting for Ctrl-C...");
    ctrlc.await.unwrap();
    println!("Got it! Exiting...");
}

If you think Ctrlc2 is a good improvement, I'd be happy to submit a PR to merge it back into CtrlC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions