Skip to content

Commit 228fd93

Browse files
committed
add Zeroable::init_zeroed
The trait function delegates to the already existing `init_zeroed` function that returns a zeroing initializer for `Self`. The syntax `..Zeroable::init_zeroed()` is already used by the initialization macros to initialize all fields that are not mentioned in the initializer with zero. Therefore it is expected that the function also exists on the trait. Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent fb143a3 commit 228fd93

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
implement it.
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
17+
- add `Zeroable::init_zeroed()` delegating to `init_zeroed()`
1718

1819
### Changed
1920

src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,18 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
14951495
/// ```rust,ignore
14961496
/// let val: Self = unsafe { core::mem::zeroed() };
14971497
/// ```
1498-
pub unsafe trait Zeroable {}
1498+
pub unsafe trait Zeroable {
1499+
/// Create a new zeroed `Self`.
1500+
///
1501+
/// The returned initializer will write `0x00` to every byte of the given `slot`.
1502+
#[inline]
1503+
fn init_zeroed() -> impl Init<Self>
1504+
where
1505+
Self: Sized,
1506+
{
1507+
init_zeroed()
1508+
}
1509+
}
14991510

15001511
/// Marker trait for types that allow `Option<Self>` to be set to all zeroes in order to write
15011512
/// `None` to that location.

tests/ui/compile-fail/init/missing_comma_with_zeroable.stderr

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
error[E0782]: expected a type, found a trait
2-
--> tests/ui/compile-fail/init/missing_comma_with_zeroable.rs:12:15
3-
|
4-
12 | a: 0..Zeroable::init_zeroed()
5-
| ^^^^^^^^
6-
|
7-
help: you can add the `dyn` keyword if you want a trait object
8-
|
9-
12 | a: 0..<dyn Zeroable>::init_zeroed()
10-
| ++++ +
11-
121
error[E0308]: mismatched types
132
--> tests/ui/compile-fail/init/missing_comma_with_zeroable.rs:11:13
143
|

0 commit comments

Comments
 (0)