Skip to main content

IClaimsCarrier

Trait IClaimsCarrier 

Source
pub trait IClaimsCarrier: Send {
    // Provided method
    fn set_claims(&mut self, _claims: Option<Box<dyn IClaims>>) { ... }
}
Expand description

Blanket trait that enables claims injection on request structs.

The default implementation is a no-op, so every T: Send satisfies it without any boilerplate. Requests that actually carry claims shadow the trait method with an inherent set_claims(&mut self, …) method; Rust’s method resolution picks the inherent method over the trait default at compile time, with zero runtime cost.

§Why not specialization?

Stable Rust has no specialization. The inherent-method-shadows-trait-default pattern achieves the same “override per-type” behavior without nightly features.

§Usage in contracts

Use the #[claims] attribute macro to automatically inject the claims field and generate the inherent set_claims method:

use rust_webx::*;
use serde::Deserialize;

#[claims]
#[derive(Default, Deserialize)]
pub struct CreateBlogPostRequest {
    pub slug: String,
    // ...
}

The macro expands to (conceptually):

pub struct CreateBlogPostRequest {
    pub slug: String,
    // ...
    #[serde(skip)]
    pub claims: Option<Box<dyn IClaims>>,
}

impl CreateBlogPostRequest {
    pub fn set_claims(&mut self, claims: Option<Box<dyn IClaims>>) {
        self.claims = claims;
    }
}

§Usage in handlers

async fn handle(&mut self, req: CreateBlogPostRequest) -> Result<BlogPostModel> {
    let uid = req.claims.as_ref()
        .and_then(|c| c.subject().parse().ok())
        .ok_or(Error::Unauthorized)?;
    // ...
}

Provided Methods§

Source

fn set_claims(&mut self, _claims: Option<Box<dyn IClaims>>)

Default no-op. Overridden by inherent set_claims on types that carry claims.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> IClaimsCarrier for T
where T: Send,

Blanket no-op implementation — every Send type is a carrier by default.