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(data: &'a [u8]) -> Self {
12        Self {
13            data: Cow::Borrowed(data),
14        }
15    }
16
17    /// Create a new frame from a borrowed byte slice (zero-copy).
18    /// This is an alias for `new` for backward compatibility.
19    pub fn new_borrowed(data: &'a [u8]) -> Self {
20        Self::new(data)
21    }
22
23    /// Create a new frame from an owned vector of bytes (for fallback).
24    pub fn new_owned(data: Vec<u8>) -> Self {
25        Self {
26            data: Cow::Owned(data),
27        }
28    }
29
30    /// get the length of the frame
31    pub fn len(&self) -> usize {
32        self.data.len()
33    }
34
35    /// check if the frame is empty
36    pub fn is_empty(&self) -> bool {
37        self.data.is_empty()
38    }
39
40    /// get the payload as a byte slice
41    pub fn payload(&self) -> &[u8] {
42        self.data.as_ref()
43    }
44}
45
46impl Deref for Frame<'_> {
47    type Target = [u8];
48
49    fn deref(&self) -> &Self::Target {
50        self.data.as_ref()
51    }
52}
53
54impl<'a> From<&'a [u8]> for Frame<'a> {
55    fn from(data: &'a [u8]) -> Self {
56        Self::new(data)
57    }
58}