lune/crates/lune-std-ffi/src/ffi/ffi_native/cast.rs

29 lines
528 B
Rust

#![allow(clippy::inline_always)]
use std::cell::Ref;
use mlua::prelude::*;
use num::cast::AsPrimitive;
use super::NativeData;
// Cast T as U
#[inline(always)]
pub fn native_num_cast<T, U>(
from: &Ref<dyn NativeData>,
into: &Ref<dyn NativeData>,
) -> LuaResult<()>
where
T: AsPrimitive<U>,
U: 'static + Copy,
{
let from_ptr = unsafe { from.get_pointer().cast::<T>() };
let into_ptr = unsafe { into.get_pointer().cast::<U>() };
unsafe {
*into_ptr = (*from_ptr).as_();
}
Ok(())
}