Rust trait + smart pointer for manual object destruction
This crate introduces the Close trait for manual object destruction. While
similar in purpose to Drop, the key difference is that the close method
takes ownership over self and allows for error propagation. For use in
conjuction with Drop, this crate further introduces the Closing<T: Close>
smart pointer, which is a zero cost abstraction that closes the contained
object upon drop if it was not closed manually.
Motivation
Having ownership over self is useful in situations where the destruction
sequence requires dropping or moving members. With drop this requires
solutions such as sticking the member in an Option, as in the following
example, which joins a thread (moving its handle) before continuing the
teardown process. The downside of this construction is added runtime cost and
reduced ergonomics in accessing the data behind the option.
;
Using close instead of drop we can avoid the option dance and write things
as one naturally would:
use ;
;
Note that besides avoiding Option, the constructor now returns the Closing
smart pointer. As a result, the second implementation can be used in precisely
the same way as the former, using automatic dereferencing to access members and
methods and joining the thread when the object goes out of scope. The
difference is that the latter allows for a more ergonomic implementation, does
not incur any runtime cost, and allows for manual closing in case error
handling is desired.