diff --git a/src/sys/uio.rs b/src/sys/uio.rs index 03f2a3a8b8..5af1093070 100644 --- a/src/sys/uio.rs +++ b/src/sys/uio.rs @@ -2,56 +2,18 @@ #![allow(improper_ctypes)] use {Errno, Result}; -use libc::{c_int, c_void, size_t, off_t}; +use libc::{self, c_int, c_void, size_t, off_t}; use std::marker::PhantomData; use std::os::unix::io::RawFd; -mod ffi { - use super::IoVec; - use libc::{ssize_t, c_int, size_t, off_t, c_void}; - use std::os::unix::io::RawFd; - - extern { - // vectorized version of write - // doc: http://man7.org/linux/man-pages/man2/writev.2.html - pub fn writev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int) -> ssize_t; - - // vectorized version of read - // doc: http://man7.org/linux/man-pages/man2/readv.2.html - pub fn readv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int) -> ssize_t; - - // vectorized write at a specified offset - // doc: http://man7.org/linux/man-pages/man2/pwritev.2.html - #[cfg(feature = "preadv_pwritev")] - pub fn pwritev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int, - offset: off_t) -> ssize_t; - - // vectorized read at a specified offset - // doc: http://man7.org/linux/man-pages/man2/preadv.2.html - #[cfg(feature = "preadv_pwritev")] - pub fn preadv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int, - offset: off_t) -> ssize_t; - - // write to a file at a specified offset - // doc: http://man7.org/linux/man-pages/man2/pwrite.2.html - pub fn pwrite(fd: RawFd, buf: *const c_void, nbyte: size_t, - offset: off_t) -> ssize_t; - - // read from a file at a specified offset - // doc: http://man7.org/linux/man-pages/man2/pread.2.html - pub fn pread(fd: RawFd, buf: *mut c_void, nbyte: size_t, offset: off_t) - -> ssize_t; - } -} - pub fn writev(fd: RawFd, iov: &[IoVec<&[u8]>]) -> Result { - let res = unsafe { ffi::writev(fd, iov.as_ptr(), iov.len() as c_int) }; + let res = unsafe { libc::writev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) }; Errno::result(res).map(|r| r as usize) } pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result { - let res = unsafe { ffi::readv(fd, iov.as_ptr(), iov.len() as c_int) }; + let res = unsafe { libc::readv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) }; Errno::result(res).map(|r| r as usize) } @@ -60,7 +22,7 @@ pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result { pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>], offset: off_t) -> Result { let res = unsafe { - ffi::pwritev(fd, iov.as_ptr(), iov.len() as c_int, offset) + libc::pwritev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset) }; Errno::result(res).map(|r| r as usize) @@ -70,7 +32,7 @@ pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>], pub fn preadv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>], offset: off_t) -> Result { let res = unsafe { - ffi::preadv(fd, iov.as_ptr(), iov.len() as c_int, offset) + libc::preadv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset) }; Errno::result(res).map(|r| r as usize) @@ -78,7 +40,7 @@ pub fn preadv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>], pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result { let res = unsafe { - ffi::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, + libc::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, offset) }; @@ -87,7 +49,7 @@ pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result { pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result{ let res = unsafe { - ffi::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t, + libc::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t, offset) }; @@ -95,11 +57,7 @@ pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result{ } #[repr(C)] -pub struct IoVec { - iov_base: *mut c_void, - iov_len: size_t, - phantom: PhantomData -} +pub struct IoVec(libc::iovec, PhantomData); impl IoVec { #[inline] @@ -108,29 +66,27 @@ impl IoVec { unsafe { slice::from_raw_parts( - self.iov_base as *const u8, - self.iov_len as usize) + self.0.iov_base as *const u8, + self.0.iov_len as usize) } } } impl<'a> IoVec<&'a [u8]> { pub fn from_slice(buf: &'a [u8]) -> IoVec<&'a [u8]> { - IoVec { + IoVec(libc::iovec { iov_base: buf.as_ptr() as *mut c_void, iov_len: buf.len() as size_t, - phantom: PhantomData - } + }, PhantomData) } } impl<'a> IoVec<&'a mut [u8]> { pub fn from_mut_slice(buf: &'a mut [u8]) -> IoVec<&'a mut [u8]> { - IoVec { + IoVec(libc::iovec { iov_base: buf.as_ptr() as *mut c_void, iov_len: buf.len() as size_t, - phantom: PhantomData - } + }, PhantomData) } }