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.
This commit is contained in:
Matt Corallo 2024-08-14 19:36:46 +00:00
parent 33e69958e0
commit b3aed9a260
4 changed files with 19 additions and 5 deletions

View 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 {

View 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
})

View 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

View 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(())