tov / libffi-rs

Rust bindings for libffi
Apache License 2.0
104 stars 36 forks source link

middle::Cif needs a variadic variation for its new function #55

Closed r0nsha closed 2 years ago

r0nsha commented 2 years ago

While transitioning from the low api to the middle api, I found that there is no new_variadic function for Cif, which is a must for me. I can't create a pull request, so I'm pasting the function I created for myself here in hope that some variation of this function will be merged :)

    /// 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 }
    }
r0nsha commented 2 years ago

Closing in favor of a PR