【发布时间】:2017-06-12 14:42:22
【问题描述】:
我有这个简单的sn-p代码,它使用一个arena进行内存分配和一个特征CloneInto,其目的是将一个结构从未知来源克隆到一个Arena中,并随着它的变化调整生命周期:
struct Arena;
impl Arena {
fn insert<'a, T: 'a>(&'a self, _: T) -> &'a mut T { unimplemented!() }
}
trait CloneInto<'a> {
type Output: 'a;
fn clone_into(&self, arena: &'a Arena) -> Self::Output;
}
可以直接使用:
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
struct Simple<'a> { a: &'a usize }
impl<'a, 'target> CloneInto<'target> for Simple<'a> {
type Output = Simple<'target>;
fn clone_into(&self, arena: &'target Arena) -> Simple<'target> {
Simple { a: arena.insert(*self.a) }
}
}
fn main() {
let arena = Arena;
let _ = Simple { a: &1 }.clone_into(&arena);
}
或者可以,直到更新到 Rust 1.18。现在compiler emits this error:
error[E0034]: multiple applicable items in scope --> <anon>:25:30 | 25 | let _ = Simple { a: &1 }.clone_into(&arena); | ^^^^^^^^^^ multiple `clone_into` found | note: candidate #1 is defined in an impl of the trait `CloneInto` for the type `Simple<'_>` --> <anon>:18:5 | 18 | / fn clone_into(&self, arena: &'target Arena) -> Simple<'target> { 19 | | Simple { a: arena.insert(*self.a) } 20 | | } | |_____^ = note: candidate #2 is defined in an impl of the trait `std::borrow::ToOwned` for the type `_`
即使我什至不导入std 或ToOwned!
【问题讨论】:
-
ToOwned在prelude 中,所以是的,您正在导入它! -
@FrancisGagné:说得好。否则不会引起冲突。
标签: rust