]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Return slices, rather than `Vec` references, in addresses 2024-08-ret-slice-not-ref-vec
authorMatt Corallo <git@bluematt.me>
Wed, 14 Aug 2024 19:36:46 +0000 (19:36 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 14 Aug 2024 19:44:13 +0000 (19:44 +0000)
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.

lightning-rapid-gossip-sync/src/processing.rs
lightning/src/onion_message/messenger.rs
lightning/src/routing/gossip.rs
lightning/src/util/ser.rs

index 80163472e2839b7f2c584feac5c0ce05f9dfa46c..84f9f5bf11934891ab85cfc7410e3295ec69e33a 100644 (file)
@@ -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 {
index 24615e537c7b5c21419693beed68dd7f98ce971b..916146d78ab6f4670b1b279644f722f5e4f252f5 100644 (file)
@@ -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
                                        })
index 22a3f6e79800e8d446c160cbe83b401d1813afb9..d6b7854215cee7f0b77364748450fa9971f3a79d 100644 (file)
@@ -1308,7 +1308,7 @@ impl NodeAnnouncementInfo {
        }
 
        /// Internet-level addresses via which one can connect to the node
-       pub fn addresses(&self) -> &Vec<SocketAddress> {
+       pub fn addresses(&self) -> &[SocketAddress] {
                match self {
                        NodeAnnouncementInfo::Relayed(relayed) => {
                                &relayed.contents.addresses
index 5f203b74d97f9d32290d9bc31c61e2dea8745888..e07065d06d9e0db8b1a290ea5294fe746f1f5918 100644 (file)
@@ -656,10 +656,24 @@ impl Readable for WithoutLength<UntrustedString> {
        }
 }
 
-impl<'a, T: Writeable> Writeable for WithoutLength<&'a Vec<T>> {
+trait AsWriteableSlice {
+       type Inner: Writeable;
+       fn as_slice(&self) -> &[Self::Inner];
+}
+
+impl<T: Writeable> AsWriteableSlice for &Vec<T> {
+       type Inner = T;
+       fn as_slice(&self) -> &[T] { &self }
+}
+impl<T: Writeable> AsWriteableSlice for &[T] {
+       type Inner = T;
+       fn as_slice(&self) -> &[T] { &self }
+}
+
+impl<S: AsWriteableSlice> Writeable for WithoutLength<S> {
        #[inline]
        fn write<W: Writer>(&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(())