#empty #collection #hash-map #vec #options #serde-json

no-std optempty

Tools for working types that may be empty. E.g., an empty String, Vec, HashMap, etc.

7 releases

0.1.13 Feb 21, 2023
0.1.12 Jan 21, 2022
0.1.11 Sep 29, 2021

#1097 in Data structures

Download history 7/week @ 2023-12-06 26/week @ 2023-12-13 19/week @ 2023-12-20 1/week @ 2023-12-27 38/week @ 2024-01-03 86/week @ 2024-01-10 106/week @ 2024-01-17 195/week @ 2024-01-24 60/week @ 2024-01-31 97/week @ 2024-02-07 210/week @ 2024-02-14 273/week @ 2024-02-21 369/week @ 2024-02-28 182/week @ 2024-03-06 230/week @ 2024-03-13 166/week @ 2024-03-20

984 downloads per month
Used in dynamodb-expression

Apache-2.0

16KB
158 lines

Crates.io Docs.rs


lib.rs:

Helpers for dealing with Options/Results of collections or other things (like Strings) that may be empty.

Examples

Always start by including the traits:

use optempty::*;

These examples only show Vec<T>, but they support any type that implements IsEmpty.

empty_into_none

Some with an empty Vec becomes None.

use optempty::*;

let some: Option<Vec<&str>> = Some(vec![]);
let none = some.empty_into_none();
assert_eq!(None, none);

Some with a non-empty Vec remains unchanged.

#
let some = Some(vec!["a", "b", "c"]);
let still_some = some.clone().empty_into_none();
assert_eq!(some, still_some);

None remains unchanged.

#
let none: Option<Vec<&str>> = None;
let still_none = none.empty_into_none();
assert_eq!(None, still_none);

empty_into_err

Ok with an empty Vec becomes Err.

use optempty::*;

let ok: Result<Vec<&str>, &str> = Ok(vec![]);
let err = ok.empty_into_err(|| "was empty");
assert_eq!(Err("was empty"), err);

Ok with a non-empty Vec remains unchanged.

#
let ok = Ok(vec!["a", "b", "c"]);
let still_ok = ok.empty_into_err(|| "was empty");
assert_eq!(Ok(vec!["a", "b", "c"]), still_ok);

Err remains unchanged.

#
let err: Result<Vec<&str>, &str> = Err("failed");
let still_err = err.empty_into_err(|| "was empty");
assert_eq!(Err("failed"), still_err);

See more examples at:

Features

Available features are:

  • querymap
  • serdejson
  • std
    • Adds support for types in std::collections in addition to types from alloc.

Default features:

  • std

Dependencies

~0–255KB