with_drop
Nostd wrapper that can be used to execute a custom destructor.
Usage
Cargo.toml
[]
= "0.0.3"
In code:
use ;
use with_drop;
let drop_sum = new;
// Drop function should have been executed
assert!;
Motivation
Take the following bit of code:
use ;
Simple, right? We start two subprocesses, collect their results and compare them against a value. Except, that this example is not entirely correct; it is not exception safe. The std::process::Child documentation specifies that wait() must be called manually (Child does not implement Drop) to properly clean up behind the processes (Failing to do so will result in zombie processes under linux).
Now, if you take a closer look at the above code example, you might spot that not every code path does call wait; if everything goes as planned, wait will be called, however if we exit early due to a failed result, wait will not be called on either child1 or child2.
This property is called exception safety (or result safety since rust does not have exceptions?); the code example
above is not exception safe. We could manually use if statements to catch all these cases, but that would grow
very unwieldy very quickly. Optimally, the Child type would implement Drop and automatically wait on the processes.
but it doesn't. Failing that, we can use with_drop()
to create a wrapper:
use ;
use with_drop;
How about finally()
Couldn't a finally() like construction be used instead? No, it can not! Our finally() guard would have to store a mutable reference of our child variables which would prevent us from calling wait_with_output (borrow checker complains).
use ;
use with_drop;
Testing
Install clippy, rustfmt and nono:
And now use these to execute the tests.
License
Copyright © (C) 2020, Karolin Varner. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Karolin Varner nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Softwear, BV BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.