pub struct DetachedRequest(_); It reads like the Request it came from, moves to any thread, and answers 500 when dropped
unreplied. Its replies hand back a Deferred while a file or path part still sends, and
waiting on one belongs off the polling thread, as that thread is the one sending it.
use std::sync::mpsc;
use ucall::*;
let (handed, received) = mpsc::channel::<DetachedRequest>();
let mut server = Server::new(&[Interface::new("127.0.0.1:0")])?;
server.get("/clip", move |request: Request| handed.send(request.detach()).unwrap())?;
let address = server.address(0).unwrap().to_owned();
// The worker outlives the scope, as the sender it waits on lives in the server's handler.
std::thread::spawn(move || {
for detached in received {
let parts = [ReplyPart::Bytes(b"frame".into())];
if let Ok(Some(deferred)) = detached.reply_parts(HTTPStatus::OK, &[], &parts) {
let _ = deferred.wait(); // or: deferred.await
}
}
});
let mut client = Client::new(ClientOptions { connections: 1, ..Default::default() })?;
std::thread::scope(|scope| {
let polling = scope.spawn(|| server.take_calls(0));
let response = client.get(format!("http://{address}/clip"));
server.stop();
polling.join().unwrap()?;
assert_eq!(response?.body(), b"frame");
Ok::<(), Error>(())
})?;
Implements
Deref