Skip to content

const fn wherever possible #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2022
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: 1 addition & 1 deletion src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Event {
/// to it are invalidated and the underlying memory is freed by firmware. The caller must ensure
/// that any clones of a closed `Event` are never used again.
#[must_use]
pub unsafe fn unsafe_clone(&self) -> Self {
pub const unsafe fn unsafe_clone(&self) -> Self {
Self(self.0)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl CStr8 {
///
/// It's the callers responsibility to ensure chars is a valid Latin-1
/// null-terminated string, with no interior null bytes.
pub unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
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.
pub fn as_ptr(&self) -> *const Char8 {
pub const fn as_ptr(&self) -> *const Char8 {
self.0.as_ptr()
}

Expand All @@ -116,7 +116,7 @@ impl CStr8 {
}

/// Converts this CStr8 to a slice of bytes containing the trailing null byte.
pub fn to_bytes_with_nul(&self) -> &[u8] {
pub const fn to_bytes_with_nul(&self) -> &[u8] {
unsafe { &*(&self.0 as *const [Char8] as *const [u8]) }
}
}
Expand Down Expand Up @@ -304,12 +304,12 @@ impl CStr16 {
}

/// Converts this C string to a u16 slice containing the trailing 0 char
pub fn to_u16_slice_with_nul(&self) -> &[u16] {
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
pub fn iter(&self) -> CStr16Iter {
pub const fn iter(&self) -> CStr16Iter {
CStr16Iter {
inner: self,
pos: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/data_types/unaligned_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
/// Returns an iterator over the slice.
///
/// The iterator yields all items from start to end.
pub fn iter(&'a self) -> UnalignedSliceIter<'a, T> {
pub const fn iter(&'a self) -> UnalignedSliceIter<'a, T> {
UnalignedSliceIter {
slice: self,
index: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/proto/console/gop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ pub struct BltPixel {

impl BltPixel {
/// Create a new pixel from RGB values.
pub fn new(red: u8, green: u8, blue: u8) -> Self {
pub const fn new(red: u8, green: u8, blue: u8) -> Self {
Self {
red,
green,
Expand Down
8 changes: 4 additions & 4 deletions src/proto/device_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl DevicePathNode {
}

/// Cast to a [`FfiDevicePath`] pointer.
pub fn as_ffi_ptr(&self) -> *const FfiDevicePath {
pub const fn as_ffi_ptr(&self) -> *const FfiDevicePath {
let ptr: *const Self = self;
ptr.cast::<FfiDevicePath>()
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl DevicePathInstance {
/// reached.
///
/// [`DevicePathNodes`]: DevicePathNode
pub fn node_iter(&self) -> DevicePathNodeIterator {
pub const fn node_iter(&self) -> DevicePathNodeIterator {
DevicePathNodeIterator {
nodes: &self.data,
stop_condition: StopCondition::AnyEndNode,
Expand Down Expand Up @@ -271,7 +271,7 @@ impl DevicePath {
}

/// Get an iterator over the [`DevicePathInstance`]s in this path.
pub fn instance_iter(&self) -> DevicePathInstanceIterator {
pub const fn instance_iter(&self) -> DevicePathInstanceIterator {
DevicePathInstanceIterator {
remaining_path: Some(self),
}
Expand All @@ -281,7 +281,7 @@ impl DevicePath {
/// `self`. Iteration ends when a path is reached where
/// [`is_end_entire`][DevicePathNode::is_end_entire] is true. That ending
/// path is not returned by the iterator.
pub fn node_iter(&self) -> DevicePathNodeIterator {
pub const fn node_iter(&self) -> DevicePathNodeIterator {
DevicePathNodeIterator {
nodes: &self.data,
stop_condition: StopCondition::EndEntireNode,
Expand Down
2 changes: 1 addition & 1 deletion src/proto/media/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct BlockIO {

impl BlockIO {
/// Pointer for block IO media.
pub fn media(&self) -> &BlockIOMedia {
pub const fn media(&self) -> &BlockIOMedia {
unsafe { &*self.media }
}

Expand Down
2 changes: 1 addition & 1 deletion src/proto/media/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl<T: File> FileInternal for T {}
pub struct FileHandle(*mut FileImpl);

impl FileHandle {
pub(super) unsafe fn new(ptr: *mut FileImpl) -> Self {
pub(super) const unsafe fn new(ptr: *mut FileImpl) -> Self {
Self(ptr)
}

Expand Down
2 changes: 1 addition & 1 deletion src/proto/network/pxe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl BaseCode {
}

/// Returns a reference to the `Mode` struct.
pub fn mode(&self) -> &Mode {
pub const fn mode(&self) -> &Mode {
unsafe { &*self.mode }
}
}
Expand Down
1 change: 1 addition & 0 deletions src/result/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl<Data: Debug> Error<Data> {
}

/// Split this error into its inner status and error data
#[allow(clippy::missing_const_for_fn)]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy mistakenly thinks this can be const

pub fn split(self) -> (Status, Data) {
(self.status, self.data)
}
Expand Down
4 changes: 3 additions & 1 deletion src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ pub enum SearchType<'guid> {

impl<'guid> SearchType<'guid> {
/// Constructs a new search type for a specified protocol.
pub fn from_proto<P: Protocol>() -> Self {
pub const fn from_proto<P: Protocol>() -> Self {
SearchType::ByProtocol(&P::GUID)
}
}
Expand Down Expand Up @@ -1925,6 +1925,7 @@ impl<'a> Drop for ProtocolsPerHandle<'a> {
impl<'a> ProtocolsPerHandle<'a> {
/// Get the protocol interface [`Guids`][Guid] that are installed on the
/// [`Handle`].
#[allow(clippy::missing_const_for_fn)] // Required until we bump the MSRV.
pub fn protocols<'b>(&'b self) -> &'b [&'a Guid] {
// convert raw pointer to slice here so that we can get
// appropriate lifetime of the slice.
Expand All @@ -1951,6 +1952,7 @@ impl<'a> Drop for HandleBuffer<'a> {

impl<'a> HandleBuffer<'a> {
/// Get an array of [`Handles`][Handle] that support the requested protocol.
#[allow(clippy::missing_const_for_fn)] // Required until we bump the MSRV.
pub fn handles(&self) -> &[Handle] {
// convert raw pointer to slice here so that we can get
// appropriate lifetime of the slice.
Expand Down
2 changes: 1 addition & 1 deletion src/table/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl Time {
///
/// [`FileInfo`]: uefi::proto::media::file::FileInfo
/// [`File::set_info`]: uefi::proto::media::file::File::set_info
pub fn invalid() -> Self {
pub const fn invalid() -> Self {
Self {
year: 0,
month: 0,
Expand Down
5 changes: 3 additions & 2 deletions src/table/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl<View: SystemTableView> SystemTable<View> {

/// Returns the config table entries, a linear array of structures
/// pointing to other system-specific tables.
#[allow(clippy::missing_const_for_fn)] // Required until we bump the MSRV.
pub fn config_table(&self) -> &[cfg::ConfigTableEntry] {
unsafe { slice::from_raw_parts(self.table.cfg_table, self.table.nr_cfg) }
}
Expand Down Expand Up @@ -125,7 +126,7 @@ impl SystemTable<Boot> {
}

/// Access boot services
pub fn boot_services(&self) -> &BootServices {
pub const fn boot_services(&self) -> &BootServices {
unsafe { &*self.table.boot }
}

Expand Down Expand Up @@ -220,7 +221,7 @@ impl SystemTable<Boot> {
/// designs that Rust uses for memory allocation, logging, and panic
/// handling require taking this risk.
#[must_use]
pub unsafe fn unsafe_clone(&self) -> Self {
pub const unsafe fn unsafe_clone(&self) -> Self {
SystemTable {
table: self.table,
_marker: PhantomData,
Expand Down