Skip to content

Commit 70dcfd6

Browse files
committed
Use try_into and move some functions
1 parent f352f0e commit 70dcfd6

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

src/libstd/sys/unix/android.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use libc::{c_int, c_void, sighandler_t, size_t, ssize_t};
3232
use libc::{ftruncate, pread, pwrite};
3333

34+
use convert::TryInto;
3435
use io;
3536
use super::{cvt, cvt_r};
3637

@@ -121,11 +122,11 @@ pub unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: i6
121122
weak!(fn pread64(c_int, *mut c_void, size_t, i64) -> ssize_t);
122123
unsafe {
123124
pread64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
124-
if offset as u64 > i32::max_value() as u64 {
125+
if let Ok(o) = offset.try_into() {
126+
cvt(pread(fd, buf, count, o))
127+
} else {
125128
Err(io::Error::new(io::Error::InvalidInput,
126129
"cannot pread >2GB"))
127-
} else {
128-
cvt(pread(fd, buf, count, offset as i32))
129130
}
130131
})
131132
}
@@ -137,11 +138,11 @@ pub unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset:
137138
weak!(fn pwrite64(c_int, *const c_void, size_t, i64) -> ssize_t);
138139
unsafe {
139140
pwrite64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
140-
if offset as u64 > i32::max_value() as u64 {
141+
if let Ok(o) = offset.try_into() {
142+
cvt(pwrite(fd, buf, count, o))
143+
} else {
141144
Err(io::Error::new(io::Error::InvalidInput,
142145
"cannot pwrite >2GB"))
143-
} else {
144-
cvt(pwrite(fd, buf, count, offset as i32))
145146
}
146147
})
147148
}

src/libstd/sys/unix/fd.rs

+30-23
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@ use sys::cvt;
1818
use sys_common::AsInner;
1919
use sys_common::io::read_to_end_uninitialized;
2020

21-
#[cfg(target_os = "android")]
22-
use super::android::{cvt_pread64, cvt_pwrite64};
23-
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
24-
use libc::{pread64, pwrite64, off64_t, ssize_t};
25-
#[cfg(not(any(target_os = "linux", target_os = "emscripten", target_os = "android")))]
26-
use libc::{pread as pread64, pwrite as pwrite64, off_t as off64_t, ssize_t};
27-
28-
#[cfg(not(target_os = "android"))]
29-
unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: off64_t)
30-
-> io::Result<ssize_t>
31-
{
32-
cvt(pread64(fd, buf, count, offset))
33-
}
34-
35-
#[cfg(not(target_os = "android"))]
36-
unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset: off64_t)
37-
-> io::Result<ssize_t>
38-
{
39-
cvt(pwrite64(fd, buf, count, offset))
40-
}
41-
4221
pub struct FileDesc {
4322
fd: c_int,
4423
}
@@ -72,11 +51,25 @@ impl FileDesc {
7251
}
7352

7453
pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
54+
#[cfg(target_os = "android")]
55+
use super::android::cvt_pread64;
56+
57+
#[cfg(not(target_os = "android"))]
58+
unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: usize, offset: i64)
59+
-> io::Result<isize>
60+
{
61+
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
62+
use libc::pread64;
63+
#[cfg(not(any(target_os = "linux", target_os = "emscripten")))]
64+
use libc::pread as pread64;
65+
cvt(pread64(fd, buf, count, offset))
66+
}
67+
7568
unsafe {
7669
cvt_pread64(self.fd,
7770
buf.as_mut_ptr() as *mut c_void,
7871
buf.len(),
79-
offset as off64_t)
72+
offset as i64)
8073
.map(|n| n as usize)
8174
}
8275
}
@@ -91,11 +84,25 @@ impl FileDesc {
9184
}
9285

9386
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
87+
#[cfg(target_os = "android")]
88+
use super::android::cvt_pwrite64;
89+
90+
#[cfg(not(target_os = "android"))]
91+
unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: usize, offset: i64)
92+
-> io::Result<isize>
93+
{
94+
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
95+
use libc::pwrite64;
96+
#[cfg(not(any(target_os = "linux", target_os = "emscripten")))]
97+
use libc::pwrite as pwrite64;
98+
cvt(pwrite64(fd, buf, count, offset))
99+
}
100+
94101
unsafe {
95102
cvt_pwrite64(self.fd,
96103
buf.as_ptr() as *const c_void,
97104
buf.len(),
98-
offset as off64_t)
105+
offset as i64)
99106
.map(|n| n as usize)
100107
}
101108
}

0 commit comments

Comments
 (0)