ExpertiDict

Rust · Avançado · Inglês · Técnico

FFI

/ˌɛf ˌɛf ˈaɪ/substantivo

Interface de funções externas: chamar código de outras linguagens, normalmente C, com extern "C", ou expor funções Rust a elas. Exige unsafe e tipos com layout compatível.

Onde usar

Aparece em bindings para bibliotecas C e em módulos de Python ou Node. Costuma-se usar repr(C), CString, CStr e ferramentas como o bindgen.

Em código

unsafe extern "C" {
  fn abs(x: i32) -> i32;
}
let r = unsafe { abs(-3) };
println!("{r}");

Exemplos

We wrap the C library through FFI.

Envolvemos a biblioteca em C por FFI.

Mark the struct repr C for the FFI boundary.

Marque a struct com repr C para a fronteira da FFI.

Cuidado

Um panic não deve atravessar a fronteira: em extern "C" o programa aborta, e para propagar é preciso extern "C-unwind".

Sinônimos

interface de funções externas

Origem

Sigla de Foreign Function Interface.

Veja também

Todos os termos de Rust