ws_sdk/stream/
data.rs

1use super::super::host::abi::*;
2use anyhow::{bail, Result};
3
4/// Retrieves the payload in the event as the stream source.
5///
6/// # Examples
7///
8/// ```no_run
9/// use ws-sdk::stream::get_data
10/// let data = get_data(rid)?;
11/// ```
12pub fn get_data(resource_id: u32) -> Result<Vec<u8>> {
13    let data_ptr = &mut (0 as i32) as *const _ as *const *mut u8;
14    let data_size = &mut (0 as i32) as *const i32;
15    match unsafe { ws_get_data(resource_id as _, data_ptr, data_size) } {
16        0 => Ok(unsafe { Vec::from_raw_parts(*data_ptr, *data_size as _, *data_size as _) }),
17        _ => bail!("fail to get data by resource id"),
18    }
19}
20
21/// Sets the data for the sink of the stream.
22///
23/// # Examples
24///
25/// ```no_run
26/// use ws-sdk::stream::set_data
27/// set_data(rid, data)?;
28/// ```
29pub fn set_data(resource_id: u32, data: Vec<u8>) -> Result<()> {
30    match unsafe { ws_set_data(resource_id as _, data.as_ptr(), data.len() as _) } {
31        0 => Ok(()),
32        _ => bail!("fail to set data by resource id"),
33    }
34}