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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::default::Default;
use std::hash::Hash;
use std::ops::Deref;
use std::option;
use crate::Message;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct MessageField<T>(pub Option<Box<T>>);
impl<T> MessageField<T> {
#[inline]
pub fn some(value: T) -> MessageField<T> {
MessageField(Some(Box::new(value)))
}
#[inline]
pub const fn none() -> MessageField<T> {
MessageField(None)
}
#[inline]
pub fn from_option(option: Option<T>) -> MessageField<T> {
match option {
Some(x) => MessageField::some(x),
None => MessageField::none(),
}
}
#[inline]
pub fn is_some(&self) -> bool {
self.0.is_some()
}
#[inline]
pub fn is_none(&self) -> bool {
self.0.is_none()
}
#[inline]
pub fn into_option(self) -> Option<T> {
self.0.map(|v| *v)
}
#[inline]
pub fn as_ref(&self) -> Option<&T> {
self.0.as_ref().map(|v| &**v)
}
#[inline]
pub fn as_mut(&mut self) -> Option<&mut T> {
self.0.as_mut().map(|v| &mut **v)
}
#[inline]
pub fn unwrap(self) -> T {
*self.0.unwrap()
}
#[inline]
pub fn unwrap_or(self, def: T) -> T {
self.0.map(|v| *v).unwrap_or(def)
}
#[inline]
pub fn unwrap_or_else<F>(self, f: F) -> T
where
F: FnOnce() -> T,
{
self.0.map(|v| *v).unwrap_or_else(f)
}
#[inline]
pub fn map<U, F>(self, f: F) -> MessageField<U>
where
F: FnOnce(T) -> U,
{
MessageField::from_option(self.into_option().map(f))
}
#[inline]
pub fn iter(&self) -> option::IntoIter<&T> {
self.as_ref().into_iter()
}
#[inline]
pub fn mut_iter(&mut self) -> option::IntoIter<&mut T> {
self.as_mut().into_iter()
}
#[inline]
pub fn take(&mut self) -> Option<T> {
self.0.take().map(|v| *v)
}
#[inline]
pub fn clear(&mut self) {
self.0 = None;
}
}
impl<T: Default> MessageField<T> {
#[inline]
pub fn unwrap_or_default(self) -> T {
*self.0.unwrap_or_default()
}
}
impl<M: Message> MessageField<M> {
pub fn get_or_default(&self) -> &M {
self.as_ref().unwrap_or_else(|| M::default_instance())
}
pub fn mut_or_insert_default(&mut self) -> &mut M {
if self.is_none() {
*self = MessageField::some(Default::default());
}
self.as_mut().unwrap()
}
}
impl<M: Message> Deref for MessageField<M> {
type Target = M;
fn deref(&self) -> &Self::Target {
self.get_or_default()
}
}
#[cfg(no)]
impl<M: Message> DerefMut for MessageField<M> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.mut_or_insert_default()
}
}
impl<T> Default for MessageField<T> {
#[inline]
fn default() -> MessageField<T> {
MessageField::none()
}
}
impl<T> From<Option<T>> for MessageField<T> {
fn from(o: Option<T>) -> Self {
MessageField::from_option(o)
}
}
impl<'a, T> IntoIterator for &'a MessageField<T> {
type Item = &'a T;
type IntoIter = option::IntoIter<&'a T>;
fn into_iter(self) -> option::IntoIter<&'a T> {
self.iter()
}
}