#![doc(hidden)]
use std::mem;
pub use crate::ffi::*;
pub use lazy_static::lazy_static;
pub use crate::handler::DefaultHandler;
pub fn new_leak_vec_ptr<T: Clone>(fill: T, length: i32) -> *mut T {
into_leak_vec_ptr(vec![fill; length as usize]).0
}
pub fn into_leak_vec_ptr<T: Clone>(mut v: Vec<T>) -> (*mut T, i32) {
v.shrink_to_fit();
assert!(v.len() == v.capacity());
let ptr = v.as_mut_ptr();
let len = v.len() as i32;
mem::forget(v);
(ptr, len)
}
pub unsafe fn vec_from_leak_ptr<T>(ptr: *mut T, len: i32) -> Vec<T> {
Vec::from_raw_parts(ptr, len as usize, len as usize)
}
pub fn from_vec_to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {
core::convert::TryInto::try_into(v)
.unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
}
pub fn new_leak_box_ptr<T>(t: T) -> *mut T {
let x: Box<T> = Box::new(t);
Box::into_raw(x)
}
pub unsafe fn box_from_leak_ptr<T>(ptr: *mut T) -> Box<T> {
Box::from_raw(ptr)
}
pub fn slice_from_byte_buffer<T: bytemuck::Pod>(buffer: Vec<u8>) -> Box<[T]> {
let buf = Box::leak(buffer.into_boxed_slice());
match bytemuck::try_cast_slice_mut(buf) {
Ok(buf) => unsafe { Box::from_raw(buf) },
Err(err) => {
unsafe { core::ptr::drop_in_place(buf) }
panic!("cast error: {}", err);
}
}
}
#[cfg(not(wasm))]
use allo_isolate::ffi::DartCObject;
#[cfg(not(wasm))]
pub type WireSyncReturn = *mut DartCObject;
#[cfg(wasm)]
pub type WireSyncReturn = wasm_bindgen::JsValue;