Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer constant over function #74587

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/libstd/sys/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sys::cvt;
use crate::sys_common::AsInner;

use libc::{c_int, c_void, ssize_t};
use libc::{c_int, c_void};

#[derive(Debug)]
pub struct FileDesc {
fd: c_int,
}

fn max_len() -> usize {
// The maximum read limit on most posix-like systems is `SSIZE_MAX`,
// with the man page quoting that if the count of bytes to read is
// greater than `SSIZE_MAX` the result is "unspecified".
//
// On macOS, however, apparently the 64-bit libc is either buggy or
// intentionally showing odd behavior by rejecting any read with a size
// larger than or equal to INT_MAX. To handle both of these the read
// size is capped on both platforms.
if cfg!(target_os = "macos") { <c_int>::MAX as usize - 1 } else { <ssize_t>::MAX as usize }
}
// The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
// with the man page quoting that if the count of bytes to read is
// greater than `SSIZE_MAX` the result is "unspecified".
//
// On macOS, however, apparently the 64-bit libc is either buggy or
// intentionally showing odd behavior by rejecting any read with a size
// larger than or equal to INT_MAX. To handle both of these the read
// size is capped on both platforms.
#[cfg(target_os = "macos")]
const READ_LIMIT: usize = c_int::MAX as usize - 1;
#[cfg(not(target_os = "macos"))]
const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder can we use isize::MAX instead?


impl FileDesc {
pub fn new(fd: c_int) -> FileDesc {
Expand All @@ -44,7 +45,7 @@ impl FileDesc {

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let ret = cvt(unsafe {
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), max_len()))
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -92,7 +93,7 @@ impl FileDesc {
cvt_pread64(
self.fd,
buf.as_mut_ptr() as *mut c_void,
cmp::min(buf.len(), max_len()),
cmp::min(buf.len(), READ_LIMIT),
offset as i64,
)
.map(|n| n as usize)
Expand All @@ -101,7 +102,7 @@ impl FileDesc {

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let ret = cvt(unsafe {
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), max_len()))
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -144,7 +145,7 @@ impl FileDesc {
cvt_pwrite64(
self.fd,
buf.as_ptr() as *const c_void,
cmp::min(buf.len(), max_len()),
cmp::min(buf.len(), READ_LIMIT),
offset as i64,
)
.map(|n| n as usize)
Expand Down
20 changes: 9 additions & 11 deletions src/libstd/sys/vxworks/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ pub struct FileDesc {
fd: c_int,
}

fn max_len() -> usize {
// The maximum read limit on most posix-like systems is `SSIZE_MAX`,
// with the man page quoting that if the count of bytes to read is
// greater than `SSIZE_MAX` the result is "unspecified".
<ssize_t>::MAX as usize
}
// The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
// with the man page quoting that if the count of bytes to read is
// greater than `SSIZE_MAX` the result is "unspecified".
const READ_LIMIT: usize = ssize_t::MAX as usize;

impl FileDesc {
pub fn new(fd: c_int) -> FileDesc {
Expand All @@ -29,7 +27,7 @@ impl FileDesc {
self.fd
}

/// Extracts the actual filedescriptor without closing it.
/// Extracts the actual file descriptor without closing it.
pub fn into_raw(self) -> c_int {
let fd = self.fd;
mem::forget(self);
Expand All @@ -38,7 +36,7 @@ impl FileDesc {

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let ret = cvt(unsafe {
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), max_len()))
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -79,7 +77,7 @@ impl FileDesc {
cvt_pread(
self.fd,
buf.as_mut_ptr() as *mut c_void,
cmp::min(buf.len(), max_len()),
cmp::min(buf.len(), READ_LIMIT),
offset as i64,
)
.map(|n| n as usize)
Expand All @@ -88,7 +86,7 @@ impl FileDesc {

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let ret = cvt(unsafe {
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), max_len()))
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
})?;
Ok(ret as usize)
}
Expand Down Expand Up @@ -124,7 +122,7 @@ impl FileDesc {
cvt_pwrite(
self.fd,
buf.as_ptr() as *const c_void,
cmp::min(buf.len(), max_len()),
cmp::min(buf.len(), READ_LIMIT),
offset as i64,
)
.map(|n| n as usize)
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/vxworks/time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cmp::Ordering;
use crate::time::Duration;
use ::core::hash::{Hash, Hasher};
use core::hash::{Hash, Hasher};

pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
use crate::convert::TryInto;
Expand Down