pub trait TupleCombinator: Sized {
type Tuple;
// Required method
fn transpose(self) -> Option<Self::Tuple>;
// Provided methods
fn map<U, F: FnOnce(Self::Tuple) -> U>(self, f: F) -> Option<U> { ... }
fn expect(self, msg: &str) -> Self::Tuple { ... }
fn unwrap(self) -> Self::Tuple { ... }
fn and(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple> { ... }
fn and_then<U, F: FnOnce(Self::Tuple) -> Option<U>>(self, f: F) -> Option<U> { ... }
fn filter<P: FnOnce(&Self::Tuple) -> bool>(
self,
predicate: P,
) -> Option<Self::Tuple> { ... }
fn or(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple> { ... }
fn or_else<F: FnOnce() -> Option<Self::Tuple>>(
self,
f: F,
) -> Option<Self::Tuple> { ... }
fn xor(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple> { ... }
}
Expand description
The traits that provides helper functions for tuples. This trait implementation mirros most of
the methods defined in Option
.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn map<U, F: FnOnce(Self::Tuple) -> U>(self, f: F) -> Option<U>
fn map<U, F: FnOnce(Self::Tuple) -> U>(self, f: F) -> Option<U>
See Option::map
.
§Examples
let tuples = (Some("foo"), Some("bar"));
assert_eq!(tuples.map(|(a, b)| format!("{}{}", a, b)).unwrap(), "foobar");
Sourcefn expect(self, msg: &str) -> Self::Tuple
fn expect(self, msg: &str) -> Self::Tuple
See Option::expect
.
§Examples
let tuples = (Some("foo"), Some(123));
assert_eq!(tuples.expect("should not panic"), ("foo", 123));
let tuples: (_, Option<i32>) = (Some("foo"), None);
tuples.expect("will panic");
Sourcefn unwrap(self) -> Self::Tuple
fn unwrap(self) -> Self::Tuple
See Option::unwrap
.
let tuples = (Some("foo"), Some(123));
assert_eq!(tuples.unwrap(), ("foo", 123));
This example will panic:
let tuples: (_, Option<i32>) = (Some("foo"), None);
tuples.unwrap();
Sourcefn and(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple>
fn and(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple>
See Option::and
.
let left = (Some("foo"), Some(123));
let right = Some(("bar", 456));
assert_eq!(left.and(right), right);
let left_none = (None, Some(123));
assert_eq!(left_none.and(right), None);
Sourcefn and_then<U, F: FnOnce(Self::Tuple) -> Option<U>>(self, f: F) -> Option<U>
fn and_then<U, F: FnOnce(Self::Tuple) -> Option<U>>(self, f: F) -> Option<U>
See Option::and_then
.
let tuples = (Some("foobar"), Some(123));
assert_eq!(tuples.and_then(|(a, b)| Some(a.len() + b)), Some(129));
assert_eq!(tuples.and_then(|(a, b)| if b % 2 != 1 { Some(b) } else { None }), None);
Sourcefn filter<P: FnOnce(&Self::Tuple) -> bool>(
self,
predicate: P,
) -> Option<Self::Tuple>
fn filter<P: FnOnce(&Self::Tuple) -> bool>( self, predicate: P, ) -> Option<Self::Tuple>
See Option::filter
.
let tuples = (Some("foobar"), Some(123));
assert_eq!(tuples.filter(|(a, b)| b % 2 == 1), Some(("foobar", 123)));
assert_eq!(tuples.filter(|(a, b)| b % 2 != 1), None);
Sourcefn or(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple>
fn or(self, optb: Option<Self::Tuple>) -> Option<Self::Tuple>
See Option::or
.
let left = (Some("foo"), Some(123));
let right = Some(("bar", 456));
assert_eq!(left.or(right), left.transpose());
let left_none = (None, Some(123));
assert_eq!(left_none.or(right), right);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.