mirror of
https://github.com/lune-org/lune.git
synced 2025-04-09 21:10:53 +01:00
Add offset option in cast method (#243)
This commit is contained in:
parent
37b55576ca
commit
4cc2698e30
3 changed files with 29 additions and 8 deletions
|
@ -21,6 +21,8 @@ pub trait CTypeCast {
|
||||||
into_ctype: &LuaAnyUserData,
|
into_ctype: &LuaAnyUserData,
|
||||||
_from: &Ref<dyn FfiData>,
|
_from: &Ref<dyn FfiData>,
|
||||||
_into: &Ref<dyn FfiData>,
|
_into: &Ref<dyn FfiData>,
|
||||||
|
_from_offset: isize,
|
||||||
|
_into_offset: isize,
|
||||||
) -> LuaResult<()> {
|
) -> LuaResult<()> {
|
||||||
// Show error if have no cast implement
|
// Show error if have no cast implement
|
||||||
Err(Self::cast_failed_with(self, from_ctype, into_ctype))
|
Err(Self::cast_failed_with(self, from_ctype, into_ctype))
|
||||||
|
@ -114,17 +116,21 @@ where
|
||||||
methods.add_function(
|
methods.add_function(
|
||||||
"cast",
|
"cast",
|
||||||
|_,
|
|_,
|
||||||
(from_type, into_type, from, into): (
|
(from_type, into_type, from, into, from_offset, into_offset): (
|
||||||
LuaAnyUserData,
|
LuaAnyUserData,
|
||||||
LuaAnyUserData,
|
LuaAnyUserData,
|
||||||
LuaAnyUserData,
|
LuaAnyUserData,
|
||||||
LuaAnyUserData,
|
LuaAnyUserData,
|
||||||
|
Option<isize>,
|
||||||
|
Option<isize>,
|
||||||
)| {
|
)| {
|
||||||
from_type.borrow::<Self>()?.cast(
|
from_type.borrow::<Self>()?.cast(
|
||||||
&from_type,
|
&from_type,
|
||||||
&into_type,
|
&into_type,
|
||||||
&from.get_ffi_data()?,
|
&from.get_ffi_data()?,
|
||||||
&into.get_ffi_data()?,
|
&into.get_ffi_data()?,
|
||||||
|
from_offset.unwrap_or(0),
|
||||||
|
into_offset.unwrap_or(0),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub mod u64;
|
||||||
pub mod u8;
|
pub mod u8;
|
||||||
pub mod usize;
|
pub mod usize;
|
||||||
|
|
||||||
// create CType userdata and export
|
// CType userdata export
|
||||||
macro_rules! create_ctypes {
|
macro_rules! create_ctypes {
|
||||||
($lua:ident, $(( $name:expr, $rust_type:ty, $libffi_type:expr ),)* ) => {
|
($lua:ident, $(( $name:expr, $rust_type:ty, $libffi_type:expr ),)* ) => {
|
||||||
Ok(vec![$((
|
Ok(vec![$((
|
||||||
|
@ -82,9 +82,9 @@ pub fn export_fixed_types(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaAnyUserD
|
||||||
|
|
||||||
// Implement type-casting for numeric ctypes
|
// Implement type-casting for numeric ctypes
|
||||||
macro_rules! define_cast_num {
|
macro_rules! define_cast_num {
|
||||||
($from_rust_type:ident, $self:ident, $from_ctype:ident, $into_ctype:ident, $from:ident, $into:ident, $($into_rust_type:ty)*) => {
|
($from_rust_type:ident, $self:ident, $from_ctype:ident, $into_ctype:ident, $from:ident, $into:ident, $fromOffset:ident, $intoOffset:ident, $($into_rust_type:ty)*) => {
|
||||||
$( if $into_ctype.is::<CTypeInfo<$into_rust_type>>() {
|
$( if $into_ctype.is::<CTypeInfo<$into_rust_type>>() {
|
||||||
num_cast::<$from_rust_type, $into_rust_type>($from, $into)
|
num_cast::<$from_rust_type, $into_rust_type>($from, $into, $fromOffset, $intoOffset)
|
||||||
} else )* {
|
} else )* {
|
||||||
Err($self.cast_failed_with($from_ctype, $into_ctype))
|
Err($self.cast_failed_with($from_ctype, $into_ctype))
|
||||||
}
|
}
|
||||||
|
@ -113,9 +113,11 @@ where
|
||||||
into_info: &LuaAnyUserData,
|
into_info: &LuaAnyUserData,
|
||||||
from: &Ref<dyn FfiData>,
|
from: &Ref<dyn FfiData>,
|
||||||
into: &Ref<dyn FfiData>,
|
into: &Ref<dyn FfiData>,
|
||||||
|
from_offset: isize,
|
||||||
|
into_offset: isize,
|
||||||
) -> LuaResult<()> {
|
) -> LuaResult<()> {
|
||||||
define_cast_num!(
|
define_cast_num!(
|
||||||
From, self, from_info, into_info, from, into,
|
From, self, from_info, into_info, from, into, from_offset, into_offset,
|
||||||
u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 usize isize
|
u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 usize isize
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,26 @@ use num::cast::AsPrimitive;
|
||||||
use super::FfiData;
|
use super::FfiData;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn num_cast<From, Into>(from: &Ref<dyn FfiData>, into: &Ref<dyn FfiData>) -> LuaResult<()>
|
pub fn num_cast<From, Into>(
|
||||||
|
from: &Ref<dyn FfiData>,
|
||||||
|
into: &Ref<dyn FfiData>,
|
||||||
|
from_offset: isize,
|
||||||
|
into_offset: isize,
|
||||||
|
) -> LuaResult<()>
|
||||||
where
|
where
|
||||||
From: AsPrimitive<Into>,
|
From: AsPrimitive<Into>,
|
||||||
Into: 'static + Copy,
|
Into: 'static + Copy,
|
||||||
{
|
{
|
||||||
let from_ptr = unsafe { from.get_inner_pointer().cast::<From>() };
|
let from_ptr = unsafe {
|
||||||
let into_ptr = unsafe { into.get_inner_pointer().cast::<Into>() };
|
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 {
|
unsafe {
|
||||||
*into_ptr = (*from_ptr).as_();
|
*into_ptr = (*from_ptr).as_();
|
||||||
|
|
Loading…
Add table
Reference in a new issue