Code Monkey home page Code Monkey logo

culpa's Introduction

version-badge license-badge rust-version-badge

Adding support for "throwing functions" to Rust through procedural macros. Functions marked with the throws attribute return Result, but the "Ok" path is used by default and you don't need to wrap ok return values in Ok. To throw errors, use ? or the throws macro.

A fork of Der Fehler updating dependencies and fixing issues while (by my understanding) boats is unable to contribute to open source.

The #[throws] attribute

The throws attribute modifies a function or method to make it return a Result. It takes an optional typename as an argument to the attribute which will be the error type of this function; if no typename is supplied, it uses the default error type for this crate.

Within the function body, returns (including the implicit final return) are automatically "Ok-wrapped." To raise errors, use ? or the throws! macro.

For example, these two functions are equivalent:

#[throws(i32)]
fn foo(x: bool) -> i32 {
    if x {
        0
    } else {
        throw!(1);
    }
}

fn bar(x: bool) -> Result<i32, i32> {
    if x {
        Ok(0)
    } else {
        Err(1)
    }
}

In functions that return Option

The attribute can be used to make a function that returns an Option using the as Option syntax, demonstrated below:

// This function returns `Option<i32>`
#[throws(as Option)]
fn foo(x: bool) -> i32 {
    if x {
        0
    } else {
        throw!();
    }
}

The throw! macro

throw! is a macro which is equivalent to the Err($e)? pattern. It takes an error type and "throws" it.

One important aspect of the throw! macro is that it allows you to return errors inside of functions marked with throws. You cannot just return errors from these functions, you need to use this macro.

Rust Version Policy

This crate only supports the current stable version of Rust, patch releases may use new features at any time.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

culpa's People

Contributors

nemo157 avatar withoutboats avatar earthengine avatar yoshuawuyts avatar maxdeviant avatar mgeier avatar tversteeg avatar ubnt-intrepid avatar

Stargazers

Tropic avatar Berkus Decker avatar Qqwy / Marten avatar Maytham avatar Christoph Grabo avatar Dakota avatar Elias Gabriel Amaral da Silva avatar zbv avatar

Watchers

 avatar David Tolnay avatar

Forkers

nemo157

culpa's Issues

Add `#[try]` that does Ok-wrapping but doesn't remove `Result` from function signature

Fehler was a good experiment in Ok-wrapping but I think that removing Result and Option is a bit too much. This view is also reflected by withoutboats' article in The registers of Rust, where it is said:

I've written before about why I think "Ok" wrapping functions would be a very good addition to Rust, and even implemented them as well. The only way my view has evolved since then (and here I think I am now more in line with the Rust project) is that using a syntax like try fn that doesn’t change the return type, so you can specify Option or Result, is probably the right approach for Rust. But overall having a syntax that keeps the user fully inside the effect (which is what Ok-wrapping really means), rather than halfway, would be not only more convenient but also more consistent with asynchrony and iteration.

I'm not proposing changing the #[throws] macro but instead defining a new macro that does not change the return type. And maybe it should be called #[try], with the understanding that #[try] fn f() -> Result<i32> could eventually become try fn f() -> Result<i32> if Ok-wrapping ever became part of the Rust proper.

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.