hakuban 0.7.2

Data-object sharing library
Documentation
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use futures::{FutureExt, StreamExt};
use wasm_bindgen::prelude::*;

use super::{WasmFutureReturningPointer, WasmResultStatus, WasmResultWithPointer};
use crate::{object::ObjectStateStream, ObjectDescriptor};


#[wasm_bindgen]
pub fn hakuban_object_state_stream_drop(object_state_stream_pointer: *mut ObjectStateStream) {
	drop(unsafe { Box::from_raw(object_state_stream_pointer) });
}


#[wasm_bindgen]
pub fn hakuban_object_state_stream_next(object_state_stream_pointer: *mut ObjectStateStream) -> *mut WasmFutureReturningPointer {
	let object_state_stream: &mut ObjectStateStream = unsafe { object_state_stream_pointer.as_mut().unwrap() };
	Box::into_raw(Box::new(WasmFutureReturningPointer::new(
		object_state_stream.next().map(|item| item.map(|state| Box::into_raw(Box::new(state)) as *mut u8).into()),
	)))
}


#[wasm_bindgen]
pub fn hakuban_object_state_stream_current(object_state_stream_pointer: *mut ObjectStateStream) -> WasmResultWithPointer {
	let object_state_stream: &mut ObjectStateStream = unsafe { object_state_stream_pointer.as_mut().unwrap() };
	if let Some(state) = object_state_stream.current() {
		WasmResultWithPointer::ok(Box::into_raw(Box::new(state)))
	} else {
		WasmResultWithPointer::error(WasmResultStatus::NotAvailable)
	}
}


#[wasm_bindgen]
pub fn hakuban_object_state_stream_descriptor(object_state_stream_pointer: *mut ObjectStateStream) -> *mut ObjectDescriptor {
	let object_state_stream = unsafe { object_state_stream_pointer.as_mut().unwrap() };
	Box::into_raw(Box::new(object_state_stream.descriptor().clone()))
}