Skip to content

Dev/result #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `MaybeZeroable` derive macro to try to derive `Zeroable`, but not error if not all fields
implement it.
- `unsafe fn cast_[pin_]init()` functions to unsafely change the initialized type of an initializer
- `impl<T, E> [Pin]Init<T, E> for Result<T, E>`, so results are now (pin-)initializers

### Changed

- `InPlaceInit` now only exists when the `alloc` or `std` features are enabled
- added support for visibility in `Zeroable` derive macro
- added support for `union`s in `Zeroable` derive macro
- renamed the crate from `pinned-init` to `pin-init` and `pinned-init-macro` to `pin-init-internal`
- blanket impls of `Init` and `PinInit` from `impl<T, E> [Pin]Init<T, E> for T` to
`impl<T> [Pin]Init<T> for T`

### Fixed

Expand Down
40 changes: 32 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,20 +1390,44 @@ where
unsafe { pin_init_from_closure(init) }
}

// SAFETY: Every type can be initialized by-value.
unsafe impl<T, E> Init<T, E> for T {
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
// SAFETY: TODO.
// SAFETY: the `__init` function always returns `Ok(())` and initializes every field of `slot`.
unsafe impl<T> Init<T> for T {
unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible> {
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
unsafe { slot.write(self) };
Ok(())
}
}

// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
unsafe impl<T, E> PinInit<T, E> for T {
// SAFETY: the `__pinned_init` function always returns `Ok(())` and initializes every field of
// `slot`. Additionally, all pinning invariants of `T` are upheld.
unsafe impl<T> PinInit<T> for T {
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible> {
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
unsafe { slot.write(self) };
Ok(())
}
}

// SAFETY: when the `__init` function returns with
// - `Ok(())`, `slot` was initialized and all pinned invariants of `T` are upheld.
// - `Err(err)`, slot was not written to.
unsafe impl<T, E> Init<T, E> for Result<T, E> {
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
unsafe { slot.write(self?) };
Ok(())
}
}

// SAFETY: when the `__pinned_init` function returns with
// - `Ok(())`, `slot` was initialized and all pinned invariants of `T` are upheld.
// - `Err(err)`, slot was not written to.
unsafe impl<T, E> PinInit<T, E> for Result<T, E> {
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
// SAFETY: TODO.
unsafe { self.__init(slot) }
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
unsafe { slot.write(self?) };
Ok(())
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/ui/compile-fail/init/invalid_init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ error[E0277]: the trait bound `impl pin_init::PinInit<Bar>: Init<Bar>` is not sa
| |______the trait `Init<Bar>` is not implemented for `impl pin_init::PinInit<Bar>`
| required by a bound introduced by this call
|
= help: the trait `Init<T, E>` is implemented for `ChainInit<I, F, T, E>`
= help: the following other types implement trait `Init<T, E>`:
ChainInit<I, F, T, E>
Result<T, E>
= note: this error originates in the macro `$crate::__init_internal` which comes from the expansion of the macro `init` (in Nightly builds, run with -Z macro-backtrace for more info)
2 changes: 1 addition & 1 deletion tests/ui/compile-fail/pin_data/missing_pin.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ error[E0277]: the trait bound `impl PinInit<usize>: Init<usize, _>` is not satis
| |__________^ the trait `Init<usize, _>` is not implemented for `impl PinInit<usize>`
|
= help: the trait `Init<usize, _>` is not implemented for `impl PinInit<usize>`
but trait `Init<impl PinInit<usize>, _>` is implemented for it
but trait `Init<impl PinInit<usize>, Infallible>` is implemented for it
= help: for that trait implementation, expected `impl PinInit<usize>`, found `usize`
note: required by a bound in `__ThePinData::a`
--> tests/ui/compile-fail/pin_data/missing_pin.rs:4:1
Expand Down