Skip to content

Commit 359036c

Browse files
committed
refactor: impl ReadReady for Console
1 parent e29eca8 commit 359036c

File tree

6 files changed

+61
-47
lines changed

6 files changed

+61
-47
lines changed

src/arch/aarch64/kernel/serial.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::arch::asm;
22

3-
use embedded_io::{ErrorType, Read, Write};
3+
use embedded_io::{ErrorType, Read, ReadReady, Write};
44

55
use crate::errno::Errno;
66

@@ -18,10 +18,6 @@ impl SerialDevice {
1818

1919
Self { addr: base as u32 }
2020
}
21-
22-
pub fn can_read(&self) -> bool {
23-
false
24-
}
2521
}
2622

2723
impl ErrorType for SerialDevice {
@@ -35,6 +31,12 @@ impl Read for SerialDevice {
3531
}
3632
}
3733

34+
impl ReadReady for SerialDevice {
35+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
36+
Ok(false)
37+
}
38+
}
39+
3840
impl Write for SerialDevice {
3941
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
4042
let port = core::ptr::with_exposed_provenance_mut::<u8>(self.addr as usize);

src/arch/riscv64/kernel/serial.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use embedded_io::{ErrorType, Read, Write};
1+
use embedded_io::{ErrorType, Read, ReadReady, Write};
22

33
use crate::errno::Errno;
44

@@ -8,10 +8,6 @@ impl SerialDevice {
88
pub fn new() -> Self {
99
Self {}
1010
}
11-
12-
pub fn can_read(&self) -> bool {
13-
false
14-
}
1511
}
1612

1713
impl ErrorType for SerialDevice {
@@ -25,6 +21,12 @@ impl Read for SerialDevice {
2521
}
2622
}
2723

24+
impl ReadReady for SerialDevice {
25+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
26+
Ok(false)
27+
}
28+
}
29+
2830
impl Write for SerialDevice {
2931
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
3032
for byte in buf {

src/arch/x86_64/kernel/serial.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::collections::VecDeque;
22
use alloc::vec::Vec;
33

4-
use embedded_io::{ErrorType, Read, Write};
4+
use embedded_io::{ErrorType, Read, ReadReady, Write};
55
use hermit_sync::{InterruptTicketMutex, Lazy};
66

77
#[cfg(feature = "pci")]
@@ -44,10 +44,6 @@ impl SerialDevice {
4444
pub fn new() -> Self {
4545
Self {}
4646
}
47-
48-
pub fn can_read(&self) -> bool {
49-
!UART_DEVICE.lock().buffer.is_empty()
50-
}
5147
}
5248

5349
impl ErrorType for SerialDevice {
@@ -68,6 +64,13 @@ impl Read for SerialDevice {
6864
}
6965
}
7066

67+
impl ReadReady for SerialDevice {
68+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
69+
let read_ready = !UART_DEVICE.lock().buffer.is_empty();
70+
Ok(read_ready)
71+
}
72+
}
73+
7174
impl Write for SerialDevice {
7275
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
7376
let mut guard = UART_DEVICE.lock();

src/console.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use core::{fmt, mem};
44

5-
use embedded_io::{ErrorType, Read, Write};
5+
use embedded_io::{ErrorType, Read, ReadReady, Write};
66
use heapless::Vec;
77
use hermit_sync::{InterruptTicketMutex, Lazy};
88

@@ -24,18 +24,6 @@ pub(crate) enum IoDevice {
2424
Virtio(VirtioUART),
2525
}
2626

27-
impl IoDevice {
28-
pub fn can_read(&self) -> bool {
29-
match self {
30-
#[cfg(not(target_arch = "riscv64"))]
31-
IoDevice::Uhyve(s) => s.can_read(),
32-
IoDevice::Uart(s) => s.can_read(),
33-
#[cfg(feature = "console")]
34-
IoDevice::Virtio(s) => s.can_read(),
35-
}
36-
}
37-
}
38-
3927
impl ErrorType for IoDevice {
4028
type Error = Errno;
4129
}
@@ -52,6 +40,18 @@ impl Read for IoDevice {
5240
}
5341
}
5442

43+
impl ReadReady for IoDevice {
44+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
45+
match self {
46+
#[cfg(not(target_arch = "riscv64"))]
47+
IoDevice::Uhyve(s) => s.read_ready(),
48+
IoDevice::Uart(s) => s.read_ready(),
49+
#[cfg(feature = "console")]
50+
IoDevice::Virtio(s) => s.read_ready(),
51+
}
52+
}
53+
}
54+
5555
impl Write for IoDevice {
5656
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
5757
match self {
@@ -85,10 +85,6 @@ impl UhyveSerial {
8585
pub const fn new() -> Self {
8686
Self {}
8787
}
88-
89-
pub fn can_read(&self) -> bool {
90-
false
91-
}
9288
}
9389

9490
#[cfg(not(target_arch = "riscv64"))]
@@ -104,6 +100,13 @@ impl Read for UhyveSerial {
104100
}
105101
}
106102

103+
#[cfg(not(target_arch = "riscv64"))]
104+
impl ReadReady for UhyveSerial {
105+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
106+
Ok(false)
107+
}
108+
}
109+
107110
#[cfg(not(target_arch = "riscv64"))]
108111
impl Write for UhyveSerial {
109112
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
@@ -129,10 +132,6 @@ impl Console {
129132
}
130133
}
131134

