Skip to content

Commit 234d115

Browse files
committed
style(lib): Address cast-possible-truncation clippy findings.
1 parent 83da75a commit 234d115

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ restriction = { level = "warn", priority = -2 }
115115

116116
arithmetic_side_effects = "allow" # TODO: consider
117117
as_conversions = "allow" # TODO: tricky
118-
cast_possible_truncation = "allow" # TODO: consider
119118
cast_precision_loss = "allow" # TODO: consider
120119
checked_conversions = "allow"
121120
else_if_without_else = "allow"

src/proto/h1/encode.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,18 @@ impl Encoder {
142142
}
143143
Kind::Length(remaining) => {
144144
trace!("sized write, len = {}", len);
145-
if len as u64 > *remaining {
146-
let limit = *remaining as usize;
147-
*remaining = 0;
148-
BufKind::Limited(msg.take(limit))
149-
} else {
150-
*remaining -= len as u64;
151-
BufKind::Exact(msg)
145+
match usize::try_from(*remaining) {
146+
// Holding more than is owed, so write only what is left.
147+
Ok(limit) if limit < len => {
148+
*remaining = 0;
149+
BufKind::Limited(msg.take(limit))
150+
}
151+
// Ok(_) => Owed at least what we hold, write all of it.
152+
// Err(_) => Owed more than `usize` can represent, write all of it.
153+
Ok(_) | Err(_) => {
154+
*remaining -= len as u64;
155+
BufKind::Exact(msg)
156+
}
152157
}
153158
}
154159
#[cfg(feature = "server")]
@@ -241,6 +246,7 @@ impl Encoder {
241246
dst.buffer(msg);
242247
!self.is_last
243248
}
249+
#[allow(clippy::cast_possible_truncation, reason="usize::MAX > len > remaining, cast truncation is impossible")]
244250
Ordering::Greater => {
245251
dst.buffer(msg.take(remaining as usize));
246252
!self.is_last
@@ -328,11 +334,7 @@ where
328334
}
329335
}
330336

331-
#[cfg(target_pointer_width = "32")]
332-
const USIZE_BYTES: usize = 4;
333-
334-
#[cfg(target_pointer_width = "64")]
335-
const USIZE_BYTES: usize = 8;
337+
const USIZE_BYTES: usize = std::mem::size_of::<usize>();
336338

337339
// each byte will become 2 hex
338340
const CHUNK_SIZE_MAX_BYTES: usize = USIZE_BYTES * 2;
@@ -369,6 +371,7 @@ impl Buf for ChunkSize {
369371
}
370372

371373
#[inline]
374+
#[allow(clippy::cast_possible_truncation)]
372375
fn advance(&mut self, cnt: usize) {
373376
assert!(cnt <= self.remaining());
374377
self.pos += cnt as u8; // just asserted cnt fits in u8
@@ -385,12 +388,14 @@ impl fmt::Debug for ChunkSize {
385388
}
386389

387390
impl fmt::Write for ChunkSize {
391+
#[allow(clippy::cast_possible_truncation, reason="bytes is structurally always less than u8::MAX")]
388392
fn write_str(&mut self, num: &str) -> fmt::Result {
389393
use std::io::Write;
390394
(&mut self.bytes[self.len.into()..])
391395
.write_all(num.as_bytes())
392396
.expect("&mut [u8].write() cannot error");
393-
self.len += num.len() as u8; // safe because bytes is never bigger than 256
397+
debug_assert!(u8::try_from(num.len()).is_ok());
398+
self.len += num.len() as u8; // safe because bytes is never bigger than 255
394399
Ok(())
395400
}
396401
}

src/proto/h2/ping.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ impl Shared {
361361
// ===== impl Bdp =====
362362

363363
/// Any higher than this likely will be hitting the TCP flow control.
364-
const BDP_LIMIT: usize = 1024 * 1024 * 16;
364+
const BDP_LIMIT: WindowSize = 1024 * 1024 * 16;
365365

366366
impl Bdp {
367367
fn calculate(&mut self, bytes: usize, rtt: Duration) -> Option<WindowSize> {
368368
// No need to do any math if we're at the limit.
369-
if self.bdp as usize == BDP_LIMIT {
369+
if self.bdp == BDP_LIMIT {
370370
self.stabilize_delay();
371371
return None;
372372
}
@@ -396,7 +396,9 @@ impl Bdp {
396396
// if the current `bytes` sample is at least 2/3 the previous
397397
// bdp, increase to double the current sample.
398398
if bytes >= self.bdp as usize * 2 / 3 {
399-
self.bdp = (bytes * 2).min(BDP_LIMIT) as WindowSize;
399+
self.bdp = WindowSize::try_from(bytes * 2)
400+
.unwrap_or(WindowSize::MAX)
401+
.min(BDP_LIMIT);
400402
trace!("BDP increased to {}", self.bdp);
401403

402404
self.stable_count = 0;

0 commit comments

Comments
 (0)