about summary refs log tree commit diff
path: root/src/libstd/io/net/tcp.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-12-23 11:53:35 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-12-29 12:16:49 -0800
commitbc83a009f655dd3896be4a7cd33cac8032a605f2 (patch)
tree3acc8533031219690fe14fa56f4427cfa9297296 /src/libstd/io/net/tcp.rs
parentbb8f4fc3b73918abd19d67be702f78e8f73d1874 (diff)
downloadrust-bc83a009f655dd3896be4a7cd33cac8032a605f2.tar.gz
rust-bc83a009f655dd3896be4a7cd33cac8032a605f2.zip
std: Second pass stabilization for `comm`
This commit is a second pass stabilization for the `std::comm` module,
performing the following actions:

* The entire `std::comm` module was moved under `std::sync::mpsc`. This movement
  reflects that channels are just yet another synchronization primitive, and
  they don't necessarily deserve a special place outside of the other
  concurrency primitives that the standard library offers.
* The `send` and `recv` methods have all been removed.
* The `send_opt` and `recv_opt` methods have been renamed to `send` and `recv`.
  This means that all send/receive operations return a `Result` now indicating
  whether the operation was successful or not.
* The error type of `send` is now a `SendError` to implement a custom error
  message and allow for `unwrap()`. The error type contains an `into_inner`
  method to extract the value.
* The error type of `recv` is now `RecvError` for the same reasons as `send`.
* The `TryRecvError` and `TrySendError` types have had public reexports removed
  of their variants and the variant names have been tweaked with enum
  namespacing rules.
* The `Messages` iterator is renamed to `Iter`

This functionality is now all `#[stable]`:

* `Sender`
* `SyncSender`
* `Receiver`
* `std::sync::mpsc`
* `channel`
* `sync_channel`
* `Iter`
* `Sender::send`
* `Sender::clone`
* `SyncSender::send`
* `SyncSender::try_send`
* `SyncSender::clone`
* `Receiver::recv`
* `Receiver::try_recv`
* `Receiver::iter`
* `SendError`
* `RecvError`
* `TrySendError::{mod, Full, Disconnected}`
* `TryRecvError::{mod, Empty, Disconnected}`
* `SendError::into_inner`
* `TrySendError::into_inner`

This is a breaking change due to the modification of where this module is
located, as well as the changing of the semantics of `send` and `recv`. Most
programs just need to rename imports of `std::comm` to `std::sync::mpsc` and
add calls to `unwrap` after a send or a receive operation.

[breaking-change]
Diffstat (limited to 'src/libstd/io/net/tcp.rs')
-rw-r--r--src/libstd/io/net/tcp.rs125
1 files changed, 67 insertions, 58 deletions
diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs
index 67aaf7dda8e..57ffcfaad30 100644
--- a/src/libstd/io/net/tcp.rs
+++ b/src/libstd/io/net/tcp.rs
@@ -484,12 +484,15 @@ impl sys_common::AsInner<TcpAcceptorImp> for TcpAcceptor {
 mod test {
     use prelude::v1::*;
 
-    use comm::channel;
+    use sync::mpsc::channel;
     use thread::Thread;
     use io::net::tcp::*;
     use io::net::ip::*;
-    use io::*;
     use io::test::*;
+    use io::{EndOfFile, TimedOut, ShortWrite, IoError};
+    use io::{ConnectionRefused, BrokenPipe, ConnectionAborted};
+    use io::{ConnectionReset, NotConnected, PermissionDenied, OtherIoError};
+    use io::{Acceptor, Listener};
 
     // FIXME #11530 this fails on android because tests are run as root
     #[cfg_attr(any(windows, target_os = "android"), ignore)]
@@ -694,11 +697,11 @@ mod test {
         let (tx, rx) = channel();
         let _t = Thread::spawn(move|| {
             drop(TcpStream::connect(addr));
-            tx.send(());
+            tx.send(()).unwrap();
         });
 
         let mut stream = acceptor.accept();
-        rx.recv();
+        rx.recv().unwrap();
         let buf = [0];
         match stream.write(&buf) {
             Ok(..) => {}
@@ -719,11 +722,11 @@ mod test {
         let (tx, rx) = channel();
         let _t = Thread::spawn(move|| {
             drop(TcpStream::connect(addr));
-            tx.send(());
+            tx.send(()).unwrap();
         });
 
         let mut stream = acceptor.accept();
-        rx.recv();
+        rx.recv().unwrap();
         let buf = [0];
         match stream.write(&buf) {
             Ok(..) => {}
@@ -969,20 +972,20 @@ mod test {
         let (tx, rx) = channel();
         let _t = Thread::spawn(move|| {
             let mut srv = TcpListener::bind(addr).listen().unwrap();
-            tx.send(());
+            tx.send(()).unwrap();
             let mut cl = srv.accept().unwrap();
             cl.write(&[10]).unwrap();
             let mut b = [0];
             cl.read(&mut b).unwrap();
-            tx.send(());
+            tx.send(()).unwrap();
         });
 
-        rx.recv();
+        rx.recv().unwrap();
         let mut c = TcpStream::connect(addr).unwrap();
         let mut b = [0, ..10];
         assert_eq!(c.read(&mut b), Ok(1));
         c.write(&[1]).unwrap();
-        rx.recv();
+        rx.recv().unwrap();
     }
 
     #[test]
@@ -1005,19 +1008,19 @@ mod test {
         let (tx, rx) = channel();
 
         let _t = Thread::spawn(move|| {
-            rx.recv();
+            rx.recv().unwrap();
             let _stream = TcpStream::connect(addr).unwrap();
             // Close
-            rx.recv();
+            rx.recv().unwrap();
         });
 
         {
             let mut acceptor = TcpListener::bind(addr).listen();
-            tx.send(());
+            tx.send(()).unwrap();
             {
                 let _stream = acceptor.accept().unwrap();
                 // Close client
-                tx.send(());
+                tx.send(()).unwrap();
             }
             // Close listener
         }
@@ -1044,14 +1047,14 @@ mod test {
         let (tx2, rx2) = channel();
         let _t = Thread::spawn(move|| {
             let mut s2 = s2;
-            rx1.recv();
+            rx1.recv().unwrap();
             s2.write(&[1]).unwrap();
-            tx2.send(());
+            tx2.send(()).unwrap();
         });
-        tx1.send(());
+        tx1.send(()).unwrap();
         let mut buf = [0, 0];
         assert_eq!(s1.read(&mut buf), Ok(1));
-        rx2.recv();
+        rx2.recv().unwrap();
     }
 
     #[test]
@@ -1064,9 +1067,9 @@ mod test {
         let _t = Thread::spawn(move|| {
             let mut s = TcpStream::connect(addr);
             s.write(&[1]).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
             s.write(&[2]).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
         });
 
         let mut s1 = acceptor.accept().unwrap();
@@ -1077,14 +1080,14 @@ mod test {
             let mut s2 = s2;
             let mut buf = [0, 0];
             s2.read(&mut buf).unwrap();
-            tx2.send(());
-            done.send(());
+            tx2.send(()).unwrap();
+            done.send(()).unwrap();
         });
         let mut buf = [0, 0];
         s1.read(&mut buf).unwrap();
-        tx1.send(());
+        tx1.send(()).unwrap();
 
-        rx.recv();
+        rx.recv().unwrap();
     }
 
     #[test]
@@ -1106,11 +1109,11 @@ mod test {
         let _t = Thread::spawn(move|| {
             let mut s2 = s2;
             s2.write(&[1]).unwrap();
-            done.send(());
+            done.send(()).unwrap();
         });
         s1.write(&[2]).unwrap();
 
-        rx.recv();
+        rx.recv().unwrap();
     }
 
     #[test]
@@ -1152,9 +1155,9 @@ mod test {
         if !cfg!(target_os = "freebsd") {
             let (tx, rx) = channel();
             let _t = Thread::spawn(move|| {
-                tx.send(TcpStream::connect(addr).unwrap());
+                tx.send(TcpStream::connect(addr).unwrap()).unwrap();
             });
-            let _l = rx.recv();
+            let _l = rx.recv().unwrap();
             for i in range(0i, 1001) {
                 match a.accept() {
                     Ok(..) => break,
@@ -1182,7 +1185,7 @@ mod test {
         Thread::spawn(move|| {
             let mut a = a;
             let _s = a.accept().unwrap();
-            let _ = rx.recv_opt();
+            let _ = rx.recv().unwrap();
         }).detach();
 
         let mut b = [0];
@@ -1219,7 +1222,7 @@ mod test {
         Thread::spawn(move|| {
             let mut a = a;
             let _s = a.accept().unwrap();
-            let _ = rx.recv_opt();
+            let _ = rx.recv().unwrap();
         }).detach();
 
         let mut s = TcpStream::connect(addr).unwrap();
@@ -1228,13 +1231,13 @@ mod test {
         let _t = Thread::spawn(move|| {
             let mut s2 = s2;
             assert!(s2.read(&mut [0]).is_err());
-            tx.send(());
+            tx.send(()).unwrap();
         });
         // this should wake up the child task
         s.close_read().unwrap();
 
         // this test will never finish if the child doesn't wake up
-        rx.recv();
+        rx.recv().unwrap();
     }
 
     #[test]
@@ -1244,9 +1247,9 @@ mod test {
         let (tx, rx) = channel::<()>();
         Thread::spawn(move|| {
             let mut s = TcpStream::connect(addr).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
             assert!(s.write(&[0]).is_ok());
-            let _ = rx.recv_opt();
+            let _ = rx.recv();
         }).detach();
 
         let mut s = a.accept().unwrap();
@@ -1265,7 +1268,7 @@ mod test {
         }
         assert_eq!(s.write(&[0]).err().unwrap().kind, TimedOut);
 
-        tx.send(());
+        tx.send(()).unwrap();
         s.set_timeout(None);
         assert_eq!(s.read(&mut [0, 0]), Ok(1));
     }
@@ -1277,7 +1280,7 @@ mod test {
         let (tx, rx) = channel::<()>();
         Thread::spawn(move|| {
             let mut s = TcpStream::connect(addr).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
             let mut amt = 0;
             while amt < 100 * 128 * 1024 {
                 match s.read(&mut [0, ..128 * 1024]) {
@@ -1285,7 +1288,7 @@ mod test {
                     Err(e) => panic!("{}", e),
                 }
             }
-            let _ = rx.recv_opt();
+            let _ = rx.recv();
         }).detach();
 
         let mut s = a.accept().unwrap();
@@ -1293,7 +1296,7 @@ mod test {
         assert_eq!(s.read(&mut [0]).err().unwrap().kind, TimedOut);
         assert_eq!(s.read(&mut [0]).err().unwrap().kind, TimedOut);
 
-        tx.send(());
+        tx.send(()).unwrap();
         for _ in range(0i, 100) {
             assert!(s.write(&[0, ..128 * 1024]).is_ok());
         }
@@ -1306,9 +1309,9 @@ mod test {
         let (tx, rx) = channel::<()>();
         Thread::spawn(move|| {
             let mut s = TcpStream::connect(addr).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
             assert!(s.write(&[0]).is_ok());
-            let _ = rx.recv_opt();
+            let _ = rx.recv();
         }).detach();
 
         let mut s = a.accept().unwrap();
@@ -1323,7 +1326,7 @@ mod test {
         }
         assert_eq!(s.write(&[0]).err().unwrap().kind, TimedOut);
 
-        tx.send(());
+        tx.send(()).unwrap();
         assert!(s.read(&mut [0]).is_ok());
     }
 
@@ -1334,9 +1337,9 @@ mod test {
         let (tx, rx) = channel::<()>();
         Thread::spawn(move|| {
             let mut s = TcpStream::connect(addr).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
             assert_eq!(s.write(&[0]), Ok(()));
-            let _ = rx.recv_opt();
+            let _ = rx.recv();
         }).detach();
 
         let mut s = a.accept().unwrap();
@@ -1345,14 +1348,14 @@ mod test {
         let _t = Thread::spawn(move|| {
             let mut s2 = s2;
             assert_eq!(s2.read(&mut [0]), Ok(1));
-            tx2.send(());
+            tx2.send(()).unwrap();
         });
 
         s.set_read_timeout(Some(20));
         assert_eq!(s.read(&mut [0]).err().unwrap().kind, TimedOut);
-        tx.send(());
+        tx.send(()).unwrap();
 
-        rx2.recv();
+        rx2.recv().unwrap();
     }
 
     #[test]
@@ -1367,9 +1370,9 @@ mod test {
         let txdone2 = txdone.clone();
         let _t = Thread::spawn(move|| {
             let mut tcp = TcpStream::connect(addr).unwrap();
-            rx.recv();
+            rx.recv().unwrap();
             tcp.write_u8(0).unwrap();
-            txdone2.send(());
+            txdone2.send(()).unwrap();
         });
 
         // Spawn off a reading clone
@@ -1379,7 +1382,7 @@ mod test {
         let _t = Thread::spawn(move|| {
             let mut tcp2 = tcp2;
             tcp2.read_u8().unwrap();
-            txdone3.send(());
+            txdone3.send(()).unwrap();
         });
 
         // Try to ensure that the reading clone is indeed reading
@@ -1390,9 +1393,9 @@ mod test {
         // clone the handle again while it's reading, then let it finish the
         // read.
         let _ = tcp.clone();
-        tx.send(());
-        rxdone.recv();
-        rxdone.recv();
+        tx.send(()).unwrap();
+        rxdone.recv().unwrap();
+        rxdone.recv().unwrap();
     }
 
     #[test]
@@ -1423,8 +1426,14 @@ mod test {
         let (tx, rx) = channel();
         let tx2 = tx.clone();
 
-        let _t = Thread::spawn(move|| { let mut a = a; tx.send(a.accept()) });
-        let _t = Thread::spawn(move|| { let mut a = a2; tx2.send(a.accept()) });
+        let _t = Thread::spawn(move|| {
+            let mut a = a;
+            tx.send(a.accept()).unwrap();
+        });
+        let _t = Thread::spawn(move|| {
+            let mut a = a2;
+            tx2.send(a.accept()).unwrap();
+        });
 
         let _t = Thread::spawn(move|| {
             let _ = TcpStream::connect(addr);
@@ -1433,8 +1442,8 @@ mod test {
             let _ = TcpStream::connect(addr);
         });
 
-        assert!(rx.recv().is_ok());
-        assert!(rx.recv().is_ok());
+        assert!(rx.recv().unwrap().is_ok());
+        assert!(rx.recv().unwrap().is_ok());
     }
 
     #[test]
@@ -1457,10 +1466,10 @@ mod test {
         let (tx, rx) = channel();
         let _t = Thread::spawn(move|| {
             let mut a = a;
-            tx.send(a.accept());
+            tx.send(a.accept()).unwrap();
         });
         a2.close_accept().unwrap();
 
-        assert_eq!(rx.recv().err().unwrap().kind, EndOfFile);
+        assert_eq!(rx.recv().unwrap().err().unwrap().kind, EndOfFile);
     }
 }