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
// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! `DnsResponse` wraps a `Message` and any associated connection details

use std::ops::{Deref, DerefMut};
use std::slice::{Iter, IterMut};

use smallvec::SmallVec;

use op::Message;

// FIXME: see https://github.com/bluejekyll/trust-dns/issues/383 for removing vec of messages and instead returning a Stream
/// A DNS response object
///
/// For Most DNS requests, only one response is expected, the exception is a multicast request.
#[derive(Clone, Debug)]
pub struct DnsResponse(SmallVec<[Message; 1]>);

// TODO: when `impl Trait` lands in stable, remove this, and expose FlatMap over answers, et al.
impl DnsResponse {
    /// Get all the messages in the Response
    pub fn messages(&self) -> Iter<Message> {
        self.0.as_slice().iter()
    }

    /// Get all the messages in the Response
    pub fn messages_mut(&mut self) -> IterMut<Message> {
        self.0.as_mut_slice().iter_mut()
    }

    /// returns the number of messages in the response
    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)
    }
}