mediaway-decoder 0.1.2

Hardware-accelerated video/audio decoding (OS-native backends)
//! Pure timestamp range validation, shared by the wasm decode path.
//!
//! `web-sys`'s `EncodedVideoChunkInit::new_with_u8_array` takes an `i32` microsecond
//! timestamp (see [MDN `EncodedVideoChunk`
//! constructor](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk/EncodedVideoChunk)),
//! so `f64` inputs (this crate's public API unit for microseconds) must be range-checked
//! before the narrowing cast. No `web-sys`/`wasm-bindgen` types here so it compiles and is
//! testable on the host target too.

#![forbid(unsafe_code)]

/// Convert a microsecond timestamp to `i32` if it fits and is finite, else `None`.
#[allow(
    dead_code,
    reason = "only called from wasm32-only wasm.rs; exercised directly by host-side unit tests below"
)]
#[allow(
    clippy::redundant_pub_crate,
    reason = "explicit crate-only scope, even though the parent `timestamp` module is also private"
)]
#[must_use]
pub(crate) fn timestamp_us_to_i32(timestamp_us: f64) -> Option<i32> {
    if !timestamp_us.is_finite()
        || timestamp_us < f64::from(i32::MIN)
        || timestamp_us > f64::from(i32::MAX)
    {
        return None;
    }
    #[allow(
        clippy::cast_possible_truncation,
        reason = "bounds-checked against i32::MIN/MAX just above the cast"
    )]
    Some(timestamp_us as i32)
}

#[cfg(test)]
#[path = "timestamp_tests.rs"]
mod tests;