pub trait IoHandler: Send {
// Required method
fn io<'async_trait>(
self: Pin<Box<Self>>,
io: IoStream,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait;
}
Expand description
An upgraded connection I/O handler.
An I/O handler performs raw I/O via the passed in IoStream
, which is
AsyncRead
, AsyncWrite
, and Unpin
.
§Example
The example below implements an EchoHandler
that echos the raw bytes back
to the client.
use std::pin::Pin;
use rocket::tokio::io;
use rocket::data::{IoHandler, IoStream};
struct EchoHandler;
#[rocket::async_trait]
impl IoHandler for EchoHandler {
async fn io(self: Pin<Box<Self>>, io: IoStream) -> io::Result<()> {
let (mut reader, mut writer) = io::split(io);
io::copy(&mut reader, &mut writer).await?;
Ok(())
}
}