From d4c89e4897f8748b2935190955fea4947097bbbe Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 26 Jul 2021 02:03:02 +0100 Subject: [PATCH] config: Introduce u32.sat() Signed-off-by: Ian Jackson --- src/bin/client.rs | 7 ++----- src/config.rs | 5 +++++ src/prelude.rs | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index 3abfc19..a9c0fad 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -35,19 +35,16 @@ async fn run_client(ic: InstanceConfig, hclient: Arc>) Ok::<_,io::Error>(()) }); - let max_outstanding = ic.max_requests_outstanding - .try_into().unwrap_or(usize::MAX); - let tx_stream = ipif.stdout.take().unwrap(); let mut tx_stream = tokio::io::BufReader::new(tx_stream).split(SLIP_ESC); let stream_for_rx = ipif.stdin .take().unwrap(); - let mut reqs = Vec::with_capacity(max_outstanding); + let mut reqs = Vec::with_capacity(ic.max_requests_outstanding.sat()); async { loop { select! { packet = tx_stream.next_segment(), - if reqs.len() < max_outstanding => { + if reqs.len() < ic.max_requests_outstanding.sat() => { let packet = packet.context("read from ipif")?; reqs.push(()); // xxx make new request diff --git a/src/config.rs b/src/config.rs index 4b94b3e..5243e8b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -86,6 +86,11 @@ pub struct Opts { pub extra_config: Vec, } +#[ext(pub)] +impl u32 { + fn sat(self) -> usize { self.try_into().unwrap_or(usize::MAX) } +} + #[ext] impl<'s> Option<&'s str> { #[throws(AE)] diff --git a/src/prelude.rs b/src/prelude.rs index 4198ed9..521d82e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -34,7 +34,7 @@ pub use tokio::task; pub use tokio::time::Duration; pub use void::{self, Void, ResultVoidExt, ResultVoidErrExt}; -pub use crate::config::{self, InstanceConfig}; +pub use crate::config::{self, InstanceConfig, u32Ext as _}; pub use crate::utils::*; pub use crate::types::*; -- 2.30.2