diff options
| author | ihc童鞋@提不起劲 <[email protected]> | 2022-05-31 00:05:44 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-31 00:05:44 +0800 |
| commit | 52618d411e18e2e515e4a4c8d43b607fbaebd1d3 (patch) | |
| tree | 2d31d2e36880356ebe285de8135e60f5f5ae9606 /examples | |
| parent | 064dfcf95db18117e6d2634a8f7d8e1a383125c2 (diff) | |
make AsyncReadRent and AsyncWriteRent take &mut self instead of &self (#74)
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/echo.rs | 2 | ||||
| -rw-r--r-- | examples/proxy.rs | 17 | ||||
| -rw-r--r-- | examples/tcp_legacy.rs | 4 | ||||
| -rw-r--r-- | examples/tcp_uring.rs | 4 | ||||
| -rw-r--r-- | examples/uds.rs | 4 |
5 files changed, 18 insertions, 13 deletions
diff --git a/examples/echo.rs b/examples/echo.rs index 377a97d..b9a738a 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -26,7 +26,7 @@ async fn main() { } } -async fn echo(stream: TcpStream) -> std::io::Result<()> { +async fn echo(mut stream: TcpStream) -> std::io::Result<()> { let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024); loop { // read diff --git a/examples/proxy.rs b/examples/proxy.rs index 69e967b..9a9b5d5 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -1,7 +1,7 @@ //! An example TCP proxy. use monoio::{ - io::{AsyncReadRent, AsyncWriteRentExt}, + io::{AsyncReadRent, AsyncWriteRent, AsyncWriteRentExt}, net::{TcpListener, TcpStream}, }; @@ -13,13 +13,15 @@ async fn main() { let listener = TcpListener::bind(LISTEN_ADDRESS) .unwrap_or_else(|_| panic!("[Server] Unable to bind to {}", LISTEN_ADDRESS)); loop { - if let Ok((in_conn, _addr)) = listener.accept().await { + if let Ok((mut in_conn, _addr)) = listener.accept().await { let out_conn = TcpStream::connect(TARGET_ADDRESS).await; - if let Ok(out_conn) = out_conn { + if let Ok(mut out_conn) = out_conn { monoio::spawn(async move { + let (mut in_r, mut in_w) = in_conn.split(); + let (mut out_r, mut out_w) = out_conn.split(); let _ = monoio::join!( - copy_one_direction(&in_conn, &out_conn), - copy_one_direction(&out_conn, &in_conn), + copy_one_direction(&mut in_r, &mut out_w), + copy_one_direction(&mut out_r, &mut in_w), ); println!("relay finished"); }); @@ -33,7 +35,10 @@ async fn main() { } } -async fn copy_one_direction(from: &TcpStream, to: &TcpStream) -> Result<Vec<u8>, std::io::Error> { +async fn copy_one_direction<FROM: AsyncReadRent, TO: AsyncWriteRent>( + mut from: FROM, + to: &mut TO, +) -> Result<Vec<u8>, std::io::Error> { let mut buf = Vec::with_capacity(8 * 1024); loop { // read diff --git a/examples/tcp_legacy.rs b/examples/tcp_legacy.rs index 9be39b2..25e9c7c 100644 --- a/examples/tcp_legacy.rs +++ b/examples/tcp_legacy.rs @@ -25,7 +25,7 @@ where tx.cancellation().await; println!("[Client] Server is ready, will connect and send data"); - let conn = TcpStream::connect(ADDRESS) + let mut conn = TcpStream::connect(ADDRESS) .await .expect("[Client] Unable to connect to server"); let buf: Vec<u8> = vec![97; 10]; @@ -41,7 +41,7 @@ where println!("[Server] Bind ready"); drop(rx); - let (conn, _addr) = listener + let (mut conn, _addr) = listener .accept() .await .expect("[Server] Unable to accept connection"); diff --git a/examples/tcp_uring.rs b/examples/tcp_uring.rs index 09912ec..5e3625a 100644 --- a/examples/tcp_uring.rs +++ b/examples/tcp_uring.rs @@ -29,7 +29,7 @@ where tx.cancellation().await; println!("[Client] Server is ready, will connect and send data"); - let conn = TcpStream::connect(ADDRESS) + let mut conn = TcpStream::connect(ADDRESS) .await .expect("[Client] Unable to connect to server"); let buf: Vec<u8> = vec![97; 10]; @@ -45,7 +45,7 @@ where println!("[Server] Bind ready"); drop(rx); - let (conn, _addr) = listener + let (mut conn, _addr) = listener .accept() .await .expect("[Server] Unable to accept connection"); diff --git a/examples/uds.rs b/examples/uds.rs index 86bd943..2d43abf 100644 --- a/examples/uds.rs +++ b/examples/uds.rs @@ -14,7 +14,7 @@ async fn main() { monoio::spawn(async move { tx.closed().await; - let client = UnixStream::connect(ADDRESS).await.unwrap(); + let mut client = UnixStream::connect(ADDRESS).await.unwrap(); let buf = "hello"; let (ret, buf) = client.write_all(buf).await; ret.unwrap(); @@ -25,7 +25,7 @@ async fn main() { let listener = UnixListener::bind(ADDRESS).unwrap(); println!("listening on {:?}", ADDRESS); drop(rx); - let (conn, addr) = listener.accept().await.unwrap(); + let (mut conn, addr) = listener.accept().await.unwrap(); println!("accepted a new connection from {:?}", addr); let buf = Vec::with_capacity(1024); let (ret, buf) = conn.read(buf).await; |
