ExtractRef

Trait ExtractRef 

Source
pub trait ExtractRef<T> {
    // Required method
    fn extract_ref(app: &AppBuilder) -> Result<&T, AppError>;

    // Provided method
    fn dependencies() -> Dependencies { ... }
}
Expand description

Trait for extracting borrowed references from the application builder.

This trait allows extracting components or service handles from the application by reference, avoiding the need for cloning. This is more efficient when you only need to read from the component.

§Type Parameters

  • T - The type to extract a reference to from the application.

§Examples

use diode::{AppBuilder, AppError, ExtractRef};

struct ConfigExtractor;

impl ExtractRef<String> for ConfigExtractor {
    fn extract_ref<'a>(app: &'a AppBuilder) -> Result<&'a String, AppError> {
        app.get_component_ref::<String>()
            .ok_or(AppError::MissingDependency)
    }
}

Required Methods§

Source

fn extract_ref(app: &AppBuilder) -> Result<&T, AppError>

Extracts a reference to a value of type T from the application builder.

§Arguments
  • app - Reference to the application builder containing components.
§Returns

Returns Ok(&T) if the component exists and can be referenced, or Err(AppError) if extraction fails.

Provided Methods§

Source

fn dependencies() -> Dependencies

Returns the dependencies required by this type.

The default implementation returns an empty dependencies set, indicating no dependencies are required.

§Returns

A Dependencies object describing required dependencies.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl ExtractRef<AppBuilder> for AppBuilder

Source§

impl<T> ExtractRef<T> for Component
where T: Send + Sync + 'static,

Source§

impl<T, S> ExtractRef<T> for S
where T: Send + Sync + 'static, S: Service<Handle = T> + 'static,