toe_beans/v4/message/options/
address.rs

1use serde::{Deserialize, Serialize};
2use std::net::Ipv4Addr;
3
4/// This represents one of several Message options that takes an Ipv4Addr value.
5#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
6pub struct AddressOption(Ipv4Addr);
7
8impl AddressOption {
9    /// Create an `AddressOption` directly from an `Ipv4Addr`.
10    pub fn new(ip: Ipv4Addr) -> Self {
11        Self(ip)
12    }
13
14    /// Used, when serializing, to add to the byte buffer.
15    #[inline]
16    pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
17        bytes.push(tag); // tag
18        bytes.push(4); // length
19        bytes.extend_from_slice(&self.0.octets()); // value
20    }
21
22    /// Get the `Ipv4Addr` that this type wraps.
23    pub fn inner(&self) -> Ipv4Addr {
24        self.0
25    }
26}
27
28impl From<&[u8]> for AddressOption {
29    fn from(value: &[u8]) -> Self {
30        Self(Ipv4Addr::new(value[0], value[1], value[2], value[3]))
31    }
32}