forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gotham_state.rs
193 lines (169 loc) · 5.67 KB
/
gotham_state.rs
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Forked from Gotham:
// https://github.com/gotham-rs/gotham/blob/bcbbf8923789e341b7a0e62c59909428ca4e22e2/gotham/src/state/mod.rs
// Copyright 2017 Gotham Project Developers. MIT license.
use log::trace;
use std::any::type_name;
use std::any::Any;
use std::any::TypeId;
use std::collections::BTreeMap;
#[derive(Default)]
pub struct GothamState {
data: BTreeMap<TypeId, Box<dyn Any>>,
}
impl GothamState {
/// Puts a value into the `GothamState` storage. One value of each type is retained.
/// Successive calls to `put` will overwrite the existing value of the same
/// type.
pub fn put<T: 'static>(&mut self, t: T) {
let type_id = TypeId::of::<T>();
trace!(" inserting record to state for type_id `{:?}`", type_id);
self.data.insert(type_id, Box::new(t));
}
/// Determines if the current value exists in `GothamState` storage.
pub fn has<T: 'static>(&self) -> bool {
let type_id = TypeId::of::<T>();
self.data.get(&type_id).is_some()
}
/// Tries to borrow a value from the `GothamState` storage.
pub fn try_borrow<T: 'static>(&self) -> Option<&T> {
let type_id = TypeId::of::<T>();
trace!(" borrowing state data for type_id `{:?}`", type_id);
self.data.get(&type_id).and_then(|b| b.downcast_ref())
}
/// Borrows a value from the `GothamState` storage.
pub fn borrow<T: 'static>(&self) -> &T {
self.try_borrow().unwrap_or_else(|| missing::<T>())
}
/// Tries to mutably borrow a value from the `GothamState` storage.
pub fn try_borrow_mut<T: 'static>(&mut self) -> Option<&mut T> {
let type_id = TypeId::of::<T>();
trace!(" mutably borrowing state data for type_id `{:?}`", type_id);
self.data.get_mut(&type_id).and_then(|b| b.downcast_mut())
}
/// Mutably borrows a value from the `GothamState` storage.
pub fn borrow_mut<T: 'static>(&mut self) -> &mut T {
self.try_borrow_mut().unwrap_or_else(|| missing::<T>())
}
/// Tries to move a value out of the `GothamState` storage and return ownership.
pub fn try_take<T: 'static>(&mut self) -> Option<T> {
let type_id = TypeId::of::<T>();
trace!(
" taking ownership from state data for type_id `{:?}`",
type_id
);
self
.data
.remove(&type_id)
.and_then(|b| b.downcast().ok())
.map(|b| *b)
}
/// Moves a value out of the `GothamState` storage and returns ownership.
///
/// # Panics
///
/// If a value of type `T` is not present in `GothamState`.
pub fn take<T: 'static>(&mut self) -> T {
self.try_take().unwrap_or_else(|| missing::<T>())
}
}
fn missing<T: 'static>() -> ! {
panic!(
"required type {} is not present in GothamState container",
type_name::<T>()
);
}
#[cfg(test)]
mod tests {
use super::GothamState;
struct MyStruct {
value: i32,
}
struct AnotherStruct {
value: &'static str,
}
type Alias1 = String;
type Alias2 = String;
#[test]
fn put_borrow1() {
let mut state = GothamState::default();
state.put(MyStruct { value: 1 });
assert_eq!(state.borrow::<MyStruct>().value, 1);
}
#[test]
fn put_borrow2() {
let mut state = GothamState::default();
assert!(!state.has::<AnotherStruct>());
state.put(AnotherStruct { value: "a string" });
assert!(state.has::<AnotherStruct>());
assert!(!state.has::<MyStruct>());
state.put(MyStruct { value: 100 });
assert!(state.has::<MyStruct>());
assert_eq!(state.borrow::<MyStruct>().value, 100);
assert_eq!(state.borrow::<AnotherStruct>().value, "a string");
}
#[test]
fn try_borrow() {
let mut state = GothamState::default();
state.put(MyStruct { value: 100 });
assert!(state.try_borrow::<MyStruct>().is_some());
assert_eq!(state.try_borrow::<MyStruct>().unwrap().value, 100);
assert!(state.try_borrow::<AnotherStruct>().is_none());
}
#[test]
fn try_borrow_mut() {
let mut state = GothamState::default();
state.put(MyStruct { value: 100 });
if let Some(a) = state.try_borrow_mut::<MyStruct>() {
a.value += 10;
}
assert_eq!(state.borrow::<MyStruct>().value, 110);
}
#[test]
fn borrow_mut() {
let mut state = GothamState::default();
state.put(MyStruct { value: 100 });
{
let a = state.borrow_mut::<MyStruct>();
a.value += 10;
}
assert_eq!(state.borrow::<MyStruct>().value, 110);
assert!(state.try_borrow_mut::<AnotherStruct>().is_none());
}
#[test]
fn try_take() {
let mut state = GothamState::default();
state.put(MyStruct { value: 100 });
assert_eq!(state.try_take::<MyStruct>().unwrap().value, 100);
assert!(state.try_take::<MyStruct>().is_none());
assert!(state.try_borrow_mut::<MyStruct>().is_none());
assert!(state.try_borrow::<MyStruct>().is_none());
assert!(state.try_take::<AnotherStruct>().is_none());
}
#[test]
fn take() {
let mut state = GothamState::default();
state.put(MyStruct { value: 110 });
assert_eq!(state.take::<MyStruct>().value, 110);
assert!(state.try_take::<MyStruct>().is_none());
assert!(state.try_borrow_mut::<MyStruct>().is_none());
assert!(state.try_borrow::<MyStruct>().is_none());
}
#[test]
fn type_alias() {
let mut state = GothamState::default();
state.put::<Alias1>("alias1".to_string());
state.put::<Alias2>("alias2".to_string());
assert_eq!(state.take::<Alias1>(), "alias2");
assert!(state.try_take::<Alias1>().is_none());
assert!(state.try_take::<Alias2>().is_none());
}
#[test]
#[should_panic(
expected = "required type deno_core::gotham_state::tests::MyStruct is not present in GothamState container"
)]
fn missing() {
let state = GothamState::default();
let _ = state.borrow::<MyStruct>();
}
}