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
use std::{cmp, fmt, ops};
pub enum Message<T, B> {
WithoutBody(T),
WithBody(T, B),
}
impl<T, B> Message<T, B> {
pub fn get_ref(&self) -> &T {
match *self {
Message::WithoutBody(ref v) => v,
Message::WithBody(ref v, _) => v,
}
}
pub fn get_mut(&mut self) -> &mut T {
match *self {
Message::WithoutBody(ref mut v) => v,
Message::WithBody(ref mut v, _) => v,
}
}
pub fn into_inner(self) -> T {
match self {
Message::WithoutBody(v) => v,
Message::WithBody(v, _) => v,
}
}
pub fn take_body(&mut self) -> Option<B> {
use std::ptr;
unsafe {
match ptr::read(self as *const Message<T, B>) {
m @ Message::WithoutBody(..) => {
ptr::write(self as *mut Message<T, B>, m);
None
}
Message::WithBody(m, b) => {
ptr::write(self as *mut Message<T, B>, Message::WithoutBody(m));
Some(b)
}
}
}
}
}
impl<T, B> cmp::PartialEq<T> for Message<T, B>
where T: cmp::PartialEq
{
fn eq(&self, other: &T) -> bool {
(**self).eq(other)
}
}
impl<T, B> ops::Deref for Message<T, B> {
type Target = T;
fn deref(&self) -> &T {
match *self {
Message::WithoutBody(ref v) => v,
Message::WithBody(ref v, _) => v,
}
}
}
impl<T, B> ops::DerefMut for Message<T, B> {
fn deref_mut(&mut self) -> &mut T {
match *self {
Message::WithoutBody(ref mut v) => v,
Message::WithBody(ref mut v, _) => v,
}
}
}
impl<T, B> fmt::Debug for Message<T, B>
where T: fmt::Debug
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Message::WithoutBody(ref v) => write!(fmt, "Message::WithoutBody({:?})", v),
Message::WithBody(ref v, _) => write!(fmt, "Message::WithBody({:?}, ...)", v),
}
}
}