opentitanlib/io/console/
ext.rs1use std::time::Duration;
6
7use anyhow::Result;
8
9use super::ConsoleDevice;
10
11pub trait ConsoleExt {
13 fn read(&self, buf: &mut [u8]) -> Result<usize>;
16
17 fn read_timeout(&self, buf: &mut [u8], timeout: Duration) -> Result<usize>;
21}
22
23impl<T: ConsoleDevice + ?Sized> ConsoleExt for T {
24 fn read(&self, buf: &mut [u8]) -> Result<usize> {
25 crate::util::runtime::block_on(std::future::poll_fn(|cx| self.poll_read(cx, buf)))
26 }
27
28 fn read_timeout(&self, buf: &mut [u8], timeout: Duration) -> Result<usize> {
29 crate::util::runtime::block_on(async {
30 tokio::time::timeout(timeout, std::future::poll_fn(|cx| self.poll_read(cx, buf))).await
31 })
32 .unwrap_or(Ok(0))
33 }
34}