Skip to main content

vortex_session/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4mod arc_swap_map;
5pub mod registry;
6mod session;
7
8use std::any::Any;
9use std::fmt::Debug;
10use std::hash::Hasher;
11
12pub use arc_swap_map::ArcSwapMap;
13pub use session::SessionGuard;
14pub use session::SessionMut;
15pub use session::VortexSession;
16pub use session::VortexSessionVar;
17
18#[derive(Debug, Clone, Copy, Default)]
19struct UnknownPluginPolicy {
20    allow_unknown: bool,
21}
22
23impl SessionVar for UnknownPluginPolicy {
24    fn as_any(&self) -> &dyn Any {
25        self
26    }
27
28    fn as_any_mut(&mut self) -> &mut dyn Any {
29        self
30    }
31}
32
33/// Trait for accessing the state of a Vortex session.
34pub trait SessionExt: Sized + private::Sealed {
35    /// Returns the [`VortexSession`].
36    fn session(&self) -> VortexSession;
37
38    /// Returns the session variable of type `V`, inserting a default one if it does not exist.
39    ///
40    /// This is [`get_or_insert_with`](Self::get_or_insert_with) with `V::default` as the
41    /// initializer.
42    fn get<V: VortexSessionVar + Default>(&self) -> SessionGuard<'_, V>;
43
44    /// Returns the session variable of type `V`, inserting the result of `init` if it does not
45    /// exist.
46    ///
47    /// Use this instead of [`get`](Self::get) when the first value of `V` depends on the session,
48    /// for example on another session variable. The value is constructed and inserted
49    /// copy-on-write: `init` runs without any lock held, so it may freely re-enter the session. A
50    /// concurrent insert of the same type is resolved by keeping the first value published, so
51    /// `init` may run even though its result is then dropped.
52    fn get_or_insert_with<V: VortexSessionVar>(
53        &self,
54        init: impl FnOnce() -> V,
55    ) -> SessionGuard<'_, V>;
56
57    /// Returns the session variable of type `V` if it exists.
58    fn get_opt<V: VortexSessionVar>(&self) -> Option<SessionGuard<'_, V>>;
59
60    /// Returns a copy-on-write [`SessionMut`] handle for the variable of type `V`, inserting a
61    /// default one first if it does not exist.
62    ///
63    /// The handle starts as a clone of the current value; mutating it through `DerefMut` and
64    /// dropping it publishes the result back into the session copy-on-write — the ergonomic
65    /// equivalent of reading the variable, modifying a clone, and re-inserting it into the session.
66    fn get_mut<V: VortexSessionVar + Default + Clone>(&self) -> SessionMut<'_, V>;
67}
68
69mod private {
70    pub trait Sealed {}
71    impl Sealed for super::VortexSession {}
72}
73
74/// This trait defines variables that can be stored against a Vortex session.
75///
76/// Users should implement this trait for anything that you want to store on a `VortexSession`.
77pub trait SessionVar: Any + Send + Sync + Debug + 'static {
78    fn as_any(&self) -> &dyn Any;
79    fn as_any_mut(&mut self) -> &mut dyn Any;
80}
81
82/// With TypeIds as keys, there's no need to hash them. They are already hashes
83/// themselves, coming from the compiler. The IdHasher just holds the u64 of
84/// the TypeId, and then returns it, instead of doing any bit fiddling.
85#[derive(Default)]
86struct IdHasher(u64);
87
88impl Hasher for IdHasher {
89    #[inline]
90    fn finish(&self) -> u64 {
91        self.0
92    }
93
94    fn write(&mut self, _: &[u8]) {
95        unreachable!("TypeId calls write_u64");
96    }
97
98    #[inline]
99    fn write_u64(&mut self, id: u64) {
100        self.0 = id;
101    }
102}