Skip to content

Commit

Permalink
lib: make InPlaceInit available even without alloc feature
Browse files Browse the repository at this point in the history
The only dependency of the InPlaceInit trait on the allocator API is the
AllocError type.  Replace it with Infallible instead, i.e. allow any
error as long as it has an "impl From<Infallible> for MyError" - which
can have a trivial implementation as seen in examples/rror.rs.

While admittedly of limited usefulness due to orphan rules, this is
a first step towards allowing usage of pinned_init entirely without
the allocator API, and therefore on stable Rust.

Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini committed Nov 4, 2024
1 parent 455d214 commit a8ed63c
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ use core::{
#[cfg(feature = "alloc")]
use core::alloc::AllocError;

// Allocations are infallible without the allocator API. In that case, just
// require From<Infallible> for the trait that is passed to the try_* macros,
#[cfg(not(feature = "alloc"))]
type AllocError = Infallible;

#[doc(hidden)]
pub mod __internal;
#[doc(hidden)]
Expand Down Expand Up @@ -1091,7 +1096,6 @@ unsafe impl<T, E> PinInit<T, E> for T {
}

/// Smart pointer that can initialize memory in-place.
#[cfg(feature = "alloc")]
pub trait InPlaceInit<T>: Sized {
/// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this
/// type.
Expand Down

0 comments on commit a8ed63c

Please sign in to comment.