Skip to content

Commit 9659a59

Browse files
Gate use of unstable try_trait_v2 feature
Add a new enabled-by-default `unstable_try_trait` feature to the `uefi` library to control whether the unstable `try_trait_v2` rustc feature is enabled. It seems like there's still a fair amount of discussion around what the Try API should look like in the tracking issue for try_trait_v2. This could lead to the API being changed in the nightly compiler, and breaking uefi-rs compilation (which is particularly annoying when using a released version rather than the latest git version). Users who want to avoid that problem can now disable `unstable_try_trait`. #452
1 parent bfa88e1 commit 9659a59

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
- Deprecated `BootServices::locate_protocol` and marked it `unsafe`. Use
2424
`BootServices::get_handle_for_protocol` and
2525
`BootServices::open_protocol` instead.
26+
- Use of the unstable `try_trait_v2` rustc feature (allowing `?` to be
27+
used with `Status`) has been placed behind an optional
28+
`unstable_try_trait` feature. This feature is enabled by default.
2629

2730
### Fixed
2831

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ categories = ["embedded", "no-std", "api-bindings"]
1919
license = "MPL-2.0"
2020

2121
[features]
22-
default = []
22+
default = ["unstable_try_trait"]
2323
alloc = []
2424
exts = []
2525
logger = []
26+
# If enabled, the unstable `try_trait_v2` feature
27+
# (https://github.com/rust-lang/rust/issues/84277) is used to allow
28+
# using `?` with `Status` values.
29+
unstable_try_trait = []
2630
# Ignore text output errors in logger as a workaround for firmware issues that
2731
# were observed on the VirtualBox UEFI implementation (see uefi-rs#121)
2832
ignore-logger-errors = []

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
//! For example, a PC with no network card might not contain a network driver,
2424
//! therefore all the network protocols will be unavailable.
2525
26-
#![feature(try_trait_v2)]
2726
#![feature(abi_efiapi)]
2827
#![feature(maybe_uninit_slice)]
2928
#![feature(negative_impls)]
3029
#![feature(ptr_metadata)]
3130
#![cfg_attr(feature = "exts", feature(vec_into_raw_parts))]
31+
#![cfg_attr(feature = "unstable_try_trait", feature(try_trait_v2))]
3232
#![no_std]
3333
// Enable some additional warnings and lints.
3434
#![warn(clippy::ptr_as_ptr, missing_docs, unused)]

src/proto/shim/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ impl ShimLock {
108108
.map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?;
109109

110110
let mut context = MaybeUninit::<Context>::uninit();
111-
(self.context)(ptr, size, context.as_mut_ptr())?;
111+
Result::from((self.context)(ptr, size, context.as_mut_ptr()))?;
112112
(self.hash)(
113113
ptr,
114114
size,
115115
context.as_mut_ptr(),
116116
&mut hashes.sha256,
117117
&mut hashes.sha1,
118-
)?;
119-
Ok(())
118+
)
119+
.into()
120120
}
121121
}

src/result/status.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use super::{Error, Result};
2+
use core::fmt::Debug;
3+
#[cfg(feature = "unstable_try_trait")]
24
use core::{
35
convert::Infallible,
6+
num::NonZeroUsize,
47
ops::{ControlFlow, FromResidual, Try},
58
};
6-
use core::{fmt::Debug, num::NonZeroUsize};
79

810
/// Bit indicating that an UEFI status code is an error
911
const ERROR_BIT: usize = 1 << (core::mem::size_of::<usize>() * 8 - 1);
@@ -172,8 +174,10 @@ impl From<Status> for Result<(), ()> {
172174
}
173175
}
174176

177+
#[cfg(feature = "unstable_try_trait")]
175178
pub struct StatusResidual(NonZeroUsize);
176179

180+
#[cfg(feature = "unstable_try_trait")]
177181
impl Try for Status {
178182
type Output = ();
179183
type Residual = StatusResidual;
@@ -190,18 +194,21 @@ impl Try for Status {
190194
}
191195
}
192196

197+
#[cfg(feature = "unstable_try_trait")]
193198
impl FromResidual for Status {
194199
fn from_residual(r: StatusResidual) -> Self {
195200
Status(r.0.into())
196201
}
197202
}
198203

204+
#[cfg(feature = "unstable_try_trait")]
199205
impl<T> FromResidual<StatusResidual> for Result<T, ()> {
200206
fn from_residual(r: StatusResidual) -> Self {
201207
Err(Status(r.0.into()).into())
202208
}
203209
}
204210

211+
#[cfg(feature = "unstable_try_trait")]
205212
impl FromResidual<core::result::Result<Infallible, Error>> for Status {
206213
fn from_residual(r: core::result::Result<Infallible, Error>) -> Self {
207214
match r {

xtask/src/cargo.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub enum Feature {
4848
Alloc,
4949
Exts,
5050
Logger,
51+
UnstableTryTrait,
5152

5253
Ci,
5354
Qemu,
@@ -59,6 +60,7 @@ impl Feature {
5960
Self::Alloc => "alloc",
6061
Self::Exts => "exts",
6162
Self::Logger => "logger",
63+
Self::UnstableTryTrait => "unstable_try_trait",
6264

6365
Self::Ci => "uefi-test-runner/ci",
6466
Self::Qemu => "uefi-test-runner/qemu",
@@ -67,7 +69,12 @@ impl Feature {
6769

6870
/// Set of features that enables more code in the root uefi crate.
6971
pub fn more_code() -> Vec<Self> {
70-
vec![Self::Alloc, Self::Exts, Self::Logger]
72+
vec![
73+
Self::Alloc,
74+
Self::Exts,
75+
Self::Logger,
76+
Self::UnstableTryTrait,
77+
]
7178
}
7279

7380
fn comma_separated_string(features: &[Feature]) -> String {

0 commit comments

Comments
 (0)