Move Arc and Mutex inside of Chain struct

This commit is contained in:
Thomas Zahner 2024-03-15 13:35:23 +01:00
parent 377aceed60
commit 17e2911700
2 changed files with 28 additions and 19 deletions

View file

@ -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<T, R> {
@ -11,26 +11,33 @@ pub(crate) enum ChainResult<T, R> {
pub(crate) type RequestChain = Chain<reqwest::Request, Status>;
#[derive(Debug)]
pub struct Chain<T, R>(Vec<Box<dyn Chainable<T, R> + Send>>);
pub struct Chain<T, R>(Arc<Mutex<Vec<Box<dyn Chainable<T, R> + Send>>>>);
impl<T, R> Default for Chain<T, R> {
fn default() -> Self {
Self(vec![])
Self(Arc::new(Mutex::new(vec![])))
}
}
impl<T, R> Clone for Chain<T, R> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T, R> Chain<T, R> {
pub(crate) fn new(values: Vec<Box<dyn Chainable<T, R> + Send>>) -> Self {
Self(values)
Self(Arc::new(Mutex::new(values)))
}
pub(crate) fn append(&mut self, other: &mut Chain<T, R>) {
self.0.append(&mut other.0);
pub(crate) fn append(&mut self, other: &Chain<T, R>) {
self.0.lock().unwrap().append(&mut other.0.lock().unwrap());
}
pub(crate) fn traverse(&mut self, mut input: T) -> ChainResult<T, R> {
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<T, R> Chain<T, R> {
}
pub(crate) fn push(&mut self, value: Box<dyn Chainable<T, R> + Send>) {
self.0.push(value);
self.0.lock().unwrap().push(value);
}
}
@ -52,7 +59,11 @@ pub(crate) trait Chainable<T, R>: 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<Result, Result> =
Chain::new(vec![Box::new(Add(7)), Box::new(Add(3))]);
let mut chain: Chain<Result, Result> = Chain::new(vec![Box::new(Add(7)), Box::new(Add(3))]);
let result = chain.traverse(Result(0));
assert_eq!(result, Next(Result(10)));
}

View file

@ -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<Mutex<RequestChain>>,
request_chain: RequestChain,
}
impl Default for ClientBuilder {
@ -452,7 +452,7 @@ pub struct Client {
/// Caches Fragments
fragment_checker: FragmentChecker,
request_chain: Arc<Mutex<RequestChain>>,
request_chain: RequestChain,
}
impl Client {
@ -538,7 +538,7 @@ impl Client {
) -> Result<Status> {
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)