Skip to content

Commit bea2eed

Browse files
authored
Rollup merge of #74587 - lzutao:consts, r=dtolnay
Prefer constant over function Just that I prefer constants over functions that can be made const.
2 parents 5629223 + 0de7fad commit bea2eed

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

src/libstd/sys/unix/fd.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ use crate::sync::atomic::{AtomicBool, Ordering};
77
use crate::sys::cvt;
88
use crate::sys_common::AsInner;
99

10-
use libc::{c_int, c_void, ssize_t};
10+
use libc::{c_int, c_void};
1111

1212
#[derive(Debug)]
1313
pub struct FileDesc {
1414
fd: c_int,
1515
}
1616

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

2930
impl FileDesc {
3031
pub fn new(fd: c_int) -> FileDesc {
@@ -44,7 +45,7 @@ impl FileDesc {
4445

4546
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
4647
let ret = cvt(unsafe {
47-
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), max_len()))
48+
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
4849
})?;
4950
Ok(ret as usize)
5051
}
@@ -92,7 +93,7 @@ impl FileDesc {
9293
cvt_pread64(
9394
self.fd,
9495
buf.as_mut_ptr() as *mut c_void,
95-
cmp::min(buf.len(), max_len()),
96+
cmp::min(buf.len(), READ_LIMIT),
9697
offset as i64,
9798
)
9899
.map(|n| n as usize)
@@ -101,7 +102,7 @@ impl FileDesc {
101102

102103
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
103104
let ret = cvt(unsafe {
104-
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), max_len()))
105+
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
105106
})?;
106107
Ok(ret as usize)
107108
}
@@ -144,7 +145,7 @@ impl FileDesc {
144145
cvt_pwrite64(
145146
self.fd,
146147
buf.as_ptr() as *const c_void,
147-
cmp::min(buf.len(), max_len()),
148+
cmp::min(buf.len(), READ_LIMIT),
148149
offset as i64,
149150
)
150151
.map(|n| n as usize)

src/libstd/sys/vxworks/fd.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ pub struct FileDesc {
1313
fd: c_int,
1414
}
1515

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

2321
impl FileDesc {
2422
pub fn new(fd: c_int) -> FileDesc {
@@ -38,7 +36,7 @@ impl FileDesc {
3836

3937
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
4038
let ret = cvt(unsafe {
41-
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), max_len()))
39+
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
4240
})?;
4341
Ok(ret as usize)
4442
}
@@ -79,7 +77,7 @@ impl FileDesc {
7977
cvt_pread(
8078
self.fd,
8179
buf.as_mut_ptr() as *mut c_void,
82-
cmp::min(buf.len(), max_len()),
80+
cmp::min(buf.len(), READ_LIMIT),
8381
offset as i64,
8482
)
8583
.map(|n| n as usize)
@@ -88,7 +86,7 @@ impl FileDesc {
8886

8987
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
9088
let ret = cvt(unsafe {
91-
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), max_len()))
89+
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
9290
})?;
9391
Ok(ret as usize)
9492
}
@@ -124,7 +122,7 @@ impl FileDesc {
124122
cvt_pwrite(
125123
self.fd,
126124
buf.as_ptr() as *const c_void,
127-
cmp::min(buf.len(), max_len()),
125+
cmp::min(buf.len(), READ_LIMIT),
128126
offset as i64,
129127
)
130128
.map(|n| n as usize)

src/libstd/sys/vxworks/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cmp::Ordering;
22
use crate::time::Duration;
3-
use ::core::hash::{Hash, Hasher};
3+
use core::hash::{Hash, Hasher};
44

55
pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
66
use crate::convert::TryInto;

0 commit comments

Comments
 (0)