Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions uefi-services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#![no_std]
#![feature(alloc_error_handler)]
#![feature(abi_efiapi)]
#![deny(clippy::must_use_candidate)]

extern crate log;
// Core types.
Expand Down Expand Up @@ -64,6 +65,7 @@ static mut LOGGER: Option<uefi::logger::Logger> = None;
/// `init` must have been called first by the UEFI app.
///
/// The returned pointer is only valid until boot services are exited.
#[must_use]
pub fn system_table() -> NonNull<SystemTable<Boot>> {
unsafe {
let table_ref = SYSTEM_TABLE
Expand Down
2 changes: 2 additions & 0 deletions uefi/src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub trait Align {
/// Calculate the offset from `val` necessary to make it aligned,
/// rounding up. For example, if `val` is 1 and the alignment is 8,
/// this will return 7. Returns 0 if `val == 0`.
#[must_use]
fn offset_up_to_alignment(val: usize) -> usize {
assert!(Self::alignment() != 0);
let r = val % Self::alignment();
Expand All @@ -84,6 +85,7 @@ pub trait Align {
}

/// Round `val` up so that it is aligned.
#[must_use]
fn round_up_to_alignment(val: usize) -> usize {
val + Self::offset_up_to_alignment(val)
}
Expand Down
13 changes: 13 additions & 0 deletions uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl CStr8 {
/// The function will start accessing memory from `ptr` until the first
/// null byte. It's the callers responsibility to ensure `ptr` points to
/// a valid null-terminated string in accessible memory.
#[must_use]
pub unsafe fn from_ptr<'ptr>(ptr: *const Char8) -> &'ptr Self {
let mut len = 0;
while *ptr.add(len) != NUL_8 {
Expand Down Expand Up @@ -106,22 +107,26 @@ impl CStr8 {
///
/// It's the callers responsibility to ensure chars is a valid Latin-1
/// null-terminated string, with no interior null bytes.
#[must_use]
pub const unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
&*(chars as *const [u8] as *const Self)
}

/// Returns the inner pointer to this CStr8.
#[must_use]
pub const fn as_ptr(&self) -> *const Char8 {
self.0.as_ptr()
}

/// Converts this CStr8 to a slice of bytes without the terminating null byte.
#[must_use]
pub fn to_bytes(&self) -> &[u8] {
let chars = self.to_bytes_with_nul();
&chars[..chars.len() - 1]
}

/// Converts this CStr8 to a slice of bytes containing the trailing null byte.
#[must_use]
pub const fn to_bytes_with_nul(&self) -> &[u8] {
unsafe { &*(&self.0 as *const [Char8] as *const [u8]) }
}
Expand Down Expand Up @@ -189,6 +194,7 @@ impl CStr16 {
/// The function will start accessing memory from `ptr` until the first
/// null byte. It's the callers responsibility to ensure `ptr` points to
/// a valid string, in accessible memory.
#[must_use]
pub unsafe fn from_ptr<'ptr>(ptr: *const Char16) -> &'ptr Self {
let mut len = 0;
while *ptr.add(len) != NUL_16 {
Expand Down Expand Up @@ -227,6 +233,7 @@ impl CStr16 {
///
/// It's the callers responsibility to ensure chars is a valid UCS-2
/// null-terminated string, with no interior null bytes.
#[must_use]
pub const unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
&*(codes as *const [u16] as *const Self)
}
Expand Down Expand Up @@ -303,27 +310,32 @@ impl CStr16 {
}

/// Returns the inner pointer to this C string
#[must_use]
pub const fn as_ptr(&self) -> *const Char16 {
self.0.as_ptr()
}

/// Get the underlying [`Char16`] slice, including the trailing null.
#[must_use]
pub const fn as_slice_with_nul(&self) -> &[Char16] {
&self.0
}

/// Converts this C string to a u16 slice
#[must_use]
pub fn to_u16_slice(&self) -> &[u16] {
let chars = self.to_u16_slice_with_nul();
&chars[..chars.len() - 1]
}

/// Converts this C string to a u16 slice containing the trailing 0 char
#[must_use]
pub const fn to_u16_slice_with_nul(&self) -> &[u16] {
unsafe { &*(&self.0 as *const [Char16] as *const [u16]) }
}

/// Returns an iterator over this C string
#[must_use]
pub const fn iter(&self) -> CStr16Iter {
CStr16Iter {
inner: self,
Expand All @@ -332,6 +344,7 @@ impl CStr16 {
}

/// Get the number of bytes in the string (including the trailing null character).
#[must_use]
pub const fn num_bytes(&self) -> usize {
self.0.len() * 2
}
Expand Down
5 changes: 5 additions & 0 deletions uefi/src/data_types/unaligned_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
}

/// Returns true if the slice has a length of 0.
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}

/// Returns the number of elements in the slice.
#[must_use]
pub const fn len(&self) -> usize {
self.len
}

/// Returns the element at `index`, or `None` if the `index` is out
/// of bounds.
#[must_use]
pub fn get(&self, index: usize) -> Option<T> {
if index < self.len {
Some(unsafe { self.data.add(index).read_unaligned() })
Expand All @@ -58,6 +61,7 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
/// Returns an iterator over the slice.
///
/// The iterator yields all items from start to end.
#[must_use]
pub const fn iter(&'a self) -> UnalignedSliceIter<'a, T> {
UnalignedSliceIter {
slice: self,
Expand Down Expand Up @@ -111,6 +115,7 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {

/// Copies `self` into a new `Vec`.
#[cfg(feature = "alloc")]
#[must_use]
pub fn to_vec(&self) -> Vec<T> {
let len = self.len();
let mut v = Vec::with_capacity(len);
Expand Down
1 change: 1 addition & 0 deletions uefi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
// Enable some additional warnings and lints.
#![warn(clippy::ptr_as_ptr, missing_docs, unused)]
#![deny(clippy::all)]
#![deny(clippy::must_use_candidate)]

// Enable once we use vec![] or similar
// #[cfg_attr(feature = "alloc", macro_use)]
Expand Down
12 changes: 12 additions & 0 deletions uefi/src/proto/console/gop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl<'boot> GraphicsOutput<'boot> {
}

/// Returns information about all available graphics modes.
#[must_use]
pub fn modes(&'_ self) -> impl ExactSizeIterator<Item = Mode> + '_ {
ModeIter {
gop: self,
Expand Down Expand Up @@ -294,6 +295,7 @@ impl<'boot> GraphicsOutput<'boot> {
}

/// Returns the frame buffer information for the current mode.
#[must_use]
pub const fn current_mode_info(&self) -> ModeInfo {
*self.mode.info
}
Expand Down Expand Up @@ -377,11 +379,13 @@ impl Mode {
/// The size of the info structure in bytes.
///
/// Newer versions of the spec might add extra information, in a backwards compatible way.
#[must_use]
pub const fn info_size(&self) -> usize {
self.info_sz
}

/// Returns a reference to the mode info structure.
#[must_use]
pub const fn info(&self) -> &ModeInfo {
&self.info
}
Expand All @@ -404,16 +408,19 @@ impl ModeInfo {
/// Returns the (horizontal, vertical) resolution.
///
/// On desktop monitors, this usually means (width, height).
#[must_use]
pub const fn resolution(&self) -> (usize, usize) {
(self.hor_res as usize, self.ver_res as usize)
}

/// Returns the format of the frame buffer.
#[must_use]
pub const fn pixel_format(&self) -> PixelFormat {
self.format
}

/// Returns the bitmask of the custom pixel format, if available.
#[must_use]
pub const fn pixel_bitmask(&self) -> Option<PixelBitmask> {
match self.format {
PixelFormat::Bitmask => Some(self.mask),
Expand All @@ -425,6 +432,7 @@ impl ModeInfo {
///
/// Due to performance reasons, the stride might not be equal to the width,
/// instead the stride might be bigger for better alignment.
#[must_use]
pub const fn stride(&self) -> usize {
self.stride as usize
}
Expand Down Expand Up @@ -475,6 +483,7 @@ pub struct BltPixel {

impl BltPixel {
/// Create a new pixel from RGB values.
#[must_use]
pub const fn new(red: u8, green: u8, blue: u8) -> Self {
Self {
red,
Expand Down Expand Up @@ -582,6 +591,7 @@ impl<'gop> FrameBuffer<'gop> {
}

/// Query the framebuffer size in bytes
#[must_use]
pub const fn size(&self) -> usize {
self.size
}
Expand All @@ -607,6 +617,7 @@ impl<'gop> FrameBuffer<'gop> {
/// - You must honor the pixel format and stride specified by the mode info
/// - There is no bound checking on memory accesses in release mode
#[inline]
#[must_use]
pub unsafe fn read_byte(&self, index: usize) -> u8 {
debug_assert!(index < self.size, "Frame buffer accessed out of bounds");
self.base.add(index).read_volatile()
Expand Down Expand Up @@ -647,6 +658,7 @@ impl<'gop> FrameBuffer<'gop> {
/// - You must honor the pixel format and stride specified by the mode info
/// - There is no bound checking on memory accesses in release mode
#[inline]
#[must_use]
pub unsafe fn read_value<T>(&self, index: usize) -> T {
debug_assert!(
index.saturating_add(mem::size_of::<T>()) <= self.size,
Expand Down
2 changes: 2 additions & 0 deletions uefi/src/proto/console/pointer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ impl<'boot> Pointer<'boot> {

/// Event to be used with `BootServices::wait_for_event()` in order to wait
/// for input from the pointer device
#[must_use]
pub const fn wait_for_input_event(&self) -> &Event {
&self.wait_for_input
}

/// Returns a reference to the pointer device information.
#[must_use]
pub const fn mode(&self) -> &PointerMode {
self.mode
}
Expand Down
1 change: 1 addition & 0 deletions uefi/src/proto/console/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<'boot> Serial<'boot> {
}

/// Returns the current I/O mode.
#[must_use]
pub const fn io_mode(&self) -> &IoMode {
self.io_mode
}
Expand Down
1 change: 1 addition & 0 deletions uefi/src/proto/console/text/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Input {

/// Event to be used with `BootServices::wait_for_event()` in order to wait
/// for a key to be available
#[must_use]
pub const fn wait_for_key_event(&self) -> &Event {
&self.wait_for_key
}
Expand Down
5 changes: 5 additions & 0 deletions uefi/src/proto/console/text/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl<'boot> Output<'boot> {
}

/// Returns whether the cursor is currently shown or not.
#[must_use]
pub const fn cursor_visible(&self) -> bool {
self.data.cursor_visible
}
Expand All @@ -142,6 +143,7 @@ impl<'boot> Output<'boot> {
}

/// Returns the column and row of the cursor.
#[must_use]
pub const fn cursor_position(&self) -> (usize, usize) {
let column = self.data.cursor_column;
let row = self.data.cursor_row;
Expand Down Expand Up @@ -260,18 +262,21 @@ pub struct OutputMode {
impl OutputMode {
/// Returns the index of this mode.
#[inline]
#[must_use]
pub const fn index(&self) -> usize {
self.index
}

/// Returns the width in columns.
#[inline]
#[must_use]
pub const fn columns(&self) -> usize {
self.dims.0
}

/// Returns the height in rows.
#[inline]
#[must_use]
pub const fn rows(&self) -> usize {
self.dims.1
}
Expand Down
1 change: 1 addition & 0 deletions uefi/src/proto/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct DebugSupport {

impl DebugSupport {
/// Returns the processor architecture of the running CPU.
#[must_use]
pub const fn arch(&self) -> ProcessorArch {
self.isa
}
Expand Down
Loading