小编pej*_*man的帖子

是否有无堆的 trait 对象?

有没有办法在堆栈内存中完全实现 trait 对象?

这是我使用的代码,Box因此是堆内存:

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);
    }
}

Run Code Online (Sandbox Code Playgroud)

我尝试使用无堆板条箱,但找不到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: …
Run Code Online (Sandbox Code Playgroud)

rust trait-objects

4
推荐指数
1
解决办法
88
查看次数

标签 统计

rust ×1

trait-objects ×1