pub trait Truthy {
fn truthy(&self) -> bool;
#[inline]
fn falsy(&self) -> bool {
!self.truthy()
}
fn or(self, default: Self) -> Self
where
Self: Sized,
{
if self.truthy() { self } else { default }
}
fn or_eq(&mut self, default: Self)
where
Self: Sized,
{
if self.falsy() {
*self = default;
}
}
fn or_else<F>(self, f: F) -> Self
where
Self: Sized,
F: FnOnce() -> Self,
{
if self.truthy() { self } else { f() }
}
fn or_else_eq<F>(&mut self, f: F)
where
Self: Sized,
F: FnOnce() -> Self,
{
if self.falsy() {
*self = f();
}
}
fn and(self, replacement: Self) -> Self
where
Self: Sized,
{
if self.falsy() { self } else { replacement }
}
fn and_eq(&mut self, replacement: Self)
where
Self: Sized,
{
if self.truthy() {
*self = replacement;
}
}
fn and_then<F>(self, f: F) -> Self
where
Self: Sized,
F: FnOnce(Self) -> Self,
{
if self.falsy() { self } else { f(self) }
}
fn and_then_eq<F>(&mut self, f: F)
where
Self: Sized,
F: FnOnce(&Self) -> Self,
{
if self.truthy() {
*self = f(self);
}
}
}
macro_rules! impl_truthy_num {
($type:ident) => {
impl $crate::Truthy for $type {
#[inline]
fn truthy(&self) -> bool {
const FALSY: $type = 0;
*self != FALSY
}
}
};
}
impl_truthy_num!(i8);
impl_truthy_num!(i16);
impl_truthy_num!(i32);
impl_truthy_num!(i64);
impl_truthy_num!(i128);
impl_truthy_num!(isize);
impl_truthy_num!(u8);
impl_truthy_num!(u16);
impl_truthy_num!(u32);
impl_truthy_num!(u64);
impl_truthy_num!(u128);
impl_truthy_num!(usize);
impl Truthy for f32 {
fn truthy(&self) -> bool {
!self.eq(&0f32)
}
}
impl Truthy for f64 {
fn truthy(&self) -> bool {
!self.eq(&0f64)
}
}
impl Truthy for str {
#[inline]
fn truthy(&self) -> bool {
!self.is_empty()
}
}
impl Truthy for &str {
#[inline]
fn truthy(&self) -> bool {
!self.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
mod strings {
use super::*;
#[test]
fn truthy() {
assert!("I have value!".truthy());
}
#[test]
fn falsy() {
assert!("".falsy());
}
}
mod i8 {
use super::*;
#[test]
fn truthy() {
assert!((1i8).truthy())
}
#[test]
fn falsy() {
assert!((0i8).falsy())
}
}
mod i16 {
use super::*;
#[test]
fn truthy() {
assert!((1i16).truthy())
}
#[test]
fn falsy() {
assert!((0i16).falsy())
}
}
mod i32 {
use super::*;
#[test]
fn truthy() {
assert!((1i32).truthy())
}
#[test]
fn falsy() {
assert!((0i32).falsy())
}
}
mod i64 {
use super::*;
#[test]
fn truthy() {
assert!((1i64).truthy())
}
#[test]
fn falsy() {
assert!((0i64).falsy())
}
}
mod i128 {
use super::*;
#[test]
fn truthy() {
assert!((1i128).truthy())
}
#[test]
fn falsy() {
assert!((0i128).falsy())
}
}
mod isize {
use super::*;
#[test]
fn truthy() {
assert!((1isize).truthy())
}
#[test]
fn falsy() {
assert!((0isize).falsy())
}
}
mod u8 {
use super::*;
#[test]
fn truthy() {
assert!((1u8).truthy())
}
#[test]
fn falsy() {
assert!((0u8).falsy())
}
}
mod u16 {
use super::*;
#[test]
fn truthy() {
assert!((1u16).truthy())
}
#[test]
fn falsy() {
assert!((0u16).falsy())
}
}
mod u32 {
use super::*;
#[test]
fn truthy() {
assert!((1u32).truthy())
}
#[test]
fn falsy() {
assert!((0u32).falsy())
}
}
mod u64 {
use super::*;
#[test]
fn truthy() {
assert!((1u64).truthy())
}
#[test]
fn falsy() {
assert!((0u64).falsy())
}
}
mod u128 {
use super::*;
#[test]
fn truthy() {
assert!((1u128).truthy())
}
#[test]
fn falsy() {
assert!((0u128).falsy())
}
}
mod usize {
use super::*;
#[test]
fn truthy() {
assert!((1usize).truthy())
}
#[test]
fn falsy() {
assert!((0usize).falsy())
}
}
mod f32 {
use super::*;
#[test]
fn truthy() {
assert!((1.0f32).truthy())
}
#[test]
fn falsy() {
assert!(!(0.0f32).truthy())
}
}
mod f64 {
use super::*;
#[test]
fn truthy() {
assert!((1.0f64).truthy())
}
#[test]
fn falsy() {
assert!(!(0.0f64).truthy())
}
}
#[test]
fn test_or() {
assert_eq!("default", "".or("default"));
assert_eq!("original", "original".or("default"));
}
#[test]
fn test_or_eq() {
let mut a = "";
let mut b = "original";
a.or_eq("default");
b.or_eq("default");
assert_eq!("default", a);
assert_eq!("original", b);
}
#[test]
fn test_or_else() {
assert_eq!("default", "".or_else(|| "default"));
assert_eq!("original", "original".or_else(|| "default"));
}
#[test]
fn test_or_else_eq() {
let mut a = "";
let mut b = "original";
a.or_else_eq(|| "default");
b.or_else_eq(|| "default");
assert_eq!("default", a);
assert_eq!("original", b);
}
#[test]
fn test_and() {
assert_eq!(0, 0u8.and(2));
assert_eq!(2, 1u8.and(2));
}
#[test]
fn test_and_eq() {
let mut a = 0u8;
let mut b = 1u8;
a.and_eq(2);
b.and_eq(2);
assert_eq!(0, a);
assert_eq!(2, b);
}
#[test]
fn test_and_then() {
assert_eq!(0, 0u8.and_then(|n| n - 1));
assert_eq!(1, 2u8.and_then(|n| n - 1));
}
#[test]
fn test_and_then_eq() {
let mut a = 0u8;
let mut b = 2u8;
a.and_then_eq(|n| *n - 1);
b.and_then_eq(|n| *n - 1);
assert_eq!(0, a);
assert_eq!(1, b);
}
}