1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-02 05:04:29 +02:00

Make chat-client reads cancellation safe (#713)

* Make chat-client reads cancellation safe

* Update chat-app references
This commit is contained in:
Mauve
2023-06-09 10:35:52 -04:00
committed by GitHub
parent 8c212b3f8c
commit 4b7b5d83ea
2 changed files with 6 additions and 7 deletions

View File

@ -25,12 +25,11 @@ async fn main() -> Result<(), tokio_websockets::Error> {
.await?;
let stdin = tokio::io::stdin();
let mut stdin = BufReader::new(stdin);
let mut stdin = BufReader::new(stdin).lines();
// ANCHOR_END: setup
// Continuous loop for concurrently sending and receiving messages.
loop {
let mut line = String::new();
tokio::select! {
incoming = ws_stream.next() => {
match incoming {
@ -39,10 +38,10 @@ async fn main() -> Result<(), tokio_websockets::Error> {
None => return Ok(()),
}
}
res = stdin.read_line(&mut line) => {
res = stdin.next_line() => {
match res {
Ok(0) => return Ok(()),
Ok(_) => ws_stream.send(Message::text(line.trim_end().to_string())).await?,
Ok(None) => return Ok(()),
Ok(Some(line)) => ws_stream.send(Message::text(line.to_string())).await?,
Err(err) => return Err(err.into()),
}
}