pub trait Provider<T>:
Send
+ Sync
+ Buildable{
// Required method
fn try_get(&self) -> Option<T>;
// Provided methods
fn missing_message(&self) -> String { ... }
fn get(&self) -> T { ... }
fn fallible_get(&self) -> Result<T, ProviderError> { ... }
}
Expand description
The provider trait represents an object that can continuously produce a value. Provider values
can be chained together using the ProviderExt
trait.
For convenience, several types from the default library implement this trait.
for<T> Option<T> : Provider<T>
for<F, T> F : Provider<T> where F : Fn() -> T
Required Methods§
Sourcefn try_get(&self) -> Option<T>
fn try_get(&self) -> Option<T>
Try to get a value from the provider.
Will return Some(v)
if value v
is available, otherwise None
is returned.
§Example
use assemble_core::provider;
let prop = provider!(|| 10);
assert_eq!(prop.try_get(), Some(10));
// options implement provider
let prop = Option::<usize>::None;
assert_eq!(prop.try_get(), None);
Provided Methods§
Sourcefn missing_message(&self) -> String
fn missing_message(&self) -> String
The missing message for this provider
Sourcefn fallible_get(&self) -> Result<T, ProviderError>
fn fallible_get(&self) -> Result<T, ProviderError>
Tries to get a value from this provider, returning an error if not available.
The error’s message is usually specified by the missing_message()
method.
§Example
use assemble_core::lazy_evaluation::Prop;
use assemble_core::provider;
let provider = provider!(|| 3_i32);
assert!(matches!(provider.fallible_get(), Ok(3)));
let mut prop = Prop::<i32>::new(Id::from("test"));
assert!(matches!(prop.fallible_get(), Err(_)));
prop.set_with(provider).unwrap();
assert!(matches!(prop.fallible_get(), Ok(3)));