How would you wrap C bitfalgs in Rust?


#1

Image a function that would look like this

foo(VIDEO | INPUT | AUDIO);

This would initialize some C library to with audio video and input support. Would you do the same thing in Rust with https://doc.rust-lang.org/bitflags/bitflags/macro.bitflags!.html?

Or would you prefer something like this

struct Capabilities{
    video: bool,
    input: bool,
    audio: bool,
}
..
let capabilities = Capabilities{
    video: true,
    audio: true,
    input: true
};
foo(&capabilities)

Or are there any other alternatives that are more Rust like?


#2

Yes.


#3

Also I should probably add that I am trying to create a higher level library that uses a C library in the back, not a direct C wrapper.


#4

It’s perfectly good to use bitflags! in high-level libraries. It’s even used in native Rust libraries with no C code at all.