Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created December 19, 2021 10:08
Show Gist options
  • Save rust-play/d96640f6e2477fa35aa08d234c476faf to your computer and use it in GitHub Desktop.
Save rust-play/d96640f6e2477fa35aa08d234c476faf to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::error::Error;
use std::slice::Iter;
struct Query {
query: String,
}
struct Response {
response: u64,
}
trait Responsability {
fn take(&self, iterator: Iter<Box<dyn Responsability>>, query: Query) -> Result<Response, Box<dyn Error>>;
}
struct ResponsabilityChain {
responsabilities: Vec<Box<dyn Responsability>>,
}
impl ResponsabilityChain {
pub fn new(responsabilities: Vec<Box<dyn Responsability>>) -> Self {
Self { responsabilities }
}
pub fn launch(&self, query: Query) -> Result<Response, Box<dyn Error>> {
let mut iterator: Iter<Box<dyn Responsability>> = self.responsabilities.iter();
let responsability = iterator.next().unwrap();
responsability.take(iterator, query)
}
}
fn main() {
println!("Helo, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment