Skip to content

Commit 8204bb2

Browse files
authored
Additional error data in Result + some API cleanup and clarification (#78)
* Rename the module holding the Result more logically * Rewrite Completion as a struct * Add support for error payloads * Clarify result module documentation * Allow error data to depend on the error status * Clean up usage of Result - Replace Result<()> with Result - Use new facility for cleanly reporting error data - Fix a few incorrect APIs (bad lifetime, error/warning confusion) - Drop some usage of core::result::Result, qualify the rest better * Fix lifetime names * Add warning support to test_string * Apply rustfmt * Plural is probably best after all * Move background color sanity check to an assertion
1 parent bfd2f4f commit 8204bb2

File tree

30 files changed

+460
-337
lines changed

30 files changed

+460
-337
lines changed

src/data_types/strs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct CStr8([Char8]);
2424

2525
impl CStr8 {
2626
/// Wraps a raw UEFI string with a safe C string wrapper
27-
pub unsafe fn from_ptr<'a>(ptr: *const Char8) -> &'a Self {
27+
pub unsafe fn from_ptr<'ptr>(ptr: *const Char8) -> &'ptr Self {
2828
let mut len = 0;
2929
while *ptr.add(len) != NUL_8 {
3030
len += 1
@@ -77,7 +77,7 @@ pub struct CStr16([Char16]);
7777

7878
impl CStr16 {
7979
/// Wraps a raw UEFI string with a safe C string wrapper
80-
pub unsafe fn from_ptr<'a>(ptr: *const Char16) -> &'a Self {
80+
pub unsafe fn from_ptr<'ptr>(ptr: *const Char16) -> &'ptr Self {
8181
let mut len = 0;
8282
while *ptr.add(len) != NUL_16 {
8383
len += 1

src/error/mod.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub mod data_types;
3636
pub(crate) use self::data_types::{unsafe_guid, Identify};
3737
pub use self::data_types::{CStr16, CStr8, Char16, Char8, Event, Guid, Handle};
3838

39-
mod error;
40-
pub use self::error::{Completion, Result, ResultExt, Status};
39+
mod result;
40+
pub use self::result::{Completion, Result, ResultExt, ResultExt2, Status};
4141

4242
pub mod table;
4343

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! This includes the system table types, `Status` codes, etc.
44
5-
pub use crate::{ResultExt, Status};
5+
pub use crate::{ResultExt, ResultExt2, Status};
66

77
// Import the basic table types.
88
pub use crate::table::boot::BootServices;

src/proto/console/gop.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'boot> GraphicsOutput<'boot> {
6868
let mut info_sz = 0;
6969
let mut info = ptr::null();
7070

71-
(self.query_mode)(self, index, &mut info_sz, &mut info).into_with(|| {
71+
(self.query_mode)(self, index, &mut info_sz, &mut info).into_with_val(|| {
7272
let info = unsafe { *info };
7373
Mode {
7474
index,
@@ -79,7 +79,7 @@ impl<'boot> GraphicsOutput<'boot> {
7979
}
8080

8181
/// Returns information about all available graphics modes.
82-
pub fn modes<'a>(&'a self) -> impl Iterator<Item = Completion<Mode>> + 'a {
82+
pub fn modes<'gop>(&'gop self) -> impl Iterator<Item = Completion<Mode>> + 'gop {
8383
ModeIter {
8484
gop: self,
8585
current: 0,
@@ -91,14 +91,14 @@ impl<'boot> GraphicsOutput<'boot> {
9191
/// of the output display to black.
9292
///
9393
/// This function will invalidate the current framebuffer.
94-
pub fn set_mode(&mut self, mode: &Mode) -> Result<()> {
94+
pub fn set_mode(&mut self, mode: &Mode) -> Result {
9595
(self.set_mode)(self, mode.index).into()
9696
}
9797

9898
/// Performs a blt (block transfer) operation on the frame buffer.
9999
///
100100
/// Every operation requires different parameters.
101-
pub fn blt(&mut self, op: BltOp) -> Result<()> {
101+
pub fn blt(&mut self, op: BltOp) -> Result {
102102
// Demultiplex the operation type.
103103
unsafe {
104104
match op {
@@ -286,13 +286,13 @@ impl<'boot> GraphicsOutput<'boot> {
286286
}
287287

288288
#[repr(C)]
289-
struct ModeData<'a> {
289+
struct ModeData<'info> {
290290
// Number of modes which the GOP supports.
291291
max_mode: u32,
292292
// Current mode.
293293
mode: u32,
294294
// Information about the current mode.
295-
info: &'a ModeInfo,
295+
info: &'info ModeInfo,
296296
// Size of the above structure.
297297
info_sz: usize,
298298
// Physical address of the frame buffer.
@@ -401,13 +401,13 @@ impl ModeInfo {
401401
}
402402

403403
/// Iterator for graphics modes.
404-
struct ModeIter<'a> {
405-
gop: &'a GraphicsOutput<'a>,
404+
struct ModeIter<'gop> {
405+
gop: &'gop GraphicsOutput<'gop>,
406406
current: u32,
407407
max: u32,
408408
}
409409

410-
impl<'a> Iterator for ModeIter<'a> {
410+
impl<'gop> Iterator for ModeIter<'gop> {
411411
type Item = Completion<Mode>;
412412

413413
fn next(&mut self) -> Option<Self::Item> {
@@ -479,7 +479,7 @@ pub enum BltRegion {
479479

480480
/// Blit operation to perform.
481481
#[derive(Debug)]
482-
pub enum BltOp<'a> {
482+
pub enum BltOp<'buf> {
483483
/// Fills a rectangle of video display with a pixel color.
484484
VideoFill {
485485
/// The color to fill with.
@@ -492,7 +492,7 @@ pub enum BltOp<'a> {
492492
/// Reads data from the video display to the buffer.
493493
VideoToBltBuffer {
494494
/// Buffer into which to copy data.
495-
buffer: &'a mut [BltPixel],
495+
buffer: &'buf mut [BltPixel],
496496
/// Coordinates of the source rectangle, in the frame buffer.
497497
src: (usize, usize),
498498
/// Location of the destination rectangle in the user-provided buffer
@@ -504,7 +504,7 @@ pub enum BltOp<'a> {
504504
/// Delta must be the stride (count of bytes in a row) of the buffer.
505505
BufferToVideo {
506506
/// Buffer from which to copy data.
507-
buffer: &'a [BltPixel],
507+
buffer: &'buf [BltPixel],
508508
/// Location of the source rectangle in the user-provided buffer.
509509
src: BltRegion,
510510
/// Coordinates of the destination rectangle, in the frame buffer.
@@ -525,13 +525,13 @@ pub enum BltOp<'a> {
525525
}
526526

527527
/// Direct access to a memory-mapped frame buffer
528-
pub struct FrameBuffer<'a> {
528+
pub struct FrameBuffer<'gop> {
529529
base: *mut u8,
530530
size: usize,
531-
_lifetime: PhantomData<&'a mut u8>,
531+
_lifetime: PhantomData<&'gop mut u8>,
532532
}
533533

534-
impl<'a> FrameBuffer<'a> {
534+
impl<'gop> FrameBuffer<'gop> {
535535
/// Access the raw framebuffer pointer
536536
///
537537
/// To use this pointer safely and correctly, you must...

src/proto/console/pointer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'boot> Pointer<'boot> {
2424
/// # Errors
2525
///
2626
/// - `DeviceError` if the device is malfunctioning and cannot be reset.
27-
pub fn reset(&mut self, extended_verification: bool) -> Result<()> {
27+
pub fn reset(&mut self, extended_verification: bool) -> Result {
2828
(self.reset)(self, extended_verification).into()
2929
}
3030

@@ -41,7 +41,7 @@ impl<'boot> Pointer<'boot> {
4141

4242
match (self.get_state)(self, &mut pointer_state) {
4343
Status::NOT_READY => Ok(None.into()),
44-
other => other.into_with(|| Some(pointer_state)),
44+
other => other.into_with_val(|| Some(pointer_state)),
4545
}
4646
}
4747

src/proto/console/serial.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Abstraction over byte stream devices, also known as serial I/O devices.
22
33
use crate::proto::Protocol;
4-
use crate::{unsafe_guid, Completion, Result, Status};
4+
use crate::{unsafe_guid, Result, Status};
55
use bitflags::bitflags;
66

77
/// Provides access to a serial I/O device.
@@ -37,7 +37,7 @@ pub struct Serial<'boot> {
3737

3838
impl<'boot> Serial<'boot> {
3939
/// Reset the device.
40-
pub fn reset(&mut self) -> Result<()> {
40+
pub fn reset(&mut self) -> Result {
4141
(self.reset)(self).into()
4242
}
4343

@@ -59,7 +59,7 @@ impl<'boot> Serial<'boot> {
5959
/// - if either `baud_rate` or `receive_fifo_depth` is less than
6060
/// the device's minimum, an error will be returned;
6161
/// this value will be rounded down to the nearest value supported by the device;
62-
pub fn set_attributes(&mut self, mode: &IoMode) -> Result<()> {
62+
pub fn set_attributes(&mut self, mode: &IoMode) -> Result {
6363
(self.set_attributes)(
6464
self,
6565
mode.baud_rate,
@@ -75,47 +75,41 @@ impl<'boot> Serial<'boot> {
7575
/// Retrieve the device's current control bits.
7676
pub fn get_control_bits(&self) -> Result<ControlBits> {
7777
let mut bits = ControlBits::empty();
78-
(self.get_control_bits)(self, &mut bits).into_with(|| bits)
78+
(self.get_control_bits)(self, &mut bits).into_with_val(|| bits)
7979
}
8080

8181
/// Sets the device's new control bits.
8282
///
8383
/// Not all bits can be modified with this function. A mask of the allowed
8484
/// bits is stored in the [`ControlBits::SETTABLE`] constant.
85-
pub fn set_control_bits(&mut self, bits: ControlBits) -> Result<()> {
85+
pub fn set_control_bits(&mut self, bits: ControlBits) -> Result {
8686
(self.set_control_bits)(self, bits).into()
8787
}
8888

8989
/// Reads data from this device.
9090
///
91-
/// Returns the number of bytes actually read from the device.
92-
/// In the case of a timeout or buffer overrun, this number will be smaller
93-
/// than the buffer's size.
94-
///
95-
/// Unlike UEFI, we handle timeouts as warnings, not errors
96-
pub fn read(&mut self, data: &mut [u8]) -> Result<usize> {
91+
/// This operation will block until the buffer has been filled with data or
92+
/// an error occurs. In the latter case, the error will indicate how many
93+
/// bytes were actually read from the device.
94+
pub fn read(&mut self, data: &mut [u8]) -> Result<(), usize> {
9795
let mut buffer_size = data.len();
98-
99-
match unsafe { (self.read)(self, &mut buffer_size, data.as_mut_ptr()) } {
100-
s @ Status::TIMEOUT => Ok(Completion::Warning(buffer_size, s)),
101-
other => other.into_with(|| buffer_size),
102-
}
96+
unsafe { (self.read)(self, &mut buffer_size, data.as_mut_ptr()) }.into_with(
97+
|| debug_assert_eq!(buffer_size, data.len()),
98+
|_| buffer_size,
99+
)
103100
}
104101

105102
/// Writes data to this device.
106103
///
107-
/// Returns the number of bytes actually written to the device.
108-
/// In the case of a timeout, this number will be smaller than
109-
/// the buffer's size.
110-
///
111-
/// Unlike UEFI, we handle timeouts as warnings, not errors
112-
pub fn write(&mut self, data: &[u8]) -> Result<usize> {
104+
/// This operation will block until the data has been fully written or an
105+
/// error occurs. In the latter case, the error will indicate how many bytes
106+
/// were actually written to the device.
107+
pub fn write(&mut self, data: &[u8]) -> Result<(), usize> {
113108
let mut buffer_size = data.len();
114-
115-
match unsafe { (self.write)(self, &mut buffer_size, data.as_ptr()) } {
116-
s @ Status::TIMEOUT => Ok(Completion::Warning(buffer_size, s)),
117-
other => other.into_with(|| buffer_size),
118-
}
109+
unsafe { (self.write)(self, &mut buffer_size, data.as_ptr()) }.into_with(
110+
|| debug_assert_eq!(buffer_size, data.len()),
111+
|_| buffer_size,
112+
)
119113
}
120114
}
121115

src/proto/console/text/input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Input {
2121
/// # Errors
2222
///
2323
/// - `DeviceError` if the device is malfunctioning and cannot be reset.
24-
pub fn reset(&mut self, extended_verification: bool) -> Result<()> {
24+
pub fn reset(&mut self, extended_verification: bool) -> Result {
2525
(self.reset)(self, extended_verification).into()
2626
}
2727

@@ -38,7 +38,7 @@ impl Input {
3838

3939
match (self.read_key_stroke)(self, &mut key) {
4040
Status::NOT_READY => Ok(None.into()),
41-
other => other.into_with(|| Some(key.into())),
41+
other => other.into_with_val(|| Some(key.into())),
4242
}
4343
}
4444

0 commit comments

Comments
 (0)