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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Manage a resource that can be started, stopped, and destroyed – i.e. a
//! [`Subject`] – and which has different [facets][`Faceted`] depending on
//! whether it is locked exclusively, shared between multiple users, or
//! unlocked/free.
//!
//! For example, a resource representing a PostgreSQL cluster would allow start,
//! stop, and destroy actions only when it is exclusively locked. The _type_ of
//! an unlocked cluster resource or a shared cluster resource would not even
//! have functions available to start, stop, or destroy the cluster.
//!
//! The intent is to codify safe behaviours into Rust's type system so that we
//! make it hard or impossible to mishandle a resource – and conversely, easier
//! to correctly handle a resource.

use super::{lock, CoordinateError, Subject};
use either::{Either, Left, Right};
use std::marker::PhantomData;

// ----------------------------------------------------------------------------

/// A resource is faceted into three types: free, shared, and exclusive. This
/// trait allows us to obtain the three facets of a resource.
pub trait Faceted<'a> {
    type FacetFree;
    type FacetShared;
    type FacetExclusive;

    fn facet_free(&'a self) -> Self::FacetFree;
    fn facet_shared(&'a self) -> Self::FacetShared;
    fn facet_exclusive(&'a self) -> Self::FacetExclusive;
}

// ----------------------------------------------------------------------------

/// An unlocked/free resource.
pub struct ResourceFree<'a, R: Subject + Faceted<'a>> {
    lock: lock::UnlockedFile,
    inner: R,
    phantom: PhantomData<&'a R>,
}

impl<'a, R> ResourceFree<'a, R>
where
    R: Subject + Faceted<'a>,
{
    pub fn new(lock: lock::UnlockedFile, inner: R) -> Self {
        Self { lock, inner, phantom: PhantomData }
    }

    /// Return the [`Faceted::FacetFree`] of the wrapped resource.
    pub fn facet(&'a self) -> R::FacetFree {
        self.inner.facet_free()
    }

    /// Attempt to obtain a shared lock on the resource.
    pub fn try_shared(
        self,
    ) -> Result<Either<Self, ResourceShared<'a, R>>, CoordinateError<R::Error>> {
        Ok(match self.lock.try_lock_shared()? {
            Left(lock) => Left(Self { inner: self.inner, lock, phantom: PhantomData }),
            Right(lock) => Right(ResourceShared { inner: self.inner, lock, phantom: PhantomData }),
        })
    }

    /// Obtain a shared lock on the resource. Can block indefinitely.
    pub fn shared(self) -> Result<ResourceShared<'a, R>, CoordinateError<R::Error>> {
        let lock = self.lock.lock_shared()?;
        Ok(ResourceShared { inner: self.inner, lock, phantom: PhantomData })
    }

    /// Attempt to obtain an exclusive lock on the resource.
    pub fn try_exclusive(
        self,
    ) -> Result<Either<Self, ResourceExclusive<'a, R>>, CoordinateError<R::Error>> {
        Ok(match self.lock.try_lock_exclusive()? {
            Left(lock) => Left(Self { inner: self.inner, lock, phantom: PhantomData }),
            Right(lock) => {
                Right(ResourceExclusive { inner: self.inner, lock, phantom: PhantomData })
            }
        })
    }

    /// Obtain an exclusive lock on the resource. Can block indefinitely.
    pub fn exclusive(self) -> Result<ResourceExclusive<'a, R>, CoordinateError<R::Error>> {
        let lock = self.lock.lock_exclusive()?;
        Ok(ResourceExclusive { inner: self.inner, lock, phantom: PhantomData })
    }

    /// Disassembles this resource into the lock and the inner, managed, value.
    /// This can only be done from an unlocked/free resource.
    pub fn into_parts(self) -> (lock::UnlockedFile, R) {
        (self.lock, self.inner)
    }
}

// ----------------------------------------------------------------------------

/// A shared resource.
pub struct ResourceShared<'a, R: Subject + Faceted<'a>> {
    lock: lock::LockedFileShared,
    inner: R,
    phantom: PhantomData<&'a R>,
}

