rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.8k stars 261 forks source link

FFI: return types for snappy functions #258

Closed ajtribick closed 3 years ago

ajtribick commented 3 years ago

On the FFI page, the Rust signatures of the functions snappy_compress, snappy_uncompress, snappy_uncompressed_length and snappy_validate_compressed_buffer are declared as returning c_int. In the C header file for snappy, the return types are given as snappy_status, which is an enum type:

typedef enum {
  SNAPPY_OK = 0,
  SNAPPY_INVALID_INPUT = 1,
  SNAPPY_BUFFER_TOO_SMALL = 2
} snappy_status;

Given that C does not guarantee that an enum's underlying type is int, shouldn't the return type on the Rust side be an enum declared with #[repr(C)]?

ajtribick commented 3 years ago

Actually looks like no, and I'm not sure that there's a "perfect" way to do this in Rust, and c_int is the best answer for now.