Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/hyper-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ impl AsyncWrite for SmolStream {

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match &mut *self {
Self::Plain(s) => Pin::new(s).poll_close(cx),
Self::Tls(s) => Pin::new(s).poll_close(cx),
Self::Plain(s) => Pin::new(s).poll_flush(cx),
Self::Tls(s) => Pin::new(s).poll_flush(cx),
}
}
}
21 changes: 20 additions & 1 deletion examples/web-crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,30 @@ use scraper::{Html, Selector};

const ROOT: &str = "https://www.rust-lang.org";

/// A guard that sends an empty string when dropped so the main loop never deadlocks.
struct FetchGuard {
sender: Sender<String>,
sent: bool,
}

impl Drop for FetchGuard {
fn drop(&mut self) {
if !self.sent {
let _ = self.sender.try_send(String::new());
}
}
}

/// Fetches the HTML contents of a web page.
async fn fetch(url: String, sender: Sender<String>) {
let mut guard = FetchGuard {
sender: sender.clone(),
sent: false,
};
let body = surf::get(&url).recv_string().await;
let body = body.unwrap_or_default();
sender.send(body).await.ok();
let _ = sender.send(body).await;
guard.sent = true;
}

/// Extracts links from an HTML body.
Expand Down
8 changes: 5 additions & 3 deletions examples/websocket-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::net::{TcpListener, TcpStream};
use std::pin::Pin;
use std::task::{Context, Poll};

use anyhow::{Context as _, Result};
use anyhow::Result;
use async_native_tls::{Identity, TlsAcceptor, TlsStream};
use async_tungstenite::{tungstenite, WebSocketStream};
use futures::sink::{Sink, SinkExt};
Expand All @@ -25,8 +25,10 @@ use tungstenite::Message;

/// Echoes messages from the client back to it.
async fn echo(mut stream: WsStream) -> Result<()> {
let msg = stream.next().await.context("expected a message")??;
stream.send(Message::text(msg.to_string())).await?;
while let Some(msg) = stream.next().await {
let msg = msg?;
stream.send(Message::text(msg.to_string())).await?;
}
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(1)
.max(1)
};

for n in 1..=num_threads {
Expand Down
Loading