From 81e9db783fd7f104ad3d15d60436a8767172e0df Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Thu, 18 Mar 2021 03:44:00 +0100 Subject: simplify addresse deserialialiser and limit allocations --- src/util/config.rs | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/src/util/config.rs b/src/util/config.rs index fe6ab979..313692fc 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -1,4 +1,3 @@ -use std::fmt; use std::io::Read; use std::net::SocketAddr; use std::path::PathBuf; @@ -92,28 +91,13 @@ fn deserialize_addr<'de, D>(deserializer: D) -> Result where D: de::Deserializer<'de>, { - struct AddrVisitor; - - impl<'de> de::Visitor<'de> for AddrVisitor { - type Value = SocketAddr; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a string representing a socket address") - } - - fn visit_str(self, s: &str) -> Result - where - E: de::Error, - { - use std::net::ToSocketAddrs; - s.to_socket_addrs() - .map_err(|_| de::Error::custom("could not resolve to a socket address"))? - .next() - .ok_or(de::Error::custom("could not resolve to a socket address")) - } - } + use std::net::ToSocketAddrs; - deserializer.deserialize_any(AddrVisitor) + <&str>::deserialize(deserializer)? + .to_socket_addrs() + .map_err(|_| de::Error::custom("could not resolve to a socket address"))? + .next() + .ok_or(de::Error::custom("could not resolve to a socket address")) } fn deserialize_vec_addr<'de, D>(deserializer: D) -> Result, D::Error> @@ -123,7 +107,7 @@ where use std::net::ToSocketAddrs; let mut res = vec![]; - for s in >::deserialize(deserializer)? { + for s in >::deserialize(deserializer)? { res.push( s.to_socket_addrs() .map_err(|_| de::Error::custom("could not resolve to a socket address"))? -- cgit v1.2.3