]> git.lizzy.rs Git - rust.git/commitdiff
Use `try_into` and move some functions
authorTobias Bucher <tobiasbucher5991@gmail.com>
Sat, 8 Oct 2016 23:06:55 +0000 (01:06 +0200)
committerTobias Bucher <tobiasbucher5991@gmail.com>
Sun, 9 Oct 2016 08:49:05 +0000 (10:49 +0200)
src/libstd/sys/unix/android.rs
src/libstd/sys/unix/fd.rs

index 08e6b45dcd35819e207de7f989f72d974901f41e..a4272a9f5223b4a66d06c79acd57997e87862e19 100644 (file)
@@ -31,6 +31,7 @@
 use libc::{c_int, c_void, sighandler_t, size_t, ssize_t};
 use libc::{ftruncate, pread, pwrite};
 
+use convert::TryInto;
 use io;
 use super::{cvt, cvt_r};
 
@@ -121,11 +122,11 @@ pub unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: i6
     weak!(fn pread64(c_int, *mut c_void, size_t, i64) -> ssize_t);
     unsafe {
         pread64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
-            if offset as u64 > i32::max_value() as u64 {
+            if let Ok(o) = offset.try_into() {
+                cvt(pread(fd, buf, count, o))
+            } else {
                 Err(io::Error::new(io::Error::InvalidInput,
                                    "cannot pread >2GB"))
-            } else {
-                cvt(pread(fd, buf, count, offset as i32))
             }
         })
     }
@@ -137,11 +138,11 @@ pub unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset:
     weak!(fn pwrite64(c_int, *const c_void, size_t, i64) -> ssize_t);
     unsafe {
         pwrite64.get().map(|f| cvt(f(fd, buf, count, offset))).unwrap_or_else(|| {
-            if offset as u64 > i32::max_value() as u64 {
+            if let Ok(o) = offset.try_into() {
+                cvt(pwrite(fd, buf, count, o))
+            } else {
                 Err(io::Error::new(io::Error::InvalidInput,
                                    "cannot pwrite >2GB"))
-            } else {
-                cvt(pwrite(fd, buf, count, offset as i32))
             }
         })
     }
index 30573d695aac7d8af8f394689cd7227da28abcb8..41bb96fed16cbcdde027568c572b9d52c4cd2b39 100644 (file)
 use sys_common::AsInner;
 use sys_common::io::read_to_end_uninitialized;
 
-#[cfg(target_os = "android")]
-use super::android::{cvt_pread64, cvt_pwrite64};
-#[cfg(any(target_os = "linux", target_os = "emscripten"))]
-use libc::{pread64, pwrite64, off64_t, ssize_t};
-#[cfg(not(any(target_os = "linux", target_os = "emscripten", target_os = "android")))]
-use libc::{pread as pread64, pwrite as pwrite64, off_t as off64_t, ssize_t};
-
-#[cfg(not(target_os = "android"))]
-unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: size_t, offset: off64_t)
-    -> io::Result<ssize_t>
-{
-    cvt(pread64(fd, buf, count, offset))
-}
-
-#[cfg(not(target_os = "android"))]
-unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: size_t, offset: off64_t)
-    -> io::Result<ssize_t>
-{
-    cvt(pwrite64(fd, buf, count, offset))
-}
-
 pub struct FileDesc {
     fd: c_int,
 }
@@ -72,11 +51,25 @@ pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
     }
 
     pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
+        #[cfg(target_os = "android")]
+        use super::android::cvt_pread64;
+
+        #[cfg(not(target_os = "android"))]
+        unsafe fn cvt_pread64(fd: c_int, buf: *mut c_void, count: usize, offset: i64)
+            -> io::Result<isize>
+        {
+            #[cfg(any(target_os = "linux", target_os = "emscripten"))]
+            use libc::pread64;
+            #[cfg(not(any(target_os = "linux", target_os = "emscripten")))]
+            use libc::pread as pread64;
+            cvt(pread64(fd, buf, count, offset))
+        }
+
         unsafe {
             cvt_pread64(self.fd,
                         buf.as_mut_ptr() as *mut c_void,
                         buf.len(),
-                        offset as off64_t)
+                        offset as i64)
                 .map(|n| n as usize)
         }
     }
@@ -91,11 +84,25 @@ pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
     }
 
     pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
+        #[cfg(target_os = "android")]
+        use super::android::cvt_pwrite64;
+
+        #[cfg(not(target_os = "android"))]
+        unsafe fn cvt_pwrite64(fd: c_int, buf: *const c_void, count: usize, offset: i64)
+            -> io::Result<isize>
+        {
+            #[cfg(any(target_os = "linux", target_os = "emscripten"))]
+            use libc::pwrite64;
+            #[cfg(not(any(target_os = "linux", target_os = "emscripten")))]
+            use libc::pwrite as pwrite64;
+            cvt(pwrite64(fd, buf, count, offset))
+        }
+
         unsafe {
             cvt_pwrite64(self.fd,
                          buf.as_ptr() as *const c_void,
                          buf.len(),
-                         offset as off64_t)
+                         offset as i64)
                 .map(|n| n as usize)
         }
     }