Skip to content

Commit 9ee7881

Browse files
authored
Merge pull request #1915 from hermit-os/io-vec_deque
perf(serial): forward serial read to VecDeque impl
2 parents 8c8a0b0 + 44c0024 commit 9ee7881

File tree

4 files changed

+13
-27
lines changed

4 files changed

+13
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bitflags = "2"
120120
build-time = "0.1.3"
121121
cfg-if = "1"
122122
crossbeam-utils = { version = "0.8", default-features = false }
123-
embedded-io = "0.7"
123+
embedded-io = { version = "0.7", features = ["alloc"] }
124124
enum_dispatch = "0.3"
125125
fdt = { version = "0.1", features = ["pretty-printing"] }
126126
free-list = "0.3"

src/arch/aarch64/kernel/serial.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,13 @@ impl ErrorType for SerialDevice {
5959

6060
impl Read for SerialDevice {
6161
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
62-
let mut guard = UART_DEVICE.lock();
63-
64-
if guard.buffer.is_empty() {
65-
Ok(0)
66-
} else {
67-
let min = buf.len().min(guard.buffer.len());
68-
69-
for (dst, src) in buf[..min].iter_mut().zip(guard.buffer.drain(..min)) {
70-
*dst = src;
71-
}
72-
73-
Ok(min)
74-
}
62+
Ok(UART_DEVICE.lock().buffer.read(buf)?)
7563
}
7664
}
7765

7866
impl ReadReady for SerialDevice {
7967
fn read_ready(&mut self) -> Result<bool, Self::Error> {
80-
Ok(!UART_DEVICE.lock().buffer.is_empty())
68+
Ok(UART_DEVICE.lock().buffer.read_ready()?)
8169
}
8270
}
8371

src/arch/x86_64/kernel/serial.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use alloc::collections::VecDeque;
2-
use alloc::vec::Vec;
32

43
use embedded_io::{ErrorType, Read, ReadReady, Write};
54
use hermit_sync::{InterruptTicketMutex, Lazy};
@@ -52,22 +51,13 @@ impl ErrorType for SerialDevice {
5251

5352
impl Read for SerialDevice {
5453
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
55-
let mut guard = UART_DEVICE.lock();
56-
if guard.buffer.is_empty() {
57-
Ok(0)
58-
} else {
59-
let min = core::cmp::min(buf.len(), guard.buffer.len());
60-
let drained = guard.buffer.drain(..min).collect::<Vec<_>>();
61-
buf[..min].copy_from_slice(drained.as_slice());
62-
Ok(min)
63-
}
54+
Ok(UART_DEVICE.lock().buffer.read(buf)?)
6455
}
6556
}
6657

6758
impl ReadReady for SerialDevice {
6859
fn read_ready(&mut self) -> Result<bool, Self::Error> {
69-
let read_ready = !UART_DEVICE.lock().buffer.is_empty();
70-
Ok(read_ready)
60+
Ok(UART_DEVICE.lock().buffer.read_ready()?)
7161
}
7262
}
7363

src/errno.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! System error numbers.
22
3+
use core::convert::Infallible;
4+
35
use num_enum::{IntoPrimitive, TryFromPrimitive};
46
use thiserror::Error;
57

@@ -669,6 +671,12 @@ pub enum Errno {
669671
Hwpoison = 133,
670672
}
671673

674+
impl From<Infallible> for Errno {
675+
fn from(value: Infallible) -> Self {
676+
match value {}
677+
}
678+
}
679+
672680
/// Returns the pointer to `errno`.
673681
#[cfg(all(
674682
not(any(feature = "common-os", feature = "nostd")),

0 commit comments

Comments
 (0)