Skip to content

uefi(filesystem): small quality of life improvements around filesystem and paths #996

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

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions uefi/src/data_types/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use core::fmt::{self, Display, Formatter};

use alloc::vec::Vec;

/// Character conversion error
#[derive(Clone, Copy, Debug)]
pub struct CharConversionError;
Expand Down Expand Up @@ -83,6 +85,30 @@ impl Char16 {
pub const unsafe fn from_u16_unchecked(val: u16) -> Self {
Self(val)
}

/// Checks if the value is within the ASCII range.
#[must_use]
pub const fn is_ascii(&self) -> bool {
self.0 <= 127
}
}

/// Provides various functions on slice-like container (e.g. Vec) of Char16.
pub trait SliceLikeChar16 {
/// Checks if all char16 in this slice are within the ASCII range.
fn is_ascii(&self) -> bool;
}

impl SliceLikeChar16 for [Char16] {
fn is_ascii(&self) -> bool {
self.iter().all(|c| c.is_ascii())
}
}

impl SliceLikeChar16 for Vec<Char16> {
fn is_ascii(&self) -> bool {
self.iter().all(|c| c.is_ascii())
}
}

impl TryFrom<char> for Char16 {
Expand Down
8 changes: 7 additions & 1 deletion uefi/src/data_types/owned_strs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::chars::{Char16, NUL_16};
use super::chars::{Char16, NUL_16, SliceLikeChar16};
use super::strs::{CStr16, FromSliceWithNulError};
use crate::data_types::strs::EqStrUntilNul;
use crate::data_types::UnalignedSlice;
Expand Down Expand Up @@ -109,6 +109,12 @@ impl CString16 {
pub fn is_empty(&self) -> bool {
self.num_chars() == 0
}

/// Checks if all characters in this string are within the ASCII range.
#[must_use]
pub fn is_ascii(&self) -> bool {
self.0.is_ascii()
}
}

impl Default for CString16 {
Expand Down
8 changes: 7 additions & 1 deletion uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::chars::{Char16, Char8, NUL_16, NUL_8};
use super::chars::{Char16, Char8, NUL_16, NUL_8, SliceLikeChar16};
use super::UnalignedSlice;
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
use core::borrow::Borrow;
Expand Down Expand Up @@ -415,6 +415,12 @@ impl CStr16 {
self.0.len() * 2
}

/// Checks if all characters in this string are within the ASCII range.
#[must_use]
pub fn is_ascii(&self) -> bool {
self.0.is_ascii()
}

/// Writes each [`Char16`] as a [`char`] (4 bytes long in Rust language) into the buffer.
/// It is up to the implementer of [`core::fmt::Write`] to convert the char to a string
/// with proper encoding/charset. For example, in the case of [`alloc::string::String`]
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/fs/file_system/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl<'a> FileSystem<'a> {
/// May create a file if [`UefiFileMode::CreateReadWrite`] is set. May
/// create a directory if [`UefiFileMode::CreateReadWrite`] and `create_dir`
/// is set. The parameter `create_dir` is ignored otherwise.
fn open(
pub fn open(
&mut self,
path: &Path,
mode: UefiFileMode,
Expand Down