Struct Context

Source
pub struct Context<S>(pub S);
Expand description

Extractor for context.

Context is global and used in every request a router with context receives. For accessing data derived from calls, see Extension.

§With Router

use blueprint_sdk::extract::Context;
use blueprint_sdk::{Job, Router};

// The application context
//
// Here you can put configuration, database connection pools, or whatever
// context you need
#[derive(Clone)]
struct AppContext {}

let context = AppContext {};

const MY_JOB_ID: u32 = 0;

// create a `Router` that holds our context
let app = Router::new()
    .route(MY_JOB_ID, handler)
    // provide the context so the router can access it
    .with_context(context);

async fn handler(
    // access the context via the `Context` extractor
    // extracting a context of the wrong type results in a compile error
    Context(context): Context<AppContext>,
) {
    // use `context`...
}

Note that Context is an extractor, so be sure to put it before any body extractors, see “the order of extractors”.

§With Job

use blueprint_sdk::Job;
use blueprint_sdk::extract::Context;

#[derive(Clone)]
struct AppContext {}

let context = AppContext {};

async fn job(Context(context): Context<AppContext>) {
    // use `context`...
}

// provide the context so the job can access it
let job_with_context = job.with_context(context);

§Sub-Contexts

Context only allows a single context type, but you can use FromRef to extract “sub-contexts”:

use blueprint_sdk::Router;
use blueprint_sdk::extract::{Context, FromRef};

// the application context
#[derive(Clone)]
struct AppContext {
    // that holds some api specific context
    api_state: ApiContext,
}

// the api specific context
#[derive(Clone)]
struct ApiContext {}

// support converting an `AppContext` in an `ApiContext`
impl FromRef<AppContext> for ApiContext {
    fn from_ref(app_state: &AppContext) -> ApiContext {
        app_state.api_state.clone()
    }
}

let context = AppContext {
    api_state: ApiContext {},
};

const HANDLER_JOB_ID: u32 = 0;
const FETCH_API_JOB_ID: u32 = 1;

let app = Router::new()
    .route(HANDLER_JOB_ID, handler)
    .route(FETCH_API_JOB_ID, fetch_api)
    .with_context(context);

async fn fetch_api(
    // access the api specific context
    Context(api_state): Context<ApiContext>,
) {
}

async fn handler(
    // we can still access to top level context
    Context(context): Context<AppContext>,
) {
}

For convenience FromRef can also be derived using #[derive(FromRef)].

§For library authors

If you’re writing a library that has an extractor that needs context, this is the recommended way to do it:

use blueprint_sdk::extract::{FromJobCallParts, FromRef};
use blueprint_sdk::job::call::Parts;
use std::convert::Infallible;

// the extractor your library provides
struct MyLibraryExtractor;

impl<S> FromJobCallParts<S> for MyLibraryExtractor
where
    // keep `S` generic but require that it can produce a `MyLibraryContext`
    // this means users will have to implement `FromRef<UserContext> for MyLibraryContext`
    MyLibraryContext: FromRef<S>,
    S: Send + Sync,
{
    type Rejection = Infallible;

    async fn from_job_call_parts(
        parts: &mut Parts,
        context: &S,
    ) -> Result<Self, Self::Rejection> {
        // get a `MyLibraryContext` from a reference to the context
        let context = MyLibraryContext::from_ref(context);

        // ...
    }
}

// the context your library needs
struct MyLibraryContext {
    // ...
}

§Shared mutable context

As context is global within a Router you can’t directly get a mutable reference to the context.

The most basic solution is to use an Arc<Mutex<_>>. Which kind of mutex you need depends on your use case. See the tokio docs for more details.

Note that holding a locked std::sync::Mutex across .await points will result in !Send futures which are incompatible with blueprint_sdk. If you need to hold a mutex across .await points, consider using a tokio::sync::Mutex instead.

§Example

use blueprint_sdk::Router;
use blueprint_sdk::extract::Context;
use std::sync::{Arc, Mutex};

#[derive(Clone)]
struct AppContext {
    data: Arc<Mutex<String>>,
}

const MY_JOB_ID: u8 = 0;

async fn job(Context(context): Context<AppContext>) {
    {
        let mut data = context.data.lock().expect("mutex was poisoned");
        *data = "updated foo".to_owned();
    }

    // ...
}

let context = AppContext {
    data: Arc::new(Mutex::new("foo".to_owned())),
};

let app = Router::new().route(MY_JOB_ID, job).with_context(context);

Tuple Fields§

§0: S

Trait Implementations§

Source§

impl<S: Clone> Clone for Context<S>

Source§

fn clone(&self) -> Context<S>

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<S: Debug> Debug for Context<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Default> Default for Context<S>

Source§

fn default() -> Context<S>

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

impl<S> Deref for Context<S>

Source§

type Target = S

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S> DerefMut for Context<S>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<OuterContext, InnerContext> FromJobCallParts<OuterContext> for Context<InnerContext>
where InnerContext: FromRef<OuterContext>, OuterContext: Send + Sync,

Source§

type Rejection = Infallible

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a job result.
Source§

async fn from_job_call_parts( _parts: &mut Parts, ctx: &OuterContext, ) -> Result<Self, Self::Rejection>

Perform the extraction.
Source§

impl<S: Copy> Copy for Context<S>

Auto Trait Implementations§

§

impl<S> Freeze for Context<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Context<S>
where S: RefUnwindSafe,

§

impl<S> Send for Context<S>
where S: Send,

§

impl<S> Sync for Context<S>
where S: Sync,

§

impl<S> Unpin for Context<S>
where S: Unpin,

§

impl<S> UnwindSafe for Context<S>
where S: UnwindSafe,

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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<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