From 4d3d889f272856d4d43dc57521e70bf5a826f8b9 Mon Sep 17 00:00:00 2001 From: lashman Date: Mon, 21 Sep 2026 01:23:45 +0300 Subject: [PATCH] explain the remaining float conversions at their use sites --- src-tauri/src/audio.rs | 9 ++++++++- src-tauri/src/capture.rs | 12 ++++++++++++ src-tauri/src/encode.rs | 12 +++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/audio.rs b/src-tauri/src/audio.rs index 2b0b27a..255b907 100644 --- a/src-tauri/src/audio.rs +++ b/src-tauri/src/audio.rs @@ -326,7 +326,14 @@ fn write_next_packet( let i32_slice = unsafe { std::slice::from_raw_parts(data_ptr as *const i32, samples) }; for &sample in i32_slice { - let f = sample as f32 / 2147483648.0; + // PCM 32-bit samples are normalised by 2^31 into -1.0..1.0, + // where f32 has more than enough precision; the cast is the + // standard full-scale conversion, not an approximation. + #[allow( + clippy::cast_precision_loss, + reason = "full-scale i32 PCM divided by 2^31 lands in -1.0..1.0" + )] + let f = sample as f32 / 2_147_483_648.0; writer .write_all(&f.to_le_bytes()) .context("writing f32 sample")?; diff --git a/src-tauri/src/capture.rs b/src-tauri/src/capture.rs index 90e155a..546608a 100644 --- a/src-tauri/src/capture.rs +++ b/src-tauri/src/capture.rs @@ -345,6 +345,12 @@ impl TonemapPipeline { let vb = [Some(self.vertex_buffer.clone())]; let strides = [16u32]; let offsets = [0u32]; + // Capture dimensions are at most 4K (well under f32's 2^24 exact-integer + // limit), so the cast is lossless for every value this can hold. + #[allow( + clippy::cast_precision_loss, + reason = "capture width/height are bounded by MAX_ENCODE_WIDTH (3840), exactly representable in f32" + )] let viewport = [D3D11_VIEWPORT { TopLeftX: 0.0, TopLeftY: 0.0, @@ -858,6 +864,12 @@ fn capture_timing(frame_count: u64, elapsed_secs: f64, pipe_fps: u32) -> Option< if frame_count == 0 || elapsed_secs <= 0.0 { return None; } + // A frame count high enough to lose f64 precision would be ~2^53 frames, + // which is centuries of capture; f64 handles this ratio exactly enough. + #[allow( + clippy::cast_precision_loss, + reason = "frame counts that could lose f64 precision are unreachable in a recording session" + )] let actual_fps = frame_count as f64 / elapsed_secs; Some((actual_fps, f64::from(pipe_fps) / actual_fps)) } diff --git a/src-tauri/src/encode.rs b/src-tauri/src/encode.rs index 2d485f4..e3d2f26 100644 --- a/src-tauri/src/encode.rs +++ b/src-tauri/src/encode.rs @@ -351,9 +351,15 @@ pub(crate) fn codec_availability(ffmpeg: &Path) -> CodecAvailability { } fn video_bitrate_kbps(req: &TranscodeRequest) -> Result { + // Byte and bitrate budgets are far below 2^53, so f64 represents them + // exactly; these casts are the only way to get u64 into the f64 maths. + #[allow( + clippy::cast_precision_loss, + reason = "target sizes and bitrates are many orders of magnitude below f64's exact-integer range" + )] let target_bits = (req.target_bytes as f64) * 8.0 * 0.95; let audio_bits = if req.keep_audio { - AUDIO_BITRATE_KBPS as f64 * 1000.0 * req.duration_secs + (AUDIO_BITRATE_KBPS as f64) * 1000.0 * req.duration_secs } else { 0.0 }; @@ -1417,7 +1423,7 @@ mod tests { #[test] fn click_filters_capped_at_max() { let clicks: Vec = (0..100) - .map(|i| Click { time_secs: i as f64, x: i, y: i, button: crate::clicks::MouseButton::Left }) + .map(|i| Click { time_secs: f64::from(i), x: i, y: i, button: crate::clicks::MouseButton::Left }) .collect(); let filter = click_filters(&clicks, 1920, 1080).unwrap(); let count = filter.matches("drawbox").count(); @@ -1484,7 +1490,7 @@ mod tests { #[test] fn gif_color_factor_matches_comment_values() { // comment claims: 256=1.0x, 128=~0.71x, 64=0.5x, 32=~0.35x, 16=0.25x - let cf = |colors: u16| (colors as f64 / 256.0).sqrt().clamp(0.15, 1.0); + let cf = |colors: u16| (f64::from(colors) / 256.0).sqrt().clamp(0.15, 1.0); assert!((cf(256) - 1.0).abs() < 1e-9); assert!((cf(128) - 2f64.sqrt() / 2.0).abs() < 1e-9); // ~0.707 assert!((cf(64) - 0.5).abs() < 1e-9);