]> git.lizzy.rs Git - rust.git/commitdiff
Added Arc::try_pin
authorAlex Gaynor <alex.gaynor@gmail.com>
Sat, 22 May 2021 14:24:50 +0000 (10:24 -0400)
committerAlex Gaynor <alex.gaynor@gmail.com>
Thu, 15 Jul 2021 11:32:05 +0000 (07:32 -0400)
This helper is in line with other other allocation helpers on Arc.

library/alloc/src/sync.rs

index 4b34a7dc894ae327d426b41a81c59502a25b4189..d821e71562212ed86fd7d083331da0b78f4d6909 100644 (file)
@@ -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<Arc<T>> {
         unsafe { Pin::new_unchecked(Arc::new(data)) }
     }
 
+    /// Constructs a new `Pin<Arc<T>>`, return an error if allocation fails.
+    #[unstable(feature = "allocator_api", issue = "32838")]
+    #[inline]
+    pub fn try_pin(data: T) -> Result<Pin<Arc<T>>, AllocError> {
+        unsafe { Ok(Pin::new_unchecked(Arc::try_new(data)?)) }
+    }
+
     /// Constructs a new `Arc<T>`, returning an error if allocation fails.
     ///
     /// # Examples