tokio_process_tools/output_stream/
policy.rs1use crate::output_stream::num_bytes::NumBytes;
2
3mod sealed {
4 pub trait DeliverySealed {}
5
6 pub trait ReplaySealed {}
7}
8
9pub trait Delivery:
11 sealed::DeliverySealed + Clone + Copy + std::fmt::Debug + PartialEq + Eq + Send + Sync + 'static
12{
13 fn guarantee(self) -> DeliveryGuarantee;
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct BestEffortDelivery;
22
23impl sealed::DeliverySealed for BestEffortDelivery {}
24
25impl Delivery for BestEffortDelivery {
26 fn guarantee(self) -> DeliveryGuarantee {
27 DeliveryGuarantee::BestEffort
28 }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct ReliableDelivery;
37
38impl sealed::DeliverySealed for ReliableDelivery {}
39
40impl Delivery for ReliableDelivery {
41 fn guarantee(self) -> DeliveryGuarantee {
42 DeliveryGuarantee::ReliableForActiveSubscribers
43 }
44}
45
46pub trait Replay:
48 sealed::ReplaySealed + Clone + Copy + std::fmt::Debug + PartialEq + Eq + Send + Sync + 'static
49{
50 fn replay_retention(self) -> Option<ReplayRetention>;
52
53 fn replay_enabled(self) -> bool;
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub struct NoReplay;
60
61impl sealed::ReplaySealed for NoReplay {}
62
63impl Replay for NoReplay {
64 fn replay_retention(self) -> Option<ReplayRetention> {
65 None
66 }
67
68 fn replay_enabled(self) -> bool {
69 false
70 }
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub struct ReplayEnabled {
76 pub replay_retention: ReplayRetention,
78}
79
80impl ReplayEnabled {
81 #[must_use]
83 pub fn new(replay_retention: ReplayRetention) -> Self {
84 replay_retention.assert_non_zero("replay_retention");
85 Self { replay_retention }
86 }
87}
88
89impl sealed::ReplaySealed for ReplayEnabled {}
90
91impl Replay for ReplayEnabled {
92 fn replay_retention(self) -> Option<ReplayRetention> {
93 Some(self.replay_retention)
94 }
95
96 fn replay_enabled(self) -> bool {
97 true
98 }
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum DeliveryGuarantee {
104 BestEffort,
107
108 ReliableForActiveSubscribers,
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub enum ReplayRetention {
115 LastChunks(usize),
117
118 LastBytes(NumBytes),
130
131 All,
136}
137
138impl ReplayRetention {
139 pub(crate) fn assert_non_zero(self, parameter_name: &str) {
140 match self {
141 ReplayRetention::LastChunks(0) => {
142 panic!("{parameter_name} must retain at least one chunk");
143 }
144 ReplayRetention::LastBytes(bytes) if bytes.bytes() == 0 => {
145 panic!("{parameter_name} must retain at least one byte");
146 }
147 ReplayRetention::LastChunks(_)
148 | ReplayRetention::LastBytes(_)
149 | ReplayRetention::All => {}
150 }
151 }
152}