Skip to content

Switch to the newer Try trait API #221

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 3 commits into from
May 19, 2021
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
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

#![cfg_attr(feature = "exts", feature(allocator_api, alloc_layout_extra))]
#![feature(auto_traits)]
#![feature(try_trait)]
#![feature(control_flow_enum)]
#![feature(try_trait_v2)]
#![feature(abi_efiapi)]
#![feature(negative_impls)]
#![feature(const_panic)]
Expand Down
32 changes: 19 additions & 13 deletions src/result/status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Completion, Error, Result};
use core::fmt::Debug;
use core::ops;
use core::ops::{ControlFlow, FromResidual, Try};
use core::{fmt::Debug, num::NonZeroUsize};

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

// An UEFI status is equivalent to a Result with no data or rerror payload

// An UEFI status is equivalent to a Result with no data or error payload
impl From<Status> for Result<(), ()> {
#[inline]
fn from(status: Status) -> Result<(), ()> {
status.into_with(|| (), |_| ())
}
}

impl ops::Try for Status {
type Ok = Completion<()>;
type Error = Error<()>;
pub struct StatusResidual(NonZeroUsize);

impl Try for Status {
type Output = Completion<()>;
type Residual = StatusResidual;

fn into_result(self) -> Result<(), ()> {
self.into()
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
match NonZeroUsize::new(self.0) {
Some(r) => ControlFlow::Break(StatusResidual(r)),
None => ControlFlow::Continue(Completion::from(self)),
}
}

fn from_error(error: Self::Error) -> Self {
error.status()
fn from_output(output: Self::Output) -> Self {
output.status()
}
}

fn from_ok(ok: Self::Ok) -> Self {
ok.status()
impl FromResidual for Status {
fn from_residual(r: StatusResidual) -> Self {
Status(r.0.into())
}
}

Expand Down
16 changes: 8 additions & 8 deletions uefi-test-runner/src/proto/pi/mp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ fn test_get_processor_info(mps: &MpServices) {
assert_eq!(cpu2.processor_id, 2);

// Check that only CPU 0 is BSP
assert_eq!(cpu0.is_bsp(), true);
assert_eq!(cpu1.is_bsp(), false);
assert_eq!(cpu2.is_bsp(), false);
assert!(cpu0.is_bsp());
assert!(!cpu1.is_bsp());
assert!(!cpu2.is_bsp());

// Check that only the second CPU is disabled
assert_eq!(cpu0.is_enabled(), true);
assert_eq!(cpu1.is_enabled(), false);
assert_eq!(cpu2.is_enabled(), true);
assert!(cpu0.is_enabled());
assert!(!cpu1.is_enabled());
assert!(cpu2.is_enabled());

// Enable second CPU back
mps.enable_disable_ap(1, true, None).unwrap().unwrap();
Expand Down Expand Up @@ -138,12 +138,12 @@ fn test_enable_disable_ap(mps: &MpServices) {
.unwrap()
.unwrap();
let cpu1 = mps.get_processor_info(1).unwrap().unwrap();
assert_eq!(cpu1.is_healthy(), false);
assert!(!cpu1.is_healthy());

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

fn test_switch_bsp_and_who_am_i(mps: &MpServices) {
Expand Down