Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a new File::append constructor #512

Open
liigo opened this issue Dec 26, 2024 · 2 comments
Open

add a new File::append constructor #512

liigo opened this issue Dec 26, 2024 · 2 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@liigo
Copy link

liigo commented Dec 26, 2024

Proposal

Problem statement

Currently, when people want to write log file, he/she must write verbose & unreadable & error-prone code, such as:

let mut file = File::options().append(true).create(true).open("./log.txt").unwrap();
let _ = file.write_fmt(format_args!("log text\n"));

The use of File::options().... also requires people thinking more about permissions & flags in detail:

  • should I need a READ permission? (.open(true)?)
  • should I need a WRITE permission? (.write(true)?)
  • maybe I forget to write .create(true) (error-prone)
  • Is there any other flags must be used? (need check more docs)

It requires people reading more docs and it's not easy to remember them all. Next time will do these again and again.

So, we need a small wrapper methods around File::options().append(true).create(true).open("./log.txt"), to make life better.

Motivating examples or use cases

A common use case is open logging file and write new lines to it's end, the file may be exists or not:

fn log(line: &str) {
    let mut file = File::append("./log.txt").unwrap();  // <-- use new File::append
    let _ = file.write_fmt(format_args!("{line}\n"))
}

the File::append("./log.txt") is very easy to use than File::options().append(true).create(true).open("./log.txt"), and it's more readable (for review).

Solution sketch

Add to std::fs::File:

// open existing or create new file for append data.
// very useful when writing log files.

pub fn append<P: AsRef<Path>>(path: P) -> io::Result<File> {
    OpenOptions::new().append(true).create(true).open(path.as_ref())
}

Alternatives

n/a

Links and related work

rust-lang/rust#134755

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@liigo liigo added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Dec 26, 2024
@the8472
Copy link
Member

the8472 commented Dec 26, 2024

The use of File::options().... also requires people thinking more about permissions & flags in detail:

IMO that's not a good argument. People should generally understand what they want and what they're doing, otherwise you get bugs, security issues or data loss.

If you already know what you're doing then append could be convenience, but imo opening log files is not something that comes up frequently? Once or twice per program maybe...

@kennytm
Copy link
Member

kennytm commented Dec 26, 2024

This function does map to the remaining fopen(3) mode though.

C Rust
fopen(p, "r") File::open(p)
fopen(p, "w") File::create(p)
fopen(p, "a") File::options().append(true).create(true).open(p) 👀
File::append(p) 🆕

Following #446 there should also be an append_buffered() constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants