stefans-utils 0.10.1

A collection of useful Rust utility functions, types, and traits.
Documentation
use crate::map::Map;
use core::fmt;

impl Map for bool {
    fn ok(self) -> Result<Self, Self> {
        if self { Ok(true) } else { Err(false) }
    }

    fn ok_or<E>(self, err: E) -> Result<Self, E> {
        if self { Ok(true) } else { Err(err) }
    }

    fn map<T>(self, f: impl FnOnce() -> T) -> Option<T> {
        if self { Some(f()) } else { None }
    }

    fn and_then<T>(self, f: impl FnOnce() -> Option<T>) -> Option<T> {
        if self { f() } else { None }
    }

    fn unwrap(self) -> Self {
        self.expect("Expected 'true', got 'false'")
    }

    fn expect(self, message: impl fmt::Display) -> Self {
        if self { self } else { panic!("{message}") }
    }
}