use [u8;6] than u64

This commit is contained in:
Ishan Jain 2023-11-07 15:31:48 +05:30
parent a1dc067cbf
commit da50551ed9
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -5,14 +5,14 @@ use std::net::{IpAddr, Ipv6Addr};
#[derive(Debug)] #[derive(Debug)]
pub struct Record { pub struct Record {
pub lla: u64, pub lla: [u8; 6],
pub address: IpAddr, pub address: IpAddr,
} }
impl Default for Record { impl Default for Record {
fn default() -> Self { fn default() -> Self {
Self { Self {
lla: 0, lla: [0; 6],
address: IpAddr::from([0, 0, 0, 0]), address: IpAddr::from([0, 0, 0, 0]),
} }
} }
@ -29,7 +29,9 @@ pub async fn fetch_reachable_neighbours(
while let Some(resp) = resp.try_next().await.map_err(|e| e.to_string())? { while let Some(resp) = resp.try_next().await.map_err(|e| e.to_string())? {
let record: Record = resp.nlas.into_iter().fold(Default::default(), |mut r, x| { let record: Record = resp.nlas.into_iter().fold(Default::default(), |mut r, x| {
match x { match x {
Nla::LinkLocalAddress(d) if !d.is_empty() => r.lla = to_u64(&d), Nla::LinkLocalAddress(d) if !d.is_empty() => {
r.lla = [d[0], d[1], d[2], d[3], d[4], d[5]]
}
Nla::Destination(d) => { Nla::Destination(d) => {
r.address = match version { r.address = match version {
IpVersion::V4 => IpAddr::from([d[0], d[1], d[2], d[3]]), IpVersion::V4 => IpAddr::from([d[0], d[1], d[2], d[3]]),
@ -42,7 +44,7 @@ pub async fn fetch_reachable_neighbours(
r r
}); });
if record.lla != 0 { if record.lla != [0; 6] {
output.push(record); output.push(record);
} }
} }
@ -50,20 +52,6 @@ pub async fn fetch_reachable_neighbours(
Ok(output) Ok(output)
} }
#[inline]
const fn to_u64(v: &[u8]) -> u64 {
let mut out = 0;
out |= (v[5] as u64) << ((5 - 5) * 8);
out |= (v[4] as u64) << ((5 - 4) * 8);
out |= (v[3] as u64) << ((5 - 3) * 8);
out |= (v[2] as u64) << ((5 - 2) * 8);
out |= (v[1] as u64) << ((5 - 1) * 8);
out |= (v[0] as u64) << ((5 - 0) * 8);
out
}
fn to_u128(v: &[u8]) -> u128 { fn to_u128(v: &[u8]) -> u128 {
let mut out = 0; let mut out = 0;
let l = v.len() - 1; let l = v.len() - 1;