Detegr / rust-ctrlc

Easy Ctrl-C handler for Rust projects
https://crates.io/crates/ctrlc
Other
599 stars 79 forks source link

Added set_once_handler #54

Closed kayleg closed 5 years ago

kayleg commented 5 years ago

I added the ability to set a handler which implements FnOnce for cases when the handler needs mutable access.

Detegr commented 5 years ago

Do you have an example when this is useful?

kayleg commented 5 years ago

I needed it to gracefully handle cleaning up a client. In the following case, I needed to take ownership of the contents of an Arc variable using Arc::try_unwrap but without the guarantee of FnOnce the ownership rules prevented me from being able to unwrap the value as it could be called multiple times.

Lines: 88 and 94 in this example: https://github.com/kayleg/cloud-pubsub/blob/master/examples/long_lived.rs

Let me know if you need more info.

Detegr commented 5 years ago

Thanks, I get the issue now. However, this is an API that I would not like to add. While I get why you need this in your example, and appreciate the effort, I find the idea of a signal handler that is run just once quite stange. Especially when the code does not remove the assigned handler after it has been run. Another reason why I'm not willing to merge this patch is that I'm working on a different API for ctrlc that deprecates set_handler as well.

I see you're using tokio, have you taken a look at the tokio-signal crate?

kayleg commented 5 years ago

Thanks for letting me know about tokio-signal, totally missed that one. After a few minutes playing with that crate it seems like what I need will be possible so I'll migrate to that one.