parity_multiaddr/
onion_addr.rs

1use std::{borrow::Cow, fmt};
2
3/// Represents an Onion v3 address
4#[derive(Clone)]
5pub struct Onion3Addr<'a>(Cow<'a, [u8; 35]>, u16);
6
7impl<'a> Onion3Addr<'a> {
8    /// Return the hash of the public key as bytes
9    pub fn hash(&self) -> &[u8; 35] {
10        self.0.as_ref()
11    }
12
13    /// Return the port
14    pub fn port(&self) -> u16 {
15        self.1
16    }
17
18    /// Consume this instance and create an owned version containing the same address
19    pub fn acquire<'b>(self) -> Onion3Addr<'b> {
20        Onion3Addr(Cow::Owned(self.0.into_owned()), self.1)
21    }
22}
23
24impl PartialEq for Onion3Addr<'_> {
25    fn eq(&self, other: &Self) -> bool {
26        self.1 == other.1 && self.0[..] == other.0[..]
27    }
28}
29
30impl Eq for Onion3Addr<'_> { }
31
32impl From<([u8; 35], u16)> for Onion3Addr<'_> {
33    fn from(parts: ([u8; 35], u16)) -> Self {
34        Self(Cow::Owned(parts.0), parts.1)
35    }
36}
37
38impl<'a> From<(&'a [u8; 35], u16)> for Onion3Addr<'a> {
39    fn from(parts: (&'a [u8; 35], u16)) -> Self {
40        Self(Cow::Borrowed(parts.0), parts.1)
41    }
42}
43
44impl fmt::Debug for Onion3Addr<'_> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
46       f.debug_tuple("Onion3Addr")
47           .field(&format!("{:02x?}", &self.0[..]))
48           .field(&self.1)
49           .finish()
50    }
51}