【问题标题】:Implementing PartialEq on a boxed trait在盒装特征上实现 PartialEq
【发布时间】:2018-02-23 16:48:36
【问题描述】:

我有一个 Rust 程序,其中包含许多不同的结构,它们都实现了一个名为 ApplyAction 的特征。另一个结构ActionList 包含一个实现ApplyAction 的盒装对象向量。我想创建一些单元测试,将ActionLists 相互比较。

有几个不同的 SO 问题涉及 PartialEq 盒装特征,我已经使用这些问题来实现一些方法。但是,在下面的(简化的)代码中(以及在Playground 上),main() 中的断言失败,因为传递给eq() 的对象的类型 ID 不同。为什么?

另外,对于这样一个简单的用例来说,这似乎非常复杂——有没有更简单的方法来做到这一点?

use std::any::TypeId;
use std::boxed::Box;
use std::fmt;
use std::mem::transmute;

#[derive(Debug, Eq, PartialEq)]
pub struct MyAction<T: fmt::Debug> {
    label: T,
}

impl<T: fmt::Debug> MyAction<T> {
    pub fn new(label: T) -> MyAction<T> {
        MyAction { label: label }
    }
}

pub trait ApplyAction<T: fmt::Debug + PartialEq>: fmt::Debug {
    fn get_type(&self) -> TypeId;
    fn is_eq(&self, other: &ApplyAction<T>) -> bool;
}

impl<T: fmt::Debug + Eq + 'static> ApplyAction<T> for MyAction<T> {
    fn get_type(&self) -> TypeId {
        TypeId::of::<MyAction<T>>()
    }

    fn is_eq(&self, other: &ApplyAction<T>) -> bool {
        if other.get_type() == TypeId::of::<Self>() {
            // Rust thinks that self and other are different types in the calls below.
            let other_ = unsafe { *transmute::<&&ApplyAction<T>, &&Self>(&other) };
            self.label == other_.label
        } else {
            false
        }
    }
}

impl<T: fmt::Debug + Eq + PartialEq + 'static> PartialEq for ApplyAction<T> {
    fn eq(&self, other: &ApplyAction<T>) -> bool {
        if other.get_type() == TypeId::of::<Self>() {
            self.is_eq(other)
        } else {
            false
        }
    }
}

#[derive(Debug)]
pub struct ActionList<T: fmt::Debug> {
    actions: Vec<Box<ApplyAction<T>>>,
}

impl<T: fmt::Debug + PartialEq> ActionList<T> {
    pub fn new() -> ActionList<T> {
        ActionList { actions: vec![] }
    }
    pub fn push<A: ApplyAction<T> + 'static>(&mut self, action: A) {
        self.actions.push(Box::new(action));
    }
}

impl<T: fmt::Debug + Eq + PartialEq + 'static> PartialEq for ActionList<T> {
    fn eq(&self, other: &ActionList<T>) -> bool {
        for (i, action) in self.actions.iter().enumerate() {
            if **action != *other.actions[i] {
                return false;
            }
        }
        true
    }
}

fn main() {
    let mut script1: ActionList<String> = ActionList::new();
    script1.push(MyAction::new("foo".to_string()));
    let mut script2: ActionList<String> = ActionList::new();
    script2.push(MyAction::new("foo".to_string()));
    let mut script3: ActionList<String> = ActionList::new();
    script3.push(MyAction::new("bar".to_string()));
    assert_eq!(script1, script2);
    assert_ne!(script1, script3);
}

【问题讨论】:

  • 你可以这样做:playground。我现在不能写完整的答案,所以请随意写你自己的。

标签: rust traits


【解决方案1】:

impl&lt;...&gt; PartialEq for ApplyAction&lt;T&gt; 中你使用了TypeId::of::&lt;Self&gt;();即未调整大小的特征对象的类型。那不是你想要的;但是删除if并直接调用self.is_eq(other),你的代码应该可以工作了。

遗憾的是,您的示例需要大量代码来实现 ApplyAction&lt;T&gt; for MyAction&lt;T&gt; - 并且对于您可能想要使用的其他动作类型也是如此。

我试图消除这种开销,但使用夜间功能,它完全消失了(否则只剩下一个小存根):

Playground

// see `default impl` below
#![feature(specialization)]

// Any::<T>::downcast_ref only works for special trait objects (`Any` and
// `Any + Send`); having a trait `T` derive from `Any` doesn't allow you to
// coerce ("cast") `&T` into `&Any` (that might change in the future).
//
// Implementing a custom `downcast_ref` which takes any
// `T: Any + ?Sized + 'static` as input leads to another problem: if `T` is a
// trait that didn't inherit `Any` you still can call `downcast_ref`, but it
// won't work (it will use the `TypeId` of the trait object instead of the
// underlying (sized) type).
//
// Use `SizedAny` instead: it's only implemented for sized types by default;
// that prevents the problem above, and we can implement `downcast_ref` without
// worrying.

