View

Struct View 

Source
pub struct View<T> { /* private fields */ }
Expand description

Extracts a view to a sub-element of the global state indicated by the path.

The type of the sub-element is given by the type parameter T.

The View extractor expects that the location pointed by the Job path exists and is deserializable into T. If the value may not exist (or is null), then make sure to use View<Option<T>>.

§Example

use mahler::{
    extract::View,
    task::{Handler, create, update, with_io, IO},
    worker::{Worker, Ready}
};
use serde::{Serialize, Deserialize};
#[derive(Serialize,Deserialize)]
struct SystemState {/* ... */};

fn foo_bar(mut view: View<i32>) -> IO<i32> {
    // view can be dereferenced into the given type
    // and is guaranteed to exist at this point
    if *view < 5 {
        *view += 1;
    }

    with_io(view, |view| async {
        // do something with view at runtime
        Ok(view)
    })
}

fn create_counter(mut view: View<Option<i32>>) -> IO<Option<i32>> {
    if view.is_none() {
        // Initialize with default value if it doesn't exist
        *view = Some(0);
    }

    with_io(view, |view| async {
        // do something with view at runtime
        Ok(view)
    })
}

let worker: Worker<SystemState, Ready> = Worker::new()
    .job("/{foo}/{bar}", create(create_counter))
    .job("/{foo}/{bar}", update(foo_bar))
    .initial_state(SystemState {/* ... */})
    .unwrap();

§Errors

Initializing the extractor will fail if the path assigned to the job cannot be resolved or the value pointed by the path cannot be deserialized into type <T>

Trait Implementations§

Source§

impl<T: Debug> Debug for View<T>

Source§

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

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

impl<T> Deref for View<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T> DerefMut for View<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T, E> From<View<T>> for IO<T, E>

Convert a View<T> directly into an IO operation.

This creates an IO operation that immediately succeeds with the given view, without performing any actual I/O. This is useful for bailing out early in jobs before creating the effectful computation.

§Examples

fn plus_one(mut view: View<u32>, Target(tgt): Target<u32>) -> IO<u32> {
    if *view >= tgt {
        // exit early if we already reached the target
        return view.into();
    }

    with_io(view, |view| async {
        // do some async work
        Ok(view)
    })
    // increase the target after the IO operation
    // terminates (at runtime)
    .map(|mut counter| {
        *counter = *counter + 1;
        counter
    })
}
Source§

fn from(view: View<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: DeserializeOwned> FromSystem for View<T>

Source§

type Error = ExtractionError

Source§

fn from_system(system: &System, context: &Context) -> Result<Self, Self::Error>

Try to initialize an extractor from the system state and context
Source§

fn is_scoped() -> bool

Return true if the extractor is scoped Read more

Auto Trait Implementations§

§

impl<T> Freeze for View<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for View<T>
where T: RefUnwindSafe,

§

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

§

impl<T> Sync for View<T>
where T: Sync,

§

impl<T> Unpin for View<T>
where T: Unpin,

§

impl<T> UnwindSafe for View<T>
where T: 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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