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
use core::ops::Deref;

#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
#[contract_trait]
pub trait Backup: Sized {
    #[must_use]
    unsafe fn backup_unchecked(&self) -> Self;

    fn backup(&self) -> BackedUp<Self> {
        BackedUp(unsafe { self.backup_unchecked() })
    }
}

#[derive(Debug, TypeLayout)]
#[repr(transparent)]
pub struct BackedUp<T: Backup>(pub(crate) T);

impl<T: Backup> Deref for BackedUp<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: Clone> Backup for T {
    #[inline]
    default unsafe fn backup_unchecked(&self) -> Self {
        self.__contracts_impl_backup_unchecked()
    }

    #[inline]
    default unsafe fn __contracts_impl_backup_unchecked(&self) -> Self {
        self.clone()
    }
}

#[contract_trait]
impl<T: Copy> Backup for T {
    #[inline]
    unsafe fn backup_unchecked(&self) -> Self {
        *self
    }
}