1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use super::Possible;
impl<T> Possible<T> {
/// Returns `true` if the option is a [`Possible::Some`] value.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let x: Possible<u32> = Possible::Some(2);
/// assert_eq!(x.is_some(), true);
///
/// let x: Possible<u32> = Possible::None;
/// assert_eq!(x.is_some(), false);
///
/// let x: Possible<u32> = Possible::Void;
/// assert_eq!(x.is_some(), false);
/// ```
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
#[inline]
pub fn is_some(&self) -> bool {
matches!(*self, Possible::Some(_))
}
/// Returns `true` if the option is a [`Possible::None`] value.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let x: Possible<u32> = Possible::Some(2);
/// assert_eq!(x.is_none(), false);
///
/// let x: Possible<u32> = Possible::None;
/// assert_eq!(x.is_none(), true);
///
/// let x: Possible<u32> = Possible::Void;
/// assert_eq!(x.is_none(), false);
/// ```
#[must_use = "if you intended to assert that this doesn't have a value, consider \
`.and_then(|_| panic!(\"`Possible` had a value when expected `Possible::None`\"))` instead"]
#[inline]
pub fn is_none(&self) -> bool {
matches!(*self, Possible::None)
}
/// Returns `true` if the option is a [`Possible::Void`] value.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let x: Possible<u32> = Possible::Some(2);
/// assert_eq!(x.is_void(), false);
///
/// let x: Possible<u32> = Possible::None;
/// assert_eq!(x.is_void(), false);
///
/// let x: Possible<u32> = Possible::Void;
/// assert_eq!(x.is_void(), true);
/// ```
#[must_use = "if you intended to assert that this doesn't have a value, consider \
`.and_then(|_| panic!(\"`Possible` had a value when expected `Possible::Void`\"))` instead"]
#[inline]
pub fn is_void(&self) -> bool {
matches!(*self, Possible::Void)
}
/// Returns `true` if the option is a [`Possible::Some`] value containing the given value.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let x: Possible<u32> = Possible::Some(2);
/// assert_eq!(x.contains(&2), true);
///
/// let x: Possible<u32> = Possible::Some(3);
/// assert_eq!(x.contains(&2), false);
///
/// let x: Possible<u32> = Possible::None;
/// assert_eq!(x.contains(&2), false);
///
/// let x: Possible<u32> = Possible::Void;
/// assert_eq!(x.contains(&2), false);
/// ```
#[must_use]
#[inline]
pub fn contains<U>(&self, x: &U) -> bool
where
U: PartialEq<T>,
{
match self {
Possible::Some(y) => x == y,
Possible::None => false,
Possible::Void => false,
}
}
}