-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtry_clone.rs
More file actions
150 lines (135 loc) · 3.1 KB
/
try_clone.rs
File metadata and controls
150 lines (135 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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.
pub trait TryClone: Sized {
/// Attempt to clone `self`, returning an error if any allocation fails
/// during cloning.
fn try_clone(&self) -> Result<Self, OutOfMemory>;
/// Clone `self`, panicking on allocation failure.
fn clone_panic_on_oom(&self) -> Self {
use super::PanicOnOom as _;
self.try_clone().panic_on_oom()
}
}
impl<T> TryClone for *mut T
where
T: ?Sized,
{
#[inline]
fn try_clone(&self) -> Result<Self, OutOfMemory> {
Ok(*self)
}
}
impl<T> TryClone for core::ptr::NonNull<T>
where
T: ?Sized,
{
#[inline]
fn try_clone(&self) -> Result<Self, OutOfMemory> {
Ok(*self)
}
}
impl<'a, T> TryClone for &'a T
where
T: ?Sized,
{
#[inline]
fn try_clone(&self) -> Result<Self, OutOfMemory> {
Ok(*self)
}
}
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 ),* $(,)? ) => {
$(
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;
}
impl<T> TryClone for mem::ManuallyDrop<T>
where
T: TryClone,
{
fn try_clone(&self) -> Result<Self, OutOfMemory> {
Ok(mem::ManuallyDrop::new((**self).try_clone()?))
}
}
#[cfg(feature = "std")]
impl_try_clone_via_clone! {
std::hash::RandomState
}
impl_try_clone_via_clone! {
hashbrown::DefaultHashBuilder
}