【问题标题】:I implemented a trait for another trait but cannot call methods from both traits我为另一个特征实现了一个特征,但不能从两个特征调用方法
【发布时间】:2015-05-29 04:10:55
【问题描述】:

我有一个叫做Sleep的特质:

pub trait Sleep {
    fn sleep(&self);
}

我可以为每个结构提供不同的睡眠实现,但事实证明,大多数人睡眠的方式非常少。你可以睡在床上:

pub trait HasBed {
    fn sleep_in_bed(&self);
    fn jump_on_bed(&self);
}

impl Sleep for HasBed {
    fn sleep(&self) {
        self.sleep_in_bed()
    }
}

如果你在露营,你可以睡在帐篷里:

pub trait HasTent {
    fn sleep_in_tent(&self);
    fn hide_in_tent(&self);
}

impl Sleep for HasTent {
    fn sleep(&self) {
        self.sleep_in_tent()
    }
}

有一些奇怪的案例。我有一个朋友可以靠墙睡觉,但大多数人,大多数时候,都会陷入一些简单的情况。

我们定义一些结构并让它们休眠:

struct Jim;

impl HasBed for Jim {
    fn sleep_in_bed(&self) {}
    fn jump_on_bed(&self) {}
}

struct Jane;

impl HasTent for Jane {
    fn sleep_in_tent(&self) {}
    fn hide_in_tent(&self) {}
}

fn main() {
    use Sleep;
    let jim = Jim;
    jim.sleep();

    let jane = Jane;
    jane.sleep();
}

呃-哦!编译错误:

error[E0599]: no method named `sleep` found for type `Jim` in the current scope
  --> src/main.rs:44:9
   |
27 | struct Jim;
   | ----------- method `sleep` not found for this
...
44 |     jim.sleep();
   |         ^^^^^
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `sleep`, perhaps you need to implement it:
           candidate #1: `Sleep`

error[E0599]: no method named `sleep` found for type `Jane` in the current scope
  --> src/main.rs:47:10
   |
34 | struct Jane;
   | ------------ method `sleep` not found for this
...
47 |     jane.sleep();
   |          ^^^^^
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `sleep`, perhaps you need to implement it:
           candidate #1: `Sleep`

这个编译器错误很奇怪,因为如果一个 trait 实现另一个 trait 有问题,我希望在我这样做的时候听到它,而不是在我尝试使用结果时在程序的最底部。

在这个例子中,只有 2 个结构体和 2 种睡眠方式,但在一般情况下,有很多结构体和几种睡眠方式(但没有结构体那么多的方式)。

Bed 主要是 Sleep 的实现,但在一般情况下,Bed 有很多用途,可以实现很多东西。

唯一直接显而易见的方法是将impl Sleep for... 转换为自己使用的宏结构,但这似乎很老套和可怕。

【问题讨论】:

标签: rust traits


【解决方案1】:

你需要为实现第一个特征的对象实现第二个特征

impl<T> Sleep for T
where
    T: HasBed,
{
    fn sleep(&self) {
        self.sleep_in_bed()
    }
}

以前,您为 trait 的类型实现 Sleep,更好地表示为 dyn HasBed。详情请见What does "dyn" mean in a type?

但是,一旦您添加第二个一揽子实现,这就会中断:

impl<T> Sleep for T
where
    T: HasTent,
{
    fn sleep(&self) {
        self.sleep_in_tent()
    }
}

error[E0119]: conflicting implementations of trait `Sleep`:
  --> src/main.rs:24:1
   |
10 | / impl<T> Sleep for T
11 | | where
12 | |     T: HasBed,
13 | | {
...  |
16 | |     }
17 | | }
   | |_- first implementation here
...
24 | / impl<T> Sleep for T
25 | | where
26 | |     T: HasTent,
27 | | {
...  |
30 | |     }
31 | | }
   | |_^ conflicting implementation

有可能实现 both HasBedHasTent。如果出现实现两者的东西,那么代码现在将是模棱两可的。解决方法是 specialization,但目前还没有稳定的实现。

你如何实现你的目标?我想你已经提出了当前最好的解决方案——写一个宏。你也可以write your own derive macro。宏确实没那么糟糕,但它们可能很难编写。

另一件事可能完全基于您为示例选择的名称,即简单地将结构嵌入到其他结构中,可选择将它们公开。由于您对Sleep 的实现基本上只取决于床/帐篷,因此这样做不会丢失任何功能。当然,有些人可能会觉得破坏了封装。您可以再次创建宏来实现各种委托。

trait Sleep {
    fn sleep(&self);
}

struct Bed;
impl Bed {
    fn jump(&self) {}
}
impl Sleep for Bed {
    fn sleep(&self) {}
}

struct Tent;
impl Tent {
    fn hide(&self) {}
}
impl Sleep for Tent {
    fn sleep(&self) {}
}

struct Jim {
    bed: Bed,
}
struct Jane {
    tent: Tent,
}

fn main() {
    let jim = Jim { bed: Bed };
    jim.bed.sleep();
}

【讨论】:

  • 这(理论上)不能在调用方使用 use HasBed; 之类的东西来消除歧义吗?
  • 我不知道编译器的详细信息,无法对启用它的复杂程度做出有根据的猜测。
  • @Drew 问题不在于实现类似的东西,而是从语言设计的角度找到一个理想的设计。并激励它(这对于解决可以以不同方式解决的问题将是一个重要的补充)。
  • 在语言的现有限制内,最好的方法是什么?这是一个很好的“你不能这样做”类型的答案,但它并没有真正解决问题的动机。预处理器宏?还有其他一些设计模式应该考虑吗?
  • @Drew 我没有给你任何好的建议,但我更新了我有的建议。现在您已经解决了关于实现特征的特征的高级问题,您还可以考虑提出一个单独的较低级别的问题,其中包含有关您的实际用例的更多详细信息。这可能会让比我聪明的人有能力给你答案! ^_^
【解决方案2】:

我们可以在这里使用关联的项目。

pub trait Sleep: Sized {
    type Env: SleepEnv;

    fn sleep(&self, env: &Self::Env) {
        env.do_sleep(self);
    }

    fn get_name(&self) -> &'static str;
}

pub trait SleepEnv {
    fn do_sleep<T: Sleep>(&self, &T);
}

然后,我们实现两个不同的睡眠环境。

struct Bed;
struct Tent;

impl SleepEnv for Bed {
    fn do_sleep<T: Sleep>(&self, person: &T) {
        println!("{} is sleeping in bed", person.get_name());
    }
}

impl SleepEnv for Tent {
    fn do_sleep<T: Sleep>(&self, person: &T) {
        println!("{} is sleeping in tent", person.get_name());
    }
}

最后一部分是它们的具体实现。

struct Jim;
struct Jane;

impl Sleep for Jim {
    type Env = Bed;
    fn get_name(&self) -> &'static str {
        "Jim"
    }
}

impl Sleep for Jane {
    type Env = Tent;
    fn get_name(&self) -> &'static str {
        "Jane"
    }
}

测试代码:

fn main() {
    let bed = Bed;
    let tent = Tent;

    let jim = Jim;
    let jane = Jane;
    jim.sleep(&bed);
    jane.sleep(&tent);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多