diff --git a/quinn-proto/src/connection/mod.rs b/quinn-proto/src/connection/mod.rs index 16e391624..0384fd1f2 100644 --- a/quinn-proto/src/connection/mod.rs +++ b/quinn-proto/src/connection/mod.rs @@ -912,7 +912,7 @@ impl Connection { } let max_datagrams = match self.config.enable_segmentation_offload { - false => NonZeroUsize::new(1).expect("known"), + false => NonZeroUsize::MIN, true => max_datagrams, }; diff --git a/quinn-proto/src/connection/transmit_buf.rs b/quinn-proto/src/connection/transmit_buf.rs index 42930ab1a..ce5c40c4d 100644 --- a/quinn-proto/src/connection/transmit_buf.rs +++ b/quinn-proto/src/connection/transmit_buf.rs @@ -123,7 +123,7 @@ impl<'a> TransmitBuf<'a> { if datagram_size < self.segment_size { // If this is a GSO batch and this datagram is smaller than the segment // size, this must be the last datagram in the batch. - self.max_datagrams = NonZeroUsize::new(self.num_datagrams + 1).expect("known"); + self.max_datagrams = NonZeroUsize::MIN.saturating_add(self.num_datagrams); } } self.datagram_start = self.buf.len(); diff --git a/quinn-udp/benches/throughput.rs b/quinn-udp/benches/throughput.rs index 9c4ddf88f..20328a852 100644 --- a/quinn-udp/benches/throughput.rs +++ b/quinn-udp/benches/throughput.rs @@ -55,7 +55,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { let gso_segments = if gso_enabled { send_state.max_gso_segments() } else { - NonZeroUsize::new(1).expect("known") + NonZeroUsize::MIN }; let msg = vec![0xAB; min(MAX_DATAGRAM_SIZE, SEGMENT_SIZE * gso_segments.get())]; let transmit = Transmit { @@ -68,7 +68,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { let gro_segments = if gro_enabled { recv_state.gro_segments() } else { - NonZeroUsize::new(1).expect("known") + NonZeroUsize::MIN }; let batch_size = if recvmmsg_enabled { BATCH_SIZE } else { 1 }; diff --git a/quinn-udp/src/fallback.rs b/quinn-udp/src/fallback.rs index f89a77d25..01a951e51 100644 --- a/quinn-udp/src/fallback.rs +++ b/quinn-udp/src/fallback.rs @@ -84,7 +84,7 @@ impl UdpSocketState { #[inline] pub fn gro_segments(&self) -> NonZeroUsize { - NonZeroUsize::new(1).expect("known") + NonZeroUsize::MIN } /// Resize the send buffer of `socket` to `bytes` diff --git a/quinn-udp/src/unix.rs b/quinn-udp/src/unix.rs index 086045784..3238161f6 100644 --- a/quinn-udp/src/unix.rs +++ b/quinn-udp/src/unix.rs @@ -1032,7 +1032,7 @@ mod gro { .or_else(|_| std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0))) { Ok(socket) => socket, - Err(_) => return NonZeroUsize::new(1).expect("known"), + Err(_) => return NonZeroUsize::MIN, }; // As defined in net/ipv4/udp_offload.c @@ -1044,7 +1044,7 @@ mod gro { // https://github.com/quinn-rs/quinn/pull/1354. match set_socket_option(&socket, libc::SOL_UDP, UDP_GRO, OPTION_ON) { Ok(()) => NonZeroUsize::new(64).expect("known"), - Err(_) => NonZeroUsize::new(1).expect("known"), + Err(_) => NonZeroUsize::MIN, } } } @@ -1096,6 +1096,6 @@ mod gro { use std::num::NonZeroUsize; pub(super) fn gro_segments() -> NonZeroUsize { - NonZeroUsize::new(1).expect("known") + NonZeroUsize::MIN } } diff --git a/quinn/src/runtime/mod.rs b/quinn/src/runtime/mod.rs index 6100df149..2f850b42a 100644 --- a/quinn/src/runtime/mod.rs +++ b/quinn/src/runtime/mod.rs @@ -67,7 +67,7 @@ pub trait AsyncUdpSocket: Send + Sync + Debug + 'static { /// Maximum number of datagrams that might be described by a single [`RecvMeta`] fn max_receive_segments(&self) -> NonZeroUsize { - NonZeroUsize::new(1).expect("known") + NonZeroUsize::MIN } /// Whether datagrams might get fragmented into multiple parts @@ -102,7 +102,7 @@ pub trait UdpSender: Send + Sync + Debug + 'static { /// Maximum number of datagrams that a [`Transmit`] may encode. fn max_transmit_segments(&self) -> NonZeroUsize { - NonZeroUsize::new(1).expect("known") + NonZeroUsize::MIN } }