Skip to content

Commit 8dd8432

Browse files
committed
remove CStr8 and replace by libcore::ffi::CStr
1 parent 4cba77a commit 8dd8432

File tree

9 files changed

+14
-150
lines changed

9 files changed

+14
-150
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
`BootServices::open_protocol`) instead.
4343
- renamed feature `ignore-logger-errors` to `panic-on-logger-errors` so that it is
4444
additive. It is now a default feature.
45+
- The library doesn't longer expose the `CStr8` type. It was not relevant in the
46+
API so far, and internally it was replaced by `core::ffi::CStr`. (Since Rust 1.62)
4547

4648
### Removed
4749

Diff for: src/data_types/chars.rs

-51
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,6 @@ use core::fmt;
99
#[derive(Clone, Copy, Debug)]
1010
pub struct CharConversionError;
1111

12-
/// A Latin-1 character
13-
#[derive(Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord)]
14-
#[repr(transparent)]
15-
pub struct Char8(u8);
16-
17-
impl TryFrom<char> for Char8 {
18-
type Error = CharConversionError;
19-
20-
fn try_from(value: char) -> Result<Self, Self::Error> {
21-
let code_point = value as u32;
22-
if code_point <= 0xff {
23-
Ok(Char8(code_point as u8))
24-
} else {
25-
Err(CharConversionError)
26-
}
27-
}
28-
}
29-
30-
impl From<Char8> for char {
31-
fn from(char: Char8) -> char {
32-
char.0 as char
33-
}
34-
}
35-
36-
impl From<u8> for Char8 {
37-
fn from(value: u8) -> Self {
38-
Char8(value)
39-
}
40-
}
41-
42-
impl From<Char8> for u8 {
43-
fn from(char: Char8) -> u8 {
44-
char.0
45-
}
46-
}
47-
48-
impl fmt::Debug for Char8 {
49-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50-
<char as fmt::Debug>::fmt(&From::from(self.0), f)
51-
}
52-
}
53-
54-
impl fmt::Display for Char8 {
55-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56-
<char as fmt::Display>::fmt(&From::from(self.0), f)
57-
}
58-
}
59-
60-
/// Latin-1 version of the NUL character
61-
pub const NUL_8: Char8 = Char8(0);
62-
6312
/// An UCS-2 code point
6413
#[derive(Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord)]
6514
#[repr(transparent)]

Diff for: src/data_types/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ pub use self::guid::Guid;
112112
pub use self::guid::{unsafe_guid, Identify};
113113

114114
pub mod chars;
115-
pub use self::chars::{Char16, Char8};
115+
pub use self::chars::Char16;
116116

117117
#[macro_use]
118118
mod enums;
119119

120120
mod strs;
121+
121122
pub use self::strs::{
122-
CStr16, CStr8, EqStrUntilNul, FromSliceWithNulError, FromStrWithBufError, UnalignedCStr16,
123+
CStr16, EqStrUntilNul, FromSliceWithNulError, FromStrWithBufError, UnalignedCStr16,
123124
UnalignedCStr16Error,
124125
};
125126

Diff for: src/data_types/strs.rs

+1-65
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::chars::{Char16, Char8, NUL_16, NUL_8};
1+
use super::chars::{Char16, NUL_16};
22
use core::fmt;
33
use core::iter::Iterator;
44
use core::marker::PhantomData;
@@ -52,70 +52,6 @@ pub enum FromStrWithBufError {
5252
BufferTooSmall,
5353
}
5454

