sheave_core::writers

Function write_extended_timestamp

Source
pub fn write_extended_timestamp<W: AsyncWrite>(
    writer: Pin<&mut W>,
    extended_timestamp: Duration,
) -> ExtendedTimestampWriter<'_, W>
Expand description

Writes an extended timestramp into streams.

ยงExamples

use std::{
    io::Result as IOResult,
    pin::{
        Pin,
        pin
    },
    time::Duration
};
use rand::random;
use sheave_core::writers::write_extended_timestamp;

#[tokio::main]
async fn main() -> IOResult<()> {
    let expected = Duration::from_millis(random::<u32>() as u64);
    let mut writer: Pin<&mut Vec<u8>> = pin!(Vec::new());
    write_extended_timestamp(writer.as_mut(), expected).await?;
    let mut written: [u8; 4] = [0; 4];
    written.copy_from_slice(&writer[..4]);
    let actual = Duration::from_millis(u32::from_be_bytes(written) as u64);
    assert_eq!(expected, actual);
    Ok(())
}