wasmtime_internal_core/alloc/try_new.rs
1use crate::error::OutOfMemory;
2
3/// Helper function to invoke `<T as TryNew>::try_new`.
4///
5/// # Example
6///
7/// ```
8/// # use wasmtime_internal_core::alloc::*;
9/// # use wasmtime_internal_core::error::Result;
10/// # fn _foo() -> Result<()> {
11/// let boxed = try_new::<Box<u32>>(36)?;
12/// assert_eq!(*boxed, 36);
13/// # Ok(())
14/// # }
15/// ```
16#[inline]
17pub fn try_new<T>(value: T::Value) -> Result<T, OutOfMemory>
18where
19 T: TryNew,
20{
21 TryNew::try_new(value)
22}
23
24/// Extension trait providing fallible allocation for types like `Arc<T>` and
25/// `Box<T>`.
26pub trait TryNew {
27 /// The inner `T` type that is getting wrapped into an `Arc<T>` or `Box<T>`.
28 type Value;
29
30 /// Allocate a new `Self`, returning `Err(OutOfMemory)` on allocation
31 /// failure.
32 fn try_new(value: Self::Value) -> Result<Self, OutOfMemory>
33 where
34 Self: Sized;
35}