问题描述
有没有办法在堆栈内存中完全实现特征对象?
extern crate alloc;
use alloc::vec::Vec;
use alloc::Boxed::Box;
pub trait ConnectionImp {
fn send_data(&self);
}
pub struct Collector {
pub connections: Vec<Box<dyn ConnectionImp>>
}
impl Collector {
pub fn new() -> Collector {
Collector {
connections: Vec::with_capacity(5),}
}
pub fn add_connection(&mut self,conn: Box<dyn ConnectionImp> ){
self.connections.push(conn);
}
}
我尝试使用无堆板条箱,但找不到 Box
的替代品。以下代码显示了我的努力结果:
use heapless::{Vec,/*pool::Box*/};
extern crate alloc;
use alloc::Boxed::Box;
pub trait ConnectionImp {
fn send_data(&self);
}
pub struct Collector {
pub connections: Vec<Box<dyn ConnectionImp>,5>
}
impl Collector {
pub fn new() -> Collector {
Collector {
connections: Vec::new(),}
}
pub fn add_connection(&mut self,conn: Box<dyn ConnectionImp> ){
self.connections.push(conn);
}
}
解决方法
是的,您可以使用 signUp(email: string,password: string): Observable<UserCredential> {
const credential = firebase.auth.EmailAuthProvider.credential(email,password);
return from(firebase.auth().currentUser.linkWithCredential(credential));
}
。动态调度的许多示例都使用 &dyn Trait
,因为它是一个更常见的用例,并且使用引用会引入生命周期,这往往会使示例更加复杂。
你的代码会变成:
Box