Skip to content

Commit 6882a5e

Browse files
committed
documentation and code improvements for Status, Error, and read()
1 parent 51245a6 commit 6882a5e

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

uefi/src/proto/media/file/dir.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Directory {
2020
Self(RegularFile::new(handle))
2121
}
2222

23-
/// Read the next directory entry
23+
/// Read the next directory entry.
2424
///
2525
/// Try to read the next directory entry into `buffer`. If the buffer is too small, report the
2626
/// required buffer size as part of the error. If there are no more directory entries, return
@@ -47,11 +47,13 @@ impl Directory {
4747
FileInfo::assert_aligned(buffer);
4848

4949
// Read the directory entry into the aligned storage
50-
self.0.read(buffer).map(|size| {
51-
if size != 0 {
52-
unsafe { Some(FileInfo::from_uefi(buffer.as_mut_ptr().cast::<c_void>())) }
53-
} else {
50+
self.0.read(buffer).map(|read_bytes| {
51+
// 0 read bytes signals that the last directory entry was read
52+
let last_directory_entry_read = read_bytes == 0;
53+
if last_directory_entry_read {
5454
None
55+
} else {
56+
unsafe { Some(FileInfo::from_uefi(buffer.as_mut_ptr().cast::<c_void>())) }
5557
}
5658
})
5759
}

uefi/src/proto/media/file/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ pub(super) struct FileImpl {
332332
) -> Status,
333333
close: extern "efiapi" fn(this: &mut FileImpl) -> Status,
334334
delete: extern "efiapi" fn(this: &mut FileImpl) -> Status,
335+
/// # Read from Regular Files
336+
/// If `self` is not a directory, the function reads the requested number of bytes from the file
337+
/// at the file’s current position and returns them in `buffer`. If the read goes beyond the end
338+
/// of the file, the read length is truncated to the end of the file. The file’s current
339+
/// position is increased by the number of bytes returned.
340+
///
341+
/// # Read from Directory
342+
/// If `self` is a directory, the function reads the directory entry at the file’s current
343+
/// position and returns the entry in `buffer`. If the `buffer` is not large enough to hold the
344+
/// current directory entry, then `EFI_BUFFER_TOO_SMALL` is returned and the current file
345+
/// position is not updated. `buffer_size` is set to be the size of the buffer needed to read
346+
/// the entry. On success, the current position is updated to the next directory entry. If there
347+
/// are no more directory entries, the read returns a zero-length buffer.
335348
read: unsafe extern "efiapi" fn(
336349
this: &mut FileImpl,
337350
buffer_size: &mut usize,

uefi/src/proto/media/file/regular.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl RegularFile {
2121
Self(handle)
2222
}
2323

24-
/// Read data from file
24+
/// Read data from file.
2525
///
2626
/// Try to read as much as possible into `buffer`. Returns the number of bytes that were
2727
/// actually read.
@@ -38,10 +38,15 @@ impl RegularFile {
3838
/// and the required buffer size is provided as output.
3939
pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Option<usize>> {
4040
let mut buffer_size = buffer.len();
41-
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) }.into_with(
41+
let status =
42+
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) };
43+
44+
status.into_with(
4245
|| buffer_size,
4346
|s| {
4447
if s == Status::BUFFER_TOO_SMALL {
48+
// `buffer_size` was updated to the required buffer size by the underlying read
49+
// function.
4550
Some(buffer_size)
4651
} else {
4752
None

uefi/src/result/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub use self::status::Status;
1414
/// Almost all UEFI operations provide a status code as an output which
1515
/// indicates either success, a warning, or an error. This type alias maps
1616
/// [`Status::SUCCESS`] to the `Ok` variant (with optional `Output` data), and
17-
/// maps both warning and error statuses to the `Err` variant (with optional
18-
/// `ErrData`).
17+
/// maps both warning and error statuses to the `Err` variant of type [`Error`],
18+
/// which may carry optional inner `ErrData`.
1919
///
2020
/// Warnings are treated as errors by default because they generally indicate
2121
/// an abnormal situation.

uefi/src/result/status.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ newtype_enum! {
1212
/// enum, as injecting an unknown value in a Rust enum is undefined behaviour.
1313
///
1414
/// For lack of a better option, we therefore model them as a newtype of usize.
15+
///
16+
/// For a convenient integration into the Rust ecosystem, there are multiple
17+
/// factory methods to convert a Status into a [`uefi::result::Result`]:
18+
/// - [`Status::into_with`]
19+
/// - [`Status::into_with_val`]
20+
/// - [`Status::into_with_err`]
1521
#[must_use]
1622
pub enum Status: usize => {
1723
/// The operation completed successfully.
@@ -122,7 +128,10 @@ impl Status {
122128
self.0 & ERROR_BIT != 0
123129
}
124130

125-
/// Converts this status code into a result with a given value.
131+
/// Converts this status code into a [`uefi::result::Result`] with a given `Ok` value.
132+
///
133+
/// If the status does not indicate success, the status representing the specific error
134+
/// code is embedded into the `Err` variant of type [`uefi::result::Error`].
126135
#[inline]
127136
pub fn into_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()> {
128137
if self.is_success() {
@@ -132,7 +141,10 @@ impl Status {
132141
}
133142
}
134143

135-
/// Converts this status code into a result with a given error payload
144+
/// Converts this status code into a [`uefi::result::Result`] with a given `Err` payload.
145+
///
146+
/// If the status does not indicate success, the status representing the specific error
147+
/// code is embedded into the `Err` variant of type [`uefi::result::Error`].
136148
#[inline]
137149
pub fn into_with_err<ErrData: Debug>(
138150
self,
@@ -145,7 +157,10 @@ impl Status {
145157
}
146158
}
147159

148-
/// Convert this status code into a result with a given value and error payload
160+
/// Convert this status code into a result with a given `Ok` value and `Err` payload.
161+
///
162+
/// If the status does not indicate success, the status representing the specific error
163+
/// code is embedded into the `Err` variant of type [`uefi::result::Error`].
149164
#[inline]
150165
pub fn into_with<T, ErrData: Debug>(
151166
self,

0 commit comments

Comments
 (0)