Quick Usage
For Clients
...where clients mean structures that may fail dropping,
Implement TryDrop
for your type and adapt
it like so:
use try_drop::TryDrop;
pub struct Foo { }
impl TryDrop for Foo {
type Error = Error;
unsafe fn try_drop(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
let foo = Foo.adapt();
...or, if you want to avoid the adapt
boilerplate:
use try_drop::{TryDrop, adapters::DropAdapter};
pub struct FooInner { }
impl TryDrop for FooInner {
type Error = Error;
unsafe fn try_drop(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
pub struct Foo(pub DropAdapter<FooInner>);
impl Foo {
pub fn from_inner(inner: FooInner) -> Self {
Foo(DropAdapter(inner))
}
}
With this, if dropping Foo
fails, it will automatically print the error to standard error.
For Servers
...where servers mean how to handle the drop errors (also means drop strategies),
Implement TryDropStrategy
for your structure.
use try_drop::TryDropStrategy;
pub struct Strategy { }
impl TryDropStrategy for Strategy {
fn handle_error(&self, error: try_drop::Error) {
}
}
...then install either for this thread,
try_drop::install_thread_local_handlers(Strategy, )
...install it globally (meaning if no thread local strategies are set, use this),
try_drop::install_global_handlers(Strategy, )
...or, if possible, install it for a structure.
struct Sample<D = ShimPrimaryHandler, DD = ShimFallbackHandler>
where
D: FallibleTryDropStrategy,
DD: TryDropStrategy,
{
primary: D,
fallback: DD,
}
impl<D, DD> Sample<D, DD>
where
D: FallibleTryDropStrategy,
DD: TryDropStrategy,
{
pub fn new_with( primary: D, fallback: DD) -> Self {
Self {
primary,
fallback,
}
}
}
let sample = Sample::new_with(Strategy, )
License
This project is licensed under the MIT License.