Skip to content

Enable file writing#973

Merged
bors merged 5 commits into
rust-lang:masterfrom
pvdrz:write-shim
Oct 2, 2019
Merged

Enable file writing#973
bors merged 5 commits into
rust-lang:masterfrom
pvdrz:write-shim

Conversation

@pvdrz

@pvdrz pvdrz commented Oct 1, 2019

Copy link
Copy Markdown
Contributor

@pvdrz pvdrz force-pushed the write-shim branch 2 times, most recently from 7bc36d6 to 5a05c04 Compare October 1, 2019 16:14
Comment thread src/shims/io.rs Outdated
Comment thread tests/run-pass/file_read.rs
Comment thread src/shims/io.rs
Comment thread src/shims/io.rs Outdated
Comment thread src/shims/io.rs
Comment thread src/shims/io.rs Outdated
Comment thread src/shims/io.rs
Comment thread src/shims/io.rs Outdated
Comment thread tests/run-pass/file_read.rs Outdated
Comment thread src/shims/io.rs Outdated
@pvdrz pvdrz force-pushed the write-shim branch 2 times, most recently from bc4fc4e to 6c36a8c Compare October 2, 2019 14:12
@oli-obk

oli-obk commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

📌 Commit 6c36a8c has been approved by oli-obk

@bors

bors commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

⌛ Testing commit 6c36a8c with merge b0de1e9...

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

bors commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

☀️ 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
Comment thread src/shims/io.rs
Comment thread src/shims/io.rs
},
)
this.remove_handle_and(fd, |mut handle, this| {
// Don't use `?` to avoid returning before reinserting the handle

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

Comment thread src/shims/io.rs
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
Copy Markdown
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.

Comment thread src/shims/io.rs
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
Copy Markdown
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
Copy Markdown
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