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
impl Extensions
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty Extensions container.
§Examples
use wsforge::prelude::*;
let extensions = Extensions::new();Sourcepub fn insert<T: Send + Sync + 'static>(&self, key: impl Into<String>, value: T)
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 valuevalue- The value to store (must beSend + 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);Sourcepub fn get<T: Send + Sync + 'static>(&self, key: &str) -> Option<Arc<T>>
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
impl Clone for Extensions
Source§fn clone(&self) -> Extensions
fn clone(&self) -> Extensions
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for Extensions
impl !RefUnwindSafe for Extensions
impl Send for Extensions
impl Sync for Extensions
impl Unpin for Extensions
impl !UnwindSafe for Extensions
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