Skip to main content

zrx_scheduler/scheduler/signal/
value.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Value.
27
28use std::any::Any;
29use std::fmt::Debug;
30
31// ----------------------------------------------------------------------------
32// Traits
33// ----------------------------------------------------------------------------
34
35/// Value.
36///
37/// Note that this trait must be explicitly implemented for types that should
38/// be handled by the scheduler, as it's not implemented for built-in types by
39/// default, which helps to make subscriptions between components intentionally
40/// explicit instead of implicit.
41///
42/// If this trait were implemented for built-in types by default, it would be
43/// too easy to accidentally subscribe to a type without realizing it, which
44/// could manifest in unexpected behavior that makes it harder to debug issues.
45/// So, [`Action::Inputs`][] and [`Action::Output`][] must explicitly implement
46/// this trait, requiring simple types to be wrapped in newtypes, which ensures
47/// that all interactions between modules are intentional and well-defined.
48///
49/// Implementors must implement [`Any`], [`Clone`], [`Debug`], [`Eq`], as well
50/// as [`Send`] and [`Sync`], so all values can be shared across threads and
51/// printed for debugging. [`Clone`] is required for distributing values to the
52/// subscribers of an [`Action`][], rendering [`Value`] as not dyn-compatible.
53/// However, this is not a problem and actually a good thing, as values should
54/// not be downcast, rather the stores and receivers that manage them.
55///
56/// [`Action`]: crate::scheduler::action::Action
57/// [`Action::Inputs`]: crate::scheduler::action::Action::Inputs
58/// [`Action::Output`]: crate::scheduler::action::Action::Output
59pub trait Value: Any + Clone + Debug + Eq + Send + Sync {}
60
61// ----------------------------------------------------------------------------
62// Trait implementations
63// ----------------------------------------------------------------------------
64
65impl Value for () {}
66
67impl<T> Value for Option<T> where T: Value {}
68
69impl<T> Value for Vec<T> where T: Value {}
70
71// ----------------------------------------------------------------------------
72// Macros
73// ----------------------------------------------------------------------------
74
75/// Implements value trait for a tuple.
76macro_rules! impl_value_for_tuple {
77    ($($T:ident),+ $(,)?) => {
78        impl<$($T),+> Value for ($($T,)+)
79        where
80            $($T: Value),+ {}
81    };
82}
83
84// ----------------------------------------------------------------------------
85
86impl_value_for_tuple!(T1);
87impl_value_for_tuple!(T1, T2);
88impl_value_for_tuple!(T1, T2, T3);
89impl_value_for_tuple!(T1, T2, T3, T4);
90impl_value_for_tuple!(T1, T2, T3, T4, T5);
91impl_value_for_tuple!(T1, T2, T3, T4, T5, T6);
92impl_value_for_tuple!(T1, T2, T3, T4, T5, T6, T7);
93impl_value_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8);