From: Ian Jackson Date: Mon, 26 Jul 2021 01:03:02 +0000 (+0100) Subject: config: Introduce u32.sat() X-Git-Tag: hippotat/1.0.0~438 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=d4c89e4897f8748b2935190955fea4947097bbbe;p=hippotat.git config: Introduce u32.sat() Signed-off-by: Ian Jackson --- 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::*;