network: Implement net::ToSocketAddrs for address messages

This commit is contained in:
Steven Roose 2020-11-08 13:49:02 +00:00
parent 4c70397a85
commit ab1e9cbb9e
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 16 additions and 3 deletions

View File

@ -18,9 +18,8 @@
//! network addresses in Bitcoin messages.
//!
use std::io;
use std::fmt;
use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6, Ipv4Addr};
use std::{fmt, io, iter};
use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6, Ipv4Addr, ToSocketAddrs};
use network::constants::ServiceFlags;
use consensus::encode::{self, Decodable, Encodable, VarInt, ReadExt, WriteExt};
@ -110,6 +109,13 @@ impl fmt::Debug for Address {
}
}
impl ToSocketAddrs for Address {
type Iter = iter::Once<SocketAddr>;
fn to_socket_addrs(&self) -> Result<Self::Iter, io::Error> {
Ok(iter::once(self.socket_addr()?))
}
}
/// Supported networks for use in BIP155 addrv2 message
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum AddrV2 {
@ -276,6 +282,13 @@ impl Decodable for AddrV2Message {
}
}
impl ToSocketAddrs for AddrV2Message {
type Iter = iter::Once<SocketAddr>;
fn to_socket_addrs(&self) -> Result<Self::Iter, io::Error> {
Ok(iter::once(self.socket_addr()?))
}
}
#[cfg(test)]
mod test {
use std::str::FromStr;