Skip to content

Commit 7c2da1b

Browse files
committed
minor tweaks
1 parent d380750 commit 7c2da1b

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

tests/pass-dep/libc/libc-pipe.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ fn test_pipe() {
3838
let data = b"123";
3939
write_all_from_slice(fds[1], data).unwrap();
4040
let mut buf4: [u8; 5] = [0; 5];
41-
let res = read_into_slice(fds[0], &mut buf4).unwrap().0.len();
42-
assert_eq!(buf4[..res], data[..res]);
43-
if res < 3 {
44-
// Drain the rest from the read end.
45-
let res = read_into_slice(fds[0], &mut buf4[res..]).unwrap().0.len();
46-
assert!(res > 0);
47-
}
41+
let (part1, rest) = read_into_slice(fds[0], &mut buf4).unwrap();
42+
assert_eq!(part1[..], data[..part1.len()]);
43+
// Write 2 more bytes so we can exactly fill the `rest`.
44+
write_all_from_slice(fds[1], b"34").unwrap();
45+
read_all_into_slice(fds[0], rest).unwrap();
4846
}
4947

5048
fn test_pipe_threaded() {

tests/utils/libc.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,8 @@ pub unsafe fn read_all(
4040
return read_so_far as libc::ssize_t;
4141
}
4242

43-
/// Read exactly `N` bytes from `fd`. Error if that many bytes could not be read.
43+
/// Try to fill the given slice by reading from `fd`. Error if that many bytes could not be read.
4444
#[track_caller]
45-
pub fn read_all_into_array<const N: usize>(fd: libc::c_int) -> Result<[u8; N], libc::ssize_t> {
46-
let mut buf = [0; N];
47-
let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) };
48-
if res >= 0 {
49-
assert_eq!(res as usize, buf.len());
50-
Ok(buf)
51-
} else {
52-
Err(res)
53-
}
54-
}
55-
5645
pub fn read_all_into_slice(fd: libc::c_int, buf: &mut [u8]) -> Result<(), libc::ssize_t> {
5746
let res = unsafe { read_all(fd, buf.as_mut_ptr().cast(), buf.len()) };
5847
if res >= 0 {
@@ -63,6 +52,17 @@ pub fn read_all_into_slice(fd: libc::c_int, buf: &mut [u8]) -> Result<(), libc::
6352
}
6453
}
6554

55+
/// Read exactly `N` bytes from `fd`. Error if that many bytes could not be read.
56+
#[track_caller]
57+
pub fn read_all_into_array<const N: usize>(fd: libc::c_int) -> Result<[u8; N], libc::ssize_t> {
58+
let mut buf = [0; N];
59+
read_all_into_slice(fd, &mut buf)?;
60+
Ok(buf)
61+
}
62+
63+
/// Do a single read from `fd` and return the part of the buffer that was written into,
64+
/// and the rest.
65+
#[track_caller]
6666
pub fn read_into_slice(
6767
fd: libc::c_int,
6868
buf: &mut [u8],

0 commit comments

Comments
 (0)