简体   繁体   English

如何编写接受特征或指向特征的智能指针(Box、Rc 等)的通用代码?

[英]How do I write generic code that accepts either a trait or a smart pointer to the trait (Box, Rc, etc)?

I am writing a discrete event simulator that simulates processes, exchanging events.我正在编写一个离散事件模拟器来模拟流程,交换事件。 Processes are implementations of the trait process, and are stored in the simulation class, which tells them when to process events.进程是特征进程的实现,并存储在模拟类中,它告诉它们何时处理事件。

A simulation can contain processes of only one type, or it can contain processes of different types.模拟可以只包含一种类型的进程,也可以包含不同类型的进程。 This is why I need a freedom to be able to choose dynamic processes.这就是为什么我需要能够自由选择动态流程的原因。

I currently do it with boxes.我目前用盒子来做。 Every code that uses the simulation needs to create boxes and put their created objects in boxes.使用模拟的每个代码都需要创建框并将其创建的对象放入框中。

trait Process {
    // methods
    fn start();
    fn process_event();
}

struct Simulation {
    processes: Vec<dyn Process>
}

impl Simulation {
    fn add_process(&mut self, p: Box<dyn Process>) {
        processes.push(p);
    }
    
    fn run() {
        // calls process_event on processes, based on simulation logic
    }
}

My biggest problem, is that in some cases these processes need to have self-references.我最大的问题是,在某些情况下,这些过程需要有自引用。 Then I need to replace box with either Pin or OwningRef, and I don't want to have every possible simulation, creating a Pin or OwningRef.然后我需要用 Pin 或 OwningRef 替换 box,并且我不想进行所有可能的模拟,创建 Pin 或 OwningRef。 So, i would want some kind of generic parameter to tell simulation to use different classes for boxes.所以,我想要某种通用参数来告诉模拟为盒子使用不同的类。

A nice extension would be to be able to not have dynamic dispatch at all, and have simulation contain actual process types, not generics.一个很好的扩展是完全没有动态调度,并且模拟包含实际的流程类型,而不是泛型。

So, i want something like - Simulation<P> where P is either a Process or Deref<Process> .所以,我想要类似 - Simulation<P> where P is either a Process or Deref<Process>东西Simulation<P> where P is either a Process or Deref<Process>

Both variants of generic Simulation are possible.通用Simulation两种变体都是可能的。

  1. Containing smart pointers to processes ( playground ):包含指向进程的智能指针( playground ):
trait Process {}
struct P {}
impl Process for P {}

struct Simulation<T: std::ops::Deref<Target = dyn Process> + Sized> {
    processes: Vec<T>
}

impl<T: std::ops::Deref<Target = dyn Process> + Sized> Simulation<T> {
    fn add_process(&mut self, p: T) {
        self.processes.push(p);
    }
    
    fn run(&self) {
        // calls process_event on processes, based on simulation logic
    }
}

fn main() {
    let mut s: Simulation<Box<dyn Process>> = Simulation { processes: vec![] };
    s.add_process(Box::new(P {}));
    s.run();
    
    let mut s: Simulation<std::rc::Rc<dyn Process>> = Simulation { processes: vec![] };
    s.add_process(std::rc::Rc::new(P {}));
    s.run();
}
  1. Containing actual process types ( playground ):包含实际流程类型( playground ):
trait Process {}
struct P {}
impl Process for P {}

struct P2 {}
impl Process for P2 {}

struct Simulation<T: Process> {
    processes: Vec<T>
}

impl<T: Process> Simulation<T> {
    fn add_process(&mut self, p: T) {
        self.processes.push(p);
    }
    
    fn run(&self) {
        // calls process_event on processes, based on simulation logic
    }
}

fn main() {
    let mut s: Simulation<P> = Simulation { processes: vec![] };
    s.add_process(P {});
    s.run();
    
    let mut s: Simulation<P2> = Simulation { processes: vec![] };
    s.add_process(P2 {});
    s.run();
}
  1. You can even have one type of Simulation, containing different types of processes ( playground ):您甚至可以拥有一种类型的模拟,其中包含不同类型的流程 ( playground ):
trait Process {}
struct P {}
impl Process for P {}

struct P2 {}
impl Process for P2 {}

struct Simulation {
    processes: Vec<Box<dyn Process>>
}

impl Simulation {
    fn add_process<T: Process + 'static>(&mut self, p: T) {
        self.processes.push(Box::new(p));
    }
    
    fn run(&self) {
        // calls process_event on processes, based on simulation logic
    }
}

fn main() {
    let mut s: Simulation = Simulation { processes: vec![] };
    s.add_process(P {});
    s.run();
    
    let mut s: Simulation = Simulation { processes: vec![] };
    s.add_process(P2 {});
    s.run();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM