首页 > 解决方案 > 如何定义包含可以在运行时定义实现的通用特征的注册表

问题描述

我在设计具有泛型类型的注册表时遇到了麻烦(游乐场):

use std::fs::File;
use std::collections::HashMap;

type Result<T> = std::result::Result<T, std::io::Error>;

// extern crate that enforce an associate type T 
trait Reader {
    type T;
    fn get_reader(&self) -> Result<Self::T>;
}

// my lib code below
trait ProtocolHandler<R> {
    fn get_reader(&self, path: &str) -> Result<Box<dyn Reader<T = R>>>;
}

struct LocalFSHandler {}

// Each Handler has fixed type for R, here File for LocalFSHandler
impl ProtocolHandler<File> for LocalFSHandler {
    fn get_reader(&self, path: &str) -> Result<Box<dyn Reader<T = File>>> {
        todo!()
    }
}

struct HandlerRegistry {
    // this doesn't compile since lacks of generic argument R
    handlers: HashMap<String, Box<dyn ProtocolHandler>>
}

impl HandlerRegistry {
    fn new() -> Self {
        let mut map: HashMap<String, Box<dyn ProtocolHandler>> = HashMap::new();
        map.insert("local", Box::new(LocalFSHandler {}));
        Self {
            handlers: map,
        }
    }
    
    fn get(&self, name: &str) -> Option<Box<dyn ProtocolHandler>> {
        self.handlers.get(name).cloned()
    }
}
// end of my lib code

// user code
fn main() {
    let registry = HandlerRegistry::new();
    // register one's own handler to registry, which is not known to my lib
    // therefore I cannot try cast by defining a Box<dyn Any> for hashmap value?
    
    // how can I made my lib handler implementation agnostic and cast to the
    // right one at runtime while getting out from the HashMap?
}

我应该如何调整我的代码以使我的注册表本身实现不可知并且可能在运行时由用户填充?

我想我不能定义 hashMap 值,Box<dyn Any>因为我不能强制转换为我的库不知道的每个可能的处理程序类型?

标签: genericsrusttraits

解决方案


您不能在同一内部保存不同特征的特征对象HashMap——如果这些不同的特征只是同一通用基本特征的不同变体,这也是正确的。

在您的情况下,您可以保留std::any::Any并确保这些对象是ProtocolHandler<T>while handling的某种变体,insert()并且get()(请注意我添加了一些内联注释):

use std::collections::HashMap;
use std::fs::File;

type Result<T> = std::result::Result<T, std::io::Error>;

trait Reader {
    type T;
    fn get_reader(&self) -> Result<Self::T>;
}

trait ProtocolHandler<R> {
    fn get_reader(&self, path: &str) -> Result<Box<dyn Reader<T = R>>>;
}

struct LocalFSHandler {}

impl ProtocolHandler<File> for LocalFSHandler {
    fn get_reader(&self, path: &str) -> Result<Box<dyn Reader<T = File>>> {
        todo!()
    }
}

struct SomeOtherHandler {}

impl ProtocolHandler<()> for SomeOtherHandler {
    fn get_reader(&self, path: &str) -> Result<Box<dyn Reader<T = ()>>> {
        todo!()
    }
}

struct HandlerRegistry {
    /* We can't only allow ProtocolHandler<R> for any R here. We could introduce
       a new trait that is held here and has a `impl<R> NewTrait for
       ProtocolHandler<R>`, but it is easier to just have Any in the HashMap and
       to check types vi bounds on our methods. 
    */
    handlers: HashMap<String, Box<dyn std::any::Any>>,
}

impl HandlerRegistry {
    fn new() -> Self {
        let map: HashMap<String, Box<dyn std::any::Any>> = HashMap::new();
        // switched the order of the next two statements to show HandlerRegistry::insert() in action
        let mut result = Self { handlers: map };
        result.insert("local", LocalFSHandler {});
        result
    }

    // both insert() and get() are generic for any ProtocolHandler type
    fn insert<T: 'static + ProtocolHandler<R>, R, K: Into<String>>(&mut self, key: K, value: T) {
        self.handlers.insert(key.into(), Box::new(value));
    }

    fn get<T: 'static + ProtocolHandler<R>, R>(&self, key: &str) -> Option<&T> {
        match self
            .handlers
            .get(key)
            .map(|obj| (**obj).downcast_ref::<T>())
        {
            Some(some) => some,
            None => None,
        }
    }
}

fn main() {
    let registry = HandlerRegistry::new();
    // If you now get "local" as a LocalFSHandler, this returns Some(LocalFSHandler)...
    assert!(registry.get::<LocalFSHandler, _>("local").is_some());
    // But if you try to get it as SomeOtherHandler, this returns None, because that's the wrong type
    assert!(registry.get::<SomeOtherHandler, _>("local").is_none());
}

操场

请注意,您在这里或多或少选择不严格输入。您将无法在编译时注意到键入问题,只有在运行时。我不知道这将在什么背景下使用——如果这真的是你想要和需要的,那就去做吧。否则,我建议重新考虑设计。例如,如果您知道 s 可能存在的所有类型RProtocolHandler<R>那么最好保存一个包含这些可能ProtocolHandler变体的枚举而不是Any.


推荐阅读