From 17e29117008e3bf0d17d612742497888b027d920 Mon Sep 17 00:00:00 2001 From: Thomas Zahner Date: Fri, 15 Mar 2024 13:35:23 +0100 Subject: [PATCH] Move Arc and Mutex inside of Chain struct --- lychee-lib/src/chain/mod.rs | 34 ++++++++++++++++++++++------------ lychee-lib/src/client.rs | 13 ++++++------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lychee-lib/src/chain/mod.rs b/lychee-lib/src/chain/mod.rs index 7034b1d..04225be 100644 --- a/lychee-lib/src/chain/mod.rs +++ b/lychee-lib/src/chain/mod.rs @@ -1,6 +1,6 @@ -use core::fmt::Debug; - use crate::Status; +use core::fmt::Debug; +use std::sync::{Arc, Mutex}; #[derive(Debug, PartialEq)] pub(crate) enum ChainResult { @@ -11,26 +11,33 @@ pub(crate) enum ChainResult { pub(crate) type RequestChain = Chain; #[derive(Debug)] -pub struct Chain(Vec + Send>>); +pub struct Chain(Arc + Send>>>>); impl Default for Chain { fn default() -> Self { - Self(vec![]) + Self(Arc::new(Mutex::new(vec![]))) } } +impl Clone for Chain { + fn clone(&self) -> Self { + Self(self.0.clone()) + } +} + + impl Chain { pub(crate) fn new(values: Vec + Send>>) -> Self { - Self(values) + Self(Arc::new(Mutex::new(values))) } - pub(crate) fn append(&mut self, other: &mut Chain) { - self.0.append(&mut other.0); + pub(crate) fn append(&mut self, other: &Chain) { + self.0.lock().unwrap().append(&mut other.0.lock().unwrap()); } pub(crate) fn traverse(&mut self, mut input: T) -> ChainResult { use ChainResult::{Done, Next}; - for e in &mut self.0 { + for e in self.0.lock().unwrap().iter_mut() { match e.chain(input) { Next(r) => input = r, Done(r) => { @@ -43,7 +50,7 @@ impl Chain { } pub(crate) fn push(&mut self, value: Box + Send>) { - self.0.push(value); + self.0.lock().unwrap().push(value); } } @@ -52,7 +59,11 @@ pub(crate) trait Chainable: Debug { } mod test { - use super::{ChainResult, ChainResult::{Done, Next}, Chainable}; + use super::{ + ChainResult, + ChainResult::{Done, Next}, + Chainable, + }; #[derive(Debug)] struct Add(usize); @@ -74,8 +85,7 @@ mod test { #[test] fn simple_chain() { use super::Chain; - let mut chain: Chain = - Chain::new(vec![Box::new(Add(7)), Box::new(Add(3))]); + let mut chain: Chain = Chain::new(vec![Box::new(Add(7)), Box::new(Add(3))]); let result = chain.traverse(Result(0)); assert_eq!(result, Next(Result(10))); } diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index 88c1839..e3d6376 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -16,7 +16,7 @@ use std::{ collections::HashSet, path::Path, - sync::{Arc, Mutex}, + sync::Arc, time::Duration, }; @@ -288,7 +288,7 @@ pub struct ClientBuilder { /// can modify the request. A chained item can also decide to exit /// early and return a status, so that subsequent chain items are /// skipped and no HTTP request is ever made. - request_chain: Arc>, + request_chain: RequestChain, } impl Default for ClientBuilder { @@ -452,7 +452,7 @@ pub struct Client { /// Caches Fragments fragment_checker: FragmentChecker, - request_chain: Arc>, + request_chain: RequestChain, } impl Client { @@ -538,7 +538,7 @@ impl Client { ) -> Result { let quirks = Quirks::default(); let mut request_chain: RequestChain = Chain::new(vec![Box::new(quirks)]); - request_chain.append(&mut self.request_chain.lock().unwrap()); + request_chain.append(&self.request_chain); if let Some(c) = credentials { request_chain.push(Box::new(c.clone())); @@ -774,7 +774,6 @@ where mod tests { use std::{ fs::File, - sync::{Arc, Mutex}, time::{Duration, Instant}, }; @@ -1126,9 +1125,9 @@ mod tests { } } - let chain = Arc::new(Mutex::new(RequestChain::new(vec![Box::new( + let chain = RequestChain::new(vec![Box::new( ExampleHandler {}, - )]))); + )]); let client = ClientBuilder::builder() .request_chain(chain)