freeswitch_rs/
frame.rs

1use crate::types::switch_frame_t;
2use std::ffi::c_void;
3use std::mem;
4
5/// A wrapper around FreeSWITCH's `switch_frame_t` structure with an associated buffer.
6///
7/// This structure combines a raw FreeSWITCH frame with a mutable byte buffer,
8/// ensuring the buffer remains valid for the lifetime of the frame.
9pub struct Frame<'a>(pub(crate) switch_frame_t, pub(crate) &'a mut [u8]);
10
11impl<'a> Frame<'a> {
12    /// Creates a new `Frame` initialized with the provided buffer.
13    ///
14    /// # Examples
15    ///
16    /// ```
17    /// use freeswitch_rs::Frame;
18    ///
19    /// let mut buffer = vec![0u8; 1024];
20    /// let frame = Frame::new(&mut buffer);
21    /// ```
22    pub fn new(buf: &'a mut [u8]) -> Self {
23        let mut f = unsafe { mem::MaybeUninit::<switch_frame_t>::zeroed().assume_init() };
24        f.buflen = buf.len().min(u32::MAX as usize) as u32;
25        f.data = buf.as_mut_ptr() as *mut c_void;
26        Self(f, buf)
27    }
28}
29
30impl<'a> Frame<'a> {
31    /// Returns a reference to the frame's data buffer.
32    pub fn data(&'a self) -> &'a [u8] {
33        self.1
34    }
35}