Issue 3223: Second phase of migration to Rust Edition
Reformat via `cargo fmt`
This commit is contained in:
102
src/ffi.rs
102
src/ffi.rs
@@ -32,7 +32,7 @@ use ordered_float::OrderedFloat;
|
||||
use std::alloc::{self, Layout};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::ffi::{c_char, c_void, CStr, CString};
|
||||
use std::ffi::{CStr, CString, c_char, c_void};
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::ManuallyDrop;
|
||||
@@ -53,27 +53,33 @@ pub struct FunctionImpl {
|
||||
}
|
||||
|
||||
impl FunctionImpl {
|
||||
unsafe fn call_void(&self, args: &[Arg], _: &mut Arena) -> Result<Value, FfiError> { unsafe {
|
||||
self.cif.call_return_into(self.code_ptr, args, Ret::void());
|
||||
Ok(Value::Number(Number::Fixnum(Fixnum::build_with(0))))
|
||||
}}
|
||||
unsafe fn call_void(&self, args: &[Arg], _: &mut Arena) -> Result<Value, FfiError> {
|
||||
unsafe {
|
||||
self.cif.call_return_into(self.code_ptr, args, Ret::void());
|
||||
Ok(Value::Number(Number::Fixnum(Fixnum::build_with(0))))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn call_int<T>(&self, args: &[Arg], arena: &mut Arena) -> Result<Value, FfiError>
|
||||
where
|
||||
Integer: From<T>,
|
||||
T: Copy + TryInto<i64> + MightNotFitInFixnum,
|
||||
{ unsafe {
|
||||
let n = self.cif.call::<T>(self.code_ptr, args);
|
||||
Ok(Value::Number(fixnum!(Number, n, arena)))
|
||||
}}
|
||||
{
|
||||
unsafe {
|
||||
let n = self.cif.call::<T>(self.code_ptr, args);
|
||||
Ok(Value::Number(fixnum!(Number, n, arena)))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn call_float<T>(&self, args: &[Arg], _: &mut Arena) -> Result<Value, FfiError>
|
||||
where
|
||||
T: Into<f64>,
|
||||
{ unsafe {
|
||||
let n = self.cif.call::<T>(self.code_ptr, args);
|
||||
Ok(Value::Number(Number::Float(OrderedFloat(n.into()))))
|
||||
}}
|
||||
{
|
||||
unsafe {
|
||||
let n = self.cif.call::<T>(self.code_ptr, args);
|
||||
Ok(Value::Number(Number::Float(OrderedFloat(n.into()))))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn call_ptr(&self, args: &[Arg], arena: &mut Arena) -> Result<Value, FfiError> {
|
||||
let ptr = unsafe { self.cif.call::<*mut c_void>(self.code_ptr, args) };
|
||||
@@ -147,7 +153,7 @@ impl FunctionImpl {
|
||||
FfiType::Ptr => FunctionImpl::call_ptr,
|
||||
FfiType::CStr => FunctionImpl::call_cstr,
|
||||
FfiType::Struct(name) => {
|
||||
return unsafe { self.call_struct(name, args, arena, structs_table) }
|
||||
return unsafe { self.call_struct(name, args, arena, structs_table) };
|
||||
}
|
||||
};
|
||||
unsafe { call_fn(self, args, arena) }
|
||||
@@ -197,14 +203,16 @@ impl StructImpl {
|
||||
ptr: NonNull<c_void>,
|
||||
layout: &mut Layout,
|
||||
val: T,
|
||||
) -> Result<(), FfiError> { unsafe {
|
||||
let (new_layout, offset) = layout
|
||||
.extend(Layout::new::<T>())
|
||||
.map_err(|_| FfiError::LayoutError)?;
|
||||
*layout = new_layout;
|
||||
ptr.byte_offset(offset as isize).cast::<T>().write(val);
|
||||
Ok(())
|
||||
}}
|
||||
) -> Result<(), FfiError> {
|
||||
unsafe {
|
||||
let (new_layout, offset) = layout
|
||||
.extend(Layout::new::<T>())
|
||||
.map_err(|_| FfiError::LayoutError)?;
|
||||
*layout = new_layout;
|
||||
ptr.byte_offset(offset as isize).cast::<T>().write(val);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
for arg in args {
|
||||
unsafe {
|
||||
@@ -258,14 +266,16 @@ impl StructImpl {
|
||||
unsafe fn read_primitive<T>(
|
||||
ptr: *mut c_void,
|
||||
layout: &mut Layout,
|
||||
) -> Result<T, FfiError> { unsafe {
|
||||
let (new_layout, offset) = layout
|
||||
.extend(Layout::new::<T>())
|
||||
.map_err(|_| FfiError::LayoutError)?;
|
||||
*layout = new_layout;
|
||||
let n = std::ptr::read::<T>(ptr.byte_offset(offset as isize).cast());
|
||||
Ok(n)
|
||||
}}
|
||||
) -> Result<T, FfiError> {
|
||||
unsafe {
|
||||
let (new_layout, offset) = layout
|
||||
.extend(Layout::new::<T>())
|
||||
.map_err(|_| FfiError::LayoutError)?;
|
||||
*layout = new_layout;
|
||||
let n = std::ptr::read::<T>(ptr.byte_offset(offset as isize).cast());
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn read_int<T>(
|
||||
ptr: *mut c_void,
|
||||
@@ -275,10 +285,12 @@ impl StructImpl {
|
||||
where
|
||||
T: Copy + TryInto<i64> + MightNotFitInFixnum,
|
||||
Integer: From<T>,
|
||||
{ unsafe {
|
||||
let n = read_primitive::<T>(ptr, layout)?;
|
||||
Ok(Value::Number(fixnum!(Number, n, arena)))
|
||||
}}
|
||||
{
|
||||
unsafe {
|
||||
let n = read_primitive::<T>(ptr, layout)?;
|
||||
Ok(Value::Number(fixnum!(Number, n, arena)))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn read_float<T>(
|
||||
ptr: *mut c_void,
|
||||
@@ -286,10 +298,12 @@ impl StructImpl {
|
||||
) -> Result<Value, FfiError>
|
||||
where
|
||||
T: Into<f64>,
|
||||
{ unsafe {
|
||||
let n = read_primitive::<T>(ptr, layout)?;
|
||||
Ok(Value::Number(Number::Float(OrderedFloat(n.into()))))
|
||||
}}
|
||||
{
|
||||
unsafe {
|
||||
let n = read_primitive::<T>(ptr, layout)?;
|
||||
Ok(Value::Number(Number::Float(OrderedFloat(n.into()))))
|
||||
}
|
||||
}
|
||||
|
||||
let mut layout = Layout::from_size_align(0, 1).map_err(|_| FfiError::LayoutError)?;
|
||||
|
||||
@@ -332,7 +346,7 @@ impl StructImpl {
|
||||
Ok(struct_val)
|
||||
}
|
||||
FfiType::Void => {
|
||||
return Err(FfiError::UnsupportedArgumentType(Some(atom!("void"))))
|
||||
return Err(FfiError::UnsupportedArgumentType(Some(atom!("void"))));
|
||||
}
|
||||
};
|
||||
returns.push(val?);
|
||||
@@ -788,10 +802,12 @@ impl ForeignFunctionTable {
|
||||
where
|
||||
T: Copy + TryInto<i64> + MightNotFitInFixnum,
|
||||
Integer: From<T>,
|
||||
{ unsafe {
|
||||
let n = ptr.cast::<T>().read();
|
||||
Value::Number(fixnum!(Number, n, arena))
|
||||
}}
|
||||
{
|
||||
unsafe {
|
||||
let n = ptr.cast::<T>().read();
|
||||
Value::Number(fixnum!(Number, n, arena))
|
||||
}
|
||||
}
|
||||
|
||||
let ptr = ptr.as_ptr()?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user