tauri/
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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{
  any::{Any, TypeId},
  cell::UnsafeCell,
  collections::HashMap,
  hash::BuildHasherDefault,
  sync::Mutex,
};

use crate::{
  ipc::{CommandArg, CommandItem, InvokeError},
  Runtime,
};

/// A guard for a state value.
///
/// See [`Manager::manage`](`crate::Manager::manage`) for usage examples.
pub struct State<'r, T: Send + Sync + 'static>(&'r T);

impl<'r, T: Send + Sync + 'static> State<'r, T> {
  /// Retrieve a borrow to the underlying value with a lifetime of `'r`.
  /// Using this method is typically unnecessary as `State` implements
  /// [`std::ops::Deref`] with a [`std::ops::Deref::Target`] of `T`.
  #[inline(always)]
  pub fn inner(&self) -> &'r T {
    self.0
  }
}

impl<T: Send + Sync + 'static> std::ops::Deref for State<'_, T> {
  type Target = T;

  #[inline(always)]
  fn deref(&self) -> &T {
    self.0
  }
}

impl<T: Send + Sync + 'static> Clone for State<'_, T> {
  fn clone(&self) -> Self {
    State(self.0)
  }
}

impl<T: Send + Sync + 'static + PartialEq> PartialEq for State<'_, T> {
  fn eq(&self, other: &Self) -> bool {
    self.0 == other.0
  }
}

impl<'r, T: Send + Sync + std::fmt::Debug> std::fmt::Debug for State<'r, T> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_tuple("State").field(&self.0).finish()
  }
}

impl<'r, 'de: 'r, T: Send + Sync + 'static, R: Runtime> CommandArg<'de, R> for State<'r, T> {
  /// Grabs the [`State`] from the [`CommandItem`]. This will never fail.
  fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
    Ok(command.message.state_ref().try_get().unwrap_or_else(|| {
      panic!(
        "state not managed for field `{}` on command `{}`. You must call `.manage()` before using this command",
        command.key, command.name
      )
    }))
  }
}

// Taken from: https://github.com/SergioBenitez/state/blob/556c1b94db8ce8427a0e72de7983ab5a9af4cc41/src/ident_hash.rs
// This is a _super_ stupid hash. It just uses its input as the hash value. This
// hash is meant to be used _only_ for "prehashed" values. In particular, we use
// this so that hashing a TypeId is essentially a noop. This is because TypeIds
// are already unique integers.
#[derive(Default)]
struct IdentHash(u64);

impl std::hash::Hasher for IdentHash {
  fn finish(&self) -> u64 {
    self.0
  }

  fn write(&mut self, bytes: &[u8]) {
    for byte in bytes {
      self.write_u8(*byte);
    }
  }

  fn write_u8(&mut self, i: u8) {
    self.0 = (self.0 << 8) | (i as u64);
  }

  fn write_u64(&mut self, i: u64) {
    self.0 = i;
  }
}

type TypeIdMap = HashMap<TypeId, Box<dyn Any>, BuildHasherDefault<IdentHash>>;

/// The Tauri state manager.
#[derive(Debug)]
pub struct StateManager {
  map: Mutex<UnsafeCell<TypeIdMap>>,
}

// SAFETY: data is accessed behind a lock
unsafe impl Sync for StateManager {}
unsafe impl Send for StateManager {}

impl StateManager {
  pub(crate) fn new() -> Self {
    Self {
      map: Default::default(),
    }
  }

  fn with_map_ref<'a, F: FnOnce(&'a TypeIdMap) -> R, R>(&'a self, f: F) -> R {
    let map = self.map.lock().unwrap();
    let map = map.get();
    // SAFETY: safe to access since we are holding a lock
    f(unsafe { &*map })
  }

  fn with_map_mut<F: FnOnce(&mut TypeIdMap) -> R, R>(&self, f: F) -> R {
    let mut map = self.map.lock().unwrap();
    let map = map.get_mut();
    f(map)
  }

  pub(crate) fn set<T: Send + Sync + 'static>(&self, state: T) -> bool {
    self.with_map_mut(|map| {
      let type_id = TypeId::of::<T>();
      let already_set = map.contains_key(&type_id);
      if !already_set {
        map.insert(type_id, Box::new(state) as Box<dyn Any>);
      }
      !already_set
    })
  }

  pub(crate) fn unmanage<T: Send + Sync + 'static>(&self) -> Option<T> {
    self.with_map_mut(|map| {
      let type_id = TypeId::of::<T>();
      map
        .remove(&type_id)
        .and_then(|ptr| ptr.downcast().ok().map(|b| *b))
    })
  }

  /// Gets the state associated with the specified type.
  pub fn get<T: Send + Sync + 'static>(&self) -> State<'_, T> {
    self
      .try_get()
      .expect("state: get() when given type is not managed")
  }

  /// Gets the state associated with the specified type.
  pub fn try_get<T: Send + Sync + 'static>(&self) -> Option<State<'_, T>> {
    self.with_map_ref(|map| {
      map
        .get(&TypeId::of::<T>())
        .and_then(|ptr| ptr.downcast_ref::<T>())
        .map(State)
    })
  }
}

