12
12
rust_decimal:: { prelude:: FromPrimitive , Decimal } ,
13
13
serde:: { de:: Error , Deserialize , Serialize } ,
14
14
std:: {
15
+ cmp:: Ordering ,
15
16
fmt:: Display ,
16
17
num:: NonZeroI64 ,
17
18
ops:: { Add , Deref , DerefMut , Div , Sub } ,
@@ -206,9 +207,24 @@ pub enum JsonBinaryEncoding {
206
207
Hex ,
207
208
}
208
209
209
- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , PartialOrd , Ord , From ) ]
210
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , From ) ]
210
211
pub enum Channel {
211
212
FixedRate ( FixedRate ) ,
213
+ RealTime ,
214
+ }
215
+
216
+ impl PartialOrd for Channel {
217
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
218
+ let rate_left = match self {
219
+ Channel :: FixedRate ( rate) => rate. duration ( ) . as_micros ( ) ,
220
+ Channel :: RealTime => FixedRate :: MIN . duration ( ) . as_micros ( ) ,
221
+ } ;
222
+ let rate_right = match other {
223
+ Channel :: FixedRate ( rate) => rate. duration ( ) . as_micros ( ) ,
224
+ Channel :: RealTime => FixedRate :: MIN . duration ( ) . as_micros ( ) ,
225
+ } ;
226
+ Some ( rate_left. cmp ( & rate_right) )
227
+ }
212
228
}
213
229
214
230
impl Serialize for Channel {
@@ -217,34 +233,30 @@ impl Serialize for Channel {
217
233
S : serde:: Serializer ,
218
234
{
219
235
match self {
220
- Channel :: FixedRate ( fixed_rate) => {
221
- if * fixed_rate == FixedRate :: MIN {
222
- return serializer. serialize_str ( "real_time" ) ;
223
- }
224
- serializer. serialize_str ( & format ! (
225
- "fixed_rate@{}ms" ,
226
- fixed_rate. duration( ) . as_millis( )
227
- ) )
228
- }
236
+ Channel :: FixedRate ( fixed_rate) => serializer. serialize_str ( & format ! (
237
+ "fixed_rate@{}ms" ,
238
+ fixed_rate. duration( ) . as_millis( )
239
+ ) ) ,
240
+ Channel :: RealTime => serializer. serialize_str ( "real_time" ) ,
229
241
}
230
242
}
231
243
}
232
244
233
245
pub mod channel_ids {
234
246
use super :: ChannelId ;
235
247
236
- pub const FIXED_RATE_1 : ChannelId = ChannelId ( 1 ) ;
248
+ pub const REAL_TIME : ChannelId = ChannelId ( 1 ) ;
237
249
pub const FIXED_RATE_50 : ChannelId = ChannelId ( 2 ) ;
238
250
pub const FIXED_RATE_200 : ChannelId = ChannelId ( 3 ) ;
239
251
}
240
252
241
253
impl Display for Channel {
242
254
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
243
255
match self {
244
- Channel :: FixedRate ( fixed_rate) => match * fixed_rate {
245
- FixedRate :: MIN => write ! ( f, "real_time" ) ,
246
- rate => write ! ( f , "fixed_rate@{}ms" , rate . duration ( ) . as_millis ( ) ) ,
247
- } ,
256
+ Channel :: FixedRate ( fixed_rate) => {
257
+ write ! ( f, "fixed_rate@{}ms" , fixed_rate . duration ( ) . as_millis ( ) )
258
+ }
259
+ Channel :: RealTime => write ! ( f , "real_time" ) ,
248
260
}
249
261
}
250
262
}
@@ -253,11 +265,11 @@ impl Channel {
253
265
pub fn id ( & self ) -> ChannelId {
254
266
match self {
255
267
Channel :: FixedRate ( fixed_rate) => match fixed_rate. duration ( ) . as_millis ( ) {
256
- 1 => channel_ids:: FIXED_RATE_1 ,
257
268
50 => channel_ids:: FIXED_RATE_50 ,
258
269
200 => channel_ids:: FIXED_RATE_200 ,
259
270
_ => panic ! ( "unknown channel: {self:?}" ) ,
260
271
} ,
272
+ Channel :: RealTime => channel_ids:: REAL_TIME ,
261
273
}
262
274
}
263
275
}
@@ -271,7 +283,7 @@ fn id_supports_all_fixed_rates() {
271
283
272
284
fn parse_channel ( value : & str ) -> Option < Channel > {
273
285
if value == "real_time" {
274
- Some ( Channel :: FixedRate ( FixedRate :: MIN ) )
286
+ Some ( Channel :: RealTime )
275
287
} else if let Some ( rest) = value. strip_prefix ( "fixed_rate@" ) {
276
288
let ms_value = rest. strip_suffix ( "ms" ) ?;
277
289
Some ( Channel :: FixedRate ( FixedRate :: from_millis (
@@ -298,9 +310,6 @@ pub struct FixedRate {
298
310
}
299
311
300
312
impl FixedRate {
301
- pub const RATE_1_MS : Self = Self {
302
- rate : DurationUs :: from_millis_u32 ( 1 ) ,
303
- } ;
304
313
pub const RATE_50_MS : Self = Self {
305
314
rate : DurationUs :: from_millis_u32 ( 50 ) ,
306
315
} ;
@@ -312,7 +321,7 @@ impl FixedRate {
312
321
// - Values are sorted.
313
322
// - 1 second contains a whole number of each interval.
314
323
// - all intervals are divisable by the smallest interval.
315
- pub const ALL : [ Self ; 3 ] = [ Self :: RATE_1_MS , Self :: RATE_50_MS , Self :: RATE_200_MS ] ;
324
+ pub const ALL : [ Self ; 2 ] = [ Self :: RATE_50_MS , Self :: RATE_200_MS ] ;
316
325
pub const MIN : Self = Self :: ALL [ 0 ] ;
317
326
318
327
pub fn from_millis ( millis : u32 ) -> Option < Self > {
0 commit comments