IntoResponse

Trait IntoResponse 

Source
pub trait IntoResponse: Send {
    // Required method
    fn into_response<'async_trait>(
        self,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
       where Self: 'async_trait;
}
Expand description

Trait for converting handler return values into WebSocket messages.

This trait allows handlers to return various types that are automatically converted to messages or no response. The framework handles the conversion transparently.

§Automatic Implementations

The trait is implemented for common return types:

  • () - No response is sent
  • String - Sent as text message
  • &str - Sent as text message
  • Message - Sent as-is
  • Vec<u8> - Sent as binary message
  • Result<T> - Automatically handles errors

§Examples

§Implementing for Custom Types

use wsforge::prelude::*;
use async_trait::async_trait;

struct CustomResponse {
    code: u32,
    data: String,
}

#[async_trait]
impl IntoResponse for CustomResponse {
    async fn into_response(self) -> Result<Option<Message>> {
        let text = format!("{}:{}", self.code, self.data);
        Ok(Some(Message::text(text)))
    }
}

async fn handler() -> Result<CustomResponse> {
    Ok(CustomResponse {
        code: 200,
        data: "Success".to_string(),
    })
}

Required Methods§

Source

fn into_response<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,

Converts this value into an optional message.

§Returns
  • Ok(Some(message)) - A message to send back to the client
  • Ok(None) - No response should be sent
  • Err(error) - An error occurred during conversion
§Examples
use wsforge::prelude::*;

let response = "Hello".to_string();
let message = response.into_response().await?;
assert!(message.is_some());

Implementations on Foreign Types§

Source§

impl IntoResponse for &str

Response that sends a string slice as a text message.

§Examples

use wsforge::prelude::*;

async fn static_handler() -> Result<&'static str> {
    Ok("Static response")
}
Source§

fn into_response<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,

Source§

impl IntoResponse for ()

Response that sends nothing back to the client.

Use this when the handler only performs side effects and doesn’t need to send a response.

§Examples

use wsforge::prelude::*;
use std::sync::Arc;

async fn log_handler(
    msg: Message,
    State(manager): State<Arc<ConnectionManager>>,
) -> Result<()> {
    println!("Received message, {} connections active", manager.count());
    Ok(())
}
Source§

fn into_response<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,

Source§

impl IntoResponse for String

Response that sends a string as a text message.

§Examples

use wsforge::prelude::*;

async fn greeting_handler() -> Result<String> {
    Ok(format!("Hello at {}", chrono::Utc::now()))
}
Source§

fn into_response<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,

Source§

impl IntoResponse for Vec<u8>

Response that sends binary data.

§Examples

use wsforge::prelude::*;

async fn binary_handler() -> Result<Vec<u8>> {
    Ok(vec![0x01, 0x02, 0x03, 0x04])
}
Source§

fn into_response<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = Result<Option<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,

Implementors§

Source§

impl IntoResponse for Message

Response that sends the message as-is.

Use this when you have full control over the message construction.

§Examples

use wsforge::prelude::*;

async fn custom_handler() -> Result<Message> {
    Ok(Message::text("Custom response"))
}
Source§

impl<T: Serialize + Send> IntoResponse for JsonResponse<T>

Source§

impl<T: IntoResponse> IntoResponse for Result<T>

Automatic error handling for handler results.

When a handler returns Result<T>, errors are automatically converted to error messages sent back to the client.

§Examples

use wsforge::prelude::*;

async fn validated_handler(msg: Message) -> Result<String> {
    let text = msg.as_text()
        .ok_or_else(|| Error::custom("Message must be text"))?;

    if text.is_empty() {
        return Err(Error::custom("Message cannot be empty"));
    }

    Ok(format!("Processed: {}", text))
}