forked from berkowski/tokio-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More concrete
pair
documentation and example
This commit adds an example about `pair` and adds some documentation about. This commit was made in response to berkowski#25.
- Loading branch information
Showing
4 changed files
with
51 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
use tokio_serial::Serial; | ||
|
||
#[tokio::main] | ||
async fn main() -> std::io::Result<()> { | ||
let (mut req, mut res) = Serial::pair()?; | ||
|
||
let f = tokio::spawn(async move { | ||
let mut buffer = [0u8; 1]; | ||
res.read_exact(&mut buffer).await?; | ||
|
||
let [command] = buffer; | ||
assert_eq!(command, 1); | ||
|
||
res.write_all(&buffer).await?; | ||
res.shutdown().await?; | ||
|
||
std::io::Result::<_>::Ok(()) | ||
}); | ||
// Write something | ||
req.write_all(&[1u8]).await?; | ||
// Read the answer | ||
let mut buffer = [0u8; 1]; | ||
req.read_exact(&mut buffer).await?; | ||
|
||
let [response] = buffer; | ||
assert_eq!(response, 1); | ||
|
||
// may be a join error, or an IO error from inside the task | ||
f.await??; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters