1mod r#impl;
2
3/// Extension trait providing additional methods for `Option`.
4pub trait OptionExt<T> {
5 /// Returns `true` if the option is a [`Some`] value containing the given value.
6 ///
7 /// # Examples
8 ///
9 /// ```
10 /// use option_ext::OptionExt;
11 ///
12 /// let x: Option<u32> = Some(2);
13 /// assert_eq!(x.contains(&2), true);
14 ///
15 /// let x: Option<u32> = Some(3);
16 /// assert_eq!(x.contains(&2), false);
17 ///
18 /// let x: Option<u32> = None;
19 /// assert_eq!(x.contains(&2), false);
20 /// ```
21 #[must_use]
22 fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T>;
23
24 /// Returns the result from applying the function to the contained value if the option is [`Some`],
25 /// or returns provided default result if the option is [`None`].
26 ///
27 /// The `f` argument of `map_or2` is only evaluated if the option is [`Some`].
28 /// The default argument of `map_or2` is always evaluated – even if the option is [`Some`].
29 /// Use [`map_or_else2`] to avoid this.
30 ///
31 /// [`map_or_else2`]: OptionExt::map_or_else2
32 ///
33 /// # Examples
34 ///
35 /// ```
36 /// use option_ext::OptionExt;
37 ///
38 /// let x = Some("bar");
39 /// assert_eq!(x.map_or2(|v| v.len(), 42), 3);
40 ///
41 /// let x: Option<&str> = None;
42 /// assert_eq!(x.map_or2(|v| v.len(), 42), 42);
43 /// ```
44 #[must_use]
45 fn map_or2<U, F: FnOnce(T) -> U>(self, f: F, default: U) -> U;
46
47 /// Returns the result from applying the function to the contained value if the option is [`Some`],
48 /// or returns the result from evaluating the provided default function if the option is [`None`].
49 ///
50 /// The `f` argument of `map_or_else2` is only evaluated if the option is [`Some`].
51 /// The default argument of `map_or_else2` is only evaluated if the option is [`None`].
52 /// Use [`map_or2`] to always evaluate the default argument.
53 ///
54 /// [`map_or2`]: OptionExt::map_or2
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use option_ext::OptionExt;
60 ///
61 /// let k = 23;
62 ///
63 /// let x = Some("bar");
64 /// assert_eq!(x.map_or_else2(|v| v.len(), || 2 * k), 3);
65 ///
66 /// let x: Option<&str> = None;
67 /// assert_eq!(x.map_or_else2(|v| v.len(), || 2 * k), 46);
68 /// ```
69 #[must_use]
70 fn map_or_else2<U, F: FnOnce(T) -> U, D: FnOnce() -> U>(self, f: F, default: D) -> U;
71}
72