use crate::peer::*;
use anyhow::Result;
#[derive(Debug, Default)]
pub struct Buffer(pub Vec<Peer>);
impl Buffer {
pub fn from_peers(ip_port_chunks: &[u8]) -> Result<Self> {
let mut b = Vec::with_capacity(ip_port_chunks.len() / IP4_LEN);
for c in ip_port_chunks.chunks_exact(IP4_LEN) {
b.push(Peer::from_be_bytes(c)?)
}
Ok(Self(b))
}
pub fn from_peers6(ip_port_chunks: &[u8]) -> Result<Self> {
let mut b = Vec::with_capacity(ip_port_chunks.len() / IP6_LEN);
for c in ip_port_chunks.chunks_exact(IP6_LEN) {
b.push(Peer::from_be_bytes(c)?)
}
Ok(Self(b))
}
pub fn from_peers_i2p(host_chunks: &[u8]) -> Result<Self> {
let mut b = Vec::with_capacity(host_chunks.len() / I2P_LEN);
for c in host_chunks.chunks_exact(I2P_LEN) {
b.push(Peer::from_be_bytes(c)?)
}
Ok(Self(b))
}
}