Skip to main content

DependencyKey

Trait DependencyKey 

Source
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§

Source

type Value: Send + Sync + 'static

Required Methods§

Source

fn live() -> Self::Value

Provided Methods§

Source

fn try_test() -> Result<Self::Value, DependencyError>

Fall back when no test double is registered for this key.

Source

fn test() -> Self::Value

Source

fn preview() -> Self::Value

Source

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".

Implementors§