Skip to content

Commit

Permalink
adds macro to implement cross conversions of ContactInfo<...>
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadnouri committed Jan 10, 2025
1 parent ee24e9b commit 23038ae
Showing 1 changed file with 31 additions and 47 deletions.
78 changes: 31 additions & 47 deletions gossip/src/contact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,56 +559,40 @@ impl TryFrom<ContactInfoLite> for ContactInfo<Box<SocketAddrCache>> {
}
}

impl From<ContactInfo<SocketAddrCache>> for ContactInfo<Box<SocketAddrCache>> {
#[inline]
fn from(node: ContactInfo<SocketAddrCache>) -> Self {
Self {
pubkey: node.pubkey,
wallclock: node.wallclock,
outset: node.outset,
shred_version: node.shred_version,
version: node.version,
addrs: node.addrs,
sockets: node.sockets,
extensions: node.extensions,
cache: Box::new(node.cache),
}
}
}

impl From<&ContactInfo<SocketAddrCache>> for ContactInfo<Box<SocketAddrCache>> {
#[inline]
fn from(node: &ContactInfo<SocketAddrCache>) -> Self {
Self {
pubkey: node.pubkey,
wallclock: node.wallclock,
outset: node.outset,
shred_version: node.shred_version,
version: node.version.clone(),
addrs: node.addrs.clone(),
sockets: node.sockets.clone(),
extensions: node.extensions.clone(),
cache: Box::new(node.cache),
macro_rules! impl_convert_from {
($a:ty, $b:ty, $f:ident, $g:tt) => {
impl From<$a> for ContactInfo<$b> {
#[inline]
fn from(node: $a) -> Self {
Self {
pubkey: node.pubkey,
wallclock: node.wallclock,
outset: node.outset,
shred_version: node.shred_version,
version: node.version.$f(),
addrs: node.addrs.$f(),
sockets: node.sockets.$f(),
extensions: node.extensions.$f(),
cache: $g(node.cache),
}
}
}
}
};
}

impl From<&ContactInfo<Box<SocketAddrCache>>> for ContactInfo<SocketAddrCache> {
#[inline]
fn from(node: &ContactInfo<Box<SocketAddrCache>>) -> Self {
Self {
pubkey: node.pubkey,
wallclock: node.wallclock,
outset: node.outset,
shred_version: node.shred_version,
version: node.version.clone(),
addrs: node.addrs.clone(),
sockets: node.sockets.clone(),
extensions: node.extensions.clone(),
cache: *node.cache,
}
}
}
impl_convert_from!(
ContactInfo<SocketAddrCache>,
Box<SocketAddrCache>,
into,
(Box::new)
);
impl_convert_from!(
&ContactInfo<SocketAddrCache>,
Box<SocketAddrCache>,
clone,
(Box::new)
);
impl_convert_from!(&ContactInfo<Box<SocketAddrCache>>, SocketAddrCache, clone, *);

impl Sanitize for ContactInfo<Box<SocketAddrCache>> {
fn sanitize(&self) -> Result<(), SanitizeError> {
Expand Down

0 comments on commit 23038ae

Please sign in to comment.