// Ported from https://github.com/SergioBenitez/state/blob/556c1b94db8ce8427a0e72de7983ab5a9af4cc41/tests/main.rs
#[cfg(test)]
mod tests {
  use super::StateManager;

  use std::sync::{Arc, RwLock};
  use std::thread;

  // Tiny structures to test that dropping works as expected.
  struct DroppingStruct(Arc<RwLock<bool>>);
  struct DroppingStructWrap(#[allow(dead_code)] DroppingStruct);

  impl Drop for DroppingStruct {
    fn drop(&mut self) {
      *self.0.write().unwrap() = true;
    }
  }

  #[test]
  fn simple_set_get() {
    let state = StateManager::new();
    assert!(state.set(1u32));
    assert_eq!(*state.get::<u32>(), 1);
  }

  #[test]
  fn simple_set_get_unmanage() {
    let state = StateManager::new();
    assert!(state.set(1u32));
    assert_eq!(*state.get::<u32>(), 1);
    assert!(state.unmanage::<u32>().is_some());
    assert!(state.unmanage::<u32>().is_none());
    assert_eq!(state.try_get::<u32>(), None);
    assert!(state.set(2u32));
    assert_eq!(*state.get::<u32>(), 2);
  }

  #[test]
  fn dst_set_get() {
    let state = StateManager::new();
    assert!(state.set::<[u32; 4]>([1, 2, 3, 4u32]));
    assert_eq!(*state.get::<[u32; 4]>(), [1, 2, 3, 4]);
  }

  #[test]
  fn set_get_remote() {
    let state = Arc::new(StateManager::new());
    let sate_ = Arc::clone(&state);
    thread::spawn(move || {
      sate_.set(10isize);
    })
    .join()
    .unwrap();

    assert_eq!(*state.get::<isize>(), 10);
  }

  #[test]
  fn two_put_get() {
    let state = StateManager::new();
    assert!(state.set("Hello, world!".to_string()));

    let s_old = state.get::<String>();
    assert_eq!(*s_old, "Hello, world!");

    assert!(!state.set::<String>("Bye bye!".into()));
    assert_eq!(*state.get::<String>(), "Hello, world!");
    assert_eq!(state.get::<String>(), s_old);
  }

  #[test]
  fn many_puts_only_one_succeeds() {
    let state = Arc::new(StateManager::new());
    let mut threads = vec![];
    for _ in 0..1000 {
      let state_ = Arc::clone(&state);
      threads.push(thread::spawn(move || state_.set(10i64)))
    }

    let results: Vec<bool> = threads.into_iter().map(|t| t.join().unwrap()).collect();
    assert_eq!(results.into_iter().filter(|&b| b).count(), 1);
    assert_eq!(*state.get::<i64>(), 10);
  }

  // Ensure setting when already set doesn't cause a drop.
  #[test]
  fn test_no_drop_on_set() {
    let state = StateManager::new();
    let drop_flag = Arc::new(RwLock::new(false));
    let dropping_struct = DroppingStruct(drop_flag.clone());

    let _drop_flag_ignore = Arc::new(RwLock::new(false));
    let _dropping_struct_ignore = DroppingStruct(_drop_flag_ignore.clone());

    state.set::<DroppingStruct>(dropping_struct);
    assert!(!state.set::<DroppingStruct>(_dropping_struct_ignore));
    assert!(!*drop_flag.read().unwrap());
  }

  // Ensure dropping a type_map drops its contents.
  #[test]
  fn drop_inners_on_drop() {
    let drop_flag_a = Arc::new(RwLock::new(false));
    let dropping_struct_a = DroppingStruct(drop_flag_a.clone());

    let drop_flag_b = Arc::new(RwLock::new(false));
    let dropping_struct_b = DroppingStructWrap(DroppingStruct(drop_flag_b.clone()));

    {
      let state = StateManager::new();
      state.set(dropping_struct_a);
      assert!(!*drop_flag_a.read().unwrap());

      state.set(dropping_struct_b);
      assert!(!*drop_flag_a.read().unwrap());
      assert!(!*drop_flag_b.read().unwrap());
    }

    assert!(*drop_flag_a.read().unwrap());
    assert!(*drop_flag_b.read().unwrap());
  }
}