From 3c5272da2f3006c15045311cb3b80d98e3cea82d Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 4 Nov 2024 12:15:27 +0100 Subject: [PATCH] lib: make InPlaceInit available even without alloc feature 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 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 --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index dfda1f1..3accb4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -259,6 +259,11 @@ use core::{ #[cfg(feature = "alloc")] use core::alloc::AllocError; +// Allocations are infallible without the allocator API. In that case, just +// require From for the trait that is passed to the try_* macros, +#[cfg(not(feature = "alloc"))] +type AllocError = Infallible; + #[doc(hidden)] pub mod __internal; #[doc(hidden)] @@ -1156,7 +1161,6 @@ unsafe impl PinInit for T { } /// Smart pointer that can initialize memory in-place. -#[cfg(feature = "alloc")] pub trait InPlaceInit: Sized { /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this /// type.