netlink_packet_route/link/
phys_id.rs

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