Created
January 17, 2023 20:52
-
-
Save ramajd/3d3712b8241b208f8174856c02761055 to your computer and use it in GitHub Desktop.
Sample code to implement Chain of responsibility in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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