diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 101 |
1 files changed, 61 insertions, 40 deletions
@@ -2,7 +2,7 @@ //! //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust -//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K +//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs //! //! <br> //! @@ -210,14 +210,15 @@ //! will require an explicit `.map_err(Error::msg)` when working with a //! non-Anyhow error type inside a function that returns Anyhow's error type. -#![doc(html_root_url = "https://docs.rs/anyhow/1.0.44")] -#![cfg_attr(backtrace, feature(backtrace))] +#![doc(html_root_url = "https://docs.rs/anyhow/1.0.69")] +#![cfg_attr(backtrace, feature(error_generic_member_access, provide_any))] #![cfg_attr(doc_cfg, feature(doc_cfg))] #![cfg_attr(not(feature = "std"), no_std)] #![deny(dead_code, unused_imports, unused_mut)] #![allow( clippy::doc_markdown, clippy::enum_glob_use, + clippy::explicit_auto_deref, clippy::missing_errors_doc, clippy::missing_panics_doc, clippy::module_name_repetitions, @@ -225,27 +226,20 @@ clippy::needless_doctest_main, clippy::new_ret_no_self, clippy::redundant_else, + clippy::return_self_not_must_use, clippy::unused_self, clippy::used_underscore_binding, clippy::wildcard_imports, clippy::wrong_self_convention )] -mod alloc { - #[cfg(not(feature = "std"))] - extern crate alloc; - - #[cfg(not(feature = "std"))] - pub use alloc::boxed::Box; - - #[cfg(feature = "std")] - pub use std::boxed::Box; -} +extern crate alloc; #[macro_use] mod backtrace; mod chain; mod context; +mod ensure; mod error; mod fmt; mod kind; @@ -373,7 +367,7 @@ pub use anyhow as format_err; /// # Ok(()) /// } /// ``` -#[repr(transparent)] +#[cfg_attr(not(doc), repr(transparent))] pub struct Error { inner: Own<ErrorImpl>, } @@ -503,6 +497,11 @@ pub type Result<T, E = Error> = core::result::Result<T, E>; /// No such file or directory (os error 2) /// ``` /// +/// Refer to the [Display representations] documentation for other forms in +/// which this context chain can be rendered. +/// +/// [Display representations]: Error#display-representations +/// /// <br> /// /// # Effect on downcasting @@ -610,13 +609,40 @@ pub trait Context<T, E>: context::private::Sealed { F: FnOnce() -> C; } +/// Equivalent to Ok::<_, anyhow::Error>(value). +/// +/// This simplifies creation of an anyhow::Result in places where type inference +/// cannot deduce the `E` type of the result — without needing to write +/// `Ok::<_, anyhow::Error>(value)`. +/// +/// One might think that `anyhow::Result::Ok(value)` would work in such cases +/// but it does not. +/// +/// ```console +/// error[E0282]: type annotations needed for `std::result::Result<i32, E>` +/// --> src/main.rs:11:13 +/// | +/// 11 | let _ = anyhow::Result::Ok(1); +/// | - ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result` +/// | | +/// | consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified +/// ``` +#[allow(non_snake_case)] +pub fn Ok<T>(t: T) -> Result<T> { + Result::Ok(t) +} + // Not public API. Referenced by macro-generated code. #[doc(hidden)] -pub mod private { +pub mod __private { use crate::Error; - use core::fmt::{Debug, Display}; + use alloc::fmt; + use core::fmt::Arguments; + pub use crate::ensure::{BothDebug, NotBothDebug}; + pub use alloc::format; pub use core::result::Result::Err; + pub use core::{concat, format_args, stringify}; #[doc(hidden)] pub mod kind { @@ -626,34 +652,29 @@ pub mod private { pub use crate::kind::BoxedKind; } + #[doc(hidden)] + #[inline] #[cold] - pub fn new_adhoc<M>(message: M) -> Error - where - M: Display + Debug + Send + Sync + 'static, - { - Error::from_adhoc(message, backtrace!()) - } - - #[cfg(anyhow_no_macro_reexport)] - pub use crate::{__anyhow_concat as concat, __anyhow_stringify as stringify}; - #[cfg(not(anyhow_no_macro_reexport))] - pub use core::{concat, stringify}; + pub fn format_err(args: Arguments) -> Error { + #[cfg(anyhow_no_fmt_arguments_as_str)] + let fmt_arguments_as_str = None::<&str>; + #[cfg(not(anyhow_no_fmt_arguments_as_str))] + let fmt_arguments_as_str = args.as_str(); - #[cfg(anyhow_no_macro_reexport)] - #[doc(hidden)] - #[macro_export] - macro_rules! __anyhow_concat { - ($($tt:tt)*) => { - concat!($($tt)*) - }; + if let Some(message) = fmt_arguments_as_str { + // anyhow!("literal"), can downcast to &'static str + Error::msg(message) + } else { + // anyhow!("interpolate {var}"), can downcast to String + Error::msg(fmt::format(args)) + } } - #[cfg(anyhow_no_macro_reexport)] #[doc(hidden)] - #[macro_export] - macro_rules! __anyhow_stringify { - ($($tt:tt)*) => { - stringify!($($tt)*) - }; + #[inline] + #[cold] + #[must_use] + pub fn must_use(error: Error) -> Error { + error } } |