Skip to content

Commit dab2c5b

Browse files
phip1611nicholasbishop
authored andcommitted
const fn wherever possible
1 parent 2e211d5 commit dab2c5b

File tree

12 files changed

+23
-22
lines changed

12 files changed

+23
-22
lines changed

src/data_types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Event {
5151
/// to it are invalidated and the underlying memory is freed by firmware. The caller must ensure
5252
/// that any clones of a closed `Event` are never used again.
5353
#[must_use]
54-
pub unsafe fn unsafe_clone(&self) -> Self {
54+
pub const unsafe fn unsafe_clone(&self) -> Self {
5555
Self(self.0)
5656
}
5757
}

src/data_types/strs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ impl CStr8 {
100100
///
101101
/// It's the callers responsibility to ensure chars is a valid Latin-1
102102
/// null-terminated string, with no interior null bytes.
103-
pub unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
103+
pub const unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
104104
&*(chars as *const [u8] as *const Self)
105105
}
106106

107107
/// Returns the inner pointer to this CStr8.
108-
pub fn as_ptr(&self) -> *const Char8 {
108+
pub const fn as_ptr(&self) -> *const Char8 {
109109
self.0.as_ptr()
110110
}
111111

@@ -116,7 +116,7 @@ impl CStr8 {
116116
}
117117

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

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

311311
/// Returns an iterator over this C string
312-
pub fn iter(&self) -> CStr16Iter {
312+
pub const fn iter(&self) -> CStr16Iter {
313313
CStr16Iter {
314314
inner: self,
315315
pos: 0,

src/data_types/unaligned_slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
5858
/// Returns an iterator over the slice.
5959
///
6060
/// The iterator yields all items from start to end.
61-
pub fn iter(&'a self) -> UnalignedSliceIter<'a, T> {
61+
pub const fn iter(&'a self) -> UnalignedSliceIter<'a, T> {
6262
UnalignedSliceIter {
6363
slice: self,
6464
index: 0,

src/proto/console/gop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ pub struct BltPixel {
475475

476476
impl BltPixel {
477477
/// Create a new pixel from RGB values.
478-
pub fn new(red: u8, green: u8, blue: u8) -> Self {
478+
pub const fn new(red: u8, green: u8, blue: u8) -> Self {
479479
Self {
480480
red,
481481
green,

src/proto/device_path/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl DevicePathNode {
141141
}
142142

143143
/// Cast to a [`FfiDevicePath`] pointer.
144-
pub fn as_ffi_ptr(&self) -> *const FfiDevicePath {
144+
pub const fn as_ffi_ptr(&self) -> *const FfiDevicePath {
145145
let ptr: *const Self = self;
146146
ptr.cast::<FfiDevicePath>()
147147
}
@@ -199,7 +199,7 @@ impl DevicePathInstance {
199199
/// reached.
200200
///
201201
/// [`DevicePathNodes`]: DevicePathNode
202-
pub fn node_iter(&self) -> DevicePathNodeIterator {
202+
pub const fn node_iter(&self) -> DevicePathNodeIterator {
203203
DevicePathNodeIterator {
204204
nodes: &self.data,
205205
stop_condition: StopCondition::AnyEndNode,
@@ -271,7 +271,7 @@ impl DevicePath {
271271
}
272272

273273
/// Get an iterator over the [`DevicePathInstance`]s in this path.
274-
pub fn instance_iter(&self) -> DevicePathInstanceIterator {
274+
pub const fn instance_iter(&self) -> DevicePathInstanceIterator {
275275
DevicePathInstanceIterator {
276276
remaining_path: Some(self),
277277
}
@@ -281,7 +281,7 @@ impl DevicePath {
281281
/// `self`. Iteration ends when a path is reached where
282282
/// [`is_end_entire`][DevicePathNode::is_end_entire] is true. That ending
283283
/// path is not returned by the iterator.
284-
pub fn node_iter(&self) -> DevicePathNodeIterator {
284+
pub const fn node_iter(&self) -> DevicePathNodeIterator {
285285
DevicePathNodeIterator {
286286
nodes: &self.data,
287287
stop_condition: StopCondition::EndEntireNode,

src/proto/media/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct BlockIO {
3131

3232
impl BlockIO {
3333
/// Pointer for block IO media.
34-
pub fn media(&self) -> &BlockIOMedia {
34+
pub const fn media(&self) -> &BlockIOMedia {
3535
unsafe { &*self.media }
3636
}
3737

src/proto/media/file/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<T: File> FileInternal for T {}
248248
pub struct FileHandle(*mut FileImpl);
249249

250250
impl FileHandle {
251-
pub(super) unsafe fn new(ptr: *mut FileImpl) -> Self {
251+
pub(super) const unsafe fn new(ptr: *mut FileImpl) -> Self {
252252
Self(ptr)
253253
}
254254

src/proto/network/pxe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ impl BaseCode {
611611
}
612612

613613
/// Returns a reference to the `Mode` struct.
614-
pub fn mode(&self) -> &Mode {
614+
pub const fn mode(&self) -> &Mode {
615615
unsafe { &*self.mode }
616616
}
617617
}

src/result/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl<Data: Debug> Error<Data> {
2626
}
2727

2828
/// Split this error into its inner status and error data
29+
#[allow(clippy::missing_const_for_fn)]
2930
pub fn split(self) -> (Status, Data) {
3031
(self.status, self.data)
3132
}

src/table/boot.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ pub enum SearchType<'guid> {
18491849

18501850
impl<'guid> SearchType<'guid> {
18511851
/// Constructs a new search type for a specified protocol.
1852-
pub fn from_proto<P: Protocol>() -> Self {
1852+
pub const fn from_proto<P: Protocol>() -> Self {
18531853
SearchType::ByProtocol(&P::GUID)
18541854
}
18551855
}
@@ -1925,7 +1925,7 @@ impl<'a> Drop for ProtocolsPerHandle<'a> {
19251925
impl<'a> ProtocolsPerHandle<'a> {
19261926
/// Get the protocol interface [`Guids`][Guid] that are installed on the
19271927
/// [`Handle`].
1928-
pub fn protocols<'b>(&'b self) -> &'b [&'a Guid] {
1928+
pub const fn protocols<'b>(&'b self) -> &'b [&'a Guid] {
19291929
// convert raw pointer to slice here so that we can get
19301930
// appropriate lifetime of the slice.
19311931
unsafe { slice::from_raw_parts(self.protocols, self.count) }
@@ -1951,7 +1951,7 @@ impl<'a> Drop for HandleBuffer<'a> {
19511951

19521952
impl<'a> HandleBuffer<'a> {
19531953
/// Get an array of [`Handles`][Handle] that support the requested protocol.
1954-
pub fn handles(&self) -> &[Handle] {
1954+
pub const fn handles(&self) -> &[Handle] {
19551955
// convert raw pointer to slice here so that we can get
19561956
// appropriate lifetime of the slice.
19571957
unsafe { slice::from_raw_parts(self.buffer, self.count) }

src/table/runtime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ impl Time {
405405
///
406406
/// [`FileInfo`]: uefi::proto::media::file::FileInfo
407407
/// [`File::set_info`]: uefi::proto::media::file::File::set_info
408-
pub fn invalid() -> Self {
408+
pub const fn invalid() -> Self {
409409
Self {
410410
year: 0,
411411
month: 0,

src/table/system.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<View: SystemTableView> SystemTable<View> {
7070

7171
/// Returns the config table entries, a linear array of structures
7272
/// pointing to other system-specific tables.
73-
pub fn config_table(&self) -> &[cfg::ConfigTableEntry] {
73+
pub const fn config_table(&self) -> &[cfg::ConfigTableEntry] {
7474
unsafe { slice::from_raw_parts(self.table.cfg_table, self.table.nr_cfg) }
7575
}
7676

@@ -125,7 +125,7 @@ impl SystemTable<Boot> {
125125
}
126126

127127
/// Access boot services
128-
pub fn boot_services(&self) -> &BootServices {
128+
pub const fn boot_services(&self) -> &BootServices {
129129
unsafe { &*self.table.boot }
130130
}
131131

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

0 commit comments

Comments
 (0)