#methods #option-t #bool #traits #return #extension #value

and_then_some

Provides an extension trait for bool with methods that return Option<T>

1 stable release

1.0.0 May 15, 2020

#2302 in Rust patterns

MIT license

5KB
71 lines

and_then_some

Provides an extension trait for bool with methods that return Option<T>.

This crate provides four methods; they all are defined in BoolExt, all take self and another argument, and all return Option<T>. The only difference is in the type of the second argument:

  • and: Option<T>
  • and_some: T
  • and_then: impl FnOnce() -> Option<T>
  • and_then_some: impl FnOnce() -> T

I do not consider this crate to be useful myself, I just created it to draw some attention to the tracking issue for RFC 2757 (methods for converting from bool to Option<T>).

This crate achieves maximum consistency: Some-wrapping is given a _some suffix in the method names while methods that take a function over a plain value have a _then suffix; and_then is consistent with Option::and_then in the type of its second argument. This naming scheme results in the amusing name and_then_some for one of the methods, which made me choose it as the crate name too. I hope .and_then_some can be stabilized under a different name though, because to me it seems like the most common out of the four operations.

No runtime deps