Rollup merge of #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 https://github.com/rust-lang/rust/issues/83352
This commit is contained in:
commit
a42e62fa0a
41 changed files with 272 additions and 189 deletions
|
@ -333,7 +333,7 @@ where
|
|||
let ret = f(g.buf);
|
||||
if str::from_utf8(&g.buf[g.len..]).is_err() {
|
||||
ret.and_then(|_| {
|
||||
Err(Error::new(ErrorKind::InvalidData, "stream did not contain valid UTF-8"))
|
||||
Err(Error::new_const(ErrorKind::InvalidData, &"stream did not contain valid UTF-8"))
|
||||
})
|
||||
} else {
|
||||
g.len = g.buf.len();
|
||||
|
@ -429,7 +429,7 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
|
|||
}
|
||||
}
|
||||
if !buf.is_empty() {
|
||||
Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
|
||||
Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1437,7 +1437,10 @@ pub trait Write {
|
|||
while !buf.is_empty() {
|
||||
match self.write(buf) {
|
||||
Ok(0) => {
|
||||
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
|
||||
return Err(Error::new_const(
|
||||
ErrorKind::WriteZero,
|
||||
&"failed to write whole buffer",
|
||||
));
|
||||
}
|
||||
Ok(n) => buf = &buf[n..],
|
||||
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
||||
|
@ -1502,7 +1505,10 @@ pub trait Write {
|
|||
while !bufs.is_empty() {
|
||||
match self.write_vectored(bufs) {
|
||||
Ok(0) => {
|
||||
return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
|
||||
return Err(Error::new_const(
|
||||
ErrorKind::WriteZero,
|
||||
&"failed to write whole buffer",
|
||||
));
|
||||
}
|
||||
Ok(n) => bufs = IoSlice::advance(bufs, n),
|
||||
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
||||
|
@ -1576,7 +1582,7 @@ pub trait Write {
|
|||
if output.error.is_err() {
|
||||
output.error
|
||||
} else {
|
||||
Err(Error::new(ErrorKind::Other, "formatter error"))
|
||||
Err(Error::new_const(ErrorKind::Other, &"formatter error"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue