-
Notifications
You must be signed in to change notification settings - Fork 390
Enable file writing #973
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
Enable file writing #973
Conversation
7bc36d6
to
5a05c04
Compare
bc4fc4e
to
6c36a8c
Compare
@bors r+ |
📌 Commit 6c36a8c has been approved by |
☀️ Test successful - checks-travis, status-appveyor |
}, | ||
) | ||
this.remove_handle_and(fd, |mut handle, this| { | ||
// Don't use `?` to avoid returning before reinserting the handle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we even remove and re-insert, instead of working with a borrowed handle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because of borrowing issues, we'd have to mutably borrow the handle, and then mutably borrow the bytes inside memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be fixed if we made the memory
field public, instead of exposing it just via getters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My naive opinion is that it would work because memory
and machine
are different fields of ecx
. So you can get a mutable reference to file handle from machine
and the mutable reference to the bytes from memory
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. I'll make a PR.
this.memory_mut() | ||
.get_mut(buf.alloc_id)? | ||
.get_bytes_mut(tcx, buf, Size::from_bytes(count)) | ||
.map(|buffer| handle.file.read(buffer)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is the actual core of the function, doing the read
, right? Please highlight that with a comment. It is kind of burried in bookkeeping.
let bytes = this.memory().get(buf.alloc_id).and_then(|alloc| { | ||
alloc | ||
.get_bytes(tcx, buf, Size::from_bytes(count)) | ||
.map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is the actual core of the function, doing the write
, right? Please highlight that with a comment. It is kind of burried in bookkeeping.
let bytes = b"Hello, World!\n"; | ||
// Test creating, writing and closing a file (closing is tested when `file` is dropped). | ||
let mut file = File::create(path).unwrap(); | ||
// Writing 0 bytes should not change the file contents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the main issue here that writing 0 bytes should not require the pointer to be valid?
r? @oli-obk