osom_lib_try_clone/
impls.rs1use core::{convert::Infallible, marker::PhantomData, mem::MaybeUninit};
2
3use super::TryClone;
4
5macro_rules! try_clone_copy_impl {
6 ($($t:ty),*) => {
7 $(
8 impl TryClone for $t {
9 type Error = Infallible;
10 #[inline(always)]
11 fn try_clone(&self) -> Result<Self, Self::Error> {
12 Ok(*self)
13 }
14 }
15 )*
16 };
17}
18
19try_clone_copy_impl!(
20 bool,
21 u8,
22 u16,
23 u32,
24 u64,
25 u128,
26 i8,
27 i16,
28 i32,
29 i64,
30 i128,
31 f32,
32 f64,
33 (),
34 char,
35 usize,
36 isize
37);
38
39impl<T: TryClone> TryClone for Option<T> {
40 type Error = <T as TryClone>::Error;
41
42 fn try_clone(&self) -> Result<Self, Self::Error> {
43 match self {
44 Some(value) => Ok(Some(value.try_clone()?)),
45 None => Ok(None),
46 }
47 }
48}
49
50impl<const N: usize, T: TryClone> TryClone for [T; N] {
51 type Error = <T as TryClone>::Error;
52
53 fn try_clone(&self) -> Result<Self, Self::Error> {
54 let mut result = MaybeUninit::<[T; N]>::uninit();
55 unsafe {
56 let mut ptr = result.as_mut_ptr().cast::<T>();
57 for value in self {
58 ptr.write(value.try_clone()?);
59 ptr = ptr.add(1);
60 }
61 Ok(result.assume_init())
62 }
63 }
64}
65
66impl<T: TryClone> TryClone for PhantomData<T> {
67 type Error = Infallible;
68
69 fn try_clone(&self) -> Result<Self, Self::Error> {
70 Ok(PhantomData)
71 }
72}