A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Define a `TryClone` trait for cloning with fallible allocation (#12519)
Nick Fitzgeraldb8e97b5b9015
diff --git a/crates/core/src/alloc.rs b/crates/core/src/alloc.rsindex a754f7966b..9cb048c824 100644--- a/crates/core/src/alloc.rs+++ b/crates/core/src/alloc.rs@@ -2,6 +2,7 @@mod arc;mod boxed;+mod try_clone;mod try_collect;mod try_new;mod vec;@@ -11,6 +12,7 @@ pub use boxed::{new_boxed_slice_from_fallible_iter, new_boxed_slice_from_iter,new_boxed_slice_from_iter_with_len, new_uninit_boxed_slice,};+pub use try_clone::TryClone;pub use try_collect::{TryCollect, TryExtend, TryFromIterator};pub use try_new::{TryNew, try_new};pub use vec::Vec;diff --git a/crates/core/src/alloc/boxed.rs b/crates/core/src/alloc/boxed.rsindex 28e3c788a0..f7a273a6ab 100644--- a/crates/core/src/alloc/boxed.rs+++ b/crates/core/src/alloc/boxed.rs@@ -1,4 +1,4 @@-use super::{TryNew, Vec, try_alloc};+use super::{TryClone, TryNew, Vec, try_alloc};use crate::error::OutOfMemory;use core::{alloc::Layout,@@ -42,6 +42,31 @@ impl<T> TryNew for Box<T> {}}+impl<T> TryClone for Box<T>+where+ T: TryClone,+{+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ let b = new_uninit_box::<T>()?;+ let v = (**self).try_clone()?;+ Ok(Box::write(b, v))+ }+}++impl<T> TryClone for Box<[T]>+where+ T: TryClone,+{+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ let mut builder = BoxedSliceBuilder::new(self.len())?;+ for v in &*self {+ builder.push(v.try_clone()?).expect("reserved capacity");+ }+ debug_assert_eq!(builder.init_len(), builder.capacity());+ Ok(builder.finish())+ }+}+/// Allocate a new `Box<[MaybeUninit<T>]>` of the given length with/// uninitialized contents, returning `Err(OutOfMemory)` on allocation failure.///diff --git a/crates/core/src/alloc/try_clone.rs b/crates/core/src/alloc/try_clone.rsnew file mode 100644index 0000000000..73e7d0a3d6--- /dev/null+++ b/crates/core/src/alloc/try_clone.rs@@ -0,0 +1,74 @@+use crate::error::OutOfMemory;++/// A trait for values that can be cloned, but contain owned, heap-allocated+/// values whose allocations may fail during cloning.+pub trait TryClone: Sized {+ /// Attempt to clone `self`, returning an error if any allocation fails+ /// during cloning.+ fn try_clone(&self) -> Result<Self, OutOfMemory>;+}++impl<T> TryClone for *mut T {+ #[inline]+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ Ok(*self)+ }+}++impl<T> TryClone for core::ptr::NonNull<T> {+ #[inline]+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ Ok(*self)+ }+}++macro_rules! impl_try_clone_via_clone {+ ( $( $ty:ty ),* $(,)? ) => {+ $(+ impl TryClone for $ty {+ #[inline]+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ Ok(self.clone())+ }+ }+ )*+ };+}++impl_try_clone_via_clone! {+ bool, char,+ u8, u16, u32, u64, u128, usize,+ i8, i16, i32, i64, i128, isize,+ f32, f64,+}++macro_rules! tuples {+ ( $( $( $t:ident ),* );*) => {+ $(+ impl<$($t),*> TryClone for ( $($t,)* )+ where+ $( $t: TryClone ),*+ {+ #[inline]+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ #[allow(non_snake_case, reason = "macro code")]+ let ( $($t,)* ) = self;+ Ok(( $( $t.try_clone()?, )* ))+ }+ }+ )*+ };+}++tuples! {+ A;+ A, B;+ A, B, C;+ A, B, C, D;+ A, B, C, D, E;+ A, B, C, D, E, F;+ A, B, C, D, E, F, G;+ A, B, C, D, E, F, G, H;+ A, B, C, D, E, F, G, H, I;+ A, B, C, D, E, F, G, H, I, J;+}diff --git a/crates/core/src/alloc/vec.rs b/crates/core/src/alloc/vec.rsindex ada602acc0..730b912343 100644--- a/crates/core/src/alloc/vec.rs+++ b/crates/core/src/alloc/vec.rs@@ -1,4 +1,4 @@-use crate::alloc::try_realloc;+use crate::alloc::{TryClone, try_realloc};use crate::error::OutOfMemory;use core::{fmt, mem,@@ -29,6 +29,19 @@ impl<T: fmt::Debug> fmt::Debug for Vec<T> {}}+impl<T> TryClone for Vec<T>+where+ T: TryClone,+{+ fn try_clone(&self) -> Result<Self, OutOfMemory> {+ let mut v = Vec::with_capacity(self.len())?;+ for x in self {+ v.push(x.try_clone()?).expect("reserved capacity");+ }+ Ok(v)+ }+}+impl<T> Vec<T> {/// Same as [`std::vec::Vec::new`].pub fn new() -> Self {diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rsindex aca49a2bd7..18c00288bc 100644--- a/crates/environ/src/collections.rs+++ b/crates/environ/src/collections.rs@@ -9,7 +9,7 @@ pub use entity_set::EntitySet;pub use hash_set::HashSet;pub use primary_map::PrimaryMap;pub use secondary_map::SecondaryMap;-pub use wasmtime_core::alloc::{TryNew, Vec, try_new};+pub use wasmtime_core::alloc::{TryClone, TryNew, Vec, try_new};/// Collections which abort on OOM.//