Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Implement `TryClone` for `Arc<T>`, `Option<T>`, and `Result<T, E>` (#12615)

Nick Fitzgeraldb57de80f1258
diff --git a/crates/core/src/alloc/try_clone.rs b/crates/core/src/alloc/try_clone.rs
index c59b62380b..85837f94c7 100644
--- a/crates/core/src/alloc/try_clone.rs
+++ b/crates/core/src/alloc/try_clone.rs
@@ -1,5 +1,6 @@
use crate::error::OutOfMemory;
use core::mem;
+use std_alloc::sync::Arc;
/// A trait for values that can be cloned, but contain owned, heap-allocated
/// values whose allocations may fail during cloning.
@@ -39,6 +40,40 @@ where
}
}
+impl<T, E> TryClone for Result<T, E>
+where
+ T: TryClone,
+ E: TryClone,
+{
+ #[inline]
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ match self {
+ Ok(x) => Ok(Ok(x.try_clone()?)),
+ Err(e) => Ok(Err(e.try_clone()?)),
+ }
+ }
+}
+
+impl<T> TryClone for Option<T>
+where
+ T: TryClone,
+{
+ #[inline]
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ match self {
+ Some(x) => Ok(Some(x.try_clone()?)),
+ None => Ok(None),
+ }
+ }
+}
+
+impl<T> TryClone for Arc<T> {
+ #[inline]
+ fn try_clone(&self) -> Result<Self, OutOfMemory> {
+ Ok(self.clone())
+ }
+}
+
macro_rules! impl_try_clone_via_clone {
( $( $ty:ty ),* $(,)? ) => {
$(