-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
File::set_len uses an immutable reference to self #47708
Comments
I do wonder how much this would break people in the wild though. How many people are truncating or extending a file that they don't have a mutable reference to? |
I do not think this is too odd considering the much more frequently-encountered gotcha! that |
I'm also not following this with my |
I looked at the crate and didn't see any In your case, though, even if there were |
@ExpHP thanks for looking at the crate! Sorry I should have been more clear. I am currently implementing a It will be slightly more difficult to clone If you had thread_a writing "foobar" and thread_b writing "bazbob" is there any guarantee that you wouldn't have "foobazbobbar" or any other permutation of the letters? If there are no such guarantees then I would prefer to require the user to do a producer/consumer structure for sending data to be written. This is just personal preference to try and avoid gotchas. Would love any criticism/feedback! Thanks! |
I doubt it. I guess I was thinking more along the lines of there are times where I simply don't mind jumbled output (e.g. println tracing) and from that I extrapolated that there must be times where jumbled output could even be considered neccessary or unavoidable due to the nature of what's being written. ...but on further thought, I can't think of any example that isn't terribly contrived. So for your library, I don't imagine anything of real value will be lost by eliminating this footgun. |
cool, thanks! 👍 |
The fact that The good news is one can define their own type and add extra semantics (eg prevent mutation unless you have a unique borrow). |
@ExpHP @vitalyd I've just released path_abs v0.3.0 and here is the docs related to this issue: https://docs.rs/path_abs/0.3.0/path_abs/#differing-method-signatures Would appreciate a review! |
Thought: how about a general warning on the type in general, plus smaller notes saying "Note that this method can change the file via a shared reference." |
@alercah I like it, but I'd remove "shared"; the proper terms are "reference" and "mutable reference", even though sometimes people do say "shared reference" for |
Add a warning to File about mutability. Fixes rust-lang#47708.
Add a warning to File about mutability. Fixes rust-lang#47708.
I am creating a wrapper for the File type and came across an issue that I thought was odd.
Both of these methods take an immutable reference (
&self
) to the File instead of a mutable reference (&mut self
).I'm not so much concerned about memory safety per se, as I assume that the operating system guarantees that operaions are memory safe, it is just that this is extremely unexpected. If I pass a function an immutable reference to a
File
, I wouldn't expect it to be able to truncate the file!Obviously this is a breaking change so probably can't be changed -- however, I was thinking we could at least add docs explaining why the reference is immutable.
The text was updated successfully, but these errors were encountered: