From 03c984f62baaaa77fcc870c7a054f53bb6e9435f Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Fri, 10 Jan 2025 11:18:05 -0600 Subject: [PATCH] adds macro to implement cross conversions of ContactInfo<...> --- gossip/src/contact_info.rs | 78 +++++++++++++++----------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/gossip/src/contact_info.rs b/gossip/src/contact_info.rs index ec5b923c46992c..97d1c3076088d1 100644 --- a/gossip/src/contact_info.rs +++ b/gossip/src/contact_info.rs @@ -559,56 +559,40 @@ impl TryFrom for ContactInfo> { } } -impl From> for ContactInfo> { - #[inline] - fn from(node: ContactInfo) -> 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> for ContactInfo> { - #[inline] - fn from(node: &ContactInfo) -> 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>> for ContactInfo { - #[inline] - fn from(node: &ContactInfo>) -> 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, + Box, + into, + (Box::new) +); +impl_convert_from!( + &ContactInfo, + Box, + clone, + (Box::new) +); +impl_convert_from!(&ContactInfo>, SocketAddrCache, clone, *); impl Sanitize for ContactInfo> { fn sanitize(&self) -> Result<(), SanitizeError> {