Skip to main content

SimpleFactory

Struct SimpleFactory 

Source
pub struct SimpleFactory<T: ?Sized + 'static>(/* private fields */);
Expand description

用于创建类型 T 实例的工厂集合。

此结构体包含一个从工厂 ID 到工厂实例的映射,这些工厂实例可以创建 目标类型 T 的装箱实例。工厂在编译时使用 inventory crate 注册, 可以通过 FactoryRegistry::factories() 检索。

类型 T 可以是非固定大小类型(trait 对象),并且必须具有 'static 生命周期。 工厂存储为静态引用,允许它们在线程间共享。

§示例

基本用法:

use rust_patterns_components::{FactoryFallback, FactoryRegistry};

// 定义产品 trait
trait Product {
    fn name(&self) -> &str;
}

// 假设已经注册了工厂(通过 inventory 机制)
// register_factory!(dyn Product, "product_a", ProductA);
// register_factory!(dyn Product, "product_b", ProductB);

// 获取工厂实例
let factory = FactoryRegistry::<dyn Product>::simple_factory();

// 通过 ID 创建特定产品
match factory.create("product_a", FactoryFallback::NoFallback) {
    Ok((id, product)) => {
        println!("创建了产品: {}, ID: {}", product.name(), id);
    }
    Err(e) => {
        println!("创建失败: {}", e);
    }
}

// 使用回退策略
let result = factory.create("", FactoryFallback::First);
// 当 ID 为空时,使用第一个可用的工厂

let result = factory.create("", FactoryFallback::Last);
// 当 ID 为空时,使用最后一个可用的工厂

错误处理:

use rust_patterns_components::{FactoryFallback, FactoryRegistry, FactoryError};

// 定义产品 trait
trait Product {
    fn name(&self) -> &str;
}

let factory = FactoryRegistry::<dyn Product>::simple_factory();

// 不存在的工厂 ID
match factory.create("nonexistent", FactoryFallback::NoFallback) {
    Err(FactoryError::FactoryNotFound(id)) => {
        println!("未找到工厂: {}", id);
    }
    _ => {}
}

// 空 ID 且无回退策略
match factory.create("", FactoryFallback::NoFallback) {
    Err(FactoryError::EmptyIdNoFallback) => {
        println!("空 ID 且未指定回退策略");
    }
    _ => {}
}

// 没有可用的工厂
match factory.create("any", FactoryFallback::NoFallback) {
    Err(FactoryError::NoFactoriesAvailable) => {
        println!("没有可用的工厂");
    }
    _ => {}
}

Implementations§

Source§

impl<T> SimpleFactory<T>
where T: ?Sized + 'static,

Source

pub fn create<'a>( &self, id: &'a str, strategy: FactoryFallback, ) -> Result<(&'a str, Box<T>), FactoryError>

使用指定的回退策略通过工厂模式创建实例。

此函数通过 ID 查找工厂并使用它创建实例。 如果 id 为空,行为取决于 strategy

  • NoFallback:返回错误
  • First:使用集合中的第一个工厂
  • Last:使用集合中的最后一个工厂 如果 id 不为空但找不到工厂,行为由 strategy 决定。
§参数
  • id - 要使用的工厂标识符,或空字符串表示默认
  • strategy - 找不到指定 ID 的工厂时使用的策略
§返回值
  • Ok((&str, Box<T>)) - 成功时返回包含使用的工厂 ID 和创建的实例的元组
  • Err(FactoryError) - 如果找不到工厂或没有可用的工厂则返回错误

Auto Trait Implementations§

§

impl<T> Freeze for SimpleFactory<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for SimpleFactory<T>

§

impl<T> Send for SimpleFactory<T>
where T: ?Sized,

§

impl<T> Sync for SimpleFactory<T>
where T: ?Sized,

§

impl<T> Unpin for SimpleFactory<T>
where T: ?Sized,

§

impl<T> UnsafeUnpin for SimpleFactory<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for SimpleFactory<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.