Trait Configurable

Source
pub trait Configurable {
    type Config: Clone + DeserializeOwned;

    // Required method
    fn build(config: Self::Config) -> Self;

    // Provided method
    fn build_from_path(path: impl AsRef<Path>) -> Result<Self>
       where Self: Sized { ... }
}
Expand description

A trait for objects that can be configured and built from configuration files.

This trait provides a standardized way to create objects from configuration parameters, either directly or from YAML files. It is commonly used for creating policies, environments, and other components of a reinforcement learning system.

§Associated Types

  • Config - The configuration type that can be deserialized from YAML

§Examples

#[derive(Clone, Deserialize)]
struct MyConfig {
    learning_rate: f32,
    hidden_size: usize,
}

struct MyObject {
    config: MyConfig,
}

impl Configurable for MyObject {
    type Config = MyConfig;

    fn build(config: Self::Config) -> Self {
        Self { config }
    }
}

// Build from a YAML file
let obj = MyObject::build_from_path("config.yaml")?;

Required Associated Types§

Source

type Config: Clone + DeserializeOwned

The configuration type for this object.

This type must implement Clone and DeserializeOwned to support building from configuration files.

Required Methods§

Source

fn build(config: Self::Config) -> Self

Builds a new instance of this object from the given configuration.

§Arguments
  • config - The configuration parameters
§Returns

A new instance of the object

Provided Methods§

Source

fn build_from_path(path: impl AsRef<Path>) -> Result<Self>
where Self: Sized,

Builds a new instance from a YAML configuration file.

This is a convenience method that reads a YAML file and builds the object using the deserialized configuration.

§Arguments
  • path - Path to the YAML configuration file
§Returns

A new instance of the object or an error if the file cannot be read or parsed

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§