Crates.io | culpa |
lib.rs | culpa |
version | 1.0.2 |
source | src |
created_at | 2023-06-29 21:49:14.120357 |
updated_at | 2024-01-10 22:56:10.701712 |
description | A library for error-handling syntax in Rust |
homepage | |
repository | https://github.com/Nullus157/culpa |
max_upload_size | |
id | 903779 |
size | 32,225 |
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.
#[throws]
attributeThe 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:
#[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)
}
}
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!();
}
}
throw!
macrothrow!
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.
This crate only supports the current stable version of Rust, patch releases may use new features at any time.
Licensed under either of
at your option.
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.