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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2022 Espresso Systems (espressosys.com)
// This file is part of the tide-disco library.

// You should have received a copy of the MIT License
// along with the tide-disco library. If not, see <https://mit-license.org/>.

//! Interfaces for methods of accessing to state.
//!
//! A common pattern is for the `State` type of an [App](crate::App) to enable some form of interior
//! mutability, with the option of read-only access, such as [RwLock]. Route handlers will then
//! acquire either shared immutable access or exclusive mutable access, where the access mutability
//! is linked to the HTTP method of the route -- a GET method implies immutable access, for example,
//! while a POST method implies mutable access.
//!
//! [tide-disco](crate) supports this pattern ergonomically for any state type which has a notion of
//! reading and writing. This module defines the traits [ReadState] and [WriteState] for states
//! which support immutable and mutable access, respectively, and implements the traits for commonly
//! used types such as [RwLock], [Mutex], and [Arc]. Of course, you are free to implement these
//! traits yourself if your state type has some other notion of shared access or interior
//! mutability.

use crate::http;
use async_std::sync::{Arc, Mutex, RwLock};
use async_trait::async_trait;
use futures::future::BoxFuture;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Method {
    Http(http::Method),
    Socket,
    Metrics,
}

impl Method {
    /// The HTTP GET method.
    pub fn get() -> Self {
        Self::Http(http::Method::Get)
    }

    /// The HTTP POST method.
    pub fn post() -> Self {
        Self::Http(http::Method::Post)
    }

    /// The HTTP PUT method.
    pub fn put() -> Self {
        Self::Http(http::Method::Put)
    }

    /// The HTTP DELETE method.
    pub fn delete() -> Self {
        Self::Http(http::Method::Delete)
    }

    /// The Tide Disco SOCKET method.
    pub fn socket() -> Self {
        Self::Socket
    }

    /// The Tide Disco METRICS method.
    pub fn metrics() -> Self {
        Self::Metrics
    }

    /// Check if a method is a standard HTTP method.
    pub fn is_http(&self) -> bool {
        matches!(self, Self::Http(_))
    }

    /// Check if a request method implies mutable access to the state.
    pub fn is_mutable(&self) -> bool {
        match self {
            Self::Http(m) => !m.is_safe(),
            Self::Socket => true,
            Self::Metrics => false,
        }
    }
}

impl From<http::Method> for Method {
    fn from(m: http::Method) -> Self {
        Self::Http(m)
    }
}

impl Display for Method {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Http(m) => write!(f, "{}", m),
            Self::Socket => write!(f, "SOCKET"),
            Self::Metrics => write!(f, "METRICS"),
        }
    }
}

pub struct ParseMethodError;

impl FromStr for Method {
    type Err = ParseMethodError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "SOCKET" => Ok(Self::Socket),
            "METRICS" => Ok(Self::Metrics),
            _ => s.parse().map_err(|_| ParseMethodError).map(Self::Http),
        }
    }
}

/// A state which allows read access.
///
/// Implementations may allow _shared_ read access (for instance, [RwLock]) but some implementations
/// do not (for instance, [Mutex]). Therefore, do not assume that [read](Self::read) is reentrant,
/// or you may have a deadlock.
#[async_trait]
pub trait ReadState {
    /// The type of state which this type allows a caller to read.
    type State: 'static;

    /// Do an operation with immutable access to the state.
    ///
    /// # Limitations
    ///
    /// The reason this function takes a visitor function to apply to the state, rather than just
    /// returning a reference to the state, is to allow implementations that cannot provide a plain
    /// reference directly. For example, [RwLock] can produce a smart reference, a
    /// [RwLockReadGuard](async_std::sync::RwLockReadGuard), but not a plain reference.
    ///
    /// Note that GATs may allow us to make this interface more ergonomic in the future. With stable
    /// GATs, this trait could be written like
    ///
    /// ```ignore
    /// trait ReadState {
    ///     type State: 'static;
    ///     type Reference<'a>: 'a + Deref<Target = Self::State>;
    ///     fn read(&self) -> Self::Reference<'_>;
    /// }
    /// ```
    ///
    /// [Like many function parameters](crate#boxed-futures) in [tide_disco](crate), the
    /// function to apply to the state is also required to return a _boxed_ future.
    async fn read<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T;
}

/// A state which allows exclusive, write access.
#[async_trait]
pub trait WriteState: ReadState {
    /// Do an operation with mutable access to the state.
    ///
    /// # Limitations
    ///
    /// The reason this function takes a visitor function to apply to the state, rather than just
    /// returning a mutable reference to the state, is to allow implementations that cannot provide
    /// a plain mutable reference directly. For example, [RwLock] can produce a smart reference, a
    /// [RwLockWriteGuard](async_std::sync::RwLockWriteGuard), but not a plain mutable reference.
    ///
    /// Note that GATs may allow us to make this interface more ergonomic in the future. With stable
    /// GATs, this trait could be written like
    ///
    /// ```ignore
    /// trait WriteState {
    ///     type State: 'static;
    ///     type MutReference<'a>: 'a + DerefMut<Target = Self::State>;
    ///     fn write(&self) -> Self::MutReference<'_>;
    /// }
    /// ```
    ///
    /// [Like many function parameters](crate#boxed-futures) in [tide_disco](crate), the
    /// function to apply to the state is also required to return a _boxed_ future.
    async fn write<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a mut Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T;
}

#[async_trait]
impl<State: 'static + Send + Sync> ReadState for RwLock<State> {
    type State = State;
    async fn read<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        op(&*self.read().await).await
    }
}

#[async_trait]
impl<State: 'static + Send + Sync> WriteState for RwLock<State> {
    async fn write<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a mut Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        op(&mut *self.write().await).await
    }
}

#[async_trait]
impl<State: 'static + Send + Sync> ReadState for Mutex<State> {
    type State = State;
    async fn read<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        op(&*self.lock().await).await
    }
}

#[async_trait]
impl<State: 'static + Send + Sync> WriteState for Mutex<State> {
    async fn write<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a mut Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        op(&mut *self.lock().await).await
    }
}

#[async_trait]
impl<R: Send + Sync + ReadState> ReadState for Arc<R> {
    type State = R::State;
    async fn read<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        (**self).read(op).await
    }
}

#[async_trait]
impl<W: Send + Sync + WriteState> WriteState for Arc<W> {
    async fn write<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a mut Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        (**self).write(op).await
    }
}

/// This allows you to do `api.get(...)` in a simple API where the state is `()`.
#[async_trait]
impl ReadState for () {
    type State = ();
    async fn read<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        op(&()).await
    }
}

/// This allows you to do `api.post(...)` in a simple API where the state is `()`.
#[async_trait]
impl WriteState for () {
    async fn write<T>(
        &self,
        op: impl Send + for<'a> FnOnce(&'a mut Self::State) -> BoxFuture<'a, T> + 'async_trait,
    ) -> T {
        op(&mut ()).await
    }
}