From a214911b773b299e37dea0c6f2644a26a5756d4b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 22 May 2021 10:24:50 -0400 Subject: [PATCH] Added Arc::try_pin This helper is in line with other other allocation helpers on Arc. --- library/alloc/src/sync.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 4b34a7dc894..d821e715622 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -19,7 +19,6 @@ use core::mem::size_of_val; use core::mem::{self, align_of_val_raw}; use core::ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}; -#[cfg(not(no_global_oom_handling))] use core::pin::Pin; use core::ptr::{self, NonNull}; #[cfg(not(no_global_oom_handling))] @@ -494,6 +493,13 @@ pub fn pin(data: T) -> Pin> { unsafe { Pin::new_unchecked(Arc::new(data)) } } + /// Constructs a new `Pin>`, return an error if allocation fails. + #[unstable(feature = "allocator_api", issue = "32838")] + #[inline] + pub fn try_pin(data: T) -> Result>, AllocError> { + unsafe { Ok(Pin::new_unchecked(Arc::try_new(data)?)) } + } + /// Constructs a new `Arc`, returning an error if allocation fails. /// /// # Examples -- 2.44.0