noneifempty

GitHub last commit Crates.io Docs.rs Project Status: Active – The project has reached a stable, usable state and is being actively developed.

About

Adds a trait NoneIfEmpty that converts a T to an Option by turning an empty T into None.

Usage

Add to your Cargo.toml: [dependencies] noneifempty = "0.1.0"

Examples

``` // Bring the trait into scope use noneifempty::NoneIfEmpty;

// Converts empty strings to None let emptystr = ""; asserteq!(emptystr.noneif_empty(), None);

// And full strings to Some let fullstr = "hello, world!"; asserteq!(fullstr.noneif_empty(), Some("hello, world!"));

// Also works with vecs, hashmaps, hashsets, custom types... let emptyvec: Vec<&str> = vec![]; let fullvec: Vec<&str> = vec!["hi"]; asserteq!(emptyvec.noneifempty(), None); asserteq!(fullvec.noneifempty(), Some(vec!["hi"]));

// Automatically implemented for Option let novec: Option> = None; let emptyvec: Option> = Some(vec![]); let fullvec: Option> = Some(vec!["hi"]); asserteq!(novec.noneifempty(), None); asserteq!(emptyvec.noneifempty(), None); asserteq!(fullvec.noneif_empty(), Some(vec!["hi"])); ```