forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#89518 - a1phyr:unix_file_vectored_at, r=worki…
…ngjubilee Add vectored positioned I/O on Unix Add methods for vectored I/O with an offset on `File` for `unix` under `#![feature(unix_file_vectored_at)]`. The new methods are wrappers around `preadv` and `pwritev`. Tracking issue: rust-lang#89517
- Loading branch information
Showing
4 changed files
with
276 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use super::*; | ||
|
||
#[test] | ||
fn read_vectored_at() { | ||
let msg = b"preadv is working!"; | ||
let dir = crate::sys_common::io::test::tmpdir(); | ||
|
||
let filename = dir.join("preadv.txt"); | ||
{ | ||
let mut file = fs::File::create(&filename).unwrap(); | ||
file.write_all(msg).unwrap(); | ||
} | ||
{ | ||
let file = fs::File::open(&filename).unwrap(); | ||
let mut buf0 = [0; 4]; | ||
let mut buf1 = [0; 3]; | ||
|
||
let mut iovec = [io::IoSliceMut::new(&mut buf0), io::IoSliceMut::new(&mut buf1)]; | ||
|
||
let n = file.read_vectored_at(&mut iovec, 4).unwrap(); | ||
|
||
assert!(n == 4 || n == 7); | ||
assert_eq!(&buf0, b"dv i"); | ||
|
||
if n == 7 { | ||
assert_eq!(&buf1, b"s w"); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn write_vectored_at() { | ||
let msg = b"pwritev is not working!"; | ||
let dir = crate::sys_common::io::test::tmpdir(); | ||
|
||
let filename = dir.join("preadv.txt"); | ||
{ | ||
let mut file = fs::File::create(&filename).unwrap(); | ||
file.write_all(msg).unwrap(); | ||
} | ||
let expected = { | ||
let file = fs::File::options().write(true).open(&filename).unwrap(); | ||
let buf0 = b" "; | ||
let buf1 = b"great "; | ||
|
||
let iovec = [io::IoSlice::new(buf0), io::IoSlice::new(buf1)]; | ||
|
||
let n = file.write_vectored_at(&iovec, 11).unwrap(); | ||
|
||
assert!(n == 4 || n == 11); | ||
|
||
if n == 4 { b"pwritev is working!" } else { b"pwritev is great !" } | ||
}; | ||
|
||
let content = fs::read(&filename).unwrap(); | ||
assert_eq!(&content, expected); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters