refactor: replace bincode with zerocopy

Replaces bincode with zerocopy for deserialization of network data, improving performance and reducing dependencies.
This commit is contained in:
Nils Andreas Svee committed 2026-02-21 18:44:43 +07:00
1 parent f3e7729f43
commit 5838b0b9ee
3 files changed
+32 -45

No files matched your search

+7 -10
View File
@@ -1,5 +1,3 @@
use bincode::error::DecodeError;
use bincode::BorrowDecode;
use neli::consts::nl::NlmF;
use neli::consts::rtnl::{Arphrd, Iff, Ifla, RtAddrFamily, Rtm, Tca};
use neli::consts::socket::NlFamily;
@@ -12,11 +10,13 @@ use neli::utils::Groups;
use std::io;
use std::str::Utf8Error;
use thiserror::Error;
use zerocopy::FromBytes;
use zerocopy_derive::{FromBytes, Immutable, KnownLayout};
#[derive(Debug, Error)]
pub enum NetlinkError {
#[error("Couldn't deserialize to struct")]
Deserialization(#[from] Box<DecodeError>),
#[error("Couldn't deserialize to struct: {0}")]
Deserialization(String),
#[error("Couldn't find intreface `{0}`")]
InterfaceNotFound(String),
@@ -52,7 +52,7 @@ pub struct Qdisc {
parent: u32,
}
#[derive(BorrowDecode, Copy, Clone, Default, Debug)]
#[derive(Copy, Clone, Default, Debug, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
pub struct RtnlLinkStats64 {
pub rx_packets: u64,
@@ -172,11 +172,8 @@ impl Netlink {
for attr in p.rtattrs().iter() {
if attr.rta_type() == &Ifla::Stats64 {
let buf = attr.rta_payload().as_ref();
let stats: RtnlLinkStats64 =
bincode::borrow_decode_from_slice(buf, bincode::config::standard())
.map_err(|e| NetlinkError::Deserialization(Box::new(e)))?
.0;
let stats = RtnlLinkStats64::read_from_bytes(buf)
.map_err(|e| NetlinkError::Deserialization(e.to_string()))?;
return Ok(stats);
}