gpt4 book ai didi

rust - 神秘的冲突实现

转载 作者:行者123 更新时间:2023-12-03 11:24:23 25 4
gpt4 key购买 nike

我试图在固定时间测试我的代码。所以我写了这样的东西:

use std::time::{SystemTime, UNIX_EPOCH};

trait Clock {
fn now(&self) -> SystemTime;
}

trait MixInClock {
type Impl;
}

struct RealClock;
impl<T: MixInClock<Impl = RealClock>> Clock for T {
fn now(&self) -> SystemTime {
SystemTime::now()
}
}

struct FakeClock;
impl <T: MixInClock<Impl = FakeClock>> Clock for T {
fn now(&self) -> SystemTime {
UNIX_EPOCH
}
}

struct DIContainer;
impl MixInClock for DIContainer {
type Impl = FakeClock;
}

这段代码给了我一个错误:

error[E0119]: conflicting implementations of trait `Clock`:
--> src/lib.rs:19:1
|
12 | impl<T: MixInClock<Impl = RealClock>> Clock for T {
| ------------------------------------------------- first implementation here
...
19 | impl <T: MixInClock<Impl = FakeClock>> Clock for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

为什么? T 不可能工具 MixInClock<Impl = RealClock>MixInClock<Impl = FakeClock>在同一时间。对?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=95e3647b30ae0e37c45ac18da495a3c5

最佳答案

您遇到了 long-standing issue with Rust's coherence rules .有如何解决这个问题的建议,但目前工作已被推迟。报价Jonas Schievink's most recent comment on the bug :

To give an update on this: In 2016, RFC 1672 was proposed to fix this, but was postponed until the Chalk integration is done. Additionally, according to rust-lang/rfcs#1672 (comment), allowing these kinds of impls would allow users to express mutually exclusive traits, which is a very powerful feature that needs more in-depth consideration by the language team (hence marking as blocked on an RFC as well).

关于rust - 神秘的冲突实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60583052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com