impl<'a, R> ResourceShared<'a, R>
where
    R: Subject + Faceted<'a>,
{
    pub fn new(lock: lock::LockedFileShared, inner: R) -> Self {
        Self { lock, inner, phantom: PhantomData }
    }

    /// Return the [`Faceted::FacetShared`] of the wrapped resource.
    pub fn facet(&'a self) -> R::FacetShared {
        self.inner.facet_shared()
    }

    /// Attempt to obtain an exclusive lock on the resource.
    pub fn try_exclusive(
        self,
    ) -> Result<Either<Self, ResourceExclusive<'a, R>>, CoordinateError<R::Error>> {
        Ok(match self.lock.try_lock_exclusive()? {
            Left(lock) => Left(Self { inner: self.inner, lock, phantom: PhantomData }),
            Right(lock) => {
                Right(ResourceExclusive { inner: self.inner, lock, phantom: PhantomData })
            }
        })
    }

    /// Obtain an exclusive lock on the resource. Can block indefinitely.
    pub fn exclusive(self) -> Result<ResourceExclusive<'a, R>, CoordinateError<R::Error>> {
        let lock = self.lock.lock_exclusive()?;
        Ok(ResourceExclusive { inner: self.inner, lock, phantom: PhantomData })
    }

    /// Attempt to release this resource.
    pub fn try_release(
        self,
    ) -> Result<Either<Self, ResourceFree<'a, R>>, CoordinateError<R::Error>> {
        Ok(match self.lock.try_unlock()? {
            Left(lock) => Left(Self { inner: self.inner, lock, phantom: PhantomData }),
            Right(lock) => Right(ResourceFree { inner: self.inner, lock, phantom: PhantomData }),
        })
    }

    /// Release this resource. Can block indefinitely.
    pub fn release(self) -> Result<ResourceFree<'a, R>, CoordinateError<R::Error>> {
        let lock = self.lock.unlock()?;
        Ok(ResourceFree { inner: self.inner, lock, phantom: PhantomData })
    }
}

// ----------------------------------------------------------------------------

/// A resource held exclusively.
pub struct ResourceExclusive<'a, R: Subject + Faceted<'a>> {
    lock: lock::LockedFileExclusive,
    inner: R,
    phantom: PhantomData<&'a R>,
}

impl<'a, R> ResourceExclusive<'a, R>
where
    R: Subject + Faceted<'a>,
{
    pub fn new(lock: lock::LockedFileExclusive, inner: R) -> Self {
        Self { lock, inner, phantom: PhantomData }
    }

    /// Return the [`Faceted::FacetExclusive`] of the wrapped resource.
    pub fn facet(&'a self) -> R::FacetExclusive {
        self.inner.facet_exclusive()
    }

    /// Attempt to obtain a shared lock on the resource.
    pub fn try_shared(
        self,
    ) -> Result<Either<Self, ResourceShared<'a, R>>, CoordinateError<R::Error>> {
        Ok(match self.lock.try_lock_shared()? {
            Left(lock) => Left(Self { inner: self.inner, lock, phantom: PhantomData }),
            Right(lock) => Right(ResourceShared { inner: self.inner, lock, phantom: PhantomData }),
        })
    }

    /// Obtain a shared lock on the resource. Can block indefinitely.
    pub fn shared(self) -> Result<ResourceShared<'a, R>, CoordinateError<R::Error>> {
        let lock = self.lock.lock_shared()?;
        Ok(ResourceShared { inner: self.inner, lock, phantom: PhantomData })
    }

    /// Attempt to release this resource.
    pub fn try_release(
        self,
    ) -> Result<Either<Self, ResourceFree<'a, R>>, CoordinateError<R::Error>> {
        Ok(match self.lock.try_unlock()? {
            Left(lock) => Left(Self { inner: self.inner, lock, phantom: PhantomData }),
            Right(lock) => Right(ResourceFree { inner: self.inner, lock, phantom: PhantomData }),
        })
    }

    /// Release this resource. Can block indefinitely.
    pub fn release(self) -> Result<ResourceFree<'a, R>, CoordinateError<R::Error>> {
        let lock = self.lock.unlock()?;
        Ok(ResourceFree { inner: self.inner, lock, phantom: PhantomData })
    }
}