#![warn(clippy::all, clippy::nursery, clippy::pedantic, rust_2018_idioms)]
#![deny(
clippy::pedantic,
clippy::float_cmp_const,
clippy::indexing_slicing,
clippy::integer_arithmetic,
clippy::unwrap_used
)]
#![allow(
clippy::implicit_return,
clippy::iter_nth_zero,
clippy::match_bool,
clippy::missing_errors_doc,
clippy::module_name_repetitions
)]
#![forbid(unsafe_code)]
#![forbid(bare_trait_objects)]
#![allow(clippy::blanket_clippy_restriction_lints)]
#![warn(clippy::cargo, clippy::restriction, missing_docs, warnings)]
#![allow(clippy::implicit_return, clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(feature = "std"), no_std)]
use core::ops::Not;
pub trait BoolExt {
#[allow(clippy::wrong_self_convention)]
fn to_option(self) -> Option<()>;
fn some<T>(self, some: T) -> Option<T>;
fn some_with<F: FnOnce() -> T, T>(self, some: F) -> Option<T>;
#[allow(clippy::wrong_self_convention, clippy::result_unit_err)]
fn to_result(self) -> Result<(), ()>;
#[allow(clippy::result_unit_err)]
fn ok<T>(self, ok: T) -> Result<T, ()>;
#[allow(clippy::result_unit_err)]
fn ok_with<F: FnOnce() -> T, T>(self, ok: F) -> Result<T, ()>;
fn err<E>(self, err: E) -> Result<(), E>;
#[allow(clippy::result_unit_err)]
fn err_with<F: FnOnce() -> E, E>(self, err: F) -> Result<(), E>;
fn ok_or_err<T, E>(self, err: E, ok: T) -> Result<T, E>;
fn ok_or_err_with<F: FnOnce() -> T, G: FnOnce() -> E, T, E>(
self,
err: G,
ok: F,
) -> Result<T, E>;
fn map<T>(self, f: T, t: T) -> T;
fn map_or<F: FnOnce() -> T, T>(self, f: T, t: F) -> T;
fn map_or_default<F: FnOnce() -> T, T: Default>(self, t: F) -> T;
fn map_or_else<F: FnOnce() -> T, G: FnOnce() -> T, T>(self, f: G, t: F) -> T;
fn and_do<F: FnOnce()>(self, t: F) -> bool;
fn or_do<F: FnOnce()>(self, f: F) -> bool;
fn and_try_do<F: FnOnce() -> Result<(), E>, E>(self, t: F) -> Result<bool, E>;
fn or_try_do<F: FnOnce() -> Result<(), E>, E>(self, f: F) -> Result<bool, E>;
fn expect(self, msg: &str);
fn expect_false(self, msg: &str);
}
#[allow(clippy::use_self)]
impl BoolExt for bool {
#[inline]
fn to_option(self) -> Option<()> {
match self {
true => Some(()),
false => None,
}
}
#[inline]
fn some<T>(self, some: T) -> Option<T> {
match self {
true => Some(some),
false => None,
}
}
#[inline]
fn some_with<F: FnOnce() -> T, T>(self, some: F) -> Option<T> {
match self {
true => Some(some()),
false => None,
}
}
#[inline]
fn to_result(self) -> Result<(), ()> {
match self {
true => Ok(()),
false => Err(()),
}
}
#[inline]
fn ok<T>(self, ok: T) -> Result<T, ()> {
match self {
true => Ok(ok),
false => Err(()),
}
}
#[inline]
fn ok_with<F: FnOnce() -> T, T>(self, ok: F) -> Result<T, ()> {
match self {
true => Ok(ok()),
false => Err(()),
}
}
#[inline]
fn err<E>(self, err: E) -> Result<(), E> {
match self {
true => Ok(()),
false => Err(err),
}
}
#[inline]
fn err_with<F: FnOnce() -> E, E>(self, err: F) -> Result<(), E> {
match self {
true => Ok(()),
false => Err(err()),
}
}
#[inline]
fn ok_or_err<T, E>(self, err: E, ok: T) -> Result<T, E> {
match self {
true => Ok(ok),
false => Err(err),
}
}
#[inline]
fn ok_or_err_with<F: FnOnce() -> T, G: FnOnce() -> E, T, E>(
self,
err: G,
ok: F,
) -> Result<T, E> {
match self {
true => Ok(ok()),
false => Err(err()),
}
}
#[inline]
fn map<T>(self, f: T, t: T) -> T {
match self {
true => t,
false => f,
}
}
#[inline]
fn map_or<F: FnOnce() -> T, T>(self, f: T, t: F) -> T {
match self {
true => t(),
false => f,
}
}
#[inline]
fn map_or_default<F: FnOnce() -> T, T: Default>(self, t: F) -> T {
self.map_or(T::default(), t)
}
#[inline]
fn map_or_else<F: FnOnce() -> T, G: FnOnce() -> T, T>(self, f: G, t: F) -> T {
match self {
true => t(),
false => f(),
}
}
#[inline]
fn and_do<F: FnOnce()>(self, t: F) -> bool {
match self {
true => t(),
false => (),
}
self
}
#[inline]
fn or_do<F: FnOnce()>(self, f: F) -> bool {
!(!self).and_do(f)
}
#[inline]
fn and_try_do<F: FnOnce() -> Result<(), E>, E>(self, t: F) -> Result<bool, E> {
match self {
true => {
t()?;
Ok(self)
}
false => Ok(self),
}
}
#[inline]
fn or_try_do<F: FnOnce() -> Result<(), E>, E>(self, f: F) -> Result<bool, E> {
(!self).and_try_do(f).map(|_| self)
}
#[inline]
fn expect(self, msg: &str) {
#[allow(clippy::panic)]
match self {
true => (),
false => panic!("{}", msg),
}
}
#[inline]
fn expect_false(self, msg: &str) {
self.not().expect(msg)
}
}