From b3a59407812b94a13fd83bf7fec36aac526359a1 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Sun, 23 Oct 2022 13:39:58 -0400 Subject: Updated docs --- src/exun.rs | 28 ++++++++++----------- src/lib.rs | 2 +- src/result.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/unexpected.rs | 6 ++--- 4 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/exun.rs b/src/exun.rs index c54f9be..9e9e1f2 100644 --- a/src/exun.rs +++ b/src/exun.rs @@ -12,7 +12,7 @@ pub use Exun::{Expected, Unexpected}; /// `Expect` is a type that represents either the expected error type /// ([`Expected`]) or an unexpected type ([`Unexpected`]). /// -/// See the [crate documentation](self) for details. +/// See the [crate documentation](crate) for details. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum Exun { /// Contains the expected type @@ -82,10 +82,10 @@ impl Exun { /// ``` /// use exun::*; /// - /// let x: Expect = Expected(2); + /// let x: Exun = Expected(2); /// assert_eq!(x.expected(), Some(2)); /// - /// let x: Expect = Unexpected("Nothing here"); + /// let x: Exun = Unexpected("Nothing here"); /// assert_eq!(x.expected(), None); /// ``` #[allow(clippy::missing_const_for_fn)] @@ -108,10 +108,10 @@ impl Exun { /// ``` /// use exun::*; /// - /// let x: Expect = Expected(2); + /// let x: Exun = Expected(2); /// assert_eq!(x.unexpected(), None); /// - /// let x: Expect = Unexpected("Nothing here"); + /// let x: Exun = Unexpected("Nothing here"); /// assert_eq!(x.unexpected(), Some("Nothing here")); /// ``` #[allow(clippy::missing_const_for_fn)] @@ -131,7 +131,7 @@ impl Exun { /// ``` /// use exun::*; /// - /// fn mutate(r: &mut Expect) { + /// fn mutate(r: &mut Exun) { /// match r.as_mut() { /// Expected(e) => *e = 42, /// Unexpected(u) => *u = 0, @@ -166,10 +166,10 @@ impl Exun { /// ``` /// use exun::*; /// - /// let x: Expect = Expected(2); + /// let x: Exun = Expected(2); /// assert_eq!(x.map(|i| i * 10), Expected(20)); /// - /// let x: Expect = Unexpected("unexpected"); + /// let x: Exun = Unexpected("unexpected"); /// assert_eq!(x.map(|i| i * 10), Unexpected("unexpected")); /// ``` pub fn map T>(self, op: F) -> Exun { @@ -195,10 +195,10 @@ impl Exun { /// /// fn stringify(x: u32) -> String { format!("error code: {x}") } /// - /// let x: Expect = Expected(2); + /// let x: Exun = Expected(2); /// assert_eq!(x.map_unexpected(stringify), Expected(2)); /// - /// let x: Expect = Unexpected(13); + /// let x: Exun = Unexpected(13); /// assert_eq!(x.map_unexpected(stringify), Unexpected("error code: 13".to_string())); /// ``` pub fn map_unexpected T>(self, op: F) -> Exun { @@ -226,14 +226,14 @@ impl Exun { /// ``` /// use exun::*; /// - /// let x: Expect = Expected(2); + /// let x: Exun = Expected(2); /// assert_eq!(x.unwrap(), 2); /// ``` /// /// ```should_panic /// use exun::*; /// - /// let x: Expect = Unexpected("emergency failure"); + /// let x: Exun = Unexpected("emergency failure"); /// x.unwrap(); // panics with `emergency failure` /// ``` pub fn unwrap(self) -> E @@ -260,14 +260,14 @@ impl Exun { /// ```should_panic /// use exun::*; /// - /// let x: Expect = Expected(2); + /// let x: Exun = Expected(2); /// x.unwrap_unexpected(); // panics wirh `2` /// ``` /// /// ``` /// use exun::*; /// - /// let x: Expect = Unexpected("emergency failure"); + /// let x: Exun = Unexpected("emergency failure"); /// assert_eq!(x.unwrap_unexpected(), "emergency failure"); /// ``` pub fn unwrap_unexpected(self) -> U diff --git a/src/lib.rs b/src/lib.rs index 38b9067..c7eef2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ mod result; #[cfg(feature = "alloc")] mod unexpected; -pub use exun::{Expected, Exun, Unexpected}; +pub use crate::exun::{Expected, Exun, Unexpected}; #[cfg(feature = "std")] pub use result::ResultErrorExt; #[cfg(feature = "alloc")] diff --git a/src/result.rs b/src/result.rs index 9f1bb89..2b8921c 100644 --- a/src/result.rs +++ b/src/result.rs @@ -13,6 +13,44 @@ use sealed::Sealed; #[cfg(feature = "std")] pub trait ResultErrorExt: Sealed { /// Converts `Result` to `Result`. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// use exun::*; + /// use core::fmt::Error; + /// + /// let res: Result = Err(Error); + /// let res: Result = res.unexpect(); + /// ``` + /// + /// Use with the try operator + /// + /// ``` + /// use exun::*; + /// use core::fmt::Error; + /// + /// fn foo() -> Result { + /// let res: Result = Err(Error); + /// Ok(res.unexpect()?) + /// } + /// ``` + /// + /// Use with the try operator and [`Exun`] + /// + /// ``` + /// use exun::*; + /// use core::fmt::Error; + /// + /// fn foo() -> Result> { + /// let res: Result = Err(Error); + /// Ok(res.unexpect()?) + /// } + /// ``` + /// + /// [`Exun`]: `crate::Exun` #[allow(clippy::missing_errors_doc)] fn unexpect(self) -> Result; } @@ -29,6 +67,41 @@ pub trait ResultMsgExt: Sealed { /// /// This is provided for compatibility with `no_std`. If your type /// implements [`Error`], then you should prefer that instead. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// use exun::*; + /// + /// let res: Result = Err("failure"); + /// let res: Result = res.unexpect_msg(); + /// ``` + /// + /// Use with the try operator + /// + /// ``` + /// use exun::*; + /// + /// fn foo() -> Result { + /// let res: Result = Err("failure"); + /// Ok(res.unexpect_msg()?) + /// } + /// ``` + /// + /// Use with the try operator and [`Exun`] + /// + /// ``` + /// use exun::*; + /// + /// fn foo() -> Result> { + /// let res: Result = Err("failure"); + /// Ok(res.unexpect_msg()?) + /// } + /// ``` + /// + /// [`Exun`]: `crate::Exun` #[allow(clippy::missing_errors_doc)] fn unexpect_msg(self) -> Result; } diff --git a/src/unexpected.rs b/src/unexpected.rs index 01ec82f..17e024d 100644 --- a/src/unexpected.rs +++ b/src/unexpected.rs @@ -78,7 +78,7 @@ impl RawUnexpected { /// ``` /// use exun::*; /// - /// let x = UnexpectedError::msg("failed"); + /// let x = RawUnexpected::msg("failed"); /// ``` pub fn msg(e: E) -> Self { Self { @@ -99,10 +99,10 @@ impl RawUnexpected { /// use exun::*; /// /// let x = RawUnexpected::new(core::fmt::Error); - /// assert!(err.source().is_some()); + /// assert!(x.source().is_some()); /// /// let x = RawUnexpected::msg("failed"); - /// assert!(err.source().is_none()); + /// assert!(x.source().is_none()); /// ``` #[must_use] pub fn source(&self) -> Option<&(dyn Error + 'static)> { -- cgit v1.2.3