Skip to content

Commit b6de1c0

Browse files
committed
Merge branch '💥-zeroable' into 🦆
2 parents 91ce918 + 9263be6 commit b6de1c0

File tree

13 files changed

+47
-125
lines changed

13 files changed

+47
-125
lines changed

‎Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/r3/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use r3_core::{bag, hunk, kernel, time};
6262
///
6363
/// [1]: r3_core#stability
6464
pub mod utils {
65-
pub use r3_core::utils::{Init, ZeroInit};
65+
pub use r3_core::utils::{Init, Zeroable, ZeroableInOption};
6666
}
6767

6868
/// The prelude module.

‎src/r3_core/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **Breaking:** `{Bind,Hunk}Definer::zeroed` now requires `T: Zeroable` instead of `T: ZeroInit`.
13+
14+
### Added
15+
16+
- `r3_core::utils::{Zeroable,ZeroableInOption}` (re-exported from `bytemuck ^1`)
17+
- Implement `Zeroable` on `r3_core::time::{Duration, Time}`
18+
19+
### Removed
20+
21+
- **Breaking:** `r3_core::utils::ZeroInit` (superseded by `Zeroable`)
22+
1023
## [0.1.4] - 2022-11-16
1124

1225
### Changed

‎src/r3_core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ seq-macro = { version = "0.3.0" }
2828
svgbobdoc = { version = "0.3.0" }
2929
tokenlock = { version = "0.3.4", default-features = false }
3030
arrayvec = { version = "0.7.1", default-features = false }
31+
bytemuck = { version = "1.12.3", features = ["derive"] }
3132
bitflags = { version = "1.2.1" }
3233
macropol = { version = "0.1.2" }
3334
either = { version = "1.6.1", default-features = false }

‎src/r3_core/src/bind.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ use crate::{
172172
closure::Closure,
173173
hunk::Hunk,
174174
kernel::{self, cfg, prelude::*, raw, raw_cfg, StartupHook},
175-
utils::{refcell::RefCell, ComptimeVec, ConstAllocator, Init, PhantomInvariant, ZeroInit},
175+
utils::{refcell::RefCell, ComptimeVec, ConstAllocator, Init, PhantomInvariant, Zeroable},
176176
};
177177

178178
mod sorter;
@@ -213,8 +213,10 @@ impl<T> BindData<T> {
213213
}
214214
}
215215

216+
// FIXME: Derive this when <https://github.com/Lokathor/bytemuck/pull/148> is
217+
// merged
216218
// Safety: Zero-initialization is valid for `MaybeUninit`
217-
unsafe impl<T> ZeroInit for BindData<T> {}
219+
unsafe impl<T> Zeroable for BindData<T> {}
218220

219221
// Main configuration interface
220222
// ----------------------------------------------------------------------------
@@ -466,8 +468,8 @@ impl<System>
466468
}
467469

