Skip to content

Commit fdd6bcb

Browse files
committed
add zeroed() & Zeroable::zeroed() functions
`zeroed()` returns a zeroed out value of a sized type implementing `Zeroable`. The function is added as a free standing function, in addition to an associated function on `Zeroable`, because then it can be marked `const` (functions in traits can't be const at the moment). Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent 228fd93 commit fdd6bcb

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- `unsafe fn cast_[pin_]init()` functions to unsafely change the initialized type of an initializer
1616
- `impl<T, E> [Pin]Init<T, E> for Result<T, E>`, so results are now (pin-)initializers
1717
- add `Zeroable::init_zeroed()` delegating to `init_zeroed()`
18+
- add new `zeroed()`, a safe version of `mem::zeroed()` and also provide it via `Zeroable::zeroed()`
1819

1920
### Changed
2021

src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,33 @@ pub unsafe trait Zeroable {
15061506
{
15071507
init_zeroed()
15081508
}
1509+
1510+
/// Create a `Self` consisting of all zeroes.
1511+
///
1512+
/// Whenever a type implements [`Zeroable`], this function should be preferred over
1513+
/// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
1514+
///
1515+
/// # Examples
1516+
///
1517+
/// ```
1518+
/// use pin_init::{Zeroable, zeroed};
1519+
///
1520+
/// #[derive(Zeroable)]
1521+
/// struct Point {
1522+
/// x: u32,
1523+
/// y: u32,
1524+
/// }
1525+
///
1526+
/// let point: Point = zeroed();
1527+
/// assert_eq!(point.x, 0);
1528+
/// assert_eq!(point.y, 0);
1529+
/// ```
1530+
fn zeroed() -> Self
1531+
where
1532+
Self: Sized,
1533+
{
1534+
zeroed()
1535+
}
15091536
}
15101537

15111538
/// Marker trait for types that allow `Option<Self>` to be set to all zeroes in order to write
@@ -1534,6 +1561,31 @@ pub fn init_zeroed<T: Zeroable>() -> impl Init<T> {
15341561
}
15351562
}
15361563

1564+
/// Create a `T` consisting of all zeroes.
1565+
///
1566+
/// Whenever a type implements [`Zeroable`], this function should be preferred over
1567+
/// [`core::mem::zeroed()`] or using `MaybeUninit<T>::zeroed().assume_init()`.
1568+
///
1569+
/// # Examples
1570+
///
1571+
/// ```
1572+
/// use pin_init::{Zeroable, zeroed};
1573+
///
1574+
/// #[derive(Zeroable)]
1575+
/// struct Point {
1576+
/// x: u32,
1577+
/// y: u32,
1578+
/// }
1579+
///
1580+
/// let point: Point = zeroed();
1581+
/// assert_eq!(point.x, 0);
1582+
/// assert_eq!(point.y, 0);
1583+
/// ```
1584+
pub const fn zeroed<T: Zeroable>() -> T {
1585+
// SAFETY:By the type invariants of `Zeroable`, all zeroes is a valid bit pattern for `T`.
1586+
unsafe { core::mem::zeroed() }
1587+
}
1588+
15371589
macro_rules! impl_zeroable {
15381590
($($({$($generics:tt)*})? $t:ty, )*) => {
15391591
// SAFETY: Safety comments written in the macro invocation.

0 commit comments

Comments
 (0)