pub trait Truthy {
fn is_truthy(&self) -> bool;
}
impl Truthy for bool {
fn is_truthy(&self) -> bool {
*self
}
}
impl Truthy for str {
fn is_truthy(&self) -> bool {
!self.is_empty() && self != "0"
}
}
impl Truthy for String {
fn is_truthy(&self) -> bool {
self.as_str().is_truthy()
}
}
impl<T> Truthy for Vec<T> {
fn is_truthy(&self) -> bool {
!self.is_empty()
}
}
impl<K, V> Truthy for std::collections::HashMap<K, V> {
fn is_truthy(&self) -> bool {
!self.is_empty()
}
}
impl<T> Truthy for Option<T> {
fn is_truthy(&self) -> bool {
self.is_some()
}
}
impl<T: Truthy + ?Sized> Truthy for &T {
fn is_truthy(&self) -> bool {
(**self).is_truthy()
}
}
macro_rules! impl_truthy_for_number {
($($t:ty),*) => {
$(
impl Truthy for $t {
fn is_truthy(&self) -> bool {
*self != 0 as $t
}
}
)*
};
}
impl_truthy_for_number!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64);
pub fn truthy<T: Truthy + ?Sized>(value: &T) -> bool {
value.is_truthy()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bools_pass_through_unchanged() {
assert!(truthy(&true));
assert!(!truthy(&false));
}
#[test]
fn empty_and_zero_strings_are_falsy() {
assert!(!truthy(&String::new()));
assert!(!truthy(""));
assert!(!truthy("0"));
}
#[test]
fn a_non_empty_non_zero_string_is_truthy() {
assert!(truthy("hello"));
assert!(truthy(&"search-query".to_string()));
assert!(truthy("00"));
}
#[test]
fn empty_collections_are_falsy() {
assert!(!truthy(&Vec::<i32>::new()));
assert!(!truthy(&std::collections::HashMap::<String, i32>::new()));
}
#[test]
fn non_empty_collections_are_truthy() {
assert!(truthy(&vec![1, 2, 3]));
let mut map = std::collections::HashMap::new();
map.insert("a", 1);
assert!(truthy(&map));
}
#[test]
fn none_is_falsy_and_some_is_truthy() {
assert!(!truthy(&Option::<i32>::None));
assert!(truthy(&Some(0)));
}
#[test]
fn a_reference_defers_to_its_referent() {
let owned = "search-query".to_string();
let borrowed: &str = owned.as_str();
assert!(truthy(&borrowed));
let empty: &str = "";
assert!(!truthy(&empty));
let n = 5i64;
let borrowed_n: &i64 = &n;
assert!(truthy(&borrowed_n));
}
#[test]
fn zero_numbers_are_falsy() {
assert!(!truthy(&0i64));
assert!(!truthy(&0.0f64));
assert!(truthy(&1i64));
assert!(truthy(&-1i64));
}
}