1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// SPDX-License-Identifier: MIT

use netlink_packet_utils::{DecodeError, Emitable, Parseable};

const VF_INFO_BROADCAST_LEN: usize = 32;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
#[non_exhaustive]
pub struct VfInfoBroadcast {
    pub addr: [u8; VF_INFO_BROADCAST_LEN],
}

impl VfInfoBroadcast {
    pub fn new(addr: &[u8]) -> Self {
        let mut ret = Self::default();
        if addr.len() > VF_INFO_BROADCAST_LEN {
            ret.addr.copy_from_slice(&addr[..VF_INFO_BROADCAST_LEN])
        } else {
            ret.addr[..addr.len()].copy_from_slice(addr)
        }
        ret
    }
}

buffer!(VfInfoBroadcastBuffer(VF_INFO_BROADCAST_LEN) {
    addr: (slice, 0..VF_INFO_BROADCAST_LEN),
});

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<VfInfoBroadcastBuffer<&'a T>>
    for VfInfoBroadcast
{
    fn parse(buf: &VfInfoBroadcastBuffer<&T>) -> Result<Self, DecodeError> {
        Ok(Self::new(buf.addr()))
    }
}

impl Emitable for VfInfoBroadcast {
    fn buffer_len(&self) -> usize {
        VF_INFO_BROADCAST_LEN
    }

    fn emit(&self, buffer: &mut [u8]) {
        let mut buffer = VfInfoBroadcastBuffer::new(buffer);
        buffer.addr_mut().copy_from_slice(&self.addr);
    }
}