netmap_rs/
frame.rs

1use std::borrow::Cow;
2use std::ops::Deref;
3
4/// A view of a packet, potentially zero-copy (for Netmap sys) or owned (for fallback).
5pub struct Frame<'a> {
6    data: Cow<'a, [u8]>,
7}
8
9impl<'a> Frame<'a> {
10    /// Create a new frame from a borrowed byte slice (zero-copy).
11    pub fn new_borrowed(data: &'a [u8]) -> Self {
12        Self {
13            data: Cow::Borrowed(data),
14        }
15    }
16
17    /// Create a new frame from an owned vector of bytes (for fallback).
18    pub fn new_owned(data: Vec<u8>) -> Self {
19        Self {
20            data: Cow::Owned(data),
21        }
22    }
23
24    /// get the length of the frame
25    pub fn len(&self) -> usize {
26        self.data.len()
27    }
28
29    /// check if the frame is empty
30    pub fn is_empty(&self) -> bool {
31        self.data.is_empty()
32    }
33
34    /// get the payload as a byte slice
35    pub fn payload(&self) -> &[u8] {
36        self.data.as_ref()
37    }
38}
39
40impl Deref for Frame<'_> {
41    type Target = [u8];
42
43    fn deref(&self) -> &Self::Target {
44        self.data.as_ref()
45    }
46}
47impl<'a> From<&'a [u8]> for Frame<'a> {
48    fn from(data: &'a [u8]) -> Self {
49        Self::new_borrowed(data)
50    }
51}