chiark / gitweb /
config: Introduce u32.sat()
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 26 Jul 2021 01:03:02 +0000 (02:03 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 26 Jul 2021 01:03:02 +0000 (02:03 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/client.rs
src/config.rs
src/prelude.rs

index 3abfc1959b8d2a2fa13563ac939a007ce24ba0a6..a9c0fad45a0380c50fd73bf86d9efbe7805ca224 100644 (file)
@@ -35,19 +35,16 @@ async fn run_client<C>(ic: InstanceConfig, hclient: Arc<hyper::Client<C>>)
     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
index 4b94b3ed73357db4659e2c3fde777e4e67b13e66..5243e8b616beef17095564f5e34c4ce2a027792e 100644 (file)
@@ -86,6 +86,11 @@ pub struct Opts {
   pub extra_config: Vec<PathBuf>,
 }
 
+#[ext(pub)]
+impl u32 {
+  fn sat(self) -> usize { self.try_into().unwrap_or(usize::MAX) }
+}
+
 #[ext]
 impl<'s> Option<&'s str> {
   #[throws(AE)]
index 4198ed9818b96ab45112e2a48b28000e300dba6f..521d82e549a64b47d7cbd95fa3c388e30d380156 100644 (file)
@@ -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::*;