mod sized_any {
    use std::any::TypeId;

    // don't allow other implementations of `SizedAny`; `SizedAny` must only be
    // implemented for sized types.
    mod seal {
        // it must be a `pub trait`, but not be reachable - hide it in
        // private mod.
        pub trait Seal {}
    }

    pub trait SizedAny: seal::Seal + 'static {
        fn get_type_id(&self) -> TypeId {
            TypeId::of::<Self>()
        }
    }

    impl<T: 'static> seal::Seal for T {}
    impl<T: 'static> SizedAny for T {}

    // `SizedAny + ?Sized` means it can be a trait object, but `SizedAny` was
    // implemented for the underlying sized type.
    pub fn downcast_ref<From, To>(v: &From) -> Option<&To>
    where
        From: SizedAny + ?Sized + 'static,
        To: 'static,
    {
        if TypeId::of::<To>() == <From as SizedAny>::get_type_id(v) {
            Some(unsafe { &*(v as *const From as *const To) })
        } else {
            None
        }
    }
}
use sized_any::*;

use std::boxed::Box;
use std::fmt;

// `ApplyAction`

fn foreign_eq<T, U>(a: &T, b: &U) -> bool
where
    T: PartialEq + 'static,
    U: SizedAny + ?Sized + 'static,
{
    if let Some(b) = downcast_ref::<U, T>(b) {
        a == b
    } else {
        false
    }
}

pub trait ApplyAction<T: 'static>: fmt::Debug + SizedAny + 'static {
    fn foreign_eq(&self, other: &ApplyAction<T>) -> bool;
}

// requires `#![feature(specialization)]` and a nightly compiler.
// could also copy the default implementation manually to each `impl` instead.
//
// this implementation only works with sized `A` types; we cannot make 
// `ApplyAction<T>` inherit `Sized`, as that would destroy object safety.
default impl<T: 'static, A: PartialEq + 'static> ApplyAction<T> for A {
    fn foreign_eq(&self, other: &ApplyAction<T>) -> bool {
        foreign_eq(self, other)
    }
}

impl<T: 'static> PartialEq for ApplyAction<T> {
    fn eq(&self, other: &ApplyAction<T>) -> bool {
        self.foreign_eq(other)
    }
}

// `MyAction`

#[derive(Debug, Eq, PartialEq)]
pub struct MyAction<T: fmt::Debug> {
    label: T,
}

impl<T: fmt::Debug> MyAction<T> {
    pub fn new(label: T) -> MyAction<T> {
        MyAction { label: label }
    }
}

impl<T: fmt::Debug + PartialEq + 'static> ApplyAction<T> for MyAction<T> {}

// `ActionList`

#[derive(Debug)]
pub struct ActionList<T> {
    actions: Vec<Box<ApplyAction<T>>>,
}

impl<T: 'static> ActionList<T> {
    pub fn new() -> ActionList<T> {
        ActionList { actions: vec![] }
    }

    pub fn push<A: ApplyAction<T> + 'static>(&mut self, action: A) {
        self.actions.push(Box::<A>::new(action));
    }
}

impl<T: 'static> PartialEq for ActionList<T> {
    fn eq(&self, other: &ActionList<T>) -> bool {
        if self.actions.len() != other.actions.len() {
            return false;
        }
        for (i, action) in self.actions.iter().enumerate() {
            if **action != *other.actions[i] {
                return false;
            }
        }
        true
    }
}

// `main`

fn main() {
    let mut script1: ActionList<String> = ActionList::new();
    script1.push(MyAction::new("foo".to_string()));
    let mut script2: ActionList<String> = ActionList::new();
    script2.push(MyAction::new("foo".to_string()));
    let mut script3: ActionList<String> = ActionList::new();
    script3.push(MyAction::new("bar".to_string()));
    assert_eq!(script1, script2);
    assert_ne!(script1, script3);
}

另见:

【讨论】:

  • 很有趣,非常感谢。我在@red75prime 的评论(关于问题)中实现了答案,不需要SizedTypeId。它似乎工作正常,除了在“真实”代码中我无法说服 Rust 自动在 ActionList 上派生 PartialEq,但简单的手动实现就可以了。我以前没见过specialization。在实际代码中,每个ApplyAction struct 都有一个方法,每个实现都需要不同,specialization 在这种情况下仍然可以使用吗?
  • 不,如果您需要不同的代码,您不能使用specialization。 @red75prime 中的示例没有明确使用 Sized,但您可能不应该为未调整大小的类型调用 impl_comparable_for!。此外,它还通过提供一个 trait 方法来解决将 &amp;T 强制转换为 &amp;Any 的问题(这是一个很好的解决方法)。
猜你喜欢
  • 2018-08-26
  • 1970-01-01
  • 2023-02-07
  • 2012-03-21
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多