Skip to content

Commit 987dcf4

Browse files
committed
Auto merge of #304 - kamalmarhubi:uio-libc, r=posborne
uio: Use bindings from libc instead of our own Refs #264
2 parents f848e2c + 5089e0f commit 987dcf4

File tree

1 file changed

+14
-58
lines changed

1 file changed

+14
-58
lines changed

src/sys/uio.rs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,18 @@
22
#![allow(improper_ctypes)]
33

44
use {Errno, Result};
5-
use libc::{c_int, c_void, size_t, off_t};
5+
use libc::{self, c_int, c_void, size_t, off_t};
66
use std::marker::PhantomData;
77
use std::os::unix::io::RawFd;
88

9-
mod ffi {
10-
use super::IoVec;
11-
use libc::{ssize_t, c_int, size_t, off_t, c_void};
12-
use std::os::unix::io::RawFd;
13-
14-
extern {
15-
// vectorized version of write
16-
// doc: http://man7.org/linux/man-pages/man2/writev.2.html
17-
pub fn writev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int) -> ssize_t;
18-
19-
// vectorized version of read
20-
// doc: http://man7.org/linux/man-pages/man2/readv.2.html
21-
pub fn readv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int) -> ssize_t;
22-
23-
// vectorized write at a specified offset
24-
// doc: http://man7.org/linux/man-pages/man2/pwritev.2.html
25-
#[cfg(feature = "preadv_pwritev")]
26-
pub fn pwritev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int,
27-
offset: off_t) -> ssize_t;
28-
29-
// vectorized read at a specified offset
30-
// doc: http://man7.org/linux/man-pages/man2/preadv.2.html
31-
#[cfg(feature = "preadv_pwritev")]
32-
pub fn preadv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int,
33-
offset: off_t) -> ssize_t;
34-
35-
// write to a file at a specified offset
36-
// doc: http://man7.org/linux/man-pages/man2/pwrite.2.html
37-
pub fn pwrite(fd: RawFd, buf: *const c_void, nbyte: size_t,
38-
offset: off_t) -> ssize_t;
39-
40-
// read from a file at a specified offset
41-
// doc: http://man7.org/linux/man-pages/man2/pread.2.html
42-
pub fn pread(fd: RawFd, buf: *mut c_void, nbyte: size_t, offset: off_t)
43-
-> ssize_t;
44-
}
45-
}
46-
479
pub fn writev(fd: RawFd, iov: &[IoVec<&[u8]>]) -> Result<usize> {
48-
let res = unsafe { ffi::writev(fd, iov.as_ptr(), iov.len() as c_int) };
10+
let res = unsafe { libc::writev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) };
4911

5012
Errno::result(res).map(|r| r as usize)
5113
}
5214

5315
pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
54-
let res = unsafe { ffi::readv(fd, iov.as_ptr(), iov.len() as c_int) };
16+
let res = unsafe { libc::readv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) };
5517

5618
Errno::result(res).map(|r| r as usize)
5719
}
@@ -60,7 +22,7 @@ pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
6022
pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
6123
offset: off_t) -> Result<usize> {
6224
let res = unsafe {
63-
ffi::pwritev(fd, iov.as_ptr(), iov.len() as c_int, offset)
25+
libc::pwritev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset)
6426
};
6527

6628
Errno::result(res).map(|r| r as usize)
@@ -70,15 +32,15 @@ pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
7032
pub fn preadv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>],
7133
offset: off_t) -> Result<usize> {
7234
let res = unsafe {
73-
ffi::preadv(fd, iov.as_ptr(), iov.len() as c_int, offset)
35+
libc::preadv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset)
7436
};
7537

7638
Errno::result(res).map(|r| r as usize)
7739
}
7840

7941
pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {
8042
let res = unsafe {
81-
ffi::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t,
43+
libc::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t,
8244
offset)
8345
};
8446

@@ -87,19 +49,15 @@ pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {
8749

8850
pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result<usize>{
8951
let res = unsafe {
90-
ffi::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t,
52+
libc::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t,
9153
offset)
9254
};
9355

9456
Errno::result(res).map(|r| r as usize)
9557
}
9658

9759
#[repr(C)]
98-
pub struct IoVec<T> {
99-
iov_base: *mut c_void,
100-
iov_len: size_t,
101-
phantom: PhantomData<T>
102-
}
60+
pub struct IoVec<T>(libc::iovec, PhantomData<T>);
10361

10462
impl<T> IoVec<T> {
10563
#[inline]
@@ -108,29 +66,27 @@ impl<T> IoVec<T> {
10866

10967
unsafe {
11068
slice::from_raw_parts(
111-
self.iov_base as *const u8,
112-
self.iov_len as usize)
69+
self.0.iov_base as *const u8,
70+
self.0.iov_len as usize)
11371
}
11472
}
11573
}
11674

11775
impl<'a> IoVec<&'a [u8]> {
11876
pub fn from_slice(buf: &'a [u8]) -> IoVec<&'a [u8]> {
119-
IoVec {
77+
IoVec(libc::iovec {
12078
iov_base: buf.as_ptr() as *mut c_void,
12179
iov_len: buf.len() as size_t,
122-
phantom: PhantomData
123-
}
80+
}, PhantomData)
12481
}
12582
}
12683

12784
impl<'a> IoVec<&'a mut [u8]> {
12885
pub fn from_mut_slice(buf: &'a mut [u8]) -> IoVec<&'a mut [u8]> {
129-
IoVec {
86+
IoVec(libc::iovec {
13087
iov_base: buf.as_ptr() as *mut c_void,
13188
iov_len: buf.len() as size_t,
132-
phantom: PhantomData
133-
}
89+
}, PhantomData)
13490
}
13591
}
13692

0 commit comments

Comments
 (0)