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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use super::Possible;
use core::iter::FromIterator;
impl<T> From<Possible<T>> for Option<T> {
/// Copies `value` into an `Option::Some`.
///
/// Note that this functionpotentially loses information since `Void` is merged into `None`.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let o: Possible<u8> = Possible::from(67);
///
/// assert_eq!(Possible::Some(67), o);
/// ```
fn from(value: Possible<T>) -> Option<T> {
match value {
Possible::Some(value) => Some(value),
Possible::None | Possible::Void => None,
}
}
}
impl<T> From<Option<T>> for Possible<T> {
/// Copies `value` into a `Possible::Some`.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let o: Possible<u8> = Possible::from(67);
///
/// assert_eq!(Possible::Some(67), o);
/// ```
fn from(value: Option<T>) -> Possible<T> {
match value {
Some(value) => Possible::Some(value),
None => Possible::None,
}
}
}
impl<T> From<T> for Possible<T> {
/// Copies `value` into a new `Possible::Some`.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let o: Possible<u8> = Possible::from(67);
///
/// assert_eq!(Possible::Some(67), o);
/// ```
fn from(value: T) -> Possible<T> {
Possible::Some(value)
}
}
impl<'a, T> From<&'a Possible<T>> for Possible<&'a T> {
/// Converts from `&Possible<T>` to `Possible<&T>`.
///
/// # Examples
///
/// Converts a `Possible<`[`String`]`>` into a `Possible<`[`usize`]`>`, preserving the original.
/// The [`map`] method takes the `self` argument by value, consuming the original,
/// so this technique uses `from` to first take an `Possible` to a reference
/// to the value inside the original.
///
/// ```
/// use possible::Possible;
///
/// let s: Possible<String> = Possible::Some(String::from("Hello, Rustaceans!"));
/// let o: Possible<usize> = Possible::from(&s).map(|ss: &String| ss.len());
///
/// println!("Can still print s: {:?}", s);
///
/// assert_eq!(o, Possible::Some(18));
/// ```
fn from(o: &'a Possible<T>) -> Possible<&'a T> {
o.as_ref()
}
}
impl<'a, T> From<&'a mut Possible<T>> for Possible<&'a mut T> {
/// Converts from `&mut Possible<T>` to `Possible<&mut T>`
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let mut s = Possible::Some(String::from("Hello"));
/// let o: Possible<&mut String> = Possible::from(&mut s);
///
/// match o {
/// Possible::Some(t) => *t = String::from("Hello, Rustaceans!"),
/// Possible::None | Possible::Void => (),
/// }
///
/// assert_eq!(s, Possible::Some(String::from("Hello, Rustaceans!")));
/// ```
fn from(o: &'a mut Possible<T>) -> Possible<&'a mut T> {
o.as_mut()
}
}
impl<A, V: FromIterator<A>> FromIterator<Possible<A>> for Possible<V> {
#[inline]
fn from_iter<I: IntoIterator<Item = Possible<A>>>(iter: I) -> Possible<V> {
match iter
.into_iter()
.map(|x| x.ok_or(()))
.collect::<Result<_, _>>()
.ok()
{
Some(v) => Possible::Some(v),
None => Possible::None,
}
}
}
impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Possible<V> {
#[inline]
fn from_iter<I: IntoIterator<Item = Option<A>>>(iter: I) -> Possible<V> {
match iter
.into_iter()
.map(|x| x.ok_or(()))
.collect::<Result<_, _>>()
.ok()
{
Some(v) => Possible::Some(v),
None => Possible::None,
}
}
}