weak_true

Crates.ioweak_true
lib.rsweak_true
version0.2.1
created_at2024-05-27 16:41:29.545043+00
updated_at2025-05-22 22:55:36.269244+00
descriptionSimilar to the automatic implicit conversion to boolean values in weakly typed languages
homepage
repositoryhttps://github.com/A4-Tacks/weak_true-rs
max_upload_size
id1253556
size21,054
A4-Tacks (A4-Tacks)

documentation

README

Similar to the automatic implicit conversion to boolean values in weakly typed languages

type impl
float self is not 0.0 / NaN
integer self != 0
reference / smart pointer inner value impl
raw pointer !self.is_null
Option self.is_some
Result self.is_ok
Poll self.is_ready
str / slice / array !self.is_empty
collections !self.is_empty
unit false
bool self
fn / tuple / char true

Examples

use weak_true::weak_true;

#[weak_true]
fn main() {
    let mut a = vec![-1, 0, 1, 2];
    let mut b = vec![4, 3];

    while a && a[a.len()-1] {
        b.push(a.pop().unwrap());
    }

    assert_eq!(a, vec![-1, 0]);
    assert_eq!(b, vec![4, 3, 2, 1]);
}
use weak_true::WeakTrue;

assert!("c".weak_true());
assert!('c'.weak_true());
assert!('\0'.weak_true());
assert!([0].weak_true());
assert!((&0 as *const i32).weak_true());
assert!(Some(0).weak_true());

assert!(f64::NAN.weak_false());
assert!(0.0.weak_false());
assert!(0.weak_false());
assert!("".weak_false());
assert!([0; 0].weak_false());

assert_eq!(1.weak_then(|| "ok"), Some("ok"));
assert_eq!(0.weak_then(|| "ok"), None);
Commit count: 14

cargo fmt