From: Matt Corallo Date: Wed, 14 Aug 2024 19:36:46 +0000 (+0000) Subject: Return slices, rather than `Vec` references, in addresses X-Git-Tag: v0.0.124-beta~11^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=b3aed9a2602735cd6efa95e42408ba65a71cfbdf;p=rust-lightning Return slices, rather than `Vec` references, in addresses Its a bit strange to return a reference to a `Vec` in Rust, when a slice is really intended as the way to do so. Worse, the bindings don't know how to map a reference to a `Vec` (but do have code to map a slice of `Clone`able objects). Here, we move `NodeAnnouncementInfo::addresses` to return a slice, though to do so we have to adapt the `WithoutLength` `Writeable` impl to support slices as well. --- diff --git a/lightning-rapid-gossip-sync/src/processing.rs b/lightning-rapid-gossip-sync/src/processing.rs index 80163472e..84f9f5bf1 100644 --- a/lightning-rapid-gossip-sync/src/processing.rs +++ b/lightning-rapid-gossip-sync/src/processing.rs @@ -178,7 +178,7 @@ where synthetic_node_announcement.features = info.features().clone(); synthetic_node_announcement.rgb = info.rgb().clone(); synthetic_node_announcement.alias = info.alias().clone(); - synthetic_node_announcement.addresses = info.addresses().clone(); + synthetic_node_announcement.addresses = info.addresses().to_vec(); }); if has_address_details { diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 24615e537..916146d78 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -595,7 +595,7 @@ where match node_details { Some((features, addresses)) if features.supports_onion_messages() && addresses.len() > 0 => { - let first_node_addresses = Some(addresses.clone()); + let first_node_addresses = Some(addresses.to_vec()); Ok(OnionMessagePath { intermediate_nodes: vec![], destination, first_node_addresses }) diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 22a3f6e79..d6b785421 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -1308,7 +1308,7 @@ impl NodeAnnouncementInfo { } /// Internet-level addresses via which one can connect to the node - pub fn addresses(&self) -> &Vec { + pub fn addresses(&self) -> &[SocketAddress] { match self { NodeAnnouncementInfo::Relayed(relayed) => { &relayed.contents.addresses diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 5f203b74d..e07065d06 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -656,10 +656,24 @@ impl Readable for WithoutLength { } } -impl<'a, T: Writeable> Writeable for WithoutLength<&'a Vec> { +trait AsWriteableSlice { + type Inner: Writeable; + fn as_slice(&self) -> &[Self::Inner]; +} + +impl AsWriteableSlice for &Vec { + type Inner = T; + fn as_slice(&self) -> &[T] { &self } +} +impl AsWriteableSlice for &[T] { + type Inner = T; + fn as_slice(&self) -> &[T] { &self } +} + +impl Writeable for WithoutLength { #[inline] fn write(&self, writer: &mut W) -> Result<(), io::Error> { - for ref v in self.0.iter() { + for ref v in self.0.as_slice() { v.write(writer)?; } Ok(())