diff --git a/serial-unix/src/tty.rs b/serial-unix/src/tty.rs index 1beddc0..5401070 100644 --- a/serial-unix/src/tty.rs +++ b/serial-unix/src/tty.rs @@ -139,6 +139,27 @@ impl io::Read for TTYPort { Err(io::Error::last_os_error()) } } + + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + try!(super::poll::wait_read_fd(self.fd, self.timeout)); + + // Saefty: Vec allocate on heap u8*vec.capacity()! + let len = unsafe { + libc::read(self.fd, buf.as_ptr() as *mut c_void, buf.capacity() as size_t) + }; + + // Safety: We asume libc::read will never return more bytes as given by count! + unsafe { + buf.set_len(len as usize); + } + + if len >= 0 { + Ok(len as usize) + } + else { + Err(io::Error::last_os_error()) + } + } } impl io::Write for TTYPort { diff --git a/serial-windows/src/com.rs b/serial-windows/src/com.rs index ba1b0cb..0711961 100644 --- a/serial-windows/src/com.rs +++ b/serial-windows/src/com.rs @@ -115,6 +115,27 @@ impl io::Read for COMPort { } } } + + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + let mut len: DWORD = 0; + + // Saefty: Vec allocate on heap u8*vec.capacity()! + match unsafe { ReadFile(self.handle, buf.as_mut_ptr() as *mut c_void, buf.capacity() as DWORD, &mut len, ptr::null_mut()) } { + 0 => Err(io::Error::last_os_error()), + _ => { + if len != 0 { + // Safety: We asume ReadFile will never return more bytes as given by nNumberOfBytesToRead! + unsafe { + buf.set_len(len as usize); + } + Ok(len as usize) + } + else { + Err(io::Error::new(io::ErrorKind::TimedOut, "Operation timed out")) + } + } + } + } } impl io::Write for COMPort {