132-
pub fn can_read(&self) -> bool {
133-
self.device.can_read()
134-
}
135-
136135
#[cfg(feature = "console")]
137136
pub fn replace_device(&mut self, device: IoDevice) {
138137
self.device = device;
@@ -149,6 +148,12 @@ impl Read for Console {
149148
}
150149
}
151150

151+
impl ReadReady for Console {
152+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
153+
self.device.read_ready()
154+
}
155+
}
156+
152157
impl Write for Console {
153158
/// Writes a buffer to the console.
154159
/// The content is buffered until a newline is encountered or the internal buffer is full.

src/drivers/console/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cfg_if::cfg_if! {
1010

1111
use alloc::vec::Vec;
1212

13-
use embedded_io::{ErrorType, Read, Write};
13+
use embedded_io::{ErrorType, Read, ReadReady, Write};
1414
use smallvec::SmallVec;
1515
use virtio::FeatureBits;
1616
use virtio::console::Config;
@@ -68,14 +68,6 @@ impl VirtioUART {
6868
pub const fn new() -> Self {
6969
Self {}
7070
}
71-
72-
pub fn can_read(&self) -> bool {
73-
if let Some(drv) = get_console_driver() {
74-
drv.lock().has_packet()
75-
} else {
76-
false
77-
}
78-
}
7971
}
8072

8173
impl ErrorType for VirtioUART {
@@ -92,6 +84,16 @@ impl Read for VirtioUART {
9284
}
9385
}
9486

87+
impl ReadReady for VirtioUART {
88+
fn read_ready(&mut self) -> Result<bool, Self::Error> {
89+
if let Some(drv) = get_console_driver() {
90+
Ok(drv.lock().has_packet())
91+
} else {
92+
Ok(false)
93+
}
94+
}
95+
}
96+
9597
impl Write for VirtioUART {
9698
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
9799
if let Some(drv) = get_console_driver() {

src/fd/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::future;
33
use core::task::Poll;
44

55
use async_trait::async_trait;
6-
use embedded_io::{Read, Write};
6+
use embedded_io::{Read, ReadReady, Write};
77
use uhyve_interface::parameters::WriteParams;
88
use uhyve_interface::{GuestVirtAddr, Hypercall};
99

@@ -20,7 +20,7 @@ pub struct GenericStdin;
2020
#[async_trait]
2121
impl ObjectInterface for GenericStdin {
2222
async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> {
23-
let available = if CONSOLE.lock().can_read() {
23+
let available = if CONSOLE.lock().read_ready()? {
2424
PollEvent::POLLIN | PollEvent::POLLRDNORM | PollEvent::POLLRDBAND
2525
} else {
2626
PollEvent::empty()

0 commit comments

Comments
 (0)