Skip to content
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

Add a variadic variation to middle::Cif #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions libffi-rs/src/middle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,40 @@ impl Cif {
Cif { cif, args, result }
}

/// Creates a new variadic [CIF](Cif) for the given argument and result
/// types.
///
/// Takes ownership of the argument and result [`Type`]s, because
/// the resulting [`Cif`] retains references to them. Defaults to
/// the platform’s default calling convention; this can be adjusted
/// using [`Cif::set_abi`].
pub fn new_variadic<I>(args: I, fixed_args: usize, result: Type) -> Self
where
I: IntoIterator<Item = Type>,
I::IntoIter: ExactSizeIterator<Item = Type>,
{
let args = args.into_iter();
let nargs = args.len();
let args = types::TypeArray::new(args);
let mut cif: low::ffi_cif = Default::default();

unsafe {
low::prep_cif_var(
&mut cif,
low::ffi_abi_FFI_DEFAULT_ABI,
fixed_args,
nargs,
result.as_raw_ptr(),
args.as_raw_ptr(),
)
}
.expect("low::prep_cif_var");

// Note that cif retains references to args and result,
// which is why we hold onto them here.
Cif { cif, args, result }
}

/// Calls a function with the given arguments.
///
/// In particular, this method invokes function `fun` passing it
Expand Down