diff options
| author | LinFeng <[email protected]> | 2023-02-23 00:38:33 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-23 00:38:33 +0800 |
| commit | 1c1ecbafbbda3d97acf38c4ac2bc69d8c1b2e365 (patch) | |
| tree | eec7e1bc33c24ba002f800eeaf2fe8820a45330c | |
| parent | 4161fe4ca1709e5c798ba8199974d3ddc70f458d (diff) | |
chore: improve stream.read examples (#143)
| -rw-r--r-- | README-zh.md | 10 | ||||
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | examples/echo.rs | 10 | ||||
| -rw-r--r-- | examples/proxy.rs | 10 |
4 files changed, 17 insertions, 25 deletions
diff --git a/README-zh.md b/README-zh.md index 118d713..ea46ee0 100644 --- a/README-zh.md +++ b/README-zh.md @@ -68,18 +68,16 @@ async fn main() { async fn echo(stream: TcpStream) -> std::io::Result<()> { let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024); + let mut res; loop { // read - let (res, _buf) = stream.read(buf).await; - buf = _buf; - let res: usize = res?; - if res == 0 { + (res, buf) = stream.read(buf).await; + if res? == 0 { return Ok(()); } // write all - let (res, _buf) = stream.write_all(buf).await; - buf = _buf; + (res, buf) = stream.write_all(buf).await; res?; // clear @@ -67,18 +67,16 @@ async fn main() { async fn echo(stream: TcpStream) -> std::io::Result<()> { let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024); + let mut res; loop { // read - let (res, _buf) = stream.read(buf).await; - buf = _buf; - let res: usize = res?; - if res == 0 { + (res, buf) = stream.read(buf).await; + if res? == 0 { return Ok(()); } // write all - let (res, _buf) = stream.write_all(buf).await; - buf = _buf; + (res, buf) = stream.write_all(buf).await; res?; // clear @@ -114,4 +112,4 @@ Monoio is licensed under the MIT license or Apache license. During developing we referenced a lot from Tokio, Mio, Tokio-uring and other related projects. We would like to thank the authors of these projects. -[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbytedance%2Fmonoio?ref=badge_large)
\ No newline at end of file +[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbytedance%2Fmonoio?ref=badge_large) diff --git a/examples/echo.rs b/examples/echo.rs index aa8d87f..7ffb4ec 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -30,18 +30,16 @@ async fn main() { async fn echo(mut stream: TcpStream) -> std::io::Result<()> { let mut buf: Vec<u8> = Vec::with_capacity(8 * 1024); + let mut res; loop { // read - let (res, _buf) = stream.read(buf).await; - buf = _buf; - let res: usize = res?; - if res == 0 { + (res, buf) = stream.read(buf).await; + if res? == 0 { return Ok(()); } // write all - let (res, _buf) = stream.write_all(buf).await; - buf = _buf; + (res, buf) = stream.write_all(buf).await; res?; // clear diff --git a/examples/proxy.rs b/examples/proxy.rs index 7ed8253..8630329 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -40,18 +40,16 @@ async fn copy_one_direction<FROM: AsyncReadRent, TO: AsyncWriteRent>( to: &mut TO, ) -> Result<Vec<u8>, std::io::Error> { let mut buf = Vec::with_capacity(8 * 1024); + let mut res; loop { // read - let (res, _buf) = from.read(buf).await; - buf = _buf; - let res: usize = res?; - if res == 0 { + (res, buf) = from.read(buf).await; + if res? == 0 { return Ok(buf); } // write all - let (res, _buf) = to.write_all(buf).await; - buf = _buf; + (res, buf) = to.write_all(buf).await; res?; // clear |
