Struct Query

Source
pub struct Query<'data, L: Lt = ()> { /* private fields */ }
Expand description

A type-erased query ready to pass to Provide::provide().

Providers may use this type to supply tagged values.

Implementations§

Source§

impl<'data, L: Lt> Query<'data, L>

Source

pub fn new_with<Tag, R>( block: impl FnOnce(&mut Query<'data, L>) -> R, arg: Tag::ArgValue, ) -> (R, Option<Tag::Value>)
where Tag: TagFor<L, ArgValue: 'data, Value: 'data>,

Creates a Query expecting a value marked with Tag and passes it to the given function.

Returns a pair of:

  1. The value of type R returned by the given function.
  2. Some(_) if the query was fulfilled, otherwise None
Source

pub fn capture_tag_ids<R>( block: impl FnOnce(&mut Query<'data, L>) -> R, on_provide_attempt: impl FnMut(TagId) + 'data, ) -> R

Creates a Query that does not expect any value, passes it to block, and calls on_provide_attempt() for every available TagId.

Returns the value of type R returned by block.

Source

pub fn is_fulfilled(&self) -> bool

Returns true if the query has been fulfilled and no values will be accepted in the future.

Source

pub fn expects<Tag: TagFor<L>>(&self) -> bool

Returns true if this query would accept a value tagged with Tag.

Note: this will return false if a value tagged with Tag was expected and has been fulfilled, as it will not accept additional values.

Source

pub fn expected_tag_id(&self) -> TagId

Returns the TagId expected by this query.

If this query has already been fulfilled, the returned ID is unspecified.

Source

pub fn put<Tag: TagFor<L>>(&mut self, value: Tag::Value) -> &mut Self

Attempts to fulfill the query with a value marked with Tag.

Source

pub fn put_with<Tag: TagFor<L>>( &mut self, f: impl FnOnce(Tag::ArgValue) -> Tag::Value, ) -> &mut Self

Attempts to fulfill the query with a function returning a value marked with Tag.

The function will not be called if the query does not accept Tag.

Source

pub fn put_where<Tag: TagFor<L>>( &mut self, predicate: impl FnOnce(&mut Tag::ArgValue) -> bool, f: impl FnOnce(Tag::ArgValue) -> Tag::Value, ) -> &mut Self

Behaves similarly to Self::put_with(), except that the query will not be fulfilled if predicate returns false.

The function will not be called if the query does not accept Tag.

Source

pub fn try_put<Tag: TagFor<L>>( &mut self, f: impl FnOnce(Tag::ArgValue) -> Result<Tag::Value, Tag::ArgValue>, ) -> &mut Self

Behaves similary to Self::put_with() when the function returns Ok(_). When the function returns Err(arg), the query will not be fulfilled.

Source

pub fn put_value<T: 'static>(&mut self, value: T) -> &mut Self

Attempts to fulfill the query with a T marked with Value<T>.

Source

pub fn put_value_with<T: 'static>( &mut self, value: impl FnOnce() -> T, ) -> &mut Self

Attempts to fulfill the query with a function returning a T marked with Value<T>.

Source

pub fn using<C>(&mut self, context: C) -> QueryUsing<'_, C, L>

Packs a context value of type C along with this query.

The context will be consumed only when fulfilling the query. If the query is not fulfilled, the context will be returned by QueryUsing::finish()

§Example
use dynamic_provider::{Lt, Provide, Query};

#[derive(Debug)]
struct MyProvider {
    x: i32,
    y: String,
    z: Vec<u8>,
}

impl<'x> Provide<Lt!['x]> for MyProvider {
    fn provide(self, query: &mut Query<'_, Lt!['x]>) -> Option<Self> {
        query
            .using(self)
            .put_value(|this| this.x)
            .put_value(|this| this.y)
            .put_value(|this| this.z)
            .finish()
    }
}

let my_provider = MyProvider {
    x: 0,
    y: "Foo".into(),
    z: vec![1, 2, 3],
};

assert_eq!(my_provider.request_value::<String>().unwrap(), "Foo");
Source§

impl<'x, L: Lt> Query<'_, LtList<'x, L>>

Source

pub fn put_ref<T: ?Sized + 'static>(&mut self, value: &'x T) -> &mut Self

Attempts to fulfill the query with a &'x T marked with Ref<Value<T>>.

Source

pub fn put_mut<T: ?Sized + 'static>(&mut self, value: &'x mut T) -> &mut Self

Attempts to fulfill the query with a &'x mut T marked with Mut<Value<T>>.

Auto Trait Implementations§

§

impl<'data, L = ()> !Freeze for Query<'data, L>

§

impl<'data, L = ()> !RefUnwindSafe for Query<'data, L>

§

impl<'data, L = ()> !Send for Query<'data, L>

§

impl<'data, L = ()> !Sized for Query<'data, L>

§

impl<'data, L = ()> !Sync for Query<'data, L>

§

impl<'data, L = ()> !Unpin for Query<'data, L>

§

impl<'data, L = ()> !UnwindSafe for Query<'data, L>

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