Skip to content

Commit 27e7501

Browse files
committed
remove CStr8 and replace by libcore::ffi::CStr
1 parent 9047806 commit 27e7501

File tree

9 files changed

+15
-152
lines changed

9 files changed

+15
-152
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- Deprecated `BootServices::locate_protocol` and marked it `unsafe`. Use
2323
`BootServices::get_handle_for_protocol` and
2424
`BootServices::open_protocol` instead.
25+
- The library doesn't longer expose the `CStr8` type. It was not relevant in the
26+
API so far, and internally it was replaced by `core::ffi::CStr`. (Since Rust 1.62)
2527

2628
### Removed
2729

@@ -39,7 +41,7 @@
3941

4042
- The `no_panic_handler` feature has been replaced with an additive
4143
`panic_handler` feature. The new feature is enabled by default.
42-
44+
4345
## uefi - 0.16.1
4446

4547
### Added
@@ -55,7 +57,7 @@
5557
function pointers. This prevents potential invalid pointer access.
5658
- Fixed an incorrect pointer cast in the `Rng` protocol that could cause
5759
undefined behavior.
58-
60+
5961
### Changed
6062

6163
- Relaxed the version requirements for the `bitflags` and `log`

src/data_types/chars.rs

Lines changed: 0 additions & 51 deletions
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)]

src/data_types/mod.rs

Lines changed: 3 additions & 2 deletions
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

src/data_types/strs.rs

Lines changed: 1 addition & 65 deletions
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

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub mod data_types;
4848
#[cfg(feature = "exts")]
4949
pub use self::data_types::CString16;
5050
pub use self::data_types::{unsafe_guid, Identify};
51-
pub use self::data_types::{CStr16, CStr8, Char16, Char8, Event, Guid, Handle};
51+
pub use self::data_types::{CStr16, Char16, Event, Guid, Handle};
5252

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

src/prelude.rs

Lines changed: 1 addition & 1 deletion
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};

src/proto/network/pxe.rs

Lines changed: 3 additions & 2 deletions
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,

uefi-macros/src/lib.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,33 +212,6 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
212212
result.into()
213213
}
214214

215-
/// Builds a `CStr8` literal at compile time from a string literal.
216-
///
217-
/// This will throw a compile error if an invalid character is in the passed string.
218-
///
219-
/// # Example
220-
/// ```
221-
/// # use uefi_macros::cstr8;
222-
/// assert_eq!(cstr8!("test").to_bytes_with_nul(), [116, 101, 115, 116, 0]);
223-
/// ```
224-
#[proc_macro]
225-
pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
226-
let input: LitStr = parse_macro_input!(input);
227-
let input = input.value();
228-
match input
229-
.chars()
230-
.map(u8::try_from)
231-
.collect::<Result<Vec<u8>, _>>()
232-
{
233-
Ok(c) => {
234-
quote!(unsafe { ::uefi::CStr8::from_bytes_with_nul_unchecked(&[ #(#c),* , 0 ]) }).into()
235-
}
236-
Err(_) => syn::Error::new_spanned(input, "invalid character in string")
237-
.into_compile_error()
238-
.into(),
239-
}
240-
}
241-
242215
/// Builds a `CStr16` literal at compile time from a string literal.
243216
///
244217
/// This will throw a compile error if an invalid character is in the passed string.

uefi-test-runner/src/proto/network.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +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
},
78
table::boot::{OpenProtocolAttributes, OpenProtocolParams},
8-
CStr8, Handle,
9+
Handle,
910
};
1011

1112
pub fn test(image: Handle, bt: &BootServices) {

0 commit comments

Comments
 (0)