summaryrefslogtreecommitdiffstats
path: root/examples/proxy.rs
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2018-01-31 20:09:15 -0800
committerGitHub <noreply@github.com>2018-01-31 20:09:15 -0800
commit65cbfced2950ac228514c176ad5c7b83da80693f (patch)
treeefff57b00bb67b5e0bce5681a0d9f274a487173b /examples/proxy.rs
parenta6162200905494745895bc8c1ba63d42cd7648af (diff)
Poll evented mutability (#37)
Generally speaking, it is unsafe to access to perform asynchronous operations using `&self`. Taking `&self` allows usage from a `Sync` context, which has unexpected results. Taking `&mut self` to perform these operations prevents using these asynchronous values from across tasks (unless they are wrapped in `RefCell` or `Mutex`.
Diffstat (limited to 'examples/proxy.rs')
-rw-r--r--examples/proxy.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/examples/proxy.rs b/examples/proxy.rs
index 98b86e9f..51735ba1 100644
--- a/examples/proxy.rs
+++ b/examples/proxy.rs
@@ -21,7 +21,7 @@ extern crate futures_cpupool;
extern crate tokio;
extern crate tokio_io;
-use std::sync::Arc;
+use std::sync::{Arc, Mutex};
use std::env;
use std::net::{Shutdown, SocketAddr};
use std::io::{self, Read, Write};
@@ -60,9 +60,9 @@ fn main() {
//
// As a result, we wrap up our client/server manually in arcs and
// use the impls below on our custom `MyTcpStream` type.
- let client_reader = MyTcpStream(Arc::new(client));
+ let client_reader = MyTcpStream(Arc::new(Mutex::new(client)));
let client_writer = client_reader.clone();
- let server_reader = MyTcpStream(Arc::new(server));
+ let server_reader = MyTcpStream(Arc::new(Mutex::new(server)));
let server_writer = server_reader.clone();
// Copy the data (in parallel) between the client and the server.
@@ -99,17 +99,17 @@ fn main() {
// `AsyncWrite::shutdown` method which actually calls `TcpStream::shutdown` to
// notify the remote end that we're done writing.
#[derive(Clone)]
-struct MyTcpStream(Arc<TcpStream>);
+struct MyTcpStream(Arc<Mutex<TcpStream>>);
impl Read for MyTcpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
- (&*self.0).read(buf)
+ self.0.lock().unwrap().read(buf)
}
}
impl Write for MyTcpStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
- (&*self.0).write(buf)
+ self.0.lock().unwrap().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
@@ -121,7 +121,7 @@ impl AsyncRead for MyTcpStream {}
impl AsyncWrite for MyTcpStream {
fn shutdown(&mut self) -> Poll<(), io::Error> {
- try!(self.0.shutdown(Shutdown::Write));
+ try!(self.0.lock().unwrap().shutdown(Shutdown::Write));
Ok(().into())
}
}