Skip to content

const fn for trivial getters where possible #545

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 1 commit into from
Nov 2, 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
6 changes: 3 additions & 3 deletions src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ impl CStr16 {
}

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

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

Expand All @@ -317,7 +317,7 @@ impl CStr16 {
}

/// Get the number of bytes in the string (including the trailing null character).
pub fn num_bytes(&self) -> usize {
pub const fn num_bytes(&self) -> usize {
self.0.len() * 2
}

Expand Down
4 changes: 2 additions & 2 deletions src/data_types/unaligned_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
}

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

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

Expand Down
16 changes: 8 additions & 8 deletions src/proto/console/gop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl<'boot> GraphicsOutput<'boot> {
}

/// Returns the frame buffer information for the current mode.
pub fn current_mode_info(&self) -> ModeInfo {
pub const fn current_mode_info(&self) -> ModeInfo {
*self.mode.info
}

Expand Down Expand Up @@ -377,12 +377,12 @@ impl Mode {
/// The size of the info structure in bytes.
///
/// Newer versions of the spec might add extra information, in a backwards compatible way.
pub fn info_size(&self) -> usize {
pub const fn info_size(&self) -> usize {
self.info_sz
}

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

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

/// Returns the bitmask of the custom pixel format, if available.
pub fn pixel_bitmask(&self) -> Option<PixelBitmask> {
pub const fn pixel_bitmask(&self) -> Option<PixelBitmask> {
match self.format {
PixelFormat::Bitmask => Some(self.mask),
_ => None,
Expand All @@ -425,7 +425,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.
pub fn stride(&self) -> usize {
pub const fn stride(&self) -> usize {
self.stride as usize
}
}
Expand Down Expand Up @@ -582,7 +582,7 @@ impl<'gop> FrameBuffer<'gop> {
}

/// Query the framebuffer size in bytes
pub fn size(&self) -> usize {
pub const fn size(&self) -> usize {
self.size
}

Expand Down
4 changes: 2 additions & 2 deletions src/proto/console/pointer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl<'boot> Pointer<'boot> {

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

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

/// Returns the current I/O mode.
pub fn io_mode(&self) -> &IoMode {
pub const fn io_mode(&self) -> &IoMode {
self.io_mode
}

Expand Down
2 changes: 1 addition & 1 deletion src/proto/console/text/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Input {

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

/// Returns whether the cursor is currently shown or not.
pub fn cursor_visible(&self) -> bool {
pub const fn cursor_visible(&self) -> bool {
self.data.cursor_visible
}

Expand All @@ -142,7 +142,7 @@ impl<'boot> Output<'boot> {
}

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

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

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

impl DebugSupport {
/// Returns the processor architecture of the running CPU.
pub fn arch(&self) -> ProcessorArch {
pub const fn arch(&self) -> ProcessorArch {
self.isa
}

Expand Down
20 changes: 10 additions & 10 deletions src/proto/device_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ impl DevicePathNode {
}

/// Type of device
pub fn device_type(&self) -> DeviceType {
pub const fn device_type(&self) -> DeviceType {
self.header.device_type
}

/// Sub type of device
pub fn sub_type(&self) -> DeviceSubType {
pub const fn sub_type(&self) -> DeviceSubType {
self.header.sub_type
}

/// Tuple of the node's type and subtype.
pub fn full_type(&self) -> (DeviceType, DeviceSubType) {
pub const fn full_type(&self) -> (DeviceType, DeviceSubType) {
(self.header.device_type, self.header.sub_type)
}

/// Size (in bytes) of the full [`DevicePathNode`], including the header.
pub fn length(&self) -> u16 {
pub const fn length(&self) -> u16 {
self.header.length
}

Expand Down Expand Up @@ -281,7 +281,7 @@ impl DevicePath {
}

/// Cast to a [`FfiDevicePath`] pointer.
pub fn as_ffi_ptr(&self) -> *const FfiDevicePath {
pub const fn as_ffi_ptr(&self) -> *const FfiDevicePath {
let p = self as *const Self;
p.cast()
}
Expand Down Expand Up @@ -615,22 +615,22 @@ const HARD_DRIVE_MEDIA_DEVICE_PATH_LENGTH: u16 = 42;

impl HardDriveMediaDevicePath {
/// Returns the format of the partition (MBR, GPT, or unknown).
pub fn partition_format(&self) -> PartitionFormat {
pub const fn partition_format(&self) -> PartitionFormat {
self.partition_format
}

/// Returns the 1-based index of the partition.
pub fn partition_number(&self) -> u32 {
pub const fn partition_number(&self) -> u32 {
self.partition_number
}

/// Returns the partition size in logical blocks.
pub fn partition_size(&self) -> u64 {
pub const fn partition_size(&self) -> u64 {
self.partition_size
}

/// Returns the starting LBA of the partition.
pub fn partition_start(&self) -> u64 {
pub const fn partition_start(&self) -> u64 {
self.partition_start
}

Expand Down Expand Up @@ -729,7 +729,7 @@ mod tests {
raw_data
}

/// Check that `node` has the expected content.
/// Check that `node` has the expected content.
fn check_node(node: &DevicePathNode, device_type: u8, sub_type: u8, node_data: &[u8]) {
assert_eq!(node.device_type().0, device_type);
assert_eq!(node.sub_type().0, sub_type);
Expand Down
4 changes: 2 additions & 2 deletions src/proto/loaded_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum LoadOptionsError {

impl LoadedImage {
/// Returns a handle to the storage device on which the image is located.
pub fn device(&self) -> Handle {
pub const fn device(&self) -> Handle {
self.device_handle
}

Expand Down Expand Up @@ -164,7 +164,7 @@ impl LoadedImage {
}

/// Returns the base address and the size in bytes of the loaded image.
pub fn info(&self) -> (*const c_void, u64) {
pub const fn info(&self) -> (*const c_void, u64) {
(self.image_base, self.image_size)
}
}
24 changes: 12 additions & 12 deletions src/proto/media/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,64 +128,64 @@ pub struct BlockIOMedia {

impl BlockIOMedia {
/// The current media ID.
pub fn media_id(&self) -> u32 {
pub const fn media_id(&self) -> u32 {
self.media_id
}

/// True if the media is removable.
pub fn is_removable_media(&self) -> bool {
pub const fn is_removable_media(&self) -> bool {
self.removable_media
}

/// True if there is a media currently present in the device.
pub fn is_media_present(&self) -> bool {
pub const fn is_media_present(&self) -> bool {
self.media_present
}

/// True if block IO was produced to abstract partition structure.
pub fn is_logical_partition(&self) -> bool {
pub const fn is_logical_partition(&self) -> bool {
self.logical_partition
}

/// True if the media is marked read-only.
pub fn is_read_only(&self) -> bool {
pub const fn is_read_only(&self) -> bool {
self.read_only
}

/// True if `writeBlocks` function writes data.
pub fn is_write_caching(&self) -> bool {
pub const fn is_write_caching(&self) -> bool {
self.write_caching
}

/// The intrinsic block size of the device.
///
/// If the media changes, then this field is updated. Returns the number of bytes per logical block.
pub fn block_size(&self) -> u32 {
pub const fn block_size(&self) -> u32 {
self.block_size
}

/// Supplies the alignment requirement for any buffer used in a data transfer.
pub fn io_align(&self) -> u32 {
pub const fn io_align(&self) -> u32 {
self.io_align
}

/// The last LBA on the device. If the media changes, then this field is updated.
pub fn last_block(&self) -> Lba {
pub const fn last_block(&self) -> Lba {
self.last_block
}

/// Returns the first LBA that is aligned to a physical block boundary.
pub fn lowest_aligned_lba(&self) -> Lba {
pub const fn lowest_aligned_lba(&self) -> Lba {
self.lowest_aligned_lba
}

/// Returns the number of logical blocks per physical block.
pub fn logical_blocks_per_physical_block(&self) -> u32 {
pub const fn logical_blocks_per_physical_block(&self) -> u32 {
self.logical_blocks_per_physical_block
}

/// Returns the optimal transfer length granularity as a number of logical blocks.
pub fn optimal_transfer_length_granularity(&self) -> u32 {
pub const fn optimal_transfer_length_granularity(&self) -> u32 {
self.optimal_transfer_length_granularity
}
}
Loading