Coder Social home page Coder Social logo

alinuxperson / try-drop Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 544 KB

Batteries included error handling mechanisms for drops which can fail

Home Page: https://crates.io/crates/try-drop

License: MIT License

Rust 100.00%
rust rust-lang rust-library library crate rust-crate drop fallible-drop fallible-drops drops

try-drop's Introduction

try-drop

Batteries included error handling mechanisms for drops which can fail

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 { /* fields */ }

impl TryDrop for Foo {
    type Error = Error;
    
    unsafe fn try_drop(&mut self) -> Result<(), Self::Error> {
        // do stuff
        Ok(())
    }
}

let foo = Foo.adapt();

...or, if you want to avoid the adapt boilerplate:

use try_drop::{TryDrop, adapters::DropAdapter};

pub struct FooInner { /* fields */ }

impl TryDrop for FooInner {
    type Error = Error;
    
    unsafe fn try_drop(&mut self) -> Result<(), Self::Error> {
        // do stuff
        Ok(())
    }
}

pub struct Foo(pub DropAdapter<FooInner>);

impl Foo {
    pub fn from_inner(inner: FooInner) -> Self {
        Foo(DropAdapter(inner))
    }
}

We must adapt it because implementing TryDrop doesn't also implement Drop with your type. This is due to the orphan rules and the fact that Drop can only be implemented for structs, enums, and unions, but not generics. 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 { /* fields */ }

impl TryDropStrategy for Strategy {
    fn handle_error(&self, error: try_drop::Error) {
        // handle the error here
    }
}

...then install either for this thread,

try_drop::install_thread_local_handlers(Strategy, /* other strategy, use the `PanicDropStrategy` if you don't know */)

...install it globally (meaning if no thread local strategies are set, use this),

try_drop::install_global_handlers(Strategy, /* other strategy */)

...or, if possible, install it for a structure.

struct Sample<D = ShimPrimaryHandler, DD = ShimFallbackHandler>
where
    D: FallibleTryDropStrategy,
    DD: TryDropStrategy,
{
    primary: D,
    fallback: DD,
    /* other fields */
}

impl<D, DD> Sample<D, DD>
where
    D: FallibleTryDropStrategy,
    DD: TryDropStrategy,
{
    pub fn new_with(/* other arguments */ primary: D, fallback: DD) -> Self {
        Self {
            // filled arguments
            primary,
            fallback,
        }
    }
}

let sample = Sample::new_with(Strategy, /* other strategy */)

License

This project is licensed under the MIT License.

try-drop's People

Contributors

alinuxperson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.