netlink_packet_route/link/
phys_id.rs

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