pub struct Extensions { /* private fields */ }Expand description
A type-safe, thread-safe container for storing arbitrary types.
This structure allows storing multiple values of different types in a single container, with type-safe retrieval. Values are stored wrapped in Arc for efficient cloning.
Extensions are used at three levels in Zel RPC:
- Server Extensions: Shared across all connections (e.g., database pools)
- Connection Extensions: Scoped to a single connection (e.g., authentication sessions)
- Request Extensions: Unique per request (e.g., trace IDs)
Implementations§
Source§impl Extensions
impl Extensions
Sourcepub fn with<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn with<T: Send + Sync + 'static>(self, value: T) -> Self
Add a value to the extensions, returning a new Extensions instance.
This method creates a new Extensions instance with the added value, leaving the original unchanged. This is useful for building up extensions in a builder-style pattern.
Values are stored by their TypeId, so each type can only be stored once. Adding a value of a type that already exists will replace the previous value.
CRITICAL: This method correctly clones the HashMap contents, not just the Arc pointer. Using (*self.map).clone() ensures we get a new HashMap with cloned Arc pointers to the values, rather than just cloning the outer Arc.
Sourcepub fn get<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn get<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Retrieve a value from the extensions by type.
Returns None if no value of the specified type exists.
Trait Implementations§
Source§impl Clone for Extensions
impl Clone for Extensions
Source§fn clone(&self) -> Extensions
fn clone(&self) -> Extensions
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more