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
use super::Possible;
use core::ops::{Deref, DerefMut};
impl<T: Deref> Possible<T> {
/// Converts from `Possible<T>` (or `&Possible<T>`) to `Possible<&T::Target>`.
///
/// Leaves the original `Possible` in-place, creating a new one with a reference
/// to the original one, additionally coercing the contents via [`Deref`].
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let x: Possible<String> = Possible::Some("hey".to_owned());
/// assert_eq!(x.as_deref(), Possible::Some("hey"));
///
/// let x: Possible<String> = Possible::None;
/// assert_eq!(x.as_deref(), Possible::None);
///
/// let x: Possible<String> = Possible::Void;
/// assert_eq!(x.as_deref(), Possible::Void);
/// ```
pub fn as_deref(&self) -> Possible<&T::Target> {
self.as_ref().map(|t| t.deref())
}
}
impl<T: DerefMut> Possible<T> {
/// Converts from `Possible<T>` (or `&mut Possible<T>`) to `Possible<&mut T::Target>`.
///
/// Leaves the original `Possible` in-place, creating a new one containing a mutable reference to
/// the inner type's `Deref::Target` type.
///
/// # Examples
///
/// ```
/// use possible::Possible;
///
/// let mut x: Possible<String> = Possible::Some("hey".to_owned());
/// assert_eq!(x.as_deref_mut().map(|x| {
/// x.make_ascii_uppercase();
/// x
/// }), Possible::Some("HEY".to_owned().as_mut_str()));
/// ```
pub fn as_deref_mut(&mut self) -> Possible<&mut T::Target> {
self.as_mut().map(|t| t.deref_mut())
}
}