pub struct VaList<'a> { /* private fields */ }c_variadic #44930)Expand description
A variable argument list, ABI-compatible with va_list in C.
This type is created in c-variadic functions when ... is desugared. A VaList
is automatically initialized (equivalent to calling va_start in C).
#![feature(c_variadic)]
use std::ffi::VaList;
/// # Safety
/// Must be passed at least `count` arguments of type `i32`.
unsafe extern "C" fn my_func(count: u32, ap: ...) -> i32 {
unsafe { vmy_func(count, ap) }
}
/// # Safety
/// Must be passed at least `count` arguments of type `i32`.
unsafe fn vmy_func(count: u32, mut ap: VaList<'_>) -> i32 {
let mut sum = 0;
for _ in 0..count {
sum += unsafe { ap.arg::<i32>() };
}
sum
}
assert_eq!(unsafe { my_func(1, 42i32) }, 42);
assert_eq!(unsafe { my_func(3, 42i32, -7i32, 20i32) }, 55);The VaList::arg method can be used to read an argument from the list. This method
automatically advances the VaList to the next argument. The C equivalent is va_arg.
Cloning a VaList performs the equivalent of C va_copy, producing an independent cursor
that arguments can be read from without affecting the original. Dropping a VaList performs
the equivalent of C va_end.
This can be used across an FFI boundary, and fully matches the platform’s va_list.
Implementations§
Source§impl<'f> VaList<'f>
impl<'f> VaList<'f>
Sourcepub const unsafe fn arg<T>(&mut self) -> Twhere
T: VaArgSafe,
🔬This is a nightly-only experimental API. (c_variadic #44930)
pub const unsafe fn arg<T>(&mut self) -> Twhere
T: VaArgSafe,
c_variadic #44930)Read an argument from the variable argument list, and advance to the next argument.
Only types that implement VaArgSafe can be read from a variable argument list.
§Safety
This function is only sound to call when there is another argument to read, and that
argument is a properly initialized value of the type T.
Calling this function with an incompatible type, an invalid value, or when there are no more variable arguments, is unsound.