Skip to content

Commit 8c0535c

Browse files
Switch to the newer Try trait API (#221)
1 parent 68d21a6 commit 8c0535c

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
2626
#![cfg_attr(feature = "exts", feature(allocator_api, alloc_layout_extra))]
2727
#![feature(auto_traits)]
28-
#![feature(try_trait)]
28+
#![feature(control_flow_enum)]
29+
#![feature(try_trait_v2)]
2930
#![feature(abi_efiapi)]
3031
#![feature(negative_impls)]
3132
#![feature(const_panic)]

src/result/status.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Completion, Error, Result};
2-
use core::fmt::Debug;
3-
use core::ops;
2+
use core::ops::{ControlFlow, FromResidual, Try};
3+
use core::{fmt::Debug, num::NonZeroUsize};
44

55
/// Bit indicating that an UEFI status code is an error
66
const ERROR_BIT: usize = 1 << (core::mem::size_of::<usize>() * 8 - 1);
@@ -162,29 +162,35 @@ impl Status {
162162
}
163163
}
164164

165-
// An UEFI status is equivalent to a Result with no data or rerror payload
166-
165+
// An UEFI status is equivalent to a Result with no data or error payload
167166
impl From<Status> for Result<(), ()> {
168167
#[inline]
169168
fn from(status: Status) -> Result<(), ()> {
170169
status.into_with(|| (), |_| ())
171170
}
172171
}
173172

174-
impl ops::Try for Status {
175-
type Ok = Completion<()>;
176-
type Error = Error<()>;
173+
pub struct StatusResidual(NonZeroUsize);
174+
175+
impl Try for Status {
176+
type Output = Completion<()>;
177+
type Residual = StatusResidual;
177178

178-
fn into_result(self) -> Result<(), ()> {
179-
self.into()
179+
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
180+
match NonZeroUsize::new(self.0) {
181+
Some(r) => ControlFlow::Break(StatusResidual(r)),
182+
None => ControlFlow::Continue(Completion::from(self)),
183+
}
180184
}
181185

182-
fn from_error(error: Self::Error) -> Self {
183-
error.status()
186+
fn from_output(output: Self::Output) -> Self {
187+
output.status()
184188
}
189+
}
185190

186-
fn from_ok(ok: Self::Ok) -> Self {
187-
ok.status()
191+
impl FromResidual for Status {
192+
fn from_residual(r: StatusResidual) -> Self {
193+
Status(r.0.into())
188194
}
189195
}
190196

uefi-test-runner/src/proto/pi/mp.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ fn test_get_processor_info(mps: &MpServices) {
5656
assert_eq!(cpu2.processor_id, 2);
5757

5858
// Check that only CPU 0 is BSP
59-
assert_eq!(cpu0.is_bsp(), true);
60-
assert_eq!(cpu1.is_bsp(), false);
61-
assert_eq!(cpu2.is_bsp(), false);
59+
assert!(cpu0.is_bsp());
60+
assert!(!cpu1.is_bsp());
61+
assert!(!cpu2.is_bsp());
6262

6363
// Check that only the second CPU is disabled
64-
assert_eq!(cpu0.is_enabled(), true);
65-
assert_eq!(cpu1.is_enabled(), false);
66-
assert_eq!(cpu2.is_enabled(), true);
64+
assert!(cpu0.is_enabled());
65+
assert!(!cpu1.is_enabled());
66+
assert!(cpu2.is_enabled());
6767

6868
// Enable second CPU back
6969
mps.enable_disable_ap(1, true, None).unwrap().unwrap();
@@ -138,12 +138,12 @@ fn test_enable_disable_ap(mps: &MpServices) {
138138
.unwrap()
139139
.unwrap();
140140
let cpu1 = mps.get_processor_info(1).unwrap().unwrap();
141-
assert_eq!(cpu1.is_healthy(), false);
141+
assert!(!cpu1.is_healthy());
142142

143143
// Mark second CPU as healthy again and check it's status
144144
mps.enable_disable_ap(1, true, Some(true)).unwrap().unwrap();
145145
let cpu1 = mps.get_processor_info(1).unwrap().unwrap();
146-
assert_eq!(cpu1.is_healthy(), true);
146+
assert!(cpu1.is_healthy());
147147
}
148148

149149
fn test_switch_bsp_and_who_am_i(mps: &MpServices) {

0 commit comments

Comments
 (0)