libc icon indicating copy to clipboard operation
libc copied to clipboard

Why not use OwnedFd and BorrowFd instead of RawFd for fd params?

Open creonico opened this issue 1 year ago • 1 comments

I just learned that OwnedFd and BorrowedFd are I/O safe alternatives to RawFd, especially for FFI, https://rust-lang.github.io/rfcs/3128-io-safety.html#ownedfd-and-borrowedfdfd-1. So why not use this types instead? For example, currently mmap receives a file descriptor parameter as an i32,

pub unsafe extern "C" fn mmap(
    addr: *mut c_void,
    len: size_t,
    prot: c_int,
    flags: c_int,
    fd: c_int,
    offset: off_t
) -> *mut c_void

But why not define it with OwnedFd or BorrowedFd instead as in this examples: https://github.com/sunfishcode/io-lifetimes?tab=readme-ov-file#the-magic-of-transparency. Please accept my apology for any oversight on my part, especially if there is an evident explanation for the situation as it stands.

creonico avatar Apr 26 '24 16:04 creonico

This crate is intended to expose the raw API of the underlying platform. If you want a fancy, safe API, you can use nix or rustix.

magicant avatar May 01 '24 15:05 magicant

I have to agree with @magicant here; this crate is intended to expose the raw bindings, with the same argument types and names detailed in the manpages. There are certainly a lot of places where it would be trivial to improve the safety of these functions, but that is out of scope for this crate as a low-level interface.

tgross35 avatar Aug 15 '24 18:08 tgross35