Skip to content

Commit 1645ca6

Browse files
authored
Rollup merge of rust-lang#83353 - m-ou-se:io-error-avoid-alloc, r=nagisa
Add internal io::Error::new_const to avoid allocations. This makes it possible to have a io::Error containing a message with zero allocations, and uses that everywhere to avoid the *three* allocations involved in `io::Error::new(kind, "message")`. The function signature isn't perfect, because it needs a reference to the `&str`. So for now, this is just a `pub(crate)` function. Later, we'll be able to use `fn new_const<MSG: &'static str>(kind: ErrorKind)` to make that a bit better. (Then we'll also be able to use some ZST trickery if that would result in more efficient code.) See rust-lang#83352
2 parents 959c9a9 + 6d503c9 commit 1645ca6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+272
-189
lines changed

std/src/ffi/c_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ impl fmt::Display for NulError {
10361036
impl From<NulError> for io::Error {
10371037
/// Converts a [`NulError`] into a [`io::Error`].
10381038
fn from(_: NulError) -> io::Error {
1039-
io::Error::new(io::ErrorKind::InvalidInput, "data provided contains a nul byte")
1039+
io::Error::new_const(io::ErrorKind::InvalidInput, &"data provided contains a nul byte")
10401040
}
10411041
}
10421042

std/src/fs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,10 @@ impl DirBuilder {
21882188
match path.parent() {
21892189
Some(p) => self.create_dir_all(p)?,
21902190
None => {
2191-
return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree"));
2191+
return Err(io::Error::new_const(
2192+
io::ErrorKind::Other,
2193+
&"failed to create whole tree",
2194+
));
21922195
}
21932196
}
21942197
match self.inner.mkdir(path) {

std/src/io/buffered/bufwriter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ impl<W: Write> BufWriter<W> {
164164

165165
match r {
166166
Ok(0) => {
167-
return Err(Error::new(
167+
return Err(Error::new_const(
168168
ErrorKind::WriteZero,
169-
"failed to write the buffered data",
169+
&"failed to write the buffered data",
170170
));
171171
}
172172
Ok(n) => guard.consume(n),

std/src/io/cursor.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ where
229229
self.pos = n;
230230
Ok(self.pos)
231231
}
232-
None => Err(Error::new(
232+
None => Err(Error::new_const(
233233
ErrorKind::InvalidInput,
234-
"invalid seek to a negative or overflowing position",
234+
&"invalid seek to a negative or overflowing position",
235235
)),
236236
}
237237
}
@@ -328,9 +328,9 @@ fn slice_write_vectored(
328328
// Resizing write implementation
329329
fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> {
330330
let pos: usize = (*pos_mut).try_into().map_err(|_| {
331-
Error::new(
331+
Error::new_const(
332332
ErrorKind::InvalidInput,
333-
"cursor position exceeds maximum possible vector length",
333+
&"cursor position exceeds maximum possible vector length",
334334
)
335335
})?;
336336
// Make sure the internal buffer is as least as big as where we

std/src/io/error.rs

+26
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ impl fmt::Debug for Error {
6969
enum Repr {
7070
Os(i32),
7171
Simple(ErrorKind),
72+
// &str is a fat pointer, but &&str is a thin pointer.
73+
SimpleMessage(ErrorKind, &'static &'static str),
7274
Custom(Box<Custom>),
7375
}
7476

@@ -259,6 +261,18 @@ impl Error {
259261
Error { repr: Repr::Custom(Box::new(Custom { kind, error })) }
260262
}
261263

264+
/// Creates a new I/O error from a known kind of error as well as a
265+
/// constant message.
266+
///
267+
/// This function does not allocate.
268+
///
269+
/// This function should maybe change to
270+
/// `new_const<const MSG: &'static str>(kind: ErrorKind)`
271+
/// in the future, when const generics allow that.
272+
pub(crate) const fn new_const(kind: ErrorKind, message: &'static &'static str) -> Error {
273+
Self { repr: Repr::SimpleMessage(kind, message) }
274+
}
275+
262276
/// Returns an error representing the last OS error which occurred.
263277
///
264278
/// This function reads the value of `errno` for the target platform (e.g.
@@ -342,6 +356,7 @@ impl Error {
342356
Repr::Os(i) => Some(i),
343357
Repr::Custom(..) => None,
344358
Repr::Simple(..) => None,
359+
Repr::SimpleMessage(..) => None,
345360
}
346361
}
347362

@@ -377,6 +392,7 @@ impl Error {
377392
match self.repr {
378393
Repr::Os(..) => None,
379394
Repr::Simple(..) => None,
395+
Repr::SimpleMessage(..) => None,
380396
Repr::Custom(ref c) => Some(&*c.error),
381397
}
382398
}
@@ -448,6 +464,7 @@ impl Error {
448464
match self.repr {
449465
Repr::Os(..) => None,
450466
Repr::Simple(..) => None,
467+
Repr::SimpleMessage(..) => None,
451468
Repr::Custom(ref mut c) => Some(&mut *c.error),
452469
}
453470
}
@@ -484,6 +501,7 @@ impl Error {
484501
match self.repr {
485502
Repr::Os(..) => None,
486503
Repr::Simple(..) => None,
504+
Repr::SimpleMessage(..) => None,
487505
Repr::Custom(c) => Some(c.error),
488506
}
489507
}
@@ -512,6 +530,7 @@ impl Error {
512530
Repr::Os(code) => sys::decode_error_kind(code),
513531
Repr::Custom(ref c) => c.kind,
514532
Repr::Simple(kind) => kind,
533+
Repr::SimpleMessage(kind, _) => kind,
515534
}
516535
}
517536
}
@@ -527,6 +546,9 @@ impl fmt::Debug for Repr {
527546
.finish(),
528547
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
529548
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
549+
Repr::SimpleMessage(kind, &message) => {
550+
fmt.debug_struct("Error").field("kind", &kind).field("message", &message).finish()
551+
}
530552
}
531553
}
532554
}
@@ -541,6 +563,7 @@ impl fmt::Display for Error {
541563
}
542564
Repr::Custom(ref c) => c.error.fmt(fmt),
543565
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
566+
Repr::SimpleMessage(_, &msg) => msg.fmt(fmt),
544567
}
545568
}
546569
}
@@ -551,6 +574,7 @@ impl error::Error for Error {
551574
fn description(&self) -> &str {
552575
match self.repr {
553576
Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
577+
Repr::SimpleMessage(_, &msg) => msg,
554578
Repr::Custom(ref c) => c.error.description(),
555579
}
556580
}
@@ -560,6 +584,7 @@ impl error::Error for Error {
560584
match self.repr {
561585
Repr::Os(..) => None,
562586
Repr::Simple(..) => None,
587+
Repr::SimpleMessage(..) => None,
563588
Repr::Custom(ref c) => c.error.cause(),
564589
}
565590
}
@@ -568,6 +593,7 @@ impl error::Error for Error {
568593
match self.repr {
569594
Repr::Os(..) => None,
570595
Repr::Simple(..) => None,
596+
Repr::SimpleMessage(..) => None,
571597
Repr::Custom(ref c) => c.error.source(),
572598
}
573599
}

std/src/io/error/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use super::{Custom, Error, ErrorKind, Repr};
22
use crate::error;
33
use crate::fmt;
4+
use crate::mem::size_of;
45
use crate::sys::decode_error_kind;
56
use crate::sys::os::error_string;
67

8+
#[test]
9+
fn test_size() {
10+
assert!(size_of::<Error>() <= size_of::<[usize; 2]>());
11+
}
12+
713
#[test]
814
fn test_debug_error() {
915
let code = 6;
@@ -51,3 +57,13 @@ fn test_downcasting() {
5157
let extracted = err.into_inner().unwrap();
5258
extracted.downcast::<TestError>().unwrap();
5359
}
60+
61+
#[test]
62+
fn test_const() {
63+
const E: Error = Error::new_const(ErrorKind::NotFound, &"hello");
64+
65+
assert_eq!(E.kind(), ErrorKind::NotFound);
66+
assert_eq!(E.to_string(), "hello");
67+
assert!(format!("{:?}", E).contains("\"hello\""));
68+
assert!(format!("{:?}", E).contains("NotFound"));
69+
}

std/src/io/impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Read for &[u8] {
263263
#[inline]
264264
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
265265
if buf.len() > self.len() {
266-
return Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"));
266+
return Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer"));
267267
}
268268
let (a, b) = self.split_at(buf.len());
269269

@@ -345,7 +345,7 @@ impl Write for &mut [u8] {
345345
if self.write(data)? == data.len() {
346346
Ok(())
347347
} else {
348-
Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"))
348+
Err(Error::new_const(ErrorKind::WriteZero, &"failed to write whole buffer"))
349349
}
350350
}
351351

std/src/io/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ where
333333
let ret = f(g.buf);
334334
if str::from_utf8(&g.buf[g.len..]).is_err() {
335335
ret.and_then(|_| {
336-
Err(Error::new(ErrorKind::InvalidData, "stream did not contain valid UTF-8"))
336+
Err(Error::new_const(ErrorKind::InvalidData, &"stream did not contain valid UTF-8"))
337337
})
338338
} else {
339339
g.len = g.buf.len();
@@ -429,7 +429,7 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
429429
}
430430
}
431431
if !buf.is_empty() {
432-
Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
432+
Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer"))
433433
} else {
434434
Ok(())
435435
}
@@ -1437,7 +1437,10 @@ pub trait Write {
14371437
while !buf.is_empty() {
14381438
match self.write(buf) {
14391439
Ok(0) => {
1440-
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
1440+
return Err(Error::new_const(
1441+
ErrorKind::WriteZero,
1442+
&"failed to write whole buffer",
1443+
));
14411444
}
14421445
Ok(n) => buf = &buf[n..],
14431446
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
@@ -1502,7 +1505,10 @@ pub trait Write {
15021505
while !bufs.is_empty() {
15031506
match self.write_vectored(bufs) {
15041507
Ok(0) => {
1505-
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
1508+
return Err(Error::new_const(
1509+
ErrorKind::WriteZero,
1510+
&"failed to write whole buffer",
1511+
));
15061512
}
15071513
Ok(n) => bufs = IoSlice::advance(bufs, n),
15081514
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
@@ -1576,7 +1582,7 @@ pub trait Write {
15761582
if output.error.is_err() {
15771583
output.error
15781584
} else {
1579-
Err(Error::new(ErrorKind::Other, "formatter error"))
1585+
Err(Error::new_const(ErrorKind::Other, &"formatter error"))
15801586
}
15811587
}
15821588
}

std/src/io/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ fn take_eof() {
152152

153153
impl Read for R {
154154
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
155-
Err(io::Error::new(io::ErrorKind::Other, ""))
155+
Err(io::Error::new_const(io::ErrorKind::Other, &""))
156156
}
157157
}
158158
impl BufRead for R {
159159
fn fill_buf(&mut self) -> io::Result<&[u8]> {
160-
Err(io::Error::new(io::ErrorKind::Other, ""))
160+
Err(io::Error::new_const(io::ErrorKind::Other, &""))
161161
}
162162
fn consume(&mut self, _amt: usize) {}
163163
}

std/src/net/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ where
8888
}
8989
}
9090
Err(last_err.unwrap_or_else(|| {
91-
Error::new(ErrorKind::InvalidInput, "could not resolve to any addresses")
91+
Error::new_const(ErrorKind::InvalidInput, &"could not resolve to any addresses")
9292
}))
9393
}

std/src/net/udp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl UdpSocket {
173173
pub fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A) -> io::Result<usize> {
174174
match addr.to_socket_addrs()?.next() {
175175
Some(addr) => self.0.send_to(buf, &addr),
176-
None => Err(Error::new(ErrorKind::InvalidInput, "no addresses to send data to")),
176+
None => Err(Error::new_const(ErrorKind::InvalidInput, &"no addresses to send data to")),
177177
}
178178
}
179179

std/src/sys/hermit/fd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ impl FileDesc {
4646
self.duplicate_path(&[])
4747
}
4848
pub fn duplicate_path(&self, _path: &[u8]) -> io::Result<FileDesc> {
49-
Err(io::Error::new(ErrorKind::Other, "duplicate isn't supported"))
49+
Err(io::Error::new_const(ErrorKind::Other, &"duplicate isn't supported"))
5050
}
5151

5252
pub fn nonblocking(&self) -> io::Result<bool> {
5353
Ok(false)
5454
}
5555

5656
pub fn set_cloexec(&self) -> io::Result<()> {
57-
Err(io::Error::new(ErrorKind::Other, "cloexec isn't supported"))
57+
Err(io::Error::new_const(ErrorKind::Other, &"cloexec isn't supported"))
5858
}
5959

6060
pub fn set_nonblocking(&self, _nonblocking: bool) -> io::Result<()> {
61-
Err(io::Error::new(ErrorKind::Other, "nonblocking isn't supported"))
61+
Err(io::Error::new_const(ErrorKind::Other, &"nonblocking isn't supported"))
6262
}
6363
}
6464

std/src/sys/hermit/fs.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl OpenOptions {
226226
(false, _, true) => Ok(O_WRONLY | O_APPEND),
227227
(true, _, true) => Ok(O_RDWR | O_APPEND),
228228
(false, false, false) => {
229-
Err(io::Error::new(ErrorKind::InvalidInput, "invalid access mode"))
229+
Err(io::Error::new_const(ErrorKind::InvalidInput, &"invalid access mode"))
230230
}
231231
}
232232
}
@@ -236,12 +236,18 @@ impl OpenOptions {
236236
(true, false) => {}
237237
(false, false) => {
238238
if self.truncate || self.create || self.create_new {
239-
return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
239+
return Err(io::Error::new_const(
240+
ErrorKind::InvalidInput,
241+
&"invalid creation mode",
242+
));
240243
}
241244
}
242245
(_, true) => {
243246
if self.truncate && !self.create_new {
244-
return Err(io::Error::new(ErrorKind::InvalidInput, "invalid creation mode"));
247+
return Err(io::Error::new_const(
248+
ErrorKind::InvalidInput,
249+
&"invalid creation mode",
250+
));
245251
}
246252
}
247253
}

std/src/sys/hermit/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ pub fn unsupported<T>() -> crate::io::Result<T> {
5555
}
5656

5757
pub fn unsupported_err() -> crate::io::Error {
58-
crate::io::Error::new(crate::io::ErrorKind::Other, "operation not supported on HermitCore yet")
58+
crate::io::Error::new_const(
59+
crate::io::ErrorKind::Other,
60+
&"operation not supported on HermitCore yet",
61+
)
5962
}
6063

6164
// This enum is used as the storage for a bunch of types which can't actually

0 commit comments

Comments
 (0)