我试图克隆盒装特征的载体。
自然地简单地在实现我的特征的所有结构上导出Clone
是不够的,因为编译器在编译时不知道实现该特征的所有结构都有Clone
。 / p>
好的,所以我尝试使用Clone
作为超级用户,但这只会导致标题中的错误。我为解决方案而感到茫然。
这里是最小工作实施(或不工作,因为我无法克隆)
#![allow(dead_code, unused_macros)]
use std::fmt::Debug;
trait MusicElement: Debug + Clone {
fn duration(&self) -> f32;
}
#[derive(Debug, Clone)]
struct Note<'a> {
name: &'a str,
duration: f32,
}
impl<'a> MusicElement for Note<'a> {
fn duration(&self) -> f32 {
self.duration
}
}
#[derive(Debug, Clone)]
struct Pause {
duration: f32,
}
impl MusicElement for Pause {
fn duration(&self) -> f32 {
self.duration
}
}
#[derive(Debug, Clone)]
struct Sequence {
elements: Vec<Box<MusicElement>>,
}
impl MusicElement for Sequence {
fn duration(&self) -> f32 {
self.elements.iter().map(|e| e.duration()).sum()
}
}
fn main() {
let a4 = |dur| Box::new(Note { name: "a4", duration: dur });
let seq = Sequence { elements: vec![a4(0.25), a4(0.25), a4(0.5)] };
println!("{:?}", seq);
let seq2 = seq.clone();
println!("{:?}", seq2);
}
出现此错误:
error[E0038]: the trait `MusicElement` cannot be made into an object
--> src/main.rs:33:5
|
33 | elements: Vec<Box<MusicElement>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MusicElement` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
这是一个link to the playground,可以轻松运行代码。
我还试图在elements
和Sequence
中制作Vec<Box<MusicElement + Clone>>
向量,但这也不起作用。
我还没能在网上找到任何有用的解决方案,所以我的问题是:如何使代码可以克隆?
答案 0 :(得分:9)
解决方案在于结合迄今为止评论中的建议 - @Lukas Kalbertodt's comment中的答案告诉您必须为所有兼容的('static + MusicElement + Clone
)类型创建一个全面的特征实现。实施所需的唯一后续步骤是将Note.name
字段的类型从&'a str
更改为String
,as metioned by @Boiethios:
#![allow(dead_code, unused_macros)]
use std::fmt::Debug;
trait MusicElement: MusicElementClone + Debug {
fn duration(&self) -> f32;
}
trait MusicElementClone {
fn clone_box(&self) -> Box<MusicElement>;
}
impl<T: 'static + MusicElement + Clone> MusicElementClone for T {
fn clone_box(&self) -> Box<MusicElement> {
Box::new(self.clone())
}
}
impl Clone for Box<MusicElement> {
fn clone(&self) -> Box<MusicElement> {
self.clone_box()
}
}
#[derive(Debug, Clone)]
struct Note {
name: String,
duration: f32,
}
impl MusicElement for Note {
fn duration(&self) -> f32 {
self.duration
}
}
#[derive(Debug, Clone)]
struct Pause {
duration: f32,
}
impl MusicElement for Pause {
fn duration(&self) -> f32 {
self.duration
}
}
#[derive(Debug, Clone)]
struct Sequence {
elements: Vec<Box<MusicElement>>,
}
impl MusicElement for Sequence {
fn duration(&self) -> f32 {
self.elements.iter().map(|e| e.duration()).sum()
}
}
fn main() {
let a4 = |dur| Box::new(Note { name: String::from("a4"), duration: dur });
let seq = Sequence { elements: vec![a4(0.25), a4(0.25), a4(0.5)] };
println!("{:?}", seq);
let seq2 = seq.clone();
println!("{:?}", seq2);
}
这个编译,所以它就足够了!
答案 1 :(得分:2)
我的objekt
crate提供了jonny's answer的可重用实现。有了它,您可以使您的原始代码在最少的更改下工作。
trait MusicElement: Debug + Clone {
fn duration(&self) -> f32;
}
#[macro_use]
extern crate objekt;
trait MusicElement: Debug + objekt::Clone {
fn duration(&self) -> f32;
}
clone_trait_object!(MusicElement);
// Everything else as you wrote it.