8000 New `middle::Type` by emiltayl · Pull Request #152 · tov/libffi-rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

New middle::Type #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libffi-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ rust-version.workspace = true

[dependencies]
libffi-sys = { path = "../libffi-sys-rs", version = "^3.3", default-features = false }
libc = "0.2.65"

[features]
complex = []
Expand Down
2 changes: 1 addition & 1 deletion libffi-rs/examples/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use libffi::middle::Type;

fn main() {
Type::structure(vec![Type::u16(), Type::u16()]);
Type::structure(vec![Type::U16, Type::U16]);
}
14 changes: 3 additions & 11 deletions libffi-rs/src/high/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//! assert!((result - 5f32).abs() < 0.0001);
//! ```

use core::convert::TryInto;

use crate::middle;
pub use middle::CodePtr;

Expand Down Expand Up @@ -74,21 +72,15 @@ pub fn arg<T: super::CType>(arg: &T) -> Arg {
/// # Safety
/// The signature of the function pointer must match the types of the arguments and the return type.
/// If the types do not match, we get UB.
pub unsafe fn call<R: super::CType>(fun: CodePtr, args: &[Arg]) -> R {
pub unsafe fn call<R: super::CRetType>(fun: CodePtr, args: &[Arg]) -> R {
let types = args.iter().map(|arg| arg.type_.clone());
let cif = middle::Cif::new(types, R::reify().into_middle());
let cif = middle::Cif::new(types, R::get_return_type());

let values = args
.iter()
.map(|arg| arg.value.clone())
.collect::<alloc::vec::Vec<_>>();
// If `R` is a small integer type, libffi implicitly extends it to
// `ffi_arg` or `ffi_sarg`. To account for this, use `R::RetType`
// as return type for the low-level call, and convert the result back.
cif.call::<R::RetType>(fun, &values)
.try_into()
.ok()
.unwrap()
cif.call::<R>(fun, &values)
}

/// Performs a dynamic call to a C function.
Expand Down
56 changes: 30 additions & 26 deletions libffi-rs/src/high/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
pub use crate::middle::{ffi_abi_FFI_DEFAULT_ABI, FfiAbi};

pub mod types;
pub use types::{CType, Type};
pub use types::{CRetType, CType, Type};

pub mod call;
pub use call::*;
Expand Down Expand Up @@ -150,14 +150,18 @@ macro_rules! define_closure_mod {
_marker: PhantomData<fn($( $T, )*) -> R>,
}

impl<$( $T, )* R> $cif<$( $T, )* R> {
impl<$( $T, )* R> $cif<$( $T, )* R>
where
R: CRetType,
{
/// Creates a new statically-typed CIF with the given argument
/// and result types.
#[allow(non_snake_case)]
pub fn new($( $T: Type<$T>, )* result: Type<R>) -> Self {
#[allow(clippy::new_without_default)]
pub fn new($( $T: Type<$T>, )*) -> Self {
let cif = middle::Cif::new(
alloc::vec![$( $T.into_middle() ),*].into_iter(),
result.into_middle());
<R as CRetType>::get_return_type());
$cif { untyped: cif, _marker: PhantomData }
}

Expand All @@ -167,11 +171,11 @@ macro_rules! define_closure_mod {
}
}

impl<$( $T: CType, )* R: CType> $cif<$( $T, )* R> {
impl<$( $T: CType, )* R: CRetType> $cif<$( $T, )* R> {
/// Creates a new statically-typed CIF by reifying the
/// argument types as `Type<T>`s.
pub fn reify() -> Self {
Self::new($( $T::reify(), )* R::reify())
Self::new($( $T::reify(), )*)
}
}

Expand Down Expand Up @@ -216,7 +220,7 @@ macro_rules! define_closure_mod {
_marker: PhantomData<fn($( $T, )*) -> R>,
}

impl<'a, $($T: CType,)* R: CType> $closure<'a, $($T,)* R> {
impl<'a, $($T: CType,)* R: CRetType> $closure<'a, $($T,)* R> {
/// Constructs a typed closure callable from C from a
/// Rust closure.
pub fn new<Callback>(callback: &'a Callback) -> Self
Expand All @@ -226,7 +230,7 @@ macro_rules! define_closure_mod {
}
}

impl<'a, $( $T, )* R: CType> $closure<'a, $( $T, )* R> {
impl<'a, $( $T, )* R: CRetType> $closure<'a, $( $T, )* R> {
/// Gets the C code pointer that is used to invoke the
/// closure.
pub fn code_ptr(&self) -> & $fnptr <'a, $( $T, )* R> {
Expand All @@ -252,10 +256,10 @@ macro_rules! define_closure_mod {
/// type of the callback must follow the libffi implicit
/// extension rules.
pub fn from_parts<U>(cif: $cif<$( $T, )* R>,
callback: $callback<U, $( $T, )* R::RetType>,
callback: $callback<U, $( $T, )* R>,
userdata: &'a U) -> Self
{
let callback: middle::Callback<U, R::RetType>
let callback: middle::Callback<U, R>
= unsafe { mem::transmute(callback) };
let closure
= middle::Closure::new(cif.untyped,
Expand All @@ -268,7 +272,7 @@ macro_rules! define_closure_mod {
}
}

impl<'a, $( $T: Copy, )* R: CType> $closure<'a, $( $T, )* R> {
impl<'a, $( $T: Copy, )* R: CRetType> $closure<'a, $( $T, )* R> {
/// Constructs a typed closure callable from C from a CIF
/// describing the calling convention for the resulting
/// function and the Rust closure to call.
Expand All @@ -284,7 +288,7 @@ macro_rules! define_closure_mod {
#[allow(non_snake_case)]
extern "C" fn static_callback<Callback>
(_cif: &low::ffi_cif,
result: &mut R::RetType,
result: &mut R,
&($( &$T, )*):
&($( &$T, )*),
userdata: &Callback)
Expand Down Expand Up @@ -312,7 +316,7 @@ macro_rules! define_closure_mod {
_marker: PhantomData<fn($( $T, )*) -> R>,
}

impl<'a, $($T: CType,)* R: CType>
impl<'a, $($T: CType,)* R: CRetType>
$closure_mut<'a, $($T,)* R>
{
/// Constructs a typed closure callable from C from a
Expand All @@ -324,7 +328,7 @@ macro_rules! define_closure_mod {
}
}

impl<'a, $( $T, )* R: CType> $closure_mut<'a, $( $T, )* R> {
impl<'a, $( $T, )* R: CRetType> $closure_mut<'a, $( $T, )* R> {
/// Gets the C code pointer that is used to invoke the
/// closure.
pub fn code_ptr(&self) -> & $fnptr <'a, $( $T, )* R> {
Expand All @@ -340,10 +344,10 @@ macro_rules! define_closure_mod {
/// type of the callback must follow the libffi implicit
/// extension rules.
pub fn from_parts<U>(cif: $cif<$( $T, )* R>,
callback: $callback_mut<U, $( $T, )* R::RetType>,
callback: $callback_mut<U, $( $T, )* R>,
userdata: &'a mut U) -> Self
{
let callback: middle::CallbackMut<U, R::RetType>
let callback: middle::CallbackMut<U, R>
= unsafe { mem::transmute(callback) };
let closure
= middle::Closure::new_mut(cif.untyped,
Expand All @@ -356,7 +360,7 @@ macro_rules! define_closure_mod {
}
}

impl<'a, $( $T: Copy, )* R: CType> $closure_mut<'a, $( $T, )* R> {
impl<'a, $( $T: Copy, )* R: CRetType> $closure_mut<'a, $( $T, )* R> {
/// Constructs a typed closure callable from C from a CIF
/// describing the calling convention for the resulting
/// function and the Rust closure to call.
Expand All @@ -373,7 +377,7 @@ macro_rules! define_closure_mod {
#[allow(non_snake_case)]
extern "C" fn static_callback<Callback>
(_cif: &low::ffi_cif,
result: &mut R::RetType,
result: &mut R,
&($( &$T, )*):
&($( &$T, )*),
userdata: &mut Callback)
Expand All @@ -398,7 +402,7 @@ macro_rules! define_closure_mod {
_marker: PhantomData<fn($( $T, )*) -> R>,
}

impl<$($T: CType,)* R: CType> $closure_once<$($T,)* R> {
impl<$($T: CType,)* R: CRetType> $closure_once<$($T,)* R> {
/// Constructs a typed closure callable from C from a
/// Rust closure.
pub fn new<Callback>(callback: Callback) -> Self
Expand All @@ -408,7 +412,7 @@ macro_rules! define_closure_mod {
}
}

impl<$( $T: Copy, )* R: CType> $closure_once<$( $T, )* R> {
impl<$( $T: Copy, )* R: CRetType> $closure_once<$( $T, )* R> {
/// Constructs a one-shot closure callable from C from a CIF
/// describing the calling convention for the resulting
/// function and the Rust closure to call.
Expand All @@ -424,7 +428,7 @@ macro_rules! define_closure_mod {
#[allow(non_snake_case)]
extern "C" fn static_callback<Callback>
(_cif: &low::ffi_cif,
result: &mut R::RetType,
result: &mut R,
&($( &$T, )*):
&($( &$T, )*),
userdata: &mut Option<Callback>)
Expand Down Expand Up @@ -455,7 +459,7 @@ macro_rules! define_closure_mod {
}
}

impl<$( $T, )* R: CType> $closure_once<$( $T, )* R> {
impl<$( $T, )* R: CRetType> $closure_once<$( $T, )* R> {
/// Gets the C code pointer that is used to invoke the
/// closure.
pub fn code_ptr(&self) -> & $fnptr <'_, $( $T, )* R> {
Expand All @@ -472,11 +476,11 @@ macro_rules! define_closure_mod {
/// extension rules.
pub fn from_parts<U: Any>(
cif: $cif<$( $T, )* R>,
callback: $callback_once<U, $( $T, )* R::RetType>,
callback: $callback_once<U, $( $T, )* R>,
userdata: U)
-> Self
{
let callback: middle::CallbackOnce<U, R::RetType>
let callback: middle::CallbackOnce<U, R>
= unsafe { mem::transmute(callback) };
let closure
= middle::ClosureOnce::new(cif.untyped,
Expand Down Expand Up @@ -557,7 +561,7 @@ mod test {
let f = |y: u64, z: u64| x + y + z;

let type_ = u64::reify();
let cif = Cif2::new(type_.clone(), type_.clone(), type_.clone());
let cif = Cif2::new(type_.clone(), type_.clone());
let closure = Closure2::new_with_cif(cif, &f);

assert_eq!(12, closure.code_ptr().call(5, 6));
Expand All @@ -572,7 +576,7 @@ mod test {
};

let type_ = u64::reify();
let cif = Cif1::new(type_.clone(), type_.clone());
let cif = Cif1::new(type_.clone());
let closure = ClosureMut1::new_with_cif(cif, &mut f);

let counter = closure.code_ptr();
Expand Down
Loading
Loading
0