Skip to content

Commit b2a505e

Browse files
uefi: Use UnicodeCollationProtocol from uefi-raw
1 parent a9226d9 commit b2a505e

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

uefi/src/proto/string/unicode_collation.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,26 @@
33
//! This protocol is used in the boot services environment to perform
44
//! lexical comparison functions on Unicode strings for given languages.
55
6-
use crate::data_types::{CStr16, CStr8, Char16, Char8};
6+
use crate::data_types::{CStr16, CStr8};
77
use crate::proto::unsafe_protocol;
88
use core::cmp::Ordering;
99
use core::fmt::{self, Display, Formatter};
10+
use uefi_raw::protocol::string::UnicodeCollationProtocol;
1011

1112
/// The Unicode Collation Protocol.
1213
///
1314
/// Used to perform case-insensitive comparisons of strings.
1415
#[derive(Debug)]
15-
#[repr(C)]
16-
#[unsafe_protocol("a4c751fc-23ae-4c3e-92e9-4964cf63f349")]
17-
pub struct UnicodeCollation {
18-
stri_coll: extern "efiapi" fn(this: &Self, s1: *const Char16, s2: *const Char16) -> isize,
19-
metai_match:
20-
extern "efiapi" fn(this: &Self, string: *const Char16, pattern: *const Char16) -> bool,
21-
str_lwr: extern "efiapi" fn(this: &Self, s: *mut Char16),
22-
str_upr: extern "efiapi" fn(this: &Self, s: *mut Char16),
23-
fat_to_str: extern "efiapi" fn(this: &Self, fat_size: usize, fat: *const Char8, s: *mut Char16),
24-
str_to_fat:
25-
extern "efiapi" fn(this: &Self, s: *const Char16, fat_size: usize, fat: *mut Char8) -> bool,
26-
}
16+
#[repr(transparent)]
17+
#[unsafe_protocol(UnicodeCollationProtocol::GUID)]
18+
pub struct UnicodeCollation(UnicodeCollationProtocol);
2719

2820
impl UnicodeCollation {
2921
/// Performs a case insensitive comparison of two
3022
/// null-terminated strings.
3123
#[must_use]
3224
pub fn stri_coll(&self, s1: &CStr16, s2: &CStr16) -> Ordering {
33-
let order = (self.stri_coll)(self, s1.as_ptr(), s2.as_ptr());
25+
let order = unsafe { (self.0.stri_coll)(&self.0, s1.as_ptr().cast(), s2.as_ptr().cast()) };
3426
order.cmp(&0)
3527
}
3628

@@ -58,7 +50,7 @@ impl UnicodeCollation {
5850
/// any single character followed by a "." followed by any string.
5951
#[must_use]
6052
pub fn metai_match(&self, s: &CStr16, pattern: &CStr16) -> bool {
61-
(self.metai_match)(self, s.as_ptr(), pattern.as_ptr())
53+
unsafe { (self.0.metai_match)(&self.0, s.as_ptr().cast(), pattern.as_ptr().cast()) }
6254
}
6355

6456
/// Converts the characters in `s` to lower case characters.
@@ -75,7 +67,7 @@ impl UnicodeCollation {
7567
*buf.get_mut(last_index + 1)
7668
.ok_or(StrConversionError::BufferTooSmall)? = 0;
7769

78-
(self.str_lwr)(self, buf.as_ptr() as *mut _);
70+
unsafe { (self.0.str_lwr)(&self.0, buf.as_mut_ptr()) };
7971

8072
Ok(unsafe { CStr16::from_u16_with_nul_unchecked(buf) })
8173
}
@@ -94,7 +86,7 @@ impl UnicodeCollation {
9486
*buf.get_mut(last_index + 1)
9587
.ok_or(StrConversionError::BufferTooSmall)? = 0;
9688

97-
(self.str_upr)(self, buf.as_ptr() as *mut _);
89+
unsafe { (self.0.str_upr)(&self.0, buf.as_mut_ptr()) };
9890

9991
Ok(unsafe { CStr16::from_u16_with_nul_unchecked(buf) })
10092
}
@@ -108,12 +100,14 @@ impl UnicodeCollation {
108100
if buf.len() < fat.as_bytes().len() {
109101
return Err(StrConversionError::BufferTooSmall);
110102
}
111-
(self.fat_to_str)(
112-
self,
113-
fat.as_bytes().len(),
114-
fat.as_ptr(),
115-
buf.as_ptr() as *mut _,
116-
);
103+
unsafe {
104+
(self.0.fat_to_str)(
105+
&self.0,
106+
fat.as_bytes().len(),
107+
fat.as_ptr().cast(),
108+
buf.as_mut_ptr(),
109+
)
110+
};
117111
Ok(unsafe { CStr16::from_u16_with_nul_unchecked(buf) })
118112
}
119113

@@ -126,12 +120,14 @@ impl UnicodeCollation {
126120
if s.as_slice_with_nul().len() > buf.len() {
127121
return Err(StrConversionError::BufferTooSmall);
128122
}
129-
let failed = (self.str_to_fat)(
130-
self,
131-
s.as_ptr(),
132-
s.as_slice_with_nul().len(),
133-
buf.as_ptr() as *mut _,
134-
);
123+
let failed = unsafe {
124+
(self.0.str_to_fat)(
125+
&self.0,
126+
s.as_ptr().cast(),
127+
s.as_slice_with_nul().len(),
128+
buf.as_mut_ptr(),
129+
)
130+
};
135131
if failed {
136132
Err(StrConversionError::ConversionFailed)
137133
} else {

0 commit comments

Comments
 (0)