netlink_packet_route/link/
phys_id.rs

1// SPDX-License-Identifier: MIT
2
3use netlink_packet_utils::{
4    traits::{Emitable, Parseable},
5    DecodeError,
6};
7
8const MAX_PHYS_ITEM_ID_LEN: usize = 32;
9
10#[derive(Debug, Clone, Copy, Eq, PartialEq)]
11#[non_exhaustive]
12pub struct LinkPhysId {
13    pub id: [u8; MAX_PHYS_ITEM_ID_LEN],
14    pub len: usize,
15}
16
17impl Parseable<[u8]> for LinkPhysId {
18    fn parse(buf: &[u8]) -> Result<Self, DecodeError> {
19        let len = buf.len() % MAX_PHYS_ITEM_ID_LEN;
20        let mut id = [0; MAX_PHYS_ITEM_ID_LEN];
21        id[..len].copy_from_slice(&buf[..len]);
22        Ok(Self { id, len })
23    }
24}
25
26impl Emitable for LinkPhysId {
27    fn buffer_len(&self) -> usize {
28        self.len
29    }
30
31    fn emit(&self, buffer: &mut [u8]) {
32        buffer[..self.len].copy_from_slice(&self.id[..self.len])
33    }
34}