468470
/// Zero-initialize the binding contents.
469-
pub const fn zeroed<T: ZeroInit>(self) -> BindDefiner<System, (), FnBindNever<T>> {
470-
// Safety: `T: ZeroInit` means it's safe to zero-initialize
471+
pub const fn zeroed<T: Zeroable>(self) -> BindDefiner<System, (), FnBindNever<T>> {
472+
// Safety: `T: Zeroable` means it's safe to zero-initialize
471473
unsafe { self.zeroed_unchecked() }
472474
}
473475

‎src/r3_core/src/hunk.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::{
99

1010
use crate::{
1111
kernel::{self, cfg, hunk, raw, raw_cfg, Cfg, StartupHook},
12-
utils::{Init, ZeroInit},
12+
utils::{Init, Zeroable},
1313
};
1414

1515
/// The priority of the [startup hooks] used to initialize [typed hunks]. It has
@@ -123,9 +123,9 @@ impl<System: raw::KernelBase + cfg::KernelStatic, T: ?Sized, InitTag>
123123
/// Zero-initialize the hunk.
124124
pub const fn zeroed(self) -> HunkDefiner<System, T, ZeroInitTag>
125125
where
126-
T: ZeroInit,
126+
T: Zeroable,
127127
{
128-
// Safety: `T: ZeroInit`, so it's zero-initializable
128+
// Safety: `T: Zeroable`, so it's zero-initializable
129129
unsafe { self.zeroed_unchecked() }
130130
}
131131

‎src/r3_core/src/time/duration.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use core::{fmt, ops};
22

3-
use crate::utils::{Init, ZeroInit};
3+
use crate::utils::{Init, Zeroable};
44

55
/// Represents a signed time span used by the API surface of R3-OS.
66
///
77
/// `Duration` is backed by `i32` and can represent the range
88
/// [-35′47.483648″, +35′47.483647″] with microsecond precision.
9-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Zeroable)]
1010
#[repr(transparent)]
1111
pub struct Duration {
1212
micros: i32,
@@ -16,10 +16,6 @@ impl Init for Duration {
1616
const INIT: Self = Self::ZERO;
1717
}
1818

19-
// Safety: `Duration` is `repr(transparent)` and the only inner field is `i32`,
20-
// which is `ZeroInit`
21-
unsafe impl ZeroInit for Duration {}
22-
2319
impl Default for Duration {
2420
fn default() -> Self {
2521
Self::INIT

‎src/r3_core/src/time/time.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::{fmt, ops};
22

33
use crate::{
44
time::Duration,
5-
utils::{Init, ZeroInit},
5+
utils::{Init, Zeroable},
66
};
77

88
/// Represents a timestamp used by the API surface of R3-OS.
@@ -13,7 +13,7 @@ use crate::{
1313
///
1414
/// `Time` is backed by `u64` and can represent up to 213,503,982 days with
1515
/// microsecond precision.
16-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Zeroable)]
1717
#[repr(transparent)]
1818
pub struct Time {
1919
micros: u64,
@@ -23,10 +23,6 @@ impl Init for Time {
2323
const INIT: Self = Self::ZERO;
2424
}
2525

26-
// Safety: `Time` is `repr(transparent)` and the only inner field is `u64`,
27-
// which is `ZeroInit`
28-
unsafe impl ZeroInit for Time {}
29-
3026
impl Default for Time {
3127
fn default() -> Self {
3228
Self::INIT

‎src/r3_core/src/utils/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
//! **This module is exempt from [the API stability guarantee][1]** unless
44
//! specified otherwise. It's exposed mostly because it's needed by macros.
55
//!
6+
//! # Re-exports
7+
//!
8+
//! - [`Zeroable`], [`ZeroableInOption`]: Re-exported from [`bytemuck`]` ^1`.
9+
//! `Zeroable` is a marker trait used in [`HunkDefiner::zeroed`][2] and
10+
//! suchlike. It can be derived for struct types.
11+
//! These re-exports are subject to the application-side API stability
12+
//! guarantee.
13+
//!
614
//! [1]: crate#stability
15+
//! [2]: crate::hunk::HunkDefiner::zeroed
716
use core::marker::PhantomData;
817

918
/// Conditional type
@@ -65,11 +74,11 @@ mod init;
6574
pub mod mem;
6675
mod rawcell;
6776
pub(crate) mod refcell;
68-
mod zeroinit;
6977
pub use aligned_storage::*;
7078
pub use init::*;
7179
pub use rawcell::*;
72-
pub use zeroinit::*;
80+
81+
pub use bytemuck::{Zeroable, ZeroableInOption};
7382

7483
/// A phantom type that is invariant over `T`.
7584
pub type PhantomInvariant<T> = core::marker::PhantomData<fn(T) -> T>;

‎src/r3_core/src/utils/rawcell.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::cell::UnsafeCell;
22

3-
use crate::utils::{Init, ZeroInit};
3+
use crate::utils::{Init, Zeroable};
44

55
/// Like `UnsafeCell`, but implements `Sync`.
66
#[derive(Debug)]
@@ -28,4 +28,6 @@ impl<T: ?Sized> RawCell<T> {
2828
impl<T: Init> Init for RawCell<T> {
2929
const INIT: Self = RawCell::new(T::INIT);
3030
}
31-
unsafe impl<T: ZeroInit> ZeroInit for RawCell<T> {}
31+
// FIXME: Derive this when <https://github.com/Lokathor/bytemuck/pull/148> is
32+
// merged
33+
unsafe impl<T: Zeroable> Zeroable for RawCell<T> {}

0 commit comments

Comments
 (0)