#[repr(transparent)]pub struct Demand<'a>(_);
🔬This is a nightly-only experimental API. (
provide_any
#96024)Expand description
用于按类型提供数据的帮助器对象。
数据提供者通过调用此类型的提供方法来提供值。
Implementations§
source§impl<'a> Demand<'a>
impl<'a> Demand<'a>
sourcepub fn provide_value<T>(&mut self, value: T) -> &mut Demand<'a>where
T: 'static,
🔬This is a nightly-only experimental API. (provide_any
#96024)
pub fn provide_value<T>(&mut self, value: T) -> &mut Demand<'a>where T: 'static,
provide_any
#96024)sourcepub fn provide_value_with<T>(
&mut self,
fulfil: impl FnOnce() -> T
) -> &mut Demand<'a>where
T: 'static,
🔬This is a nightly-only experimental API. (provide_any
#96024)
pub fn provide_value_with<T>( &mut self, fulfil: impl FnOnce() -> T ) -> &mut Demand<'a>where T: 'static,
provide_any
#96024)sourcepub fn provide_ref<T>(&mut self, value: &'a T) -> &mut Demand<'a>where
T: 'static + ?Sized,
🔬This is a nightly-only experimental API. (provide_any
#96024)
pub fn provide_ref<T>(&mut self, value: &'a T) -> &mut Demand<'a>where T: 'static + ?Sized,
provide_any
#96024)sourcepub fn provide_ref_with<T>(
&mut self,
fulfil: impl FnOnce() -> &'a T
) -> &mut Demand<'a>where
T: 'static + ?Sized,
🔬This is a nightly-only experimental API. (provide_any
#96024)
pub fn provide_ref_with<T>( &mut self, fulfil: impl FnOnce() -> &'a T ) -> &mut Demand<'a>where T: 'static + ?Sized,
provide_any
#96024)提供使用闭包计算的引用。
裁判类型必须以 'static
为界,但可以是未定义大小的。
Examples
以 &str
的形式提供对字段的引用。
#![feature(provide_any)]
use std::any::{Provider, Demand};
impl Provider for SomeConcreteType {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
demand.provide_ref_with::<str>(|| {
if today_is_a_weekday() {
&self.business
} else {
&self.party
}
});
}
}
Runsourcepub fn would_be_satisfied_by_value_of<T>(&self) -> boolwhere
T: 'static,
🔬This is a nightly-only experimental API. (provide_any
#96024)
pub fn would_be_satisfied_by_value_of<T>(&self) -> boolwhere T: 'static,
provide_any
#96024)如果提供指定类型的值,请检查是否满足 Demand
。
如果类型不匹配或已提供,则返回 false。
Examples
检查是否仍需要提供 u8
,然后提供。
#![feature(provide_any)]
use std::any::{Provider, Demand};
struct Parent(Option<u8>);
impl Provider for Parent {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
if let Some(v) = self.0 {
demand.provide_value::<u8>(v);
}
}
}
struct Child {
parent: Parent,
}
impl Child {
// 假装这需要大量资源来评估。
fn an_expensive_computation(&self) -> Option<u8> {
Some(99)
}
}
impl Provider for Child {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
// 一般来说,我们不知道这个调用会不会提供一个 `u8` 值...
self.parent.provide(demand);
// ...所以我们在运行昂贵的计算之前检查是否需要 `u8`。
if demand.would_be_satisfied_by_value_of::<u8>() {
if let Some(v) = self.an_expensive_computation() {
demand.provide_value::<u8>(v);
}
}
// 无论父母提供值还是我们提供值,现在都将满足需求。
assert!(!demand.would_be_satisfied_by_value_of::<u8>());
}
}
let parent = Parent(Some(42));
let child = Child { parent };
assert_eq!(Some(42), std::any::request_value::<u8>(&child));
let parent = Parent(None);
let child = Child { parent };
assert_eq!(Some(99), std::any::request_value::<u8>(&child));
Runsourcepub fn would_be_satisfied_by_ref_of<T>(&self) -> boolwhere
T: 'static + ?Sized,
🔬This is a nightly-only experimental API. (provide_any
#96024)
pub fn would_be_satisfied_by_ref_of<T>(&self) -> boolwhere T: 'static + ?Sized,
provide_any
#96024)如果提供对指定类型值的引用,检查是否满足 Demand
。
如果类型不匹配或已提供,则返回 false。
Examples
检查是否仍需要提供 &str
,然后提供。
#![feature(provide_any)]
use std::any::{Provider, Demand};
struct Parent(Option<String>);
impl Provider for Parent {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
if let Some(v) = &self.0 {
demand.provide_ref::<str>(v);
}
}
}
struct Child {
parent: Parent,
name: String,
}
impl Child {
// 假装这需要大量资源来评估。
fn an_expensive_computation(&self) -> Option<&str> {
Some(&self.name)
}
}
impl Provider for Child {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
// 一般来说,不知道这个调用会不会提供 `str` 引用...
self.parent.provide(demand);
// ...所以我们在运行昂贵的计算之前检查是否需要 `&str`。
if demand.would_be_satisfied_by_ref_of::<str>() {
if let Some(v) = self.an_expensive_computation() {
demand.provide_ref::<str>(v);
}
}
// 无论父母是否提供了引用或我们提供了,现在都将满足需求。
assert!(!demand.would_be_satisfied_by_ref_of::<str>());
}
}
let parent = Parent(Some("parent".into()));
let child = Child { parent, name: "child".into() };
assert_eq!(Some("parent"), std::any::request_ref::<str>(&child));
let parent = Parent(None);
let child = Child { parent, name: "child".into() };
assert_eq!(Some("child"), std::any::request_ref::<str>(&child));
Run