redux/action/
action_with_meta.rs

1use std::time::Duration;
2
3use crate::{SystemTime, Timestamp};
4
5use super::{ActionId, ActionMeta, RecursionDepth};
6
7/// Action with additional metadata like: id.
8#[derive(Debug, Clone)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct ActionWithMeta<Action> {
11    pub(super) meta: ActionMeta,
12
13    #[cfg_attr(feature = "serde", serde(flatten))]
14    pub(super) action: Action,
15}
16
17impl<Action> ActionWithMeta<Action> {
18    pub fn meta(&self) -> &ActionMeta {
19        &self.meta
20    }
21
22    pub fn action(&self) -> &Action {
23        &self.action
24    }
25
26    #[inline(always)]
27    pub fn id(&self) -> ActionId {
28        self.meta.id()
29    }
30
31    /// Recursion depth of a given action.
32    #[inline(always)]
33    pub fn depth(&self) -> RecursionDepth {
34        self.meta.depth()
35    }
36
37    #[inline(always)]
38    pub fn time(&self) -> Timestamp {
39        self.meta.time()
40    }
41
42    #[inline(always)]
43    pub fn sys_time(&self) -> SystemTime {
44        self.meta.sys_time()
45    }
46
47    #[inline(always)]
48    pub fn time_as_nanos(&self) -> u64 {
49        self.meta.time_as_nanos()
50    }
51
52    #[inline(always)]
53    pub fn duration_since_epoch(&self) -> Duration {
54        self.meta.duration_since_epoch()
55    }
56
57    #[inline(always)]
58    pub fn duration_since(&self, other: &ActionWithMeta<Action>) -> Duration {
59        self.meta.duration_since(&other.meta)
60    }
61
62    /// Splits the struct into a tuple of action and it's metadata.
63    #[inline(always)]
64    pub fn split(self) -> (Action, ActionMeta) {
65        (self.action, self.meta)
66    }
67}