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
use super::Possible;
use core::pin::Pin;
impl<T> Possible<T> {
/// Converts from `&Possible<T>` to `Possible<&T>`.
///
/// # Examples
///
/// Converts an `Possible<`[`String`]`>` into an `Possible<`[`usize`]`>`, preserving the original.
/// The [`map`] method takes the `self` argument by value, consuming the original,
/// so this technique uses `as_ref` to first take an `Possible` to a reference
/// to the value inside the original.
///
/// [`map`]: Possible::map
/// [`String`]: ../../std/string/struct.String.html
///
/// ```
/// use possible::Possible;
/// let text: Possible<String> = Possible::Some("Hello, world!".to_string());
/// // First, cast `Possible<String>` to `Possible<&String>` with `as_ref`,
/// // then consume *that* with `map`, leaving `text` on the stack.
/// let text_length: Possible<usize> = text.as_ref().map(|s| s.len());
/// println!("still can print text: {:?}", text);
/// ```
#[inline]
pub const fn as_ref(&self) -> Possible<&T> {
match *self {
Possible::Some(ref x) => Possible::Some(x),
Possible::None => Possible::None,
Possible::Void => Possible::Void,
}
}
/// Converts from `&mut Possible<T>` to `Possible<&mut T>`.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let mut x = Possible::Some(2);
/// match x.as_mut() {
/// Possible::Some(v) => *v = 42,
/// Possible::None | Possible::Void => {},
/// }
/// assert_eq!(x, Possible::Some(42));
/// ```
#[inline]
pub fn as_mut(&mut self) -> Possible<&mut T> {
match *self {
Possible::Some(ref mut x) => Possible::Some(x),
Possible::None => Possible::None,
Possible::Void => Possible::Void,
}
}
/// Converts from [`Pin`]`<&Possible<T>>` to `Possible<`[`Pin`]`<&T>>`.
#[inline]
pub fn as_pin_ref(self: Pin<&Self>) -> Possible<Pin<&T>> {
// SAFETY: `x` is guaranteed to be pinned because it comes from `self`
// which is pinned.
unsafe { Pin::get_ref(self).as_ref().map(|x| Pin::new_unchecked(x)) }
}
/// Converts from [`Pin`]`<&mut Possible<T>>` to `Possible<`[`Pin`]`<&mut T>>`.
#[inline]
pub fn as_pin_mut(self: Pin<&mut Self>) -> Possible<Pin<&mut T>> {
// SAFETY: `get_unchecked_mut` is never used to move the `Possible` inside `self`.
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
unsafe {
Pin::get_unchecked_mut(self)
.as_mut()
.map(|x| Pin::new_unchecked(x))
}
}
}