impl Debug for UtPex

This commit is contained in:
Igor Katson 2024-08-25 13:20:13 +01:00
parent 90bfb85bcc
commit c7b7dc300f
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 43 additions and 19 deletions

View file

@ -12,7 +12,7 @@ pub struct PexPeerInfo {
pub addr: SocketAddr,
}
#[derive(Debug, Serialize, Default, Deserialize)]
#[derive(Serialize, Default, Deserialize)]
pub struct UtPex<B> {
added: B,
#[serde(rename = "added.f")]
@ -26,6 +26,27 @@ pub struct UtPex<B> {
dropped6: B,
}
impl<B> core::fmt::Debug for UtPex<B>
where
B: AsRef<[u8]>,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
struct IterDebug<I>(I);
impl<I> core::fmt::Debug for IterDebug<I>
where
I: Iterator<Item = PexPeerInfo> + Clone,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.0.clone()).finish()
}
}
f.debug_struct("UtPex")
.field("added", &IterDebug(self.added_peers()))
.field("dropped", &IterDebug(self.dropped_peers()))
.finish()
}
}
impl<B> CloneToOwned for UtPex<B>
where
B: CloneToOwned,
@ -52,7 +73,7 @@ where
buf: &'a B,
flags: &'a Option<B>,
ip_len: usize,
) -> impl Iterator<Item = PexPeerInfo> + 'a {
) -> impl Iterator<Item = PexPeerInfo> + Clone + 'a {
let addrs = buf.as_ref().chunks_exact(ip_len + 2).map(move |c| {
let ip = match ip_len {
4 => IpAddr::from(TryInto::<[u8; 4]>::try_into(&c[..4]).unwrap()),
@ -74,12 +95,12 @@ where
})
}
pub fn added_peers(&self) -> impl Iterator<Item = PexPeerInfo> + '_ {
pub fn added_peers(&self) -> impl Iterator<Item = PexPeerInfo> + Clone + '_ {
self.added_peers_inner(&self.added, &self.added_f, 4)
.chain(self.added_peers_inner(&self.added6, &self.added6_f, 16))
}
pub fn dropped_peers(&self) -> impl Iterator<Item = PexPeerInfo> + '_ {
pub fn dropped_peers(&self) -> impl Iterator<Item = PexPeerInfo> + Clone + '_ {
self.added_peers_inner(&self.dropped, &None, 4)
.chain(self.added_peers_inner(&self.dropped6, &None, 16))
}