Skip to content

Commit 0be7381

Browse files
committed
style(lib): Address cast-possible-truncation clippy findings.
1 parent c9f0165 commit 0be7381

3 files changed

Lines changed: 31 additions & 19 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: 26 additions & 15 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")]
@@ -242,8 +247,15 @@ impl Encoder {
242247
!self.is_last
243248
}
244249
Ordering::Greater => {
245-
dst.buffer(msg.take(remaining as usize));
246-
!self.is_last
250+
match usize::try_from(remaining) {
251+
Ok(limit) => {
252+
dst.buffer(msg.take(limit));
253+
!self.is_last
254+
}
255+
// if len > remaining (this arm), then remaining must be < usize::MAX transitively.
256+
// usize::MAX > len > remaining -> remaining cannot fail to cast.
257+
_ => unreachable!("it is impossible that remaining > usize::MAX")
258+
}
247259
}
248260
Ordering::Less => {
249261
dst.buffer(msg);
@@ -328,11 +340,7 @@ where
328340
}
329341
}
330342

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;
343+
const USIZE_BYTES: usize = std::mem::size_of::<usize>();
336344

337345
// each byte will become 2 hex
338346
const CHUNK_SIZE_MAX_BYTES: usize = USIZE_BYTES * 2;
@@ -369,6 +377,7 @@ impl Buf for ChunkSize {
369377
}
370378

371379
#[inline]
380+
#[allow(clippy::cast_possible_truncation)]
372381
fn advance(&mut self, cnt: usize) {
373382
assert!(cnt <= self.remaining());
374383
self.pos += cnt as u8; // just asserted cnt fits in u8
@@ -385,12 +394,14 @@ impl fmt::Debug for ChunkSize {
385394
}
386395

387396
impl fmt::Write for ChunkSize {
397+
#[allow(clippy::cast_possible_truncation, reason="bytes is structurally always less than u8::MAX")]
388398
fn write_str(&mut self, num: &str) -> fmt::Result {
389399
use std::io::Write;
390400
(&mut self.bytes[self.len.into()..])
391401
.write_all(num.as_bytes())
392402
.expect("&mut [u8].write() cannot error");
393-
self.len += num.len() as u8; // safe because bytes is never bigger than 256
403+
debug_assert!(u8::try_from(num.len()).is_ok());
404+
self.len += num.len() as u8; // safe because bytes is never bigger than 255
394405
Ok(())
395406
}
396407
}

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)