snapshot of https://github.com/tauri-apps/tauri.git (dev, 2026-09-23; blobs > 131072B omitted)
Some checks failed
Audit / audit-js (push) Failing after 0s
bench / bench (map[os:ubuntu-latest runner:xvfb-run --auto-servernum target:x86_64-unknown-linux-gnu], nightly) (push) Failing after 0s
covector version or publish / run-integration-tests (ubuntu-latest) (push) Failing after 0s
Audit / audit-rust (push) Failing after 0s
test `@tauri-apps/cli` / test (ubuntu-latest) (push) Failing after 0s
test `tauri-cli` / test (map[os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Failing after 0s
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:ubuntu-latest target:x86_64-unknown-linux-gnu toolchain:1.90]) (push) Failing after 0s
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:ubuntu-latest target:x86_64-unknown-linux-gnu toolchain:1.90]) (push) Failing after 0s
test core / test (map[args:--no-default-features key:no-default], map[cargo:cross command:build os:ubuntu-latest target:aarch64-linux-android toolchain:1.90]) (push) Failing after 0s
deploy schema worker / deploy (push) Failing after 0s
lint rust / clippy (map[cargo:cargo include: os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Failing after 0s
lint rust / clippy (map[cargo:cross include:--package api os:ubuntu-latest target:aarch64-linux-android]) (push) Failing after 0s
supply chain health status / check rust dependencies with cargo vet (push) Failing after 0s
test `@tauri-apps/api` e2e (mobile) / android (push) Failing after 0s
test `@tauri-apps/api` e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
test core / test (map[args:--all-features key:all], map[cargo:cross command:build os:ubuntu-latest target:aarch64-linux-android toolchain:1.90]) (push) Failing after 0s
Udeps / changes (push) Failing after 0s
Udeps / setup (push) Has been skipped
Udeps / udeps (push) Has been skipped
bench / bench (map[os:macos-latest runner: target:aarch64-apple-darwin], nightly) (push) Has been cancelled
bench / bench (map[os:windows-latest runner: target:x86_64-pc-windows-msvc], nightly) (push) Has been cancelled
covector version or publish / run-integration-tests (macos-latest) (push) Has been cancelled
covector version or publish / run-integration-tests (windows-latest) (push) Has been cancelled
covector version or publish / version-or-publish (push) Has been cancelled
test `@tauri-apps/cli` / test (macos-latest) (push) Has been cancelled
test `tauri-cli` / test (map[args:--no-default-features --features native-tls-vendored os:windows-latest restore-only:true target:aarch64-pc-windows-msvc]) (push) Has been cancelled
test `tauri-cli` / test (map[os:macos-latest target:x86_64-apple-darwin]) (push) Has been cancelled
test `tauri-cli` / test (map[os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:build os:macos-latest target:aarch64-apple-ios toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:macos-14 target:aarch64-apple-darwin toolchain:1.90]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include: os:macos-latest target:aarch64-apple-darwin]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include: os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include:--package api os:macos-latest target:aarch64-apple-ios]) (push) Has been cancelled
test `@tauri-apps/api` e2e (mobile) / ios (push) Has been cancelled
test `@tauri-apps/api` e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test `@tauri-apps/api` e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
test `@tauri-apps/cli` / test (windows-latest) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:windows-latest target:x86_64-pc-windows-msvc toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:build os:macos-latest target:aarch64-apple-ios toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:macos-14 target:aarch64-apple-darwin toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:windows-latest target:x86_64-pc-windows-msvc toolchain:1.90]) (push) Has been cancelled
Some checks failed
Audit / audit-js (push) Failing after 0s
bench / bench (map[os:ubuntu-latest runner:xvfb-run --auto-servernum target:x86_64-unknown-linux-gnu], nightly) (push) Failing after 0s
covector version or publish / run-integration-tests (ubuntu-latest) (push) Failing after 0s
Audit / audit-rust (push) Failing after 0s
test `@tauri-apps/cli` / test (ubuntu-latest) (push) Failing after 0s
test `tauri-cli` / test (map[os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Failing after 0s
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:ubuntu-latest target:x86_64-unknown-linux-gnu toolchain:1.90]) (push) Failing after 0s
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:ubuntu-latest target:x86_64-unknown-linux-gnu toolchain:1.90]) (push) Failing after 0s
test core / test (map[args:--no-default-features key:no-default], map[cargo:cross command:build os:ubuntu-latest target:aarch64-linux-android toolchain:1.90]) (push) Failing after 0s
deploy schema worker / deploy (push) Failing after 0s
lint rust / clippy (map[cargo:cargo include: os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Failing after 0s
lint rust / clippy (map[cargo:cross include:--package api os:ubuntu-latest target:aarch64-linux-android]) (push) Failing after 0s
supply chain health status / check rust dependencies with cargo vet (push) Failing after 0s
test `@tauri-apps/api` e2e (mobile) / android (push) Failing after 0s
test `@tauri-apps/api` e2e / test (map[name:Linux os:ubuntu-latest]) (push) Failing after 0s
test core / test (map[args:--all-features key:all], map[cargo:cross command:build os:ubuntu-latest target:aarch64-linux-android toolchain:1.90]) (push) Failing after 0s
Udeps / changes (push) Failing after 0s
Udeps / setup (push) Has been skipped
Udeps / udeps (push) Has been skipped
bench / bench (map[os:macos-latest runner: target:aarch64-apple-darwin], nightly) (push) Has been cancelled
bench / bench (map[os:windows-latest runner: target:x86_64-pc-windows-msvc], nightly) (push) Has been cancelled
covector version or publish / run-integration-tests (macos-latest) (push) Has been cancelled
covector version or publish / run-integration-tests (windows-latest) (push) Has been cancelled
covector version or publish / version-or-publish (push) Has been cancelled
test `@tauri-apps/cli` / test (macos-latest) (push) Has been cancelled
test `tauri-cli` / test (map[args:--no-default-features --features native-tls-vendored os:windows-latest restore-only:true target:aarch64-pc-windows-msvc]) (push) Has been cancelled
test `tauri-cli` / test (map[os:macos-latest target:x86_64-apple-darwin]) (push) Has been cancelled
test `tauri-cli` / test (map[os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:build os:macos-latest target:aarch64-apple-ios toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:macos-14 target:aarch64-apple-darwin toolchain:1.90]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include: os:macos-latest target:aarch64-apple-darwin]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include: os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
lint rust / clippy (map[cargo:cargo include:--package api os:macos-latest target:aarch64-apple-ios]) (push) Has been cancelled
test `@tauri-apps/api` e2e (mobile) / ios (push) Has been cancelled
test `@tauri-apps/api` e2e / test (map[name:Windows os:windows-latest]) (push) Has been cancelled
test `@tauri-apps/api` e2e / test (map[name:macOS os:macos-latest]) (push) Has been cancelled
test `@tauri-apps/cli` / test (windows-latest) (push) Has been cancelled
test core / test (map[args:--all-features key:all], map[cargo:cargo command:test os:windows-latest target:x86_64-pc-windows-msvc toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:build os:macos-latest target:aarch64-apple-ios toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:macos-14 target:aarch64-apple-darwin toolchain:1.90]) (push) Has been cancelled
test core / test (map[args:--no-default-features key:no-default], map[cargo:cargo command:test os:windows-latest target:x86_64-pc-windows-msvc toolchain:1.90]) (push) Has been cancelled
This commit is contained in:
291
crates/tauri/src/state.rs
Normal file
291
crates/tauri/src/state.rs
Normal file
@@ -0,0 +1,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},
|
||||
collections::HashMap,
|
||||
hash::BuildHasherDefault,
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Manager, Runtime,
|
||||
ipc::{CommandArg, CommandItem, InvokeError},
|
||||
};
|
||||
|
||||
/// A guard for a state value.
|
||||
///
|
||||
/// See [`Manager::manage`](`crate::Manager::manage`) for usage examples.
|
||||
pub struct State<'r, T>(&'r T);
|
||||
|
||||
impl<'r, T> 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> std::ops::Deref for State<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for State<'_, T> {
|
||||
fn clone(&self) -> Self {
|
||||
State(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> PartialEq for State<'_, T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: std::fmt::Debug> std::fmt::Debug for State<'_, 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> {
|
||||
command.message.webview_ref().try_state().ok_or_else(|| {
|
||||
InvokeError::from_anyhow(anyhow::anyhow!(
|
||||
"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;
|
||||
}
|
||||
}
|
||||
|
||||
/// Safety:
|
||||
/// - The `key` must equal to `(*value).type_id()`, see the safety doc in methods of [StateManager] for details.
|
||||
/// - Once you insert a value, you can't remove/mutated/move it anymore, see [StateManager::try_get] for details.
|
||||
type TypeIdMap = HashMap<TypeId, Box<dyn Any + Sync + Send>, BuildHasherDefault<IdentHash>>;
|
||||
|
||||
/// The Tauri state manager.
|
||||
// TODO: make private for v3
|
||||
#[derive(Debug)]
|
||||
pub struct StateManager {
|
||||
map: Mutex<TypeIdMap>,
|
||||
}
|
||||
|
||||
impl StateManager {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
map: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set<T: Send + Sync + 'static>(&self, state: T) -> bool {
|
||||
let mut map = self.map.lock().unwrap();
|
||||
let type_id = TypeId::of::<T>();
|
||||
let already_set = map.contains_key(&type_id);
|
||||
if !already_set {
|
||||
let state = Box::new(state) as Box<dyn Any + Sync + Send>;
|
||||
map.insert(type_id, state);
|
||||
}
|
||||
!already_set
|
||||
}
|
||||
|
||||
/// SAFETY: Calling this method will move the `value`,
|
||||
/// which will cause references obtained through [Self::try_get] to dangle.
|
||||
pub(crate) unsafe fn unmanage<T: Send + Sync + 'static>(&self) -> Option<T> {
|
||||
let mut map = self.map.lock().unwrap();
|
||||
let type_id = TypeId::of::<T>();
|
||||
let state = map.remove(&type_id)?;
|
||||
let value = state
|
||||
.downcast::<T>()
|
||||
.expect("the type of the key should be same as the type of the value");
|
||||
Some(*value)
|
||||
}
|
||||
|
||||
/// Gets the state associated with the specified type.
|
||||
pub fn get<T: 'static>(&self) -> State<'_, T> {
|
||||
self
|
||||
.try_get()
|
||||
.unwrap_or_else(|| panic!("state not found for type {}", std::any::type_name::<T>()))
|
||||
}
|
||||
|
||||
/// Gets the state associated with the specified type.
|
||||
pub fn try_get<T: 'static>(&self) -> Option<State<'_, T>> {
|
||||
let map = self.map.lock().unwrap();
|
||||
let type_id = TypeId::of::<T>();
|
||||
let state = map.get(&type_id)?;
|
||||
let value = state
|
||||
.downcast_ref::<T>()
|
||||
.expect("the type of the key should be same as the type of the value");
|
||||
// SAFETY: We ensure the lifetime of `value` is the same as [StateManager] and `value` will not be mutated/moved.
|
||||
let v_ref = unsafe { &*(value as *const T) };
|
||||
Some(State(v_ref))
|
||||
}
|
||||
}
|
||||
|
||||
// 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]
|
||||
#[should_panic(expected = "state not found for type core::option::Option<alloc::string::String>")]
|
||||
fn get_panics() {
|
||||
let state = StateManager::new();
|
||||
state.get::<Option<String>>();
|
||||
}
|
||||
|
||||
#[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);
|
||||
// safety: the reference returned by `try_get` is already dropped.
|
||||
assert!(unsafe { state.unmanage::<u32>() }.is_some());
|
||||
assert!(unsafe { 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);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user