Skip to content

TryFrom<core::ffi::CStr> implementation for CStr8 #581

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 23, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Implementations for the trait `EqStrUntilNul` now allow `?Sized` inputs. This means that
you can write `some_cstr16.eq_str_until_nul("test")` instead of
`some_cstr16.eq_str_until_nul(&"test")` now.
- Added `TryFrom<core::ffi::CStr>` implementation for `CStr8`.

## uefi-macros - [Unreleased]

Expand Down
24 changes: 24 additions & 0 deletions uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::chars::{Char16, Char8, NUL_16, NUL_8};
use super::UnalignedSlice;
use core::ffi::CStr;
use core::fmt;
use core::iter::Iterator;
use core::mem::MaybeUninit;
Expand Down Expand Up @@ -58,6 +59,11 @@ pub enum FromStrWithBufError {
/// This type is largely inspired by [`core::ffi::CStr`] with the exception that all characters are
/// guaranteed to be 8 bit long.
///
/// A [`CStr8`] can be constructed from a [`core::ffi::CStr`] via a `try_from` call:
/// ```ignore
/// let cstr8: &CStr8 = TryFrom::try_from(cstr).unwrap();
/// ```
///
/// For convenience, a [`CStr8`] is comparable with [`core::str`] and
/// `alloc::string::String` from the standard library through the trait [`EqStrUntilNul`].
#[repr(transparent)]
Expand Down Expand Up @@ -156,6 +162,14 @@ impl<StrType: AsRef<str> + ?Sized> EqStrUntilNul<StrType> for CStr8 {
}
}

impl<'a> TryFrom<&'a CStr> for &'a CStr8 {
type Error = FromSliceWithNulError;

fn try_from(cstr: &'a CStr) -> Result<Self, Self::Error> {
CStr8::from_bytes_with_nul(cstr.to_bytes_with_nul())
}
}

/// An UCS-2 null-terminated string.
///
/// This type is largely inspired by [`core::ffi::CStr`] with the exception that all characters are
Expand Down Expand Up @@ -453,6 +467,16 @@ mod tests {
use crate::alloc::string::String;
use uefi_macros::{cstr16, cstr8};

// Tests if our CStr8 type can be constructed from a valid core::ffi::CStr
#[test]
fn test_cstr8_from_cstr() {
let msg = "hello world\0";
let cstr = unsafe { CStr::from_ptr(msg.as_ptr().cast()) };
let cstr8: &CStr8 = TryFrom::try_from(cstr).unwrap();
assert!(cstr8.eq_str_until_nul(msg));
assert!(msg.eq_str_until_nul(cstr8));
}

#[test]
fn test_cstr16_num_bytes() {
let s = CStr16::from_u16_with_nul(&[65, 66, 67, 0]).unwrap();
Expand Down