首页 > 解决方案 > 特征对象如何将具有泛型方法的特征作为参数?

问题描述

所以特征对象不能有泛型的方法——看起来不错。但是在这种语言中,使用抽象机制的唯一方法是通过泛型和特征对象。这意味着对于每个特征,我必须事先决定它是否可以用作对象,并在任何地方使用 dyn 而不是 impl。并且它内部的所有特征都必须以相同的方式来支持这一点。这种感觉非常难看。您能提出什么建议或告诉我为什么要这样设计吗?

fn main() {}

// some abstracted thing
trait Required {
    fn f(&mut self, simple: i32);
}

// this trait doesn't know that it's going to be used by DynTrait
// it just takes Required as an argument
// nothing special
trait UsedByDyn {
    // this generic method doesn't allow this trait to be dyn itself
    // no dyn here: we don't know about DynTrait in this scope
    fn f(&mut self, another: impl Required);
}

// this trait needs to use UsedByDyn as a function argument
trait DynTrait {
    // since UsedByDyn uses generic methods it can't be dyn itself
    // the trait `UsedByDyn` cannot be made into an object
    //fn f(&mut self, used: Box<dyn UsedByDyn>);

    // we can't use UsedByDyn without dyn either otherwise Holder can't use us as dyn
    // the trait `DynTrait` cannot be made into an object
    // fn f(&mut self, used: impl UsedByDyn);

    // how to use UsedByDyn here?
}

struct Holder {
    CanBeDyn: Box<dyn DynTrait>,
}

标签: genericsrusttraitsabstractiontrait-objects

解决方案


这意味着对于每个特征,我必须事先决定它是否可以用作对象,并在任何地方使用 dyn 而不是 impl。

你可以这样做,但幸运的是,这不是唯一的选择。

你也可以像往常一样编写你的特征,在适当的地方使用泛型。如果/当您需要 trait 对象时,请定义一个您在本地使用的新的对象安全 trait,并在该位置公开您实际需要的 API 子集。

例如,假设您拥有或使用了一个非对象安全的特征:

trait Serialize {
    /// Serialize self to the given IO sink
    fn serialize(&self, sink: &mut impl io::Write);
}

该特征不能用作特征对象,因为它(可能是为了确保最大效率)具有通用方法。但这并不需要阻止您的代码使用特征对象来访问特征的功能。假设您需要将Serialize值装箱以便将它们保存在一个向量中,您将把它保存到一个文件

// won't compile
struct Pool {
    objs: Vec<Box<dyn Serialize>>,
}

impl Pool {
    fn add(&mut self, obj: impl Serialize + 'static) {
        self.objs.push(Box::new(obj) as Box<dyn Serialize>);
    }

    fn save(&self, file: &Path) -> io::Result<()> {
        let mut file = io::BufWriter::new(std::fs::File::create(file)?);
        for obj in self.objs.iter() {
            obj.serialize(&mut file);
        }
        Ok(())
    }
}

以上内容无法编译,因为Serialize它不是对象安全的。但是 - 您可以轻松定义满足以下需求的新对象安全特征Pool

// object-safe trait, Pool's implementation detail
trait SerializeFile {
    fn serialize(&self, sink: &mut io::BufWriter<std::fs::File>);
}

// Implement `SerializeFile` for any T that implements Serialize
impl<T> SerializeFile for T
where
    T: Serialize,
{
    fn serialize(&self, sink: &mut io::BufWriter<std::fs::File>) {
        // here we can access `Serialize` because `T` is a concrete type
        Serialize::serialize(self, sink);
    }
}

现在Pool几乎可以使用dyn SerializeFile游乐场):

struct Pool {
    objs: Vec<Box<dyn SerializeFile>>,
}

impl Pool {
    fn add(&mut self, obj: impl Serialize + 'static) {
        self.objs.push(Box::new(obj) as Box<dyn SerializeFile>);
    }

    // save() defined the same as before
    ...
}

定义一个单独的对象安全 trait 似乎是不必要的工作 - 如果原始 trait 足够简单,您当然可以从一开始就使其对象安全。但是有些特征要么太通用,要么太面向性能,以至于从一开始就无法做到对象安全,在这种情况下,最好记住保持通用性是可以的。当您确实需要对象安全版本时,它通常用于具体任务,其中根据原始特征实现的自定义对象安全特征将完成这项工作。


推荐阅读