noneifempty 0.1.3

Convert T to None if empty
Documentation
  • Coverage
  • 50%
    2 out of 4 items documented1 out of 4 items with examples
  • Size
  • Source code size: 8.38 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 232.40 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • barrowsys/noneifempty
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • barrowsys

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.2"

Examples

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

// Converts empty strings to None
let empty_str = "";
assert_eq!(empty_str.none_if_empty(), None);

// And full strings to Some
let full_str  = "hello, world!";
assert_eq!(full_str.none_if_empty(),  Some("hello, world!"));

// Also works with vecs, hashmaps, hashsets, custom types...
let empty_vec: Vec<&str> = vec![];
let full_vec:  Vec<&str> = vec!["hi"];
assert_eq!(empty_vec.none_if_empty(), None);
assert_eq!(full_vec.none_if_empty(),  Some(vec!["hi"]));

// Automatically implemented for Option<T: NoneIfEmpty>
let no_vec:    Option<Vec<&str>>  = None;
let empty_vec: Option<Vec<&str>>  = Some(vec![]);
let full_vec:  Option<Vec<&str>>  = Some(vec!["hi"]);
assert_eq!(no_vec.none_if_empty(),    None);
assert_eq!(empty_vec.none_if_empty(), None);
assert_eq!(full_vec.none_if_empty(),  Some(vec!["hi"]));