has-some

Crates.iohas-some
lib.rshas-some
version
sourcesrc
created_at2024-12-06 18:12:51.716284
updated_at2024-12-07 13:52:50.26816
descriptionThe opposite of is_empty (and is_empty for filters)
homepagehttps://github.com/bassmanitram/has-some
repositoryhttps://github.com/bassmanitram/has-some
max_upload_size
id1474503
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Martin Bartlett (bassmanitram)

documentation

https://docs.rs/has-some

README

has-some

Implement the opposite of is_empty to promote better semantics for iterator filters (and other situations) where !T.is_empty() is counterintuitive, as well as introduce filter_friendly versions of T::is_empty.

Using is_empty in an iterator filter method is relatively verbose because you can't pass the T::is_empty function when the iterator item is a reference, and, anyway, you usually want to retain things that are not empty, a predicate for which you'll always need a closure.

Basically, it stands that the semantics of "not is_empty" are annoying (well, to me) when "has some" is clearer, and even T::is_empty is annoying when using filters.

This crate, then, addresses those annoyances.

Examples

It's not really rocket science, but here you go with an example where is_empty passed as a function reference to an iterator filter does work:

use has_some::HasSome

let vector = vec!["some_data".to_owned(), "".to_owned(), "more data".to_owned(), "".to_owned()];
let vector2 = vector.clone();

// If you want the empties, you can do
let empties = vector.into_iter().filter(String::is_empty).collect::<Vec<String>>();
assert_eq!(["", ""], empties.as_slice());

// If you want the non-empties, you can now do
let non_empties = vector2.into_iter().filter(String::has_some).collect::<Vec<String>>();
assert_eq!(["some_data", "more data"], non_empties.as_slice());

And a common example where you have Items that are double references:

use has_some::HasSome

let vector = vec!["some_data", "", "more data", ""];

// If you want the empties, you can do
let empties = vector.iter().filter(str::is_empty3).collect::<Vec<&&str>>();
assert_eq!([&"", &""], empties.as_slice());

// If you want the non-empties, you can now do
let non_empties = vector2.iter().filter(str::has_some3).collect::<Vec<&&str>>();
assert_eq!([&"some_data", &"more data"], non_empties.as_slice());

CHANGES

2.0.0

  • Added the filter-friendly methods - will break custom implementations that are based on the version 1 trait
  • Added an implementation for str
  • Improved and updated documentation

1.0.0

Just has_some

Commit count: 5

cargo fmt