Extensions

Struct Extensions 

Source
pub struct Extensions { /* private fields */ }
Expand description

Container for request-scoped extension data.

Extensions provide a way to pass arbitrary data through the request pipeline. This is useful for middleware to attach data (like authentication info, request IDs) that handlers can later extract.

§Thread Safety

Extensions are thread-safe and can be safely shared across tasks.

§Examples

§Adding and Retrieving Data

use wsforge::prelude::*;

let extensions = Extensions::new();

// Add data
extensions.insert("request_id", "req_123");
extensions.insert("user_id", 42_u64);

// Retrieve data
if let Some(request_id) = extensions.get::<&str>("request_id") {
    println!("Request ID: {}", request_id);
}

if let Some(user_id) = extensions.get::<u64>("user_id") {
    println!("User ID: {}", user_id);
}

§Use in Middleware

use wsforge::prelude::*;

async fn auth_middleware(
    msg: Message,
    conn: Connection,
    extensions: &Extensions,
) -> Result<()> {
    // Extract and validate auth token
    let token = extract_token(&msg)?;
    let user_id = validate_token(&token)?;

    // Store for handler to use
    extensions.insert("user_id", user_id);

    Ok(())
}

Implementations§

Source§

impl Extensions

Source

pub fn new() -> Self

Creates a new empty Extensions container.

§Examples
use wsforge::prelude::*;

let extensions = Extensions::new();
Source

pub fn insert<T: Send + Sync + 'static>(&self, key: impl Into<String>, value: T)

Inserts a value into the extensions.

The value is stored under the given key and can be retrieved later using the same key and type.

§Arguments
  • key - A unique identifier for this value
  • value - The value to store (must be Send + Sync + 'static)
§Examples
use wsforge::prelude::*;

let extensions = Extensions::new();

// Store different types
extensions.insert("count", 42_u32);
extensions.insert("name", "Alice".to_string());
extensions.insert("active", true);
Source

pub fn get<T: Send + Sync + 'static>(&self, key: &str) -> Option<Arc<T>>

Retrieves a value from the extensions.

Returns None if the key doesn’t exist or if the stored type doesn’t match the requested type.

§Type Safety

The returned value must match the type that was originally inserted. Attempting to retrieve with a different type will return None.

§Examples
use wsforge::prelude::*;

let extensions = Extensions::new();
extensions.insert("count", 42_u32);

// Correct type - succeeds
let count: Option<Arc<u32>> = extensions.get("count");
assert_eq!(*count.unwrap(), 42);

// Wrong type - returns None
let wrong: Option<Arc<String>> = extensions.get("count");
assert!(wrong.is_none());

Trait Implementations§

Source§

impl Clone for Extensions

Source§

fn clone(&self) -> Extensions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Extensions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more