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
#![no_std]
pub trait OptionalIndex<I>
where
I: ?Sized,
{
type Output: Clone + ?Sized;
fn optional_index(&self, index: I) -> Option<&Self::Output>;
}
pub trait OptionalIndexMut<I>: OptionalIndex<I>
where
I: ?Sized,
{
fn optional_index_mut(&mut self, index: I) -> Option<&mut Self::Output>;
}
impl<T, I> OptionalIndex<I> for &T
where
T: OptionalIndex<I>,
{
type Output = <T as OptionalIndex<I>>::Output;
fn optional_index(&self, index: I) -> Option<&Self::Output> {
(*self).optional_index(index)
}
}
impl<T, I> OptionalIndex<I> for &mut T
where
T: OptionalIndex<I>,
{
type Output = <T as OptionalIndex<I>>::Output;
fn optional_index(&self, index: I) -> Option<&Self::Output> {
(**self).optional_index(index)
}
}
impl<T, I> OptionalIndexMut<I> for &mut T
where
T: OptionalIndexMut<I>,
{
fn optional_index_mut(&mut self, index: I) -> Option<&mut Self::Output> {
(*self).optional_index_mut(index)
}
}
impl<T, I> OptionalIndex<I> for Option<T>
where
T: OptionalIndex<I>,
{
type Output = <T as OptionalIndex<I>>::Output;
fn optional_index(&self, index: I) -> Option<&Self::Output> {
self.as_ref().and_then(|t| t.optional_index(index))
}
}
impl<T, I> OptionalIndexMut<I> for Option<T>
where
T: OptionalIndexMut<I>,
{
fn optional_index_mut(&mut self, index: I) -> Option<&mut Self::Output> {
self.as_mut().and_then(|t| t.optional_index_mut(index))
}
}