Repository: google/OpenSK
File: third_party/libtock-drivers/src/usb_ctap_hid.rs (Rust)

Code before:

/// Waits to receive a packet.
///
/// Returns None if the transaction timed out, else its status.
#[allow(clippy::let_and_return)]
pub fn recv_with_timeout(
    buf: &mut [u8; 64],
    timeout_delay: Duration<isize>,
) -> Option<SendOrRecvStatus> {
    #[cfg(feature = "verbose_usb")]
    writeln!(
        Console::new(),
        "Receiving packet with timeout of {}ms",
        timeout_delay.ms(),
    )
    .unwrap();

    let result = recv_with_timeout_detail(buf, timeout_delay);

    #[cfg(feature = "verbose_usb")]
    if let Some(SendOrRecvStatus::Received(interface)) = result {
        writeln!(
            Console::new(),
            "Received packet = {:02x?} on interface {:?}",
            buf as &[u8],
            interface as u8,
        )
        .unwrap();
    }

    result
}

/// Either sends or receives a packet within a given time.
///
/// Because USB transactions are initiated by the host, we don't decide whether an IN transaction
/// (send for us), an OUT transaction (receive for us), or no transaction at all will happen next.
///
/// - If an IN transaction happens first, the initial content of buf is sent to the host and the
/// Sent status is returned.
/// - If an OUT transaction happens first, the content of buf is replaced by the packet received
/// from the host and Received status is returned. In that case, the original content of buf is not
/// sent to the host, and it's up to the caller to retry sending or to handle the packet received
/// from the host.
/// If the timeout elapses, return None.
#[allow(clippy::let_and_return)]
pub fn send_or_recv_with_timeout(
    buf: &mut [u8; 64],
    timeout_delay: Duration<isize>,
    interface: UsbInterface,
) -> Option<SendOrRecvStatus> {

Review comment (bug):
Why `as u8` for `{:?}`? I see 2 options:
1. Keep as `UsbInterface` and debug-print with `{:?}`. This might increase the binary size.
2. Convert to `u8` and print with `{}`.

Code after:

/// Waits to receive a packet.
///
/// Returns None if the transaction timed out, else its status.
#[allow(clippy::let_and_return)]
pub fn recv_with_timeout(
    buf: &mut [u8; 64],
    timeout_delay: Duration<isize>,
) -> Option<SendOrRecvStatus> {
    #[cfg(feature = "verbose_usb")]
    writeln!(
        Console::new(),
        "Receiving packet with timeout of {}ms",
        timeout_delay.ms(),
    )
    .unwrap();

    let result = recv_with_timeout_detail(buf, timeout_delay);

    #[cfg(feature = "verbose_usb")]
    if let Some(SendOrRecvStatus::Received(interface)) = result {
        writeln!(
            Console::new(),
            "Received packet = {:02x?} on interface {}",
            buf as &[u8],
            interface as u8,
        )
        .unwrap();
    }

    result
}

/// Either sends or receives a packet within a given time.
///
/// Because USB transactions are initiated by the host, we don't decide whether an IN transaction
/// (send for us), an OUT transaction (receive for us), or no transaction at all will happen next.
///
/// - If an IN transaction happens first, the initial content of buf is sent to the host and the
/// Sent status is returned.
/// - If an OUT transaction happens first, the content of buf is replaced by the packet received
/// from the host and Received status is returned. In that case, the original content of buf is not
/// sent to the host, and it's up to the caller to retry sending or to handle the packet received
/// from the host.
/// If the timeout elapses, return None.
#[allow(clippy::let_and_return)]
pub fn send_or_recv_with_timeout(
    buf: &mut [u8; 64],
    timeout_delay: Duration<isize>,
    interface: UsbInterface,
) -> Option<SendOrRecvStatus> {