Skip to content

Commit

Permalink
Merge branch '💥-zeroable' into 🦆
Browse files Browse the repository at this point in the history
  • Loading branch information
yvt committed Nov 20, 2022
2 parents 91ce918 + 9263be6 commit b6de1c0
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 125 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/r3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub use r3_core::{bag, hunk, kernel, time};
///
/// [1]: r3_core#stability
pub mod utils {
pub use r3_core::utils::{Init, ZeroInit};
pub use r3_core::utils::{Init, Zeroable, ZeroableInOption};
}

/// The prelude module.
Expand Down
13 changes: 13 additions & 0 deletions src/r3_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- **Breaking:** `{Bind,Hunk}Definer::zeroed` now requires `T: Zeroable` instead of `T: ZeroInit`.

### Added

- `r3_core::utils::{Zeroable,ZeroableInOption}` (re-exported from `bytemuck ^1`)
- Implement `Zeroable` on `r3_core::time::{Duration, Time}`

### Removed

- **Breaking:** `r3_core::utils::ZeroInit` (superseded by `Zeroable`)

## [0.1.4] - 2022-11-16

### Changed
Expand Down
1 change: 1 addition & 0 deletions src/r3_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ seq-macro = { version = "0.3.0" }
svgbobdoc = { version = "0.3.0" }
tokenlock = { version = "0.3.4", default-features = false }
arrayvec = { version = "0.7.1", default-features = false }
bytemuck = { version = "1.12.3", features = ["derive"] }
bitflags = { version = "1.2.1" }
macropol = { version = "0.1.2" }
either = { version = "1.6.1", default-features = false }
Expand Down
10 changes: 6 additions & 4 deletions src/r3_core/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ use crate::{
closure::Closure,
hunk::Hunk,
kernel::{self, cfg, prelude::*, raw, raw_cfg, StartupHook},
utils::{refcell::RefCell, ComptimeVec, ConstAllocator, Init, PhantomInvariant, ZeroInit},
utils::{refcell::RefCell, ComptimeVec, ConstAllocator, Init, PhantomInvariant, Zeroable},
};

mod sorter;
Expand Down Expand Up @@ -213,8 +213,10 @@ impl<T> BindData<T> {
}
}

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

// Main configuration interface
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -466,8 +468,8 @@ impl<System>
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/r3_core/src/hunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::{

use crate::{
kernel::{self, cfg, hunk, raw, raw_cfg, Cfg, StartupHook},
utils::{Init, ZeroInit},
utils::{Init, Zeroable},
};

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

Expand Down
8 changes: 2 additions & 6 deletions src/r3_core/src/time/duration.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core::{fmt, ops};

use crate::utils::{Init, ZeroInit};
use crate::utils::{Init, Zeroable};

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

// Safety: `Duration` is `repr(transparent)` and the only inner field is `i32`,
// which is `ZeroInit`
unsafe impl ZeroInit for Duration {}

impl Default for Duration {
fn default() -> Self {
Self::INIT
Expand Down
8 changes: 2 additions & 6 deletions src/r3_core/src/time/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::{fmt, ops};

use crate::{
time::Duration,
utils::{Init, ZeroInit},
utils::{Init, Zeroable},
};

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

// Safety: `Time` is `repr(transparent)` and the only inner field is `u64`,
// which is `ZeroInit`
unsafe impl ZeroInit for Time {}

impl Default for Time {
fn default() -> Self {
Self::INIT
Expand Down
13 changes: 11 additions & 2 deletions src/r3_core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
//! **This module is exempt from [the API stability guarantee][1]** unless
//! specified otherwise. It's exposed mostly because it's needed by macros.
//!
//! # Re-exports
//!
//! - [`Zeroable`], [`ZeroableInOption`]: Re-exported from [`bytemuck`]` ^1`.
//! `Zeroable` is a marker trait used in [`HunkDefiner::zeroed`][2] and
//! suchlike. It can be derived for struct types.
//! These re-exports are subject to the application-side API stability
//! guarantee.
//!
//! [1]: crate#stability
//! [2]: crate::hunk::HunkDefiner::zeroed
use core::marker::PhantomData;

/// Conditional type
Expand Down Expand Up @@ -65,11 +74,11 @@ mod init;
pub mod mem;
mod rawcell;
pub(crate) mod refcell;
mod zeroinit;
pub use aligned_storage::*;
pub use init::*;
pub use rawcell::*;
pub use zeroinit::*;

pub use bytemuck::{Zeroable, ZeroableInOption};

/// A phantom type that is invariant over `T`.
pub type PhantomInvariant<T> = core::marker::PhantomData<fn(T) -> T>;
Expand Down
6 changes: 4 additions & 2 deletions src/r3_core/src/utils/rawcell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::cell::UnsafeCell;

use crate::utils::{Init, ZeroInit};
use crate::utils::{Init, Zeroable};

/// Like `UnsafeCell`, but implements `Sync`.
#[derive(Debug)]
Expand Down Expand Up @@ -28,4 +28,6 @@ impl<T: ?Sized> RawCell<T> {
impl<T: Init> Init for RawCell<T> {
const INIT: Self = RawCell::new(T::INIT);
}
unsafe impl<T: ZeroInit> ZeroInit for RawCell<T> {}
// FIXME: Derive this when <https://github.com/Lokathor/bytemuck/pull/148> is
// merged
unsafe impl<T: Zeroable> Zeroable for RawCell<T> {}
98 changes: 0 additions & 98 deletions src/r3_core/src/utils/zeroinit.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/r3_kernel/src/utils/int.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::{fmt, ops};
use num_integer::Integer;

use super::{Init, ZeroInit};
use super::{Init, Zeroable};

/// Get the smallest unsigned integer type capable of representing the specified
/// value.
Expand Down Expand Up @@ -31,7 +31,7 @@ pub trait BinInteger:
+ ops::DivAssign
+ fmt::Debug
+ Init
+ ZeroInit
+ Zeroable
+ Send
+ Sync
+ num_traits::cast::ToPrimitive
Expand Down
2 changes: 1 addition & 1 deletion src/r3_kernel/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod rawcell;
mod vec;
pub use self::{aligned_storage::*, freeze::*, int::*, prio_bitmap::*, rawcell::*, vec::*};
use r3_core::utils::{AllocError, Allocator, ConstAllocator};
pub use r3_core::utils::{Init, ZeroInit};
pub use r3_core::utils::{Init, Zeroable};
pub(crate) mod panicking;

/// A "type function" producing a type.
Expand Down

0 comments on commit b6de1c0

Please sign in to comment.