Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,10 @@ pub use ::pin_init_internal::pin_data;
/// ```
pub use ::pin_init_internal::pinned_drop;

/// Derives the [`Zeroable`] trait for the given struct.
/// Derives the [`Zeroable`] trait for the given `struct` or `union`.
///
/// This can only be used for structs where every field implements the [`Zeroable`] trait.
/// This can only be used for `struct`s/`union`s where every field implements the [`Zeroable`]
/// trait.
///
/// # Examples
///
Expand All @@ -406,14 +407,25 @@ pub use ::pin_init_internal::pinned_drop;
///
/// #[derive(Zeroable)]
/// pub struct DriverData {
/// id: i64,
/// pub(crate) id: i64,
/// buf_ptr: *mut u8,
/// len: usize,
/// }
/// ```
///
/// ```
/// use pin_init::Zeroable;
///
/// #[derive(Zeroable)]
/// pub union SignCast {
/// signed: i64,
/// unsigned: u64,
/// }
/// ```
pub use ::pin_init_internal::Zeroable;

/// Derives the [`Zeroable`] trait for the given struct if all fields implement [`Zeroable`].
/// Derives the [`Zeroable`] trait for the given `struct` or `union` if all fields implement
/// [`Zeroable`].
///
/// Contrary to the derive macro named [`macro@Zeroable`], this one silently fails when a field
/// doesn't implement [`Zeroable`].
Expand All @@ -426,15 +438,15 @@ pub use ::pin_init_internal::Zeroable;
/// // implmements `Zeroable`
/// #[derive(MaybeZeroable)]
/// pub struct DriverData {
/// id: i64,
/// pub(crate) id: i64,
/// buf_ptr: *mut u8,
/// len: usize,
/// }
///
/// // does not implmement `Zeroable`
/// #[derive(MaybeZeroable)]
/// pub struct DriverData2 {
/// id: i64,
/// pub(crate) id: i64,
/// buf_ptr: *mut u8,
/// len: usize,
/// // this field doesn't implement `Zeroable`
Expand Down Expand Up @@ -1580,7 +1592,7 @@ impl_tuple_zeroable!(A, B, C, D, E, F, G, H, I, J);
/// });
/// ```
pub trait Wrapper<T> {
/// Create an pin-initializer for a [`Self`] containing `T` form the `value_init` initializer.
/// Creates an pin-initializer for a [`Self`] containing `T` from the `value_init` initializer.
fn pin_init<E>(value_init: impl PinInit<T, E>) -> impl PinInit<Self, E>;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/compile-fail/zeroable/not_all_fields_zeroable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate pin_init;
use pin_init::*;

#[derive(Zeroable)]
struct Foo {
a: usize,
b: &'static Foo,
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/compile-fail/zeroable/not_all_fields_zeroable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the trait bound `&'static Foo: pin_init::Zeroable` is not satisfied
--> tests/ui/compile-fail/zeroable/not_all_fields_zeroable.rs:7:8
|
7 | b: &'static Foo,
| ^^^^^^^^^^^^ the trait `pin_init::Zeroable` is not implemented for `&'static Foo`
|
note: required by a bound in `assert_zeroable`
--> tests/ui/compile-fail/zeroable/not_all_fields_zeroable.rs:4:10
|
4 | #[derive(Zeroable)]
| ^^^^^^^^ required by this bound in `assert_zeroable`
= note: this error originates in the macro `::pin_init::__derive_zeroable` which comes from the expansion of the derive macro `Zeroable` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider removing the leading `&`-reference
|
7 - b: &'static Foo,
7 + b: Foo,
|
42 changes: 42 additions & 0 deletions tests/ui/expand/zeroable.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use pin_init::*;
struct Foo {
a: usize,
pub(crate) b: usize,
}
#[automatically_derived]
unsafe impl ::pin_init::Zeroable for Foo {}
const _: () = {
fn assert_zeroable<T: ?::core::marker::Sized + ::pin_init::Zeroable>() {}
fn ensure_zeroable() {
assert_zeroable::<usize>();
assert_zeroable::<usize>();
}
};
struct Bar {
a: usize,
b: &'static usize,
}
#[automatically_derived]
unsafe impl ::pin_init::Zeroable for Bar
where
usize: for<'__dummy> ::pin_init::Zeroable,
&'static usize: for<'__dummy> ::pin_init::Zeroable,
{}
trait Trait {}
struct WithGenerics<'a, T, U: Trait> {
a: T,
u: &'a U,
}
#[automatically_derived]
unsafe impl<
'a,
T: ::pin_init::Zeroable,
U: ::pin_init::Zeroable + Trait,
> ::pin_init::Zeroable for WithGenerics<'a, T, U> {}
const _: () = {
fn assert_zeroable<T: ?::core::marker::Sized + ::pin_init::Zeroable>() {}
fn ensure_zeroable<'a, T: ::pin_init::Zeroable, U: ::pin_init::Zeroable + Trait>() {
assert_zeroable::<T>();
assert_zeroable::<&'a U>();
}
};
21 changes: 21 additions & 0 deletions tests/ui/expand/zeroable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use pin_init::*;

#[derive(Zeroable)]
struct Foo {
a: usize,
pub(crate) b: usize,
}

#[derive(MaybeZeroable)]
struct Bar {
a: usize,
b: &'static usize,
}

trait Trait {}

#[derive(Zeroable)]
struct WithGenerics<'a, T, U: Trait> {
a: T,
u: &'a U,
}