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
use std::ops::{Deref, DerefMut};
use std::slice::{Iter, IterMut};
use smallvec::SmallVec;
use op::Message;
#[derive(Clone, Debug)]
pub struct DnsResponse(SmallVec<[Message; 1]>);
impl DnsResponse {
pub fn messages(&self) -> Iter<Message> {
self.0.as_slice().iter()
}
pub fn messages_mut(&mut self) -> IterMut<Message> {
self.0.as_mut_slice().iter_mut()
}
pub fn len(&self) -> usize {
self.0.len()
}
}
impl Deref for DnsResponse {
type Target = Message;
fn deref(&self) -> &Self::Target {
&self.0[0]
}
}
impl DerefMut for DnsResponse {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0[0]
}
}
impl From<DnsResponse> for Message {
fn from(mut response: DnsResponse) -> Message {
response.0.remove(0)
}
}
impl From<Message> for DnsResponse {
fn from(message: Message) -> DnsResponse {
DnsResponse(SmallVec::from([message]))
}
}
impl From<SmallVec<[Message; 1]>> for DnsResponse {
fn from(messages: SmallVec<[Message; 1]>) -> DnsResponse {
debug_assert!(
!messages.is_empty(),
"There should be at least one message in any DnsResponse"
);
DnsResponse(messages)
}
}