Refactor(netlink): Replaces zerocopy with bytemuck
Migrates from `zerocopy` to `bytemuck` for improved data serialization and deserialization in netlink communication. This enhances maintainability.
This commit is contained in:
3 files changed
+29
-31
No files matched your search
+7
-7
@@ -1,3 +1,5 @@
|
||||
use bytemuck::AnyBitPattern;
|
||||
use bytemuck::checked::{CheckedCastError, try_from_bytes};
|
||||
use neli::consts::nl::NlmF;
|
||||
use neli::consts::rtnl::{Arphrd, Iff, Ifla, RtAddrFamily, Rtm, Tca};
|
||||
use neli::consts::socket::NlFamily;
|
||||
@@ -10,13 +12,11 @@ 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: {0}")]
|
||||
Deserialization(String),
|
||||
Deserialization(CheckedCastError),
|
||||
|
||||
#[error("Couldn't find intreface `{0}`")]
|
||||
InterfaceNotFound(String),
|
||||
@@ -52,7 +52,7 @@ pub struct Qdisc {
|
||||
parent: u32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, FromBytes, Immutable, KnownLayout)]
|
||||
#[derive(AnyBitPattern, Copy, Clone, Default, Debug)]
|
||||
#[repr(C)]
|
||||
pub struct RtnlLinkStats64 {
|
||||
pub rx_packets: u64,
|
||||
@@ -172,10 +172,10 @@ impl Netlink {
|
||||
for attr in p.rtattrs().iter() {
|
||||
if attr.rta_type() == &Ifla::Stats64 {
|
||||
let buf = attr.rta_payload().as_ref();
|
||||
let stats = RtnlLinkStats64::read_from_bytes(buf)
|
||||
.map_err(|e| NetlinkError::Deserialization(e.to_string()))?;
|
||||
let stats: &RtnlLinkStats64 = try_from_bytes::<RtnlLinkStats64>(buf)
|
||||
.map_err(|e| NetlinkError::Deserialization(e))?;
|
||||
|
||||
return Ok(stats);
|
||||
return Ok(stats.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user