55-
/// A Latin-1 null-terminated string
56-
///
57-
/// This type is largely inspired by `std::ffi::CStr`, see the documentation of
58-
/// `CStr` for more details on its semantics.
59-
#[repr(transparent)]
60-
pub struct CStr8([Char8]);
61-
62-
impl CStr8 {
63-
/// Wraps a raw UEFI string with a safe C string wrapper
64-
///
65-
/// # Safety
66-
///
67-
/// The function will start accessing memory from `ptr` until the first
68-
/// null byte. It's the callers responsability to ensure `ptr` points to
69-
/// a valid string, in accessible memory.
70-
pub unsafe fn from_ptr<'ptr>(ptr: *const Char8) -> &'ptr Self {
71-
let mut len = 0;
72-
while *ptr.add(len) != NUL_8 {
73-
len += 1
74-
}
75-
let ptr = ptr.cast::<u8>();
76-
Self::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len + 1))
77-
}
78-
79-
/// Creates a C string wrapper from bytes
80-
pub fn from_bytes_with_nul(chars: &[u8]) -> Result<&Self, FromSliceWithNulError> {
81-
let nul_pos = chars.iter().position(|&c| c == 0);
82-
if let Some(nul_pos) = nul_pos {
83-
if nul_pos + 1 != chars.len() {
84-
return Err(FromSliceWithNulError::InteriorNul(nul_pos));
85-
}
86-
Ok(unsafe { Self::from_bytes_with_nul_unchecked(chars) })
87-
} else {
88-
Err(FromSliceWithNulError::NotNulTerminated)
89-
}
90-
}
91-
92-
/// Unsafely creates a C string wrapper from bytes
93-
///
94-
/// # Safety
95-
///
96-
/// It's the callers responsability to ensure chars is a valid Latin-1
97-
/// null-terminated string, with no interior null bytes.
98-
pub unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
99-
&*(chars as *const [u8] as *const Self)
100-
}
101-
102-
/// Returns the inner pointer to this C string
103-
pub fn as_ptr(&self) -> *const Char8 {
104-
self.0.as_ptr()
105-
}
106-
107-
/// Converts this C string to a slice of bytes
108-
pub fn to_bytes(&self) -> &[u8] {
109-
let chars = self.to_bytes_with_nul();
110-
&chars[..chars.len() - 1]
111-
}
112-
113-
/// Converts this C string to a slice of bytes containing the trailing 0 char
114-
pub fn to_bytes_with_nul(&self) -> &[u8] {
115-
unsafe { &*(&self.0 as *const [Char8] as *const [u8]) }
116-
}
117-
}
118-
11955
/// An UCS-2 null-terminated string
12056
///
12157
/// This type is largely inspired by `std::ffi::CStr`, see the documentation of

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub mod data_types;
4747
#[cfg(feature = "exts")]
4848
pub use self::data_types::CString16;
4949
pub use self::data_types::{unsafe_guid, Identify};
50-
pub use self::data_types::{CStr16, CStr8, Char16, Char8, Event, Guid, Handle};
50+
pub use self::data_types::{CStr16, Char16, Event, Guid, Handle};
5151

5252
mod result;
5353
pub use self::result::{Error, Result, ResultExt, Status};

Diff for: src/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ pub use crate::table::runtime::RuntimeServices;
1010
pub use crate::table::{Boot, SystemTable};
1111

1212
// Import the macro for creating the custom entry point, as well as the cstr macros.
13-
pub use uefi_macros::{cstr16, cstr8, entry};
13+
pub use uefi_macros::{cstr16, entry};

Diff for: src/proto/network/pxe.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use core::{
99
use bitflags::bitflags;
1010
use uefi_macros::{unsafe_guid, Protocol};
1111

12-
use crate::{CStr8, Char8, Result, Status};
12+
use crate::{Result, Status};
13+
use core::ffi::{c_char, CStr as CStr8};
1314

1415
use super::{IpAddress, MacAddress};
1516

@@ -38,7 +39,7 @@ pub struct BaseCode {
3839
buffer_size: &mut u64,
3940
block_size: Option<&usize>,
4041
server_ip: &IpAddress,
41-
filename: *const Char8,
42+
filename: *const c_char,
4243
info: Option<&MtftpInfo>,
4344
dont_use_buffer: bool,
4445
) -> Status,

Diff for: uefi-macros/src/lib.rs

-27
Original file line numberDiff line numberDiff line change
@@ -289,33 +289,6 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
289289
result.into()
290290
}
291291

292-
/// Builds a `CStr8` literal at compile time from a string literal.
293-
///
294-
/// This will throw a compile error if an invalid character is in the passed string.
295-
///
296-
/// # Example
297-
/// ```
298-
/// # use uefi_macros::cstr8;
299-
/// assert_eq!(cstr8!("test").to_bytes_with_nul(), [116, 101, 115, 116, 0]);
300-
/// ```
301-
#[proc_macro]
302-
pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
303-
let input: LitStr = parse_macro_input!(input);
304-
let input = input.value();
305-
match input
306-
.chars()
307-
.map(u8::try_from)
308-
.collect::<Result<Vec<u8>, _>>()
309-
{
310-
Ok(c) => {
311-
quote!(unsafe { ::uefi::CStr8::from_bytes_with_nul_unchecked(&[ #(#c),* , 0 ]) }).into()
312-
}
313-
Err(_) => syn::Error::new_spanned(input, "invalid character in string")
314-
.into_compile_error()
315-
.into(),
316-
}
317-
}
318-
319292
/// Builds a `CStr16` literal at compile time from a string literal.
320293
///
321294
/// This will throw a compile error if an invalid character is in the passed string.

Diff for: uefi-test-runner/src/proto/network.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use core::ffi::CStr as CStr8;
12
use uefi::{
23
prelude::BootServices,
34
proto::network::{
45
pxe::{BaseCode, DhcpV4Packet, IpFilter, IpFilters, UdpOpFlags},
56
IpAddress,
67
},
7-
CStr8,
8+
table::boot::{OpenProtocolAttributes, OpenProtocolParams},
9+
Handle,
810
};
911

1012
pub fn test(bt: &BootServices) {

0 commit comments

Comments
 (0)