summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-23 13:39:58 -0400
committerBotahamec <botahamec@outlook.com>2022-10-23 13:39:58 -0400
commitb3a59407812b94a13fd83bf7fec36aac526359a1 (patch)
tree27f9bf78f3a7c917bd2fa3db99b25d03881eecef /src
parentf4b5e2afc59a30687a0630504e233ecb63339783 (diff)
Updated docs
Diffstat (limited to 'src')
-rw-r--r--src/exun.rs28
-rw-r--r--src/lib.rs2
-rw-r--r--src/result.rs73
-rw-r--r--src/unexpected.rs6
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<E, U> {
/// Contains the expected type
@@ -82,10 +82,10 @@ impl<E, U> Exun<E, U> {
/// ```
/// use exun::*;
///
- /// let x: Expect<i32, &str> = Expected(2);
+ /// let x: Exun<i32, &str> = Expected(2);
/// assert_eq!(x.expected(), Some(2));
///
- /// let x: Expect<i32, &str> = Unexpected("Nothing here");
+ /// let x: Exun<i32, &str> = Unexpected("Nothing here");
/// assert_eq!(x.expected(), None);
/// ```
#[allow(clippy::missing_const_for_fn)]
@@ -108,10 +108,10 @@ impl<E, U> Exun<E, U> {
/// ```
/// use exun::*;
///
- /// let x: Expect<i32, &str> = Expected(2);
+ /// let x: Exun<i32, &str> = Expected(2);
/// assert_eq!(x.unexpected(), None);
///
- /// let x: Expect<i32, &str> = Unexpected("Nothing here");
+ /// let x: Exun<i32, &str> = Unexpected("Nothing here");
/// assert_eq!(x.unexpected(), Some("Nothing here"));
/// ```
#[allow(clippy::missing_const_for_fn)]
@@ -131,7 +131,7 @@ impl<E, U> Exun<E, U> {
/// ```
/// use exun::*;
///
- /// fn mutate(r: &mut Expect<i32, i32>) {
+ /// fn mutate(r: &mut Exun<i32, i32>) {
/// match r.as_mut() {
/// Expected(e) => *e = 42,
/// Unexpected(u) => *u = 0,
@@ -166,10 +166,10 @@ impl<E, U> Exun<E, U> {
/// ```
/// use exun::*;
///
- /// let x: Expect<i32, &str> = Expected(2);
+ /// let x: Exun<i32, &str> = Expected(2);
/// assert_eq!(x.map(|i| i * 10), Expected(20));
///
- /// let x: Expect<i32, &str> = Unexpected("unexpected");
+ /// let x: Exun<i32, &str> = Unexpected("unexpected");
/// assert_eq!(x.map(|i| i * 10), Unexpected("unexpected"));
/// ```
pub fn map<T, F: FnOnce(E) -> T>(self, op: F) -> Exun<T, U> {
@@ -195,10 +195,10 @@ impl<E, U> Exun<E, U> {
///
/// fn stringify(x: u32) -> String { format!("error code: {x}") }
///
- /// let x: Expect<u32, u32> = Expected(2);
+ /// let x: Exun<u32, u32> = Expected(2);
/// assert_eq!(x.map_unexpected(stringify), Expected(2));
///
- /// let x: Expect<u32, u32> = Unexpected(13);
+ /// let x: Exun<u32, u32> = Unexpected(13);
/// assert_eq!(x.map_unexpected(stringify), Unexpected("error code: 13".to_string()));
/// ```
pub fn map_unexpected<T, F: FnOnce(U) -> T>(self, op: F) -> Exun<E, T> {
@@ -226,14 +226,14 @@ impl<E, U> Exun<E, U> {
/// ```
/// use exun::*;
///
- /// let x: Expect<u32, &str> = Expected(2);
+ /// let x: Exun<u32, &str> = Expected(2);
/// assert_eq!(x.unwrap(), 2);
/// ```
///
/// ```should_panic
/// use exun::*;
///
- /// let x: Expect<u32, &str> = Unexpected("emergency failure");
+ /// let x: Exun<u32, &str> = Unexpected("emergency failure");
/// x.unwrap(); // panics with `emergency failure`
/// ```
pub fn unwrap(self) -> E
@@ -260,14 +260,14 @@ impl<E, U> Exun<E, U> {
/// ```should_panic
/// use exun::*;
///
- /// let x: Expect<u32, &str> = Expected(2);
+ /// let x: Exun<u32, &str> = Expected(2);
/// x.unwrap_unexpected(); // panics wirh `2`
/// ```
///
/// ```
/// use exun::*;
///
- /// let x: Expect<u32, &str> = Unexpected("emergency failure");
+ /// let x: Exun<u32, &str> = 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<T>: Sealed {
/// Converts `Result<T, E>` to `Result<T, RawUnexpected>`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use exun::*;
+ /// use core::fmt::Error;
+ ///
+ /// let res: Result<i32, Error> = Err(Error);
+ /// let res: Result<i32, RawUnexpected> = res.unexpect();
+ /// ```
+ ///
+ /// Use with the try operator
+ ///
+ /// ```
+ /// use exun::*;
+ /// use core::fmt::Error;
+ ///
+ /// fn foo() -> Result<i32, UnexpectedError> {
+ /// let res: Result<i32, Error> = Err(Error);
+ /// Ok(res.unexpect()?)
+ /// }
+ /// ```
+ ///
+ /// Use with the try operator and [`Exun`]
+ ///
+ /// ```
+ /// use exun::*;
+ /// use core::fmt::Error;
+ ///
+ /// fn foo() -> Result<i32, Exun<(), UnexpectedError>> {
+ /// let res: Result<i32, Error> = Err(Error);
+ /// Ok(res.unexpect()?)
+ /// }
+ /// ```
+ ///
+ /// [`Exun`]: `crate::Exun`
#[allow(clippy::missing_errors_doc)]
fn unexpect(self) -> Result<T, RawUnexpected>;
}
@@ -29,6 +67,41 @@ pub trait ResultMsgExt<T>: 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<i32, &str> = Err("failure");
+ /// let res: Result<i32, RawUnexpected> = res.unexpect_msg();
+ /// ```
+ ///
+ /// Use with the try operator
+ ///
+ /// ```
+ /// use exun::*;
+ ///
+ /// fn foo() -> Result<i32, UnexpectedError> {
+ /// let res: Result<i32, &str> = Err("failure");
+ /// Ok(res.unexpect_msg()?)
+ /// }
+ /// ```
+ ///
+ /// Use with the try operator and [`Exun`]
+ ///
+ /// ```
+ /// use exun::*;
+ ///
+ /// fn foo() -> Result<i32, Exun<(), UnexpectedError>> {
+ /// let res: Result<i32, &str> = Err("failure");
+ /// Ok(res.unexpect_msg()?)
+ /// }
+ /// ```
+ ///
+ /// [`Exun`]: `crate::Exun`
#[allow(clippy::missing_errors_doc)]
fn unexpect_msg(self) -> Result<T, RawUnexpected>;
}
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: Display + Debug + Send + Sync + 'static>(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)> {