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>
impl<'data, L: Lt> Query<'data, L>
Sourcepub 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>,
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:
- The value of type
R
returned by the given function. Some(_)
if the query was fulfilled, otherwiseNone
Sourcepub fn capture_tag_ids<R>(
block: impl FnOnce(&mut Query<'data, L>) -> R,
on_provide_attempt: impl FnMut(TagId) + 'data,
) -> R
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
.
Sourcepub fn is_fulfilled(&self) -> bool
pub fn is_fulfilled(&self) -> bool
Returns true
if the query has been fulfilled and no values will be accepted in the future.
Sourcepub fn expects<Tag: TagFor<L>>(&self) -> bool
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.
Sourcepub fn expected_tag_id(&self) -> TagId
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.
Sourcepub fn put<Tag: TagFor<L>>(&mut self, value: Tag::Value) -> &mut Self
pub fn put<Tag: TagFor<L>>(&mut self, value: Tag::Value) -> &mut Self
Attempts to fulfill the query with a value marked with Tag
.
Sourcepub fn put_with<Tag: TagFor<L>>(
&mut self,
f: impl FnOnce(Tag::ArgValue) -> Tag::Value,
) -> &mut Self
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
.
Sourcepub fn put_where<Tag: TagFor<L>>(
&mut self,
predicate: impl FnOnce(&mut Tag::ArgValue) -> bool,
f: impl FnOnce(Tag::ArgValue) -> Tag::Value,
) -> &mut Self
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
.
Sourcepub fn try_put<Tag: TagFor<L>>(
&mut self,
f: impl FnOnce(Tag::ArgValue) -> Result<Tag::Value, Tag::ArgValue>,
) -> &mut Self
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.
Sourcepub fn put_value<T: 'static>(&mut self, value: T) -> &mut Self
pub fn put_value<T: 'static>(&mut self, value: T) -> &mut Self
Attempts to fulfill the query with a T
marked with Value<T>
.
Sourcepub fn put_value_with<T: 'static>(
&mut self,
value: impl FnOnce() -> T,
) -> &mut Self
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>
.
Sourcepub fn using<C>(&mut self, context: C) -> QueryUsing<'_, C, L>
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>>
impl<'x, L: Lt> Query<'_, LtList<'x, L>>
Sourcepub fn put_ref<T: ?Sized + 'static>(&mut self, value: &'x T) -> &mut Self
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>>
.
Sourcepub fn put_mut<T: ?Sized + 'static>(&mut self, value: &'x mut T) -> &mut Self
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>>
.