我正在尝试克隆盒装特征的向量。自然,简单地推导Clone
实现我的特征的所有结构是不够的,因为编译器在编译时并不知道所有实现该特征的结构都具有Clone
.
好的,然后我尝试将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`
这里是操场的链接,便于代码运行。
我也尝试在a中制作elements
矢量,但这也不起作用。Sequence
Vec<Box<MusicElement + Clone>>
我无法在网上找到任何有用的解决方案,所以这是我的问题:如何使代码可克隆?