Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/core/src/alloc/try_clone.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 ),* $(,)? ) => {
$(
Expand Down