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
use core::hash::Hash;

use necsim_core::cogs::{Backup, Habitat, LineageReference, MathsCore};

#[derive(PartialEq, Eq, Hash, Debug, TypeLayout)]
#[allow(clippy::module_name_repetitions)]
#[repr(transparent)]
pub struct InMemoryLineageReference(usize);

#[contract_trait]
impl Backup for InMemoryLineageReference {
    unsafe fn backup_unchecked(&self) -> Self {
        Self(self.0)
    }
}

impl<M: MathsCore, H: Habitat<M>> LineageReference<M, H> for InMemoryLineageReference {}

impl InMemoryLineageReference {
    /// # Safety
    ///
    /// A new [`InMemoryLineageReference`] must only be issued from a [`usize`]
    /// to create a new and unique reference. It is not allowed to issue
    /// multiple [`InMemoryLineageReference`]s from the same [`usize`].
    ///
    /// In case where a temporary and unexposed clone of the
    /// [`InMemoryLineageReference`] is required, use
    /// [`Backup::backup_unchecked`].
    #[must_use]
    pub unsafe fn issue(reference: usize) -> Self {
        Self(reference)
    }
}

impl From<InMemoryLineageReference> for usize {
    fn from(reference: InMemoryLineageReference) -> Self {
        reference.0
    }
}

impl<'a> From<&'a InMemoryLineageReference> for usize {
    fn from(reference: &'a InMemoryLineageReference) -> Self {
        reference.0
    }
}