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
use std::cmp;
use std::fmt::{self, Debug, Display, Formatter};
use std::default::Default;
use std::hash::{Hash, Hasher};
pub enum MaybeOwned<'a, T: 'a> {
Borrowed(&'a T),
Owned(T)
}
impl<'a, T: 'a> MaybeOwned<'a, T> {
#[inline]
pub fn as_ref<'b>(&'b self) -> &'b T where 'b: 'a {
match *self {
MaybeOwned::Borrowed(v) => v,
MaybeOwned::Owned(ref v) => &v
}
}
}
impl<'a, T: 'a + Clone> MaybeOwned<'a, T> {
#[inline]
pub fn get_or_clone(self) -> T {
match self {
MaybeOwned::Borrowed(v) => v.clone(),
MaybeOwned::Owned(v) => v
}
}
}
impl<'a, T: 'a + Debug> Debug for MaybeOwned<'a, T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
MaybeOwned::Borrowed(v) => write!(f, "Borrw({:?})", v),
MaybeOwned::Owned(ref v) => write!(f, "Owned({:?})", v)
}
}
}
impl<'a, T: 'a + Display> Display for MaybeOwned<'a, T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
MaybeOwned::Borrowed(v) => write!(f, "Borrw({})", v),
MaybeOwned::Owned(ref v) => write!(f, "Owned({})", v)
}
}
}
impl<'a, T: 'a + PartialEq> cmp::PartialEq for MaybeOwned<'a, T> {
#[inline]
fn eq(&self, other: &MaybeOwned<T>) -> bool {
self.as_ref().eq(other.as_ref())
}
#[inline]
fn ne(&self, other: &MaybeOwned<T>) -> bool {
self.as_ref().ne(other.as_ref())
}
}
impl<'a, T: 'a + Eq> cmp::Eq for MaybeOwned<'a, T> { }
impl<'a, T: 'a + PartialOrd> cmp::PartialOrd for MaybeOwned<'a, T> {
#[inline]
fn partial_cmp(&self, other: &MaybeOwned<T>) -> Option<cmp::Ordering> {
self.as_ref().partial_cmp(other.as_ref())
}
#[inline]
fn lt(&self, other: &MaybeOwned<T>) -> bool {
self.as_ref().lt(other.as_ref())
}
#[inline]
fn le(&self, other: &MaybeOwned<T>) -> bool {
self.as_ref().le(other.as_ref())
}
#[inline]
fn gt(&self, other: &MaybeOwned<T>) -> bool {
self.as_ref().gt(other.as_ref())
}
#[inline]
fn ge(&self, other: &MaybeOwned<T>) -> bool {
self.as_ref().ge(other.as_ref())
}
}
impl<'a, T: 'a + Ord> cmp::Ord for MaybeOwned<'a, T> {
#[inline]
fn cmp(&self, other: &MaybeOwned<T>) -> cmp::Ordering {
self.as_ref().cmp(other.as_ref())
}
}
impl<'a, T: 'a + Default> Default for MaybeOwned<'a, T> {
#[inline]
fn default() -> MaybeOwned<'a, T> {
MaybeOwned::Owned(Default::default())
}
}
impl<'a, T: 'a + Hash> Hash for MaybeOwned<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_ref().hash(state)
}
}