Skip to content

Instantly share code, notes, and snippets.

@ramajd
Created January 17, 2023 20:52
Show Gist options
  • Save ramajd/3d3712b8241b208f8174856c02761055 to your computer and use it in GitHub Desktop.
Save ramajd/3d3712b8241b208f8174856c02761055 to your computer and use it in GitHub Desktop.
Sample code to implement Chain of responsibility in Rust
trait IProcessor<'a> {
fn set_next(&'a mut self, next: &'a mut dyn IProcessor<'a>);
}
struct Processor<'a> {
next: Option<&'a mut dyn IProcessor<'a>>,
}
impl<'a> IProcessor<'a> for Processor<'a> {
fn set_next(&'a mut self, next: &'a mut dyn IProcessor<'a>) {
self.next = Some(next);
}
}
struct Container<'a> {
p1: Processor<'a>,
p2: Processor<'a>,
}
impl<'a> Container<'a> {
fn new() -> Self {
let p1 = Processor { next: None };
let p2 = Processor { next: None };
// TODO: set p2 as next for p1
Self { p1, p2 }
}
}
fn main() {
let mut c = Container::new();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment