diff --git a/src/tcp.rs b/src/tcp.rs index b99e99e19..6fe7d2cf9 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::io::{self, ErrorKind, Read, Write}; use std::mem; use std::net::{self, SocketAddr, Shutdown}; @@ -127,6 +128,12 @@ impl Iterator for NonblockingIter { } } +impl fmt::Debug for TcpListener { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.listener.io().fmt(f) + } +} + impl Stream for TcpListener { type Item = Ready; type Error = io::Error; @@ -335,6 +342,12 @@ impl<'a> Write for &'a TcpStream { } } +impl fmt::Debug for TcpStream { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.source.io().fmt(f) + } +} + impl Stream for TcpStream { type Item = Ready; type Error = io::Error; diff --git a/src/udp.rs b/src/udp.rs index 1b5f4995a..52d957eaa 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,7 @@ use std::io; use std::net::{self, SocketAddr, Ipv4Addr, Ipv6Addr}; use std::sync::Arc; +use std::fmt; use futures::stream::Stream; use futures::{Future, failed, Task, Poll}; @@ -71,16 +72,22 @@ impl UdpSocket { /// /// Address type can be any implementor of `ToSocketAddrs` trait. See its /// documentation for concrete examples. - pub fn send_to(&self, buf: &[u8], target: &SocketAddr) - -> io::Result> { - self.source.io().send_to(buf, target) + pub fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result { + match self.source.io().send_to(buf, target) { + Ok(Some(n)) => Ok(n), + Ok(None) => Err(io::Error::new(io::ErrorKind::WouldBlock, "would block")), + Err(e) => Err(e), + } } /// Receives data from the socket. On success, returns the number of bytes /// read and the address from whence the data came. - pub fn recv_from(&self, buf: &mut [u8]) - -> io::Result> { - self.source.io().recv_from(buf) + pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { + match self.source.io().recv_from(buf) { + Ok(Some(n)) => Ok(n), + Ok(None) => Err(io::Error::new(io::ErrorKind::WouldBlock, "would block")), + Err(e) => Err(e), + } } /// Gets the value of the `SO_BROADCAST` option for this socket. @@ -224,6 +231,12 @@ impl UdpSocket { } } +impl fmt::Debug for UdpSocket { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.source.io().fmt(f) + } +} + impl Stream for UdpSocket { type Item = Ready; type Error = io::Error; diff --git a/tests/udp.rs b/tests/udp.rs new file mode 100644 index 000000000..faf9f25df --- /dev/null +++ b/tests/udp.rs @@ -0,0 +1,43 @@ +extern crate futures; +extern crate futures_mio; + +use futures::Future; +use futures::stream::Stream; + +macro_rules! t { + ($e:expr) => (match $e { + Ok(e) => e, + Err(e) => panic!("{} failed with {:?}", stringify!($e), e), + }) +} + +#[test] +fn send_messages() { + let mut l = t!(futures_mio::Loop::new()); + let a = l.handle().udp_bind(&"127.0.0.1:0".parse().unwrap()); + let b = l.handle().udp_bind(&"127.0.0.1:0".parse().unwrap()); + let (a, b) = t!(l.run(a.join(b))); + let a_addr = t!(a.local_addr()); + let b_addr = t!(b.local_addr()); + + let ((ar, a), (br, b)) = t!(l.run(a.into_future().join(b.into_future()))); + let ar = ar.unwrap(); + let br = br.unwrap(); + + assert!(ar.is_write()); + assert!(!ar.is_read()); + assert!(br.is_write()); + assert!(!br.is_read()); + + assert_eq!(t!(a.send_to(b"1234", &b_addr)), 4); + let (br, b) = t!(l.run(b.into_future())); + let br = br.unwrap(); + + assert!(br.is_read()); + + let mut buf = [0; 32]; + let (size, addr) = t!(b.recv_from(&mut buf)); + assert_eq!(size, 4); + assert_eq!(&buf[..4], b"1234"); + assert_eq!(addr, a_addr); +}