Skip to content

Document how Rust array/slice types are translated for extern "C" functions. #30382

@briansmith

Description

@briansmith

Currently I write:

extern {
    fn X25519(out_shared_key: *mut u8/*[32]*/, private_key: *const u8/*[32]*/,
              peers_public_value: *const u8/*[32]*/) -> libc::c_int;
}

I would like to write:

extern {
    fn X25519(out_shared_key: &mut [u8;32], private_key: &[u8;32],
              peers_public_value: &[u8;32]) -> libc::c_int;
}

The C declaration is:

int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
           const uint8_t peers_public_value[32]);

which is equivalent to:

int X25519(uint8_t *out_shared_key, const uint8_t *private_key,
           const uint8_t *peers_public_value);

The second form of the Rust ffi declaration is safer because it makes it clear that the function doesn't accept NULL pointers and it also makes it clear how long the input arrays are expected to be. However, the Rust reference doesn't document how it translates a reference to a slice or array to a C type, so using the second form is undefined behavior. I would like the Rust Reference to be amended to make it clear that it will call translate references to arrays/slices by passing the result of as_ptr/as_mut_ptr to the C function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions