pub struct Next { /* private fields */ }Expand description
Represents the next middleware or handler in the chain.
Next is used to pass control to the next step in the middleware pipeline.
When a middleware calls next.run(), it invokes the next middleware or,
if there are no more middleware, the final handler.
§Examples
use wsforge::prelude::*;
use async_trait::async_trait;
struct MyMiddleware;
#[async_trait]
impl Middleware for MyMiddleware {
async fn handle(
&self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
mut next: Next,
) -> Result<Option<Message>> {
println!("Before next");
// Call the next middleware/handler
let response = next.run(message, conn, state, extensions).await?;
println!("After next");
Ok(response)
}
}Implementations§
Source§impl Next
impl Next
Sourcepub fn new(chain: Arc<MiddlewareChain>, index: usize) -> Self
pub fn new(chain: Arc<MiddlewareChain>, index: usize) -> Self
Creates a new Next instance.
§Arguments
chain- The middleware chain to executeindex- Current position in the chain
Sourcepub async fn run(
self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
) -> Result<Option<Message>>
pub async fn run( self, message: Message, conn: Connection, state: AppState, extensions: Extensions, ) -> Result<Option<Message>>
Call the next middleware in the chain.
This method executes the next middleware in the sequence. If all middleware have been executed, it calls the final handler.
§Arguments
message- The WebSocket message being processedconn- The connection that sent the messagestate- Application stateextensions- Request-scoped extension data
§Returns
Returns the response from the next middleware or handler, or None if
no response should be sent.
§Examples
use wsforge::prelude::*;
use async_trait::async_trait;
struct TimingMiddleware;
#[async_trait]
impl Middleware for TimingMiddleware {
async fn handle(
&self,
message: Message,
conn: Connection,
state: AppState,
extensions: Extensions,
mut next: Next,
) -> Result<Option<Message>> {
let start = std::time::Instant::now();
let response = next.run(message, conn, state, extensions).await?;
let duration = start.elapsed();
println!("Request took: {:?}", duration);
Ok(response)
}
}Auto Trait Implementations§
impl Freeze for Next
impl !RefUnwindSafe for Next
impl Send for Next
impl Sync for Next
impl Unpin for Next
impl UnsafeUnpin for Next
impl !UnwindSafe for Next
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more