Skip to content

Commit b719bb4

Browse files
committed
Remove superfluous argument to read_bytes
Slices already encode their lengths; use that instead. Remove the unused return value while I'm here. Closes #225.
1 parent f37cb6e commit b719bb4

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/unix_term.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::env;
2+
use std::convert::TryFrom as _;
23
use std::fmt::Display;
34
use std::fs;
45
use std::io::{self, BufRead, BufReader};
@@ -212,7 +213,7 @@ fn read_single_char(fd: RawFd) -> io::Result<Option<char>> {
212213
// if there is something to be read, take 1 byte from it
213214
let mut buf: [u8; 1] = [0];
214215

215-
read_bytes(fd, &mut buf, 1)?;
216+
read_bytes(fd, &mut buf)?;
216217
Ok(Some(buf[0] as char))
217218
} else {
218219
//there is nothing to be read
@@ -223,22 +224,25 @@ fn read_single_char(fd: RawFd) -> io::Result<Option<char>> {
223224
// Similar to libc::read. Read count bytes into slice buf from descriptor fd.
224225
// If successful, return the number of bytes read.
225226
// Will return an error if nothing was read, i.e when called at end of file.
226-
fn read_bytes(fd: RawFd, buf: &mut [u8], count: u8) -> io::Result<u8> {
227-
let read = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut _, count as usize) };
228-
if read < 0 {
229-
Err(io::Error::last_os_error())
230-
} else if read == 0 {
231-
Err(io::Error::new(
232-
io::ErrorKind::UnexpectedEof,
233-
"Reached end of file",
234-
))
235-
} else if buf[0] == b'\x03' {
236-
Err(io::Error::new(
237-
io::ErrorKind::Interrupted,
238-
"read interrupted",
239-
))
240-
} else {
241-
Ok(read as u8)
227+
fn read_bytes(fd: RawFd, buf: &mut [u8]) -> io::Result<()> {
228+
let read = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut _, buf.len()) };
229+
match usize::try_from(read) {
230+
Err(std::num::TryFromIntError { .. }) => Err(io::Error::last_os_error()),
231+
Ok(read) => {
232+
if read != buf.len() {
233+
Err(io::Error::new(
234+
io::ErrorKind::UnexpectedEof,
235+
"Reached end of file",
236+
))
237+
} else if buf.starts_with(b"\x03") {
238+
Err(io::Error::new(
239+
io::ErrorKind::Interrupted,
240+
"read interrupted",
241+
))
242+
} else {
243+
Ok(())
244+
}
245+
}
242246
}
243247
}
244248

@@ -301,15 +305,15 @@ fn read_single_key_impl(fd: RawFd) -> Result<Key, io::Error> {
301305

302306
break if byte & 224u8 == 192u8 {
303307
// a two byte unicode character
304-
read_bytes(fd, &mut buf[1..], 1)?;
308+
read_bytes(fd, &mut buf[1..][..1])?;
305309
Ok(key_from_utf8(&buf[..2]))
306310
} else if byte & 240u8 == 224u8 {
307311
// a three byte unicode character
308-
read_bytes(fd, &mut buf[1..], 2)?;
312+
read_bytes(fd, &mut buf[1..][..2])?;
309313
Ok(key_from_utf8(&buf[..3]))
310314
} else if byte & 248u8 == 240u8 {
311315
// a four byte unicode character
312-
read_bytes(fd, &mut buf[1..], 3)?;
316+
read_bytes(fd, &mut buf[1..][..3])?;
313317
Ok(key_from_utf8(&buf[..4]))
314318
} else {
315319
Ok(match c {

0 commit comments

Comments
 (0)