-
Notifications
You must be signed in to change notification settings - Fork 89
Async support and more #133
Copy link
Copy link
Open
Description
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,
-
The return value of the function
set_handleris a thread handle, which can be used to wait for its end. -
The parameter passed into the function
set_handleris 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...");
}- The AsyncCtrlC structure is implemented, which is a Future that users can use for any asynchronous runtime such as
tokioorasync-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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels