Skip to content

[unsafe] Replace fs::File with libc::FILE - #135

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:file
May 21, 2026
Merged

nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:file

Conversation

@lucic71

@lucic71 lucic71 commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Currently we translate FILE* as *mut std::fs:FILE. This makes it difficult to translate functions such as feof and ferrror.

feof and ferror both peek the internal state of the FILE object. std::fs::File doet not keep an internal state, it propagates the error right at the point of the usage, for example when performing a read or a write. On the other side, *mut libc::FILE, which is the direct FFI equivalent of FILE*, keeps the internal state, making the translation of feof and ferror easy.

Another advantage of switching to *mut libc::FILE is that it simplifies all stdio.h rules. For example fwrites becomes a simple call to libc::fwrite, instead of the hand-rolled implementation that we have now for writing a buffer into std::fs::File.

@nunoplopes
nunoplopes merged commit 656c792 into Cpp2Rust:master May 21, 2026
9 checks passed
joaotgouveia pushed a commit to joaotgouveia/cpp2rust that referenced this pull request Oct 27, 2026
Currently we translate `FILE*` as `*mut std::fs:FILE`. This makes it
difficult to translate functions such as feof and ferrror.

feof and ferror both peek the internal state of the FILE object.
std::fs::File doet not keep an internal state, it propagates the error
right at the point of the usage, for example when performing a read or a
write. On the other side, `*mut libc::FILE`, which is the direct FFI
equivalent of `FILE*`, keeps the internal state, making the translation
of feof and ferror easy.

Another advantage of switching to `*mut libc::FILE` is that it
simplifies all stdio.h rules. For example fwrites becomes a simple call
to `libc::fwrite`, instead of the hand-rolled implementation that we
have now for writing a buffer into `std::fs::File`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants