title: Idiomatic way to handle writes to a TcpStream while waiting on read
body: <p>As a way to familiarize myself with Rust &amp; networking in general I started writing a very basic telnet chat server.  Everything appears to be going well, but right now I end up with blocks of unsafe code, and I wanted to know if there was a better way to do things.</p>
  
  <p>I spawn a task to listen for connections similar to this: <a href="https://stackoverflow.com/questions/17445485/example-tcp-server-written-in-rust">Example TCP server written in Rust</a>  Once a user connects and I get a TcpStream I put it in a Connection struct.  A Connection struct uses channels to communicate with two tasks- one for reading from its TcpStream, and one for writing.  The reading task blocks on the TcpStream's read() method and sends any input back to the Connection struct.  The writer blocks on a Port's recv() method and writes anything it receives to the TcpStream.  In this way, the main loop of the program can simply maintain a vector of Connection structs to check for user input and write to them at will.  The issue is that with this implementation the TcpStream must be be shared by both the read &amp; write tasks, and the mutable write() method called whilst the mutable read() method is still blocking in the other task.  In 0.8 I did this with an Rc and unsafe_borrow_mut's, but I'd like to do it in a better fashion if I can- the objections of Rust's type system in this case may be completely valid, for all I know. Any comments on overall design would be welcome as well.  Thanks.</p>