lune/crates/lune-std-ffi/src/ffi/cast.rs
2024-11-02 14:39:11 +00:00

35 lines
661 B
Rust

use std::cell::Ref;
use mlua::prelude::*;
use num::cast::AsPrimitive;
use super::FfiData;
#[inline]
pub fn num_cast<From, Into>(
from: &Ref<dyn FfiData>,
into: &Ref<dyn FfiData>,
from_offset: isize,
into_offset: isize,
) -> LuaResult<()>
where
From: AsPrimitive<Into>,
Into: 'static + Copy,
{
let from_ptr = unsafe {
from.get_inner_pointer()
.byte_offset(from_offset)
.cast::<From>()
};
let into_ptr = unsafe {
into.get_inner_pointer()
.byte_offset(into_offset)
.cast::<Into>()
};
unsafe {
*into_ptr = (*from_ptr).as_();
}
Ok(())
}