pub struct State<T>(pub T);Expand description
Extractor that injects a piece of the shared application state into a handler.
State<T> resolves through FromContext whenever T: FromRef<S>, so a handler can take
State<T> for any state component - including types defined in other crates (a broker publisher,
a client pool), which a per-field FromContext impl could not cover under the orphan rule.
Derive FromRef on the state to make every field available.
§Examples
use std::convert::Infallible;
use ruststream::memory::prelude::*;
use serde::Deserialize;
#[derive(Deserialize)]
struct Order {
id: u64,
}
#[derive(Clone)]
struct Pricing {
currency: &'static str,
}
#[derive(Clone)]
struct Region(&'static str);
/// Each field is a component a handler takes with `State<T>`.
#[derive(FromRef)]
struct AppState {
pricing: Pricing,
region: Region,
}
#[subscriber("orders")]
async fn price(order: &Order, State(pricing): State<Pricing>) -> HandlerOutcome {
tracing::info!(order.id, pricing.currency, "priced");
HandlerOutcome::ack()
}
fn app() -> impl App {
RustStream::new(AppInfo::new("pricing", "0.1.0"))
.on_startup(async move |()| {
Ok::<_, Infallible>(AppState {
pricing: Pricing { currency: "EUR" },
region: Region("eu"),
})
})
.with_broker(MemoryBroker::new(), |b| {
b.include(price);
})
}Tuple Fields§
§0: TTrait Implementations§
impl<T> Copy for State<T>where
T: Copy,
Source§impl<C, S, T> FromContext<C, S> for State<T>
impl<C, S, T> FromContext<C, S> for State<T>
Source§type Rejection = !
type Rejection = !
The error returned when extraction fails. It is converted into a
HandlerOutcome (the
reflexive conversion makes HandlerOutcome itself a valid rejection, and Infallible
works for an extractor that never fails) and the delivery is settled by that outcome,
skipping the handler body.Auto Trait Implementations§
impl<T> Freeze for State<T>where
T: Freeze,
impl<T> RefUnwindSafe for State<T>where
T: RefUnwindSafe,
impl<T> Send for State<T>where
T: Send,
impl<T> Sync for State<T>where
T: Sync,
impl<T> Unpin for State<T>where
T: Unpin,
impl<T> UnsafeUnpin for State<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for State<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more