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
#![cfg_attr(not(test), no_std)]

use core::{fmt, marker::PhantomData};

/// A phantomdata-like type taking a single invariant lifetime.
///
/// Used to manipulate and store the unique invariant lifetime produce by `Guard`.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Id<'id> {
    phantom: PhantomData<&'id mut &'id fn(&'id ()) -> &'id ()>,
}

impl<'id> Id<'id> {
    /// Do not use this function; use the `guard!` macro instead.
    pub unsafe fn new() -> Self {
        Id {
            phantom: PhantomData,
        }
    }
}

impl<'id> fmt::Debug for Id<'id> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("#[invariant] 'id").finish()
    }
}

impl<'id> From<Guard<'id>> for Id<'id> {
    fn from(_guard: Guard<'id>) -> Self {
        Id {
            phantom: PhantomData,
        }
    }
}

/// An invariant lifetime phantomdata that is guaranteed to be unique.
///
/// In effect, this means that `'id` is a "generative brand"
#[derive(Eq, PartialEq)]
pub struct Guard<'id> {
    #[allow(unused)]
    id: Id<'id>,
}

impl<'id> Guard<'id> {
    /// Do not use this function; use the `guard!` macro instead.
    pub unsafe fn new(id: Id<'id>) -> Guard<'id> {
        Guard { id }
    }
}

impl<'id> fmt::Debug for Guard<'id> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("#[unique] 'id").finish()
    }
}

/// Create a `Guard` with a unique lifetime.
///
/// Multiple invocations will not unify:
///
/// ```rust,compile_fail
/// # use generativity::make_guard;
/// make_guard!(a);
/// make_guard!(b);
/// dbg!(a == b); // ERROR (here == is a static check)
/// ```
#[macro_export]
macro_rules! make_guard {
    ($name:ident) => {
        let tag = unsafe { $crate::Id::new() };
        let _guard;
        let $name = unsafe { $crate::Guard::new(tag) };
        {
            if false {
                #[allow(non_camel_case_types)]
                struct make_guard<'id>(&'id $crate::Id<'id>);
                impl<'id> ::core::ops::Drop for make_guard<'id> {
                    fn drop(&mut self) {}
                }
                _guard = make_guard(&tag);
            }
        }
    };
}

#[test]
#[allow(clippy::eq_op)]
fn dont_error_in_general() {
    make_guard!(a);
    make_guard!(b);
    dbg!(a == a);
    dbg!(b == b); // OK
}