rinf/
signal_trait.rs

1//! This module provides functionality for defining and handling signals
2//! in a type-safe way, ensuring that all structs and enums
3//! require their inner structs and enums to implement the signal trait.
4
5use crate::channel::SignalReceiver;
6use crate::interface::DartSignalPack;
7use serde::{Deserialize, Serialize};
8use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
9
10/// Capability of sending signals from Rust to Dart.
11pub trait RustSignal: Serialize {
12  /// Sends a signal to Dart.
13  /// Passing data from Dart to Rust is a zero-copy operation.
14  fn send_signal_to_dart(&self);
15}
16
17/// Capability of sending signals from Rust to Dart with binary data.
18pub trait RustSignalBinary: Serialize {
19  /// Sends a signal to Dart with separate binary data.
20  /// Passing data from Dart to Rust is a zero-copy operation.
21  fn send_signal_to_dart(&self, binary: Vec<u8>);
22}
23
24/// Capability of sending signals from Dart to Rust.
25pub trait DartSignal: for<'a> Deserialize<'a> {
26  /// Returns the receiver that listens for signals from Dart.
27  /// If this function is called multiple times,
28  /// only the most recent receiver remains active,
29  /// and all previous ones become inactive after receiving `None`.
30  fn get_dart_signal_receiver() -> SignalReceiver<DartSignalPack<Self>>;
31}
32
33/// Capability of sending signals from Dart to Rust with binary data.
34pub trait DartSignalBinary: for<'a> Deserialize<'a> {
35  /// Returns the receiver that listens for signals from Dart.
36  /// If this function is called multiple times,
37  /// only the most recent receiver remains active,
38  /// and all previous ones become inactive after receiving `None`.
39  fn get_dart_signal_receiver() -> SignalReceiver<DartSignalPack<Self>>;
40}
41
42/// Enables a type to be nested within a signal struct or enum.
43pub trait SignalPiece {
44  /// This function is a no-op.
45  /// It's purely used for checking that
46  /// a field implements the `SignalPiece` trait.
47  #[doc(hidden)]
48  fn be_signal_piece(&self) {}
49}
50
51// Implement the trait for simple primitives.
52impl SignalPiece for i8 {}
53impl SignalPiece for i16 {}
54impl SignalPiece for i32 {}
55impl SignalPiece for i64 {}
56impl SignalPiece for i128 {}
57impl SignalPiece for u8 {}
58impl SignalPiece for u16 {}
59impl SignalPiece for u32 {}
60impl SignalPiece for u64 {}
61impl SignalPiece for u128 {}
62impl SignalPiece for f32 {}
63impl SignalPiece for f64 {}
64impl SignalPiece for bool {}
65impl SignalPiece for char {}
66impl SignalPiece for String {}
67impl SignalPiece for &str {}
68
69// Implement the trait for container types.
70impl<T> SignalPiece for Box<T> where T: SignalPiece {}
71impl<T> SignalPiece for Option<T> where T: SignalPiece {}
72
73// Implement the trait for collection types.
74impl<T, const N: usize> SignalPiece for [T; N] where T: SignalPiece {}
75impl<T> SignalPiece for Vec<T> where T: SignalPiece {}
76impl<T> SignalPiece for HashSet<T> where T: SignalPiece {}
77impl<T> SignalPiece for BTreeSet<T> where T: SignalPiece {}
78impl<K, V> SignalPiece for HashMap<K, V>
79where
80  K: SignalPiece,
81  V: SignalPiece,
82{
83}
84impl<K, V> SignalPiece for BTreeMap<K, V>
85where
86  K: SignalPiece,
87  V: SignalPiece,
88{
89}
90
91// Implement the trait for tuples.
92impl SignalPiece for () {}
93impl<T1> SignalPiece for (T1,) where T1: SignalPiece {}
94impl<T1, T2> SignalPiece for (T1, T2)
95where
96  T1: SignalPiece,
97  T2: SignalPiece,
98{
99}
100impl<T1, T2, T3> SignalPiece for (T1, T2, T3)
101where
102  T1: SignalPiece,
103  T2: SignalPiece,
104  T3: SignalPiece,
105{
106}
107impl<T1, T2, T3, T4> SignalPiece for (T1, T2, T3, T4)
108where
109  T1: SignalPiece,
110  T2: SignalPiece,
111  T3: SignalPiece,
112  T4: SignalPiece,
113{
114}