pub trait DependencyKey:
Send
+ Sync
+ 'static {
type Value: Send + Sync + 'static;
// Required method
fn live() -> Self::Value;
// Provided methods
fn try_test() -> Result<Self::Value, DependencyError> { ... }
fn test() -> Self::Value { ... }
fn preview() -> Self::Value { ... }
fn register(values: &DependencyValues, value: Self::Value) { ... }
}Expand description
Register a dependency value under its key type (UDF DependencyKey).
Value must be Send + Sync + 'static so it can live in the shared dependency
bag and be accessed from async effects on any runtime thread, e.g.:
use std::sync::Arc;
use rust_dependencies::{DependencyKey, DependencyValues};
struct ConfigKey;
#[derive(Clone)]
struct Config {
api_base: String,
}
impl DependencyKey for ConfigKey {
type Value = Config;
fn live() -> Self::Value {
Config { api_base: "https://api.example.com".into() }
}
}
let bag = DependencyValues::live().with(ConfigKey::live());Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn try_test() -> Result<Self::Value, DependencyError>
fn try_test() -> Result<Self::Value, DependencyError>
Fall back when no test double is registered for this key.
fn test() -> Self::Value
fn preview() -> Self::Value
fn register(values: &DependencyValues, value: Self::Value)
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".