about summary refs log tree commit diff
path: root/library/std/src/net
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-03-24 01:52:29 +0100
committerGitHub <noreply@github.com>2021-03-24 01:52:29 +0100
commita42e62fa0a59d0ba620889f97513929a113a6fbd (patch)
treec8dabc69676343818b44914550e763ddd18d214d /library/std/src/net
parent2f611da1d66ae98b53358bcb7739884524b7e18d (diff)
parent6bbcc5bfbbfd9ba5a6d584a753fa32d80e3a7a17 (diff)
downloadrust-a42e62fa0a59d0ba620889f97513929a113a6fbd.tar.gz
rust-a42e62fa0a59d0ba620889f97513929a113a6fbd.zip
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
Diffstat (limited to 'library/std/src/net')
-rw-r--r--library/std/src/net/mod.rs2
-rw-r--r--library/std/src/net/udp.rs2
2 files changed, 2 insertions, 2 deletions
diff --git a/library/std/src/net/mod.rs b/library/std/src/net/mod.rs
index d4b1552fec5..d814e9b25ba 100644
--- a/library/std/src/net/mod.rs
+++ b/library/std/src/net/mod.rs
@@ -88,6 +88,6 @@ where
         }
     }
     Err(last_err.unwrap_or_else(|| {
-        Error::new(ErrorKind::InvalidInput, "could not resolve to any addresses")
+        Error::new_const(ErrorKind::InvalidInput, &"could not resolve to any addresses")
     }))
 }
diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs
index 2377a76a33d..b9af5992dff 100644
--- a/library/std/src/net/udp.rs
+++ b/library/std/src/net/udp.rs
@@ -173,7 +173,7 @@ impl UdpSocket {
     pub fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A) -> io::Result<usize> {
         match addr.to_socket_addrs()?.next() {
             Some(addr) => self.0.send_to(buf, &addr),
-            None => Err(Error::new(ErrorKind::InvalidInput, "no addresses to send data to")),
+            None => Err(Error::new_const(ErrorKind::InvalidInput, &"no addresses to send data to")),
         }
     }