Skip to content

Commit ca54ff0

Browse files
committed
add "unstable_alloc" feature and {get_boxed_info, read_entry_boxed_}in-methods
1 parent afed1d8 commit ca54ff0

File tree

7 files changed

+56
-22
lines changed

7 files changed

+56
-22
lines changed

Diff for: CHANGELOG.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
# Changelog
22

33
## uefi - [Unreleased]
4+
5+
### Added
46
- Implementations for the trait `EqStrUntilNul` now allow `?Sized` inputs. This means that
57
you can write `some_cstr16.eq_str_until_nul("test")` instead of
68
`some_cstr16.eq_str_until_nul(&"test")` now.
79
- Added `TryFrom<core::ffi::CStr>` implementation for `CStr8`.
810
- Added `Directory::read_entry_boxed` which works similar to `File::get_boxed_info`. This allows
9-
easier iteration over the entries in a directory.
10-
- Added an `core::error::Error` implementation for `Error` to ease
11-
integration with error-handling crates. (requires the **unstable** feature)
11+
easier iteration over the entries in a directory. (requires the **unstable_alloc** feature)
1212
- Added `Directory::read_entry_boxed_in` and `File::get_boxed_info_in` that use the `allocator_api`
13-
feature. (requires the **unstable** feature)
13+
feature. (requires the **unstable_alloc** feature)
14+
- Added an `core::error::Error` implementation for `Error` to ease
15+
integration with error-handling crates. (requires the **unstable** feature)
16+
17+
### Changed
18+
- `File::get_boxed_info` requires the **unstable** feature
19+
20+
### Removed
1421

1522
## uefi-macros - [Unreleased]
1623

24+
### Added
25+
26+
### Changed
27+
28+
### Removed
29+
1730
## uefi-services - [Unreleased]
1831

32+
### Added
33+
34+
### Changed
35+
36+
### Removed
37+
1938
## uefi - 0.18.0 (2022-11-15)
2039

2140
### Added

Diff for: uefi-test-runner/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ publish = false
66
edition = "2021"
77

88
[dependencies]
9-
uefi = { path = "../uefi", features = ['alloc'] }
9+
uefi = { path = "../uefi", features = ["alloc", "unstable_alloc"] }
1010
uefi-services = { path = "../uefi-services" }
1111

1212
log = { version = "0.4.17", default-features = false }

Diff for: uefi/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ logger = []
1919
# were observed on the VirtualBox UEFI implementation (see uefi-rs#121).
2020
# In those cases, this feature can be excluded by removing the default features.
2121
panic-on-logger-errors = []
22+
# Enables all generic functionality that requires unstable Rust language features. To use
23+
# features behind this feature gate, you usually need a nightly toolchain.
2224
unstable = []
25+
# Like the `unstable` feature but specific to `alloc`-related functionality.
26+
unstable_alloc = ["unstable", "alloc"]
2327

2428
[dependencies]
2529
bitflags = "1.3.1"

Diff for: uefi/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@
6262
#![feature(ptr_metadata)]
6363
#![cfg_attr(feature = "alloc", feature(vec_into_raw_parts))]
6464
#![cfg_attr(feature = "unstable", feature(error_in_core))]
65-
#![cfg_attr(feature = "unstable", feature(allocator_api))]
66-
#![cfg_attr(feature = "unstable", feature(slice_ptr_get))]
65+
#![cfg_attr(feature = "unstable_alloc", feature(allocator_api))]
6766
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
6867
#![no_std]
6968
// Enable some additional warnings and lints.

Diff for: uefi/src/mem.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
//! This is a utility module with helper methods for allocations/memory.
22
3-
use crate::ResultExt;
4-
use crate::{Result, Status};
5-
use ::alloc::{alloc::Global, boxed::Box};
6-
use core::alloc::{Allocator, Layout};
7-
use core::fmt::Debug;
8-
use core::ptr::NonNull;
9-
use uefi::data_types::Align;
10-
use uefi::Error;
3+
#[cfg(feature = "unstable_alloc")]
4+
use {
5+
crate::ResultExt,
6+
crate::{Result, Status},
7+
::alloc::boxed::Box,
8+
alloc::alloc::Global,
9+
core::alloc::Allocator,
10+
core::alloc::Layout,
11+
core::fmt::Debug,
12+
core::ptr::NonNull,
13+
uefi::data_types::Align,
14+
uefi::Error,
15+
};
1116

1217
/// Wrapper around [`make_boxed_in`] that uses [`Global`]/the system allocator.
1318
#[allow(unused)]
19+
#[cfg(feature = "unstable_alloc")]
1420
pub fn make_boxed<
1521
'a,
1622
Data: Align + ?Sized + Debug + 'a,
@@ -31,6 +37,7 @@ pub fn make_boxed<
3137
/// success.
3238
///
3339
/// It takes a custom allocator via the `allocator_api`.
40+
#[cfg(feature = "unstable_alloc")]
3441
pub fn make_boxed_in<
3542
'a,
3643
A: Allocator,
@@ -71,7 +78,12 @@ pub fn make_boxed_in<
7178
let data: &mut Data = match data {
7279
Ok(data) => data,
7380
Err(err) => {
74-
unsafe { allocator.deallocate(heap_buf.as_non_null_ptr(), layout) };
81+
// The deallocate function only takes the begin pointer but not a pointer to the
82+
// whole slice. To prevent unsafe slice features, we do the conversion here manually
83+
// - at the costs of some clutter code.
84+
let ptr = unsafe { &mut *heap_buf.as_ptr().cast::<u8>() };
85+
let ptr = NonNull::new(ptr).unwrap();
86+
unsafe { allocator.deallocate(ptr, layout) };
7587
return Err(err);
7688
}
7789
};

Diff for: uefi/src/proto/media/file/dir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::data_types::Align;
33
use crate::Result;
44
use core::ffi::c_void;
55

6-
#[cfg(feature = "alloc")]
6+
#[cfg(feature = "unstable_alloc")]
77
use {crate::mem::make_boxed_in, alloc::alloc::Global, alloc::boxed::Box, core::alloc::Allocator};
88

99
/// A `FileHandle` that is also a directory.
@@ -62,7 +62,7 @@ impl Directory {
6262
}
6363

6464
/// Wrapper around [`Self::read_entry_boxed_in`] that uses the [`Global`] allocator.
65-
#[cfg(feature = "alloc")]
65+
#[cfg(feature = "unstable_alloc")]
6666
pub fn read_entry_boxed(&mut self) -> Result<Option<Box<FileInfo>>> {
6767
self.read_entry_boxed_in(Global)
6868
}
@@ -71,7 +71,7 @@ impl Directory {
7171
/// implications and requirements. On failure, the payload of `Err` is `()´.
7272
///
7373
/// It allows to use a custom allocator via the `allocator_api` feature.
74-
#[cfg(feature = "alloc")]
74+
#[cfg(feature = "unstable_alloc")]
7575
pub fn read_entry_boxed_in<A: Allocator>(
7676
&mut self,
7777
allocator: A,

Diff for: uefi/src/proto/media/file/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use core::ffi::c_void;
1616
use core::fmt::Debug;
1717
use core::mem;
1818
use core::ptr;
19-
#[cfg(feature = "alloc")]
19+
#[cfg(feature = "unstable_alloc")]
2020
use {alloc::alloc::Global, alloc::boxed::Box, core::alloc::Allocator, uefi::mem::make_boxed_in};
2121

2222
pub use self::info::{FileInfo, FileProtocolInfo, FileSystemInfo, FileSystemVolumeLabel, FromUefi};
@@ -161,14 +161,14 @@ pub trait File: Sized {
161161
(self.imp().flush)(self.imp()).into()
162162
}
163163

164-
#[cfg(feature = "alloc")]
165164
/// Wrapper around [`Self::get_boxed_info_in`] that uses the [`Global`] allocator.
165+
#[cfg(feature = "unstable_alloc")]
166166
fn get_boxed_info<Info: FileProtocolInfo + ?Sized + Debug>(&mut self) -> Result<Box<Info>> {
167167
self.get_boxed_info_in(Global)
168168
}
169169

170-
#[cfg(feature = "alloc")]
171170
/// Read the dynamically allocated info for a file.
171+
#[cfg(feature = "unstable_alloc")]
172172
fn get_boxed_info_in<Info: FileProtocolInfo + ?Sized + Debug, A: Allocator>(
173173
&mut self,
174174
allocator: A,

0 commit comments

Comments
 (0)