rquickjs_extra_utils/ffi/
c_vec.rs

1use rquickjs::{Result, TypedArray, Value};
2
3use crate::result::ResultExt;
4
5#[derive(Debug)]
6pub struct CVec<'js> {
7    ptr: *const u8,
8    len: usize,
9    #[allow(dead_code)]
10    value: Value<'js>,
11}
12
13#[allow(dead_code)]
14impl<'js> CVec<'js> {
15    pub fn from_array(array: TypedArray<'js, u8>) -> Result<Self> {
16        let raw = array.as_raw().or_throw(array.ctx())?;
17        Ok(Self {
18            ptr: raw.ptr.as_ptr(),
19            len: raw.len,
20            value: array.into_value(),
21        })
22    }
23
24    pub fn as_ptr(&self) -> *const u8 {
25        self.ptr
26    }
27
28    pub fn len(&self) -> usize {
29        self.len
30    }
31
32    pub fn is_empty(&self) -> bool {
33        self.len == 0
34    }
35
36    pub fn as_slice(&self) -> &[u8] {
37        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
38    }
39}