Skip to content

Commit 7b71719

Browse files
committed
Use io::Error::new_const everywhere to avoid allocations.
1 parent 9678362 commit 7b71719

39 files changed

+230
-189
lines changed

library/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

library/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) {

library/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),

library/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

library/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

library/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
}
@@ -1432,7 +1432,10 @@ pub trait Write {
14321432
while !buf.is_empty() {
14331433
match self.write(buf) {
14341434
Ok(0) => {
1435-
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
1435+
return Err(Error::new_const(
1436+
ErrorKind::WriteZero,
1437+
&"failed to write whole buffer",
1438+
));
14361439
}
14371440
Ok(n) => buf = &buf[n..],
14381441
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
@@ -1497,7 +1500,10 @@ pub trait Write {
14971500
while !bufs.is_empty() {
14981501
match self.write_vectored(bufs) {
14991502
Ok(0) => {
1500-
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
1503+
return Err(Error::new_const(
1504+
ErrorKind::WriteZero,
1505+
&"failed to write whole buffer",
1506+
));
15011507
}
15021508
Ok(n) => bufs = IoSlice::advance(bufs, n),
15031509
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
@@ -1571,7 +1577,7 @@ pub trait Write {
15711577
if output.error.is_err() {
15721578
output.error
15731579
} else {
1574-
Err(Error::new(ErrorKind::Other, "formatter error"))
1580+
Err(Error::new_const(ErrorKind::Other, &"formatter error"))
15751581
}
15761582
}
15771583
}

library/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
}

library/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
}

library/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

library/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

library/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
}

library/std/src/sys/hermit/mod.rs

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

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

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

0 commit comments

Comments
 (0)