Series: The Rust Annals

Vol. I Issue 100 nlopes.dev

Announcing Rust 1.99.0

Rust 1.99 lets code define C-ABI variadic functions and query size and alignment through raw pointers to non-Sized types.

Rust code can now define C-ABI variadic functions. Previously, Rust could call externally defined variadic functions but could not define them. Rust 1.99 also stabilizes size and alignment queries on raw pointers to non-Sized types. The Box::leak documentation now recommends against patterns that later deallocate the leaked memory.

Rust can now define C-ABI variadic functions

Rust 1.99 lets Rust code define variadic functions with the "C" and "C-unwind" ABIs. These functions use a variable argument list, written ..., and accept an arbitrary number of arguments. Rust could already call externally defined variadic functions such as libc::printf. In this example, the caller must supply at least two i32 arguments.

/// SAFETY: must be called with (at least) 2 i32 arguments.
unsafe extern "C" fn sum(mut args: ...) -> i32 {
    // SAFETY: guaranteed by the caller.
    let a = unsafe { args.next_arg::<i32>() };
    let b = unsafe { args.next_arg::<i32>() };
    a + b
}

fn foo() -> i32 {
    unsafe { sum(0i32, 2i32) }
}

The type of ... is VaList. VaList is ABI-compatible with the C va_list type across targets. The VaArgSafe trait guards which types can be read from a VaList. See the VaList documentation and the VaArgSafe documentation.

Rust 1.99 also stabilizes definitions of naked variadic functions with non-"C" ABIs. These functions must be written with inline assembly. See the Rust Reference on C-variadic functions.

Size and alignment queries are stable for raw pointers to non-Sized types

Rust 1.99 settles the safety requirements for retrieving size and alignment from raw pointers to both Sized and non-Sized types. It stabilizes three functions for this: Layout::for_value_raw, mem::size_of_val_raw, and mem::align_of_val_raw. For Sized types, these queries were already possible on stable and are trivially safe.

Box::leak documentation recommends against deallocating leaked memory

Rust 1.99 updates the Box::leak documentation to recommend against patterns that later deallocate the leaked memory. This update changes only the documentation. It does not change the language semantics. Such code was found to have problematic interactions with current and potential future compiler optimizations. Such code is especially problematic with the upcoming stabilization of custom allocators. The guidance also applies to other leak functions in the standard library. See the Box::leak documentation.

IntoIterator is stable for Box<[T; N]> and its borrowed forms

Rust 1.99 stabilizes three IntoIterator implementations for Box<[T; N]> and its borrowed forms:

Reproduced from the Rust blog under its publication licence. Typeset in Literata.