diff options
Diffstat (limited to 'src/libstd/net/udp.rs')
| -rw-r--r-- | src/libstd/net/udp.rs | 72 |
1 files changed, 42 insertions, 30 deletions
diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index a9e4457f423..0096b827ca4 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -1,6 +1,6 @@ use crate::fmt; use crate::io::{self, Error, ErrorKind}; -use crate::net::{ToSocketAddrs, SocketAddr, Ipv4Addr, Ipv6Addr}; +use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs}; use crate::sys_common::net as net_imp; use crate::sys_common::{AsInner, FromInner, IntoInner}; use crate::time::Duration; @@ -171,12 +171,10 @@ impl UdpSocket { /// socket.send_to(&[0; 10], "127.0.0.1:4242").expect("couldn't send data"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A) - -> io::Result<usize> { + 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(ErrorKind::InvalidInput, "no addresses to send data to")), } } @@ -817,15 +815,21 @@ impl UdpSocket { } impl AsInner<net_imp::UdpSocket> for UdpSocket { - fn as_inner(&self) -> &net_imp::UdpSocket { &self.0 } + fn as_inner(&self) -> &net_imp::UdpSocket { + &self.0 + } } impl FromInner<net_imp::UdpSocket> for UdpSocket { - fn from_inner(inner: net_imp::UdpSocket) -> UdpSocket { UdpSocket(inner) } + fn from_inner(inner: net_imp::UdpSocket) -> UdpSocket { + UdpSocket(inner) + } } impl IntoInner<net_imp::UdpSocket> for UdpSocket { - fn into_inner(self) -> net_imp::UdpSocket { self.0 } + fn into_inner(self) -> net_imp::UdpSocket { + self.0 + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -838,12 +842,12 @@ impl fmt::Debug for UdpSocket { #[cfg(all(test, not(any(target_os = "cloudabi", target_os = "emscripten", target_env = "sgx"))))] mod tests { use crate::io::ErrorKind; - use crate::net::*; use crate::net::test::{next_test_ip4, next_test_ip6}; + use crate::net::*; use crate::sync::mpsc::channel; use crate::sys_common::AsInner; - use crate::time::{Instant, Duration}; use crate::thread; + use crate::time::{Duration, Instant}; fn each_ip(f: &mut dyn FnMut(SocketAddr, SocketAddr)) { f(next_test_ip4(), next_test_ip4()); @@ -856,16 +860,14 @@ mod tests { Ok(t) => t, Err(e) => panic!("received error for `{}`: {}", stringify!($e), e), } - } + }; } #[test] fn bind_error() { match UdpSocket::bind("1.1.1.1:9999") { Ok(..) => panic!(), - Err(e) => { - assert_eq!(e.kind(), ErrorKind::AddrNotAvailable) - } + Err(e) => assert_eq!(e.kind(), ErrorKind::AddrNotAvailable), } } @@ -875,7 +877,7 @@ mod tests { let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let client = t!(UdpSocket::bind(&client_ip)); rx1.recv().unwrap(); t!(client.send_to(&[99], &server_ip)); @@ -917,7 +919,7 @@ mod tests { let sock1 = t!(UdpSocket::bind(&addr1)); let sock2 = t!(UdpSocket::bind(&addr2)); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let mut buf = [0, 0]; assert_eq!(sock2.recv_from(&mut buf).unwrap(), (1, addr1)); assert_eq!(buf[0], 1); @@ -928,7 +930,7 @@ mod tests { let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { rx1.recv().unwrap(); t!(sock3.send_to(&[1], &addr2)); tx2.send(()).unwrap(); @@ -948,7 +950,7 @@ mod tests { let (tx1, rx) = channel(); let tx2 = tx1.clone(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { t!(sock2.send_to(&[1], &addr1)); rx.recv().unwrap(); t!(sock2.send_to(&[2], &addr1)); @@ -958,7 +960,7 @@ mod tests { let sock3 = t!(sock1.try_clone()); let (done, rx) = channel(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let mut buf = [0, 0]; t!(sock3.recv_from(&mut buf)); tx2.send(()).unwrap(); @@ -981,7 +983,7 @@ mod tests { let (tx, rx) = channel(); let (serv_tx, serv_rx) = channel(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let mut buf = [0, 1]; rx.recv().unwrap(); t!(sock2.recv_from(&mut buf)); @@ -992,15 +994,19 @@ mod tests { let (done, rx) = channel(); let tx2 = tx.clone(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { match sock3.send_to(&[1], &addr2) { - Ok(..) => { let _ = tx2.send(()); } + Ok(..) => { + let _ = tx2.send(()); + } Err(..) => {} } done.send(()).unwrap(); }); match sock1.send_to(&[2], &addr2) { - Ok(..) => { let _ = tx.send(()); } + Ok(..) => { + let _ = tx.send(()); + } Err(..) => {} } drop(tx); @@ -1012,13 +1018,13 @@ mod tests { #[test] fn debug() { - let name = if cfg!(windows) {"socket"} else {"fd"}; + let name = if cfg!(windows) { "socket" } else { "fd" }; let socket_addr = next_test_ip4(); let udpsock = t!(UdpSocket::bind(&socket_addr)); let udpsock_inner = udpsock.0.socket().as_inner(); - let compare = format!("UdpSocket {{ addr: {:?}, {}: {:?} }}", - socket_addr, name, udpsock_inner); + let compare = + format!("UdpSocket {{ addr: {:?}, {}: {:?} }}", socket_addr, name, udpsock_inner); assert_eq!(format!("{:?}", udpsock), compare); } @@ -1063,8 +1069,11 @@ mod tests { loop { let kind = stream.recv_from(&mut buf).err().expect("expected error").kind(); if kind != ErrorKind::Interrupted { - assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut, - "unexpected_error: {:?}", kind); + assert!( + kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut, + "unexpected_error: {:?}", + kind + ); break; } } @@ -1088,8 +1097,11 @@ mod tests { loop { let kind = stream.recv_from(&mut buf).err().expect("expected error").kind(); if kind != ErrorKind::Interrupted { - assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut, - "unexpected_error: {:?}", kind); + assert!( + kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut, + "unexpected_error: {:?}", + kind + ); break; } } |
