Skip to content

Commit 8a90f93

Browse files
authored
Merge pull request Nullus157#7 from Nemo157/nightly-fix
Fix nightly, improve docs
2 parents 9ae83c7 + e2c105f commit 8a90f93

File tree

2 files changed

+100
-90
lines changed

2 files changed

+100
-90
lines changed

macros/src/throws.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ impl Fold for Throws {
111111
return i;
112112
}
113113
let return_type = self.args.ret(i);
114-
let syn::ReturnType::Type(_, ty) = &return_type else { unreachable!() };
114+
let syn::ReturnType::Type(_, ty) = &return_type else {
115+
unreachable!()
116+
};
115117
struct ImplTraitToInfer;
116118
impl Fold for ImplTraitToInfer {
117119
fn fold_type(&mut self, i: syn::Type) -> syn::Type {
@@ -139,10 +141,13 @@ impl Fold for Throws {
139141

140142
fn make_fn_block(ty: &syn::Type, inner: &syn::Block) -> syn::Block {
141143
let mut block: syn::Block = syn::parse2(quote::quote! {{
142-
let __ret = #inner;
144+
#[allow(clippy::diverging_sub_expression)]
145+
{
146+
let __ret = { #inner };
143147

144-
#[allow(unreachable_code)]
145-
<#ty as ::culpa::__internal::_Succeed>::from_ok(__ret)
148+
#[allow(unreachable_code)]
149+
<#ty as ::culpa::__internal::_Succeed>::from_ok(__ret)
150+
}
146151
}})
147152
.unwrap();
148153
block.brace_token = inner.brace_token;

src/lib.rs

Lines changed: 91 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,97 @@
11
#![no_std]
2+
3+
//! Annotations a function that "throws" a Result.
4+
//!
5+
//! Inside functions tagged with `throws`, you can use `?` and the `throw!` macro to return errors,
6+
//! but you don't need to wrap the successful return values in `Ok`.
7+
//!
8+
//! Using this syntax, you can write fallible functions almost as if they were nonfallible. Every
9+
//! time a function call would return a `Result`, you "re-raise" the error using `?`, and if you
10+
//! wish to raise your own error, you can return it with the `throw!` macro.
11+
//!
12+
//! ## Example
13+
//! ```
14+
//! use std::io::{self, Read};
15+
//!
16+
//! use culpa::{throw, throws};
17+
//!
18+
//! #[throws(io::Error)]
19+
//! fn check() {
20+
//! let mut file = std::fs::File::open("The_House_of_the_Spirits.txt")?;
21+
//! let mut text = String::new();
22+
//! file.read_to_string(&mut text)?;
23+
//!
24+
//! if !text.starts_with("Barrabas came to us by sea, the child Clara wrote") {
25+
//! throw!(io::Error::from_raw_os_error(22));
26+
//! }
27+
//!
28+
//! println!("Okay!");
29+
//! }
30+
//! ```
31+
//!
32+
//! # Default Error Type
33+
//!
34+
//! This macro supports a "default error type" - if you do not pass a type to the macro, it will
35+
//! use the type named `Error` in this scope. So if you have defined an error type in this
36+
//! module, that will be the error thrown by this function.
37+
//!
38+
//! You can access this feature by omitting the arguments entirely or by passing `_` as the type.
39+
//!
40+
//! ## Example
41+
//!
42+
//! ```
43+
//! use culpa::throws;
44+
//!
45+
//! // Set the default error type for this module:
46+
//! type Error = std::io::Error;
47+
//!
48+
//! #[throws]
49+
//! fn print() {
50+
//! let file = std::fs::read_to_string("my_file.txt")?;
51+
//! println!("{}", file);
52+
//! }
53+
//! ```
54+
//!
55+
//! # Throwing as an Option
56+
//!
57+
//! This syntax can also support functions which return an `Option` instead of a `Result`. The
58+
//! way to access this is to pass `as Option` as the argument to `throw`.
59+
//!
60+
//! In functions that return `Option`, you can use the `throw!()` macro without any argument to
61+
//! return `None`.
62+
//!
63+
//! ## Example
64+
//!
65+
//! ```
66+
//! use culpa::{throw, throws};
67+
//!
68+
//! #[throws(as Option)]
69+
//! fn example<T: Eq + Ord>(slice: &[T], needle: &T) -> usize {
70+
//! if !slice.contains(needle) {
71+
//! throw!();
72+
//! }
73+
//! slice.binary_search(needle).ok()?
74+
//! }
75+
//! ```
76+
//!
77+
//! # Other `Try` types
78+
//!
79+
//! The `?` syntax in Rust is controlled by a trait called `Try`, which is currently unstable.
80+
//! Because this feature is unstable and I don't want to maintain compatibility if its interface
81+
//! changes, this crate currently only works with two stable `Try` types: Result and Option.
82+
//! However, its designed so that it will hopefully support other `Try` types as well in the
83+
//! future.
84+
//!
85+
//! It's worth noting that `Try` also has some other stable implementations: specifically `Poll`.
86+
//! Because of the somewhat unusual implementation of `Try` for those types, this crate does not
87+
//! support `throws` syntax on functions that return `Poll` (so you can't use this syntax when
88+
//! implementing a Future by hand, for example). I hope to come up with a way to support Poll in
89+
//! the future.
90+
291
#[doc(inline)]
3-
/// Annotations a function that "throws" a Result.
4-
///
5-
/// Inside functions tagged with `throws`, you can use `?` and the `throw!` macro to return errors,
6-
/// but you don't need to wrap the successful return values in `Ok`.
7-
///
8-
/// Using this syntax, you can write fallible functions almost as if they were nonfallible. Every
9-
/// time a function call would return a `Result`, you "re-raise" the error using `?`, and if you
10-
/// wish to raise your own error, you can return it with the `throw!` macro.
11-
///
12-
/// ## Example
13-
/// ```should_panic
14-
/// use std::io::{self, Read};
15-
///
16-
/// use culpa::{throw, throws};
17-
///
18-
/// #[throws(io::Error)]
19-
/// fn main() {
20-
/// let mut file = std::fs::File::open("The_House_of_the_Spirits.txt")?;
21-
/// let mut text = String::new();
22-
/// file.read_to_string(&mut text)?;
23-
///
24-
/// if !text.starts_with("Barrabas came to us by sea, the child Clara wrote") {
25-
/// throw!(io::Error::from_raw_os_error(22));
26-
/// }
27-
///
28-
/// println!("Okay!");
29-
/// }
30-
/// ```
31-
///
32-
/// # Default Error Type
33-
///
34-
/// This macro supports a "default error type" - if you do not pass a type to the macro, it will
35-
/// use the type named `Error` in this scope. So if you have defined an error type in this
36-
/// module, that will be the error thrown by this function.
37-
///
38-
/// You can access this feature by omitting the arguments entirely or by passing `_` as the type.
39-
///
40-
/// ## Example
41-
///
42-
/// ```should_panic
43-
/// use culpa::throws;
44-
///
45-
/// // Set the default error type for this module:
46-
/// type Error = std::io::Error;
47-
///
48-
/// #[throws]
49-
/// fn main() {
50-
/// let file = std::fs::read_to_string("my_file.txt")?;
51-
/// println!("{}", file);
52-
/// }
53-
/// ```
54-
///
55-
/// # Throwing as an Option
56-
///
57-
/// This syntax can also support functions which return an `Option` instead of a `Result`. The
58-
/// way to access this is to pass `as Option` as the argument to `throw`.
59-
///
60-
/// In functions that return `Option`, you can use the `throw!()` macro without any argument to
61-
/// return `None`.
62-
///
63-
/// ## Example
64-
///
65-
/// ```
66-
/// use culpa::{throw, throws};
67-
///
68-
/// #[throws(as Option)]
69-
/// fn example<T: Eq + Ord>(slice: &[T], needle: &T) -> usize {
70-
/// if !slice.contains(needle) {
71-
/// throw!();
72-
/// }
73-
/// slice.binary_search(needle).ok()?
74-
/// }
75-
/// ```
76-
///
77-
/// # Other `Try` types
78-
///
79-
/// The `?` syntax in Rust is controlled by a trait called `Try`, which is currently unstable.
80-
/// Because this feature is unstable and I don't want to maintain compatibility if its interface
81-
/// changes, this crate currently only works with two stable `Try` types: Result and Option.
82-
/// However, its designed so that it will hopefully support other `Try` types as well in the
83-
/// future.
92+
/// Annotates a function that "throws" a Result.
8493
///
85-
/// It's worth noting that `Try` also has some other stable implementations: specifically `Poll`.
86-
/// Because of the somewhat unusual implementation of `Try` for those types, this crate does not
87-
/// support `throws` syntax on functions that return `Poll` (so you can't use this syntax when
88-
/// implementing a Future by hand, for example). I hope to come up with a way to support Poll in
89-
/// the future.
94+
/// See the main crate docs for more details.
9095
pub use culpa_macros::throws;
9196

9297
/// Throw an error.

0 commit comments

Comments
 (0)