Skip to content

Commit

Permalink
Miscellaneous fixups
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Reidel <adrian@travitia.xyz>
  • Loading branch information
Gelbpunkt committed Jul 8, 2023
1 parent 416304e commit c19f506
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ This is a simple websocket echo server without any proper error handling.
More examples can be found in the [examples folder](https://github.com/Gelbpunkt/tokio-websockets/tree/main/examples).

```rust
use futures_util::SinkExt;
use futures_util::{SinkExt, StreamExt};
use http::Uri;
use tokio::net::TcpListener;
use tokio_websockets::{ClientBuilder, Error, Message, ServerBuilder};
Expand Down Expand Up @@ -91,7 +91,7 @@ async fn main() -> Result<(), Error> {
});

let uri = Uri::from_static("ws://127.0.0.1:3000");
let mut client = ClientBuilder::from_uri(uri).connect().await?;
let (mut client, _) = ClientBuilder::from_uri(uri).connect().await?;

client.send(Message::text(String::from("Hello world!"))).await?;

Expand Down
8 changes: 2 additions & 6 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,7 @@ impl StreamState {

/// A websocket stream that full messages can be read from and written to.
///
/// The stream implements [`futures_util::Sink`], but due to language
/// limitations it currently does not implement [`futures_util::Stream`]. A
/// [`WebsocketStream::next`] method serves as a replacement.
/// The stream implements [`futures_util::Sink`] and [`futures_util::Stream`].
///
/// You must use a [`ClientBuilder`] or [`ServerBuilder`] to
/// obtain a websocket stream.
Expand Down Expand Up @@ -756,9 +754,7 @@ where
}

// Encode it into the buffer
if let Err(e) = self.as_mut().start_send(message) {
return Err(e);
}
self.as_mut().start_send(message)?;

// Attempt to write other pending messages
self.as_mut().try_write_pending(cx)?;
Expand Down
8 changes: 2 additions & 6 deletions tests/cancellation_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl AsyncWrite for SuperSlow {
}
}

const TO_SEND: &'static [u8] = &[1, 2, 3, 4];
const TO_SEND: &[u8] = &[1, 2, 3, 4];

#[tokio::test]
async fn test_cancellation_safety() {
Expand All @@ -83,11 +83,7 @@ async fn test_cancellation_safety() {
loop {
// Cancellable futures should be possible to re-create at any time and resume as
// if they were created once and then polled a few times
let recv_fut = server.next();
tokio::pin!(recv_fut);

// Poll the future once
if let Poll::Ready(val) = recv_fut.poll_unpin(&mut cx) {
if let Poll::Ready(val) = server.next().poll_unpin(&mut cx) {
let msg = val.expect("eof").expect("err");
assert_eq!(msg.as_data(), TO_SEND);
break;
Expand Down

0 comments on commit c19f506

Please sign in to comment.