Adds a trait NoneIfEmpty that converts a T to an Option
Add to your Cargo.toml:
[dependencies]
noneifempty = "0.1.2"
``` // 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