Skip to content

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

Merged
merged 5 commits into from
Oct 2, 2019
Merged

Enable file writing #973

merged 5 commits into from
Oct 2, 2019

Conversation

pvdrz
Copy link
Contributor

@pvdrz pvdrz commented Oct 1, 2019

@pvdrz pvdrz force-pushed the write-shim branch 2 times, most recently from 7bc36d6 to 5a05c04 Compare October 1, 2019 16:14
@pvdrz pvdrz force-pushed the write-shim branch 2 times, most recently from bc4fc4e to 6c36a8c Compare October 2, 2019 14:12
@oli-obk
Copy link
Contributor

oli-obk commented Oct 2, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Oct 2, 2019

📌 Commit 6c36a8c has been approved by oli-obk

@bors
Copy link
Contributor

bors commented Oct 2, 2019

⌛ Testing commit 6c36a8c with merge b0de1e9...

bors added a commit that referenced this pull request Oct 2, 2019
@bors
Copy link
Contributor

bors commented Oct 2, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: oli-obk
Pushing b0de1e9 to master...

@bors bors merged commit 6c36a8c into rust-lang:master Oct 2, 2019
},
)
this.remove_handle_and(fd, |mut handle, this| {
// Don't use `?` to avoid returning before reinserting the handle
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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))
Copy link
Member

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))
Copy link
Member

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.
Copy link
Member

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?

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.

5 participants