File: README.md

package info (click to toggle)
rust-fehler 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: makefile: 4
file content (89 lines) | stat: -rw-r--r-- 2,513 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Der Fehler

[![Documentation](https://docs.rs/fehler/badge.svg)](https://docs.rs/fehler/*/fehler/)

Der Fehler is a library to add 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.

Der Fehler provides these items:

### 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, `return`s (including the implicit final return) are
automatically "Ok-wrapped." To raise errors, use `?` or the `throws!` macro.

For example, these two functions are equivalent:

```rust
#[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:

```rust
// This function returns `Option<i32>`
#[throws(as Option)]
fn foo(x: bool) -> i32 {
    if x {
        return 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.

# TODO

* Make throws work on closures and async blocks (attributes are not allowed on
  expressions on stable)
* Make throws work on Try types other than Result and Option (TRy is not on
  stable).

## License

Licensed under either of

 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

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.