1212 rust_decimal:: { prelude:: FromPrimitive , Decimal } ,
1313 serde:: { de:: Error , Deserialize , Serialize } ,
1414 std:: {
15+ cmp:: Ordering ,
1516 fmt:: Display ,
1617 num:: NonZeroI64 ,
1718 ops:: { Add , Deref , DerefMut , Div , Sub } ,
@@ -206,9 +207,24 @@ pub enum JsonBinaryEncoding {
206207 Hex ,
207208}
208209
209- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , PartialOrd , Ord , From ) ]
210+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , From ) ]
210211pub enum Channel {
211212 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+ }
212228}
213229
214230impl Serialize for Channel {
@@ -217,34 +233,30 @@ impl Serialize for Channel {
217233 S : serde:: Serializer ,
218234 {
219235 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" ) ,
229241 }
230242 }
231243}
232244
233245pub mod channel_ids {
234246 use super :: ChannelId ;
235247
236- pub const FIXED_RATE_1 : ChannelId = ChannelId ( 1 ) ;
248+ pub const REAL_TIME : ChannelId = ChannelId ( 1 ) ;
237249 pub const FIXED_RATE_50 : ChannelId = ChannelId ( 2 ) ;
238250 pub const FIXED_RATE_200 : ChannelId = ChannelId ( 3 ) ;
239251}
240252
241253impl Display for Channel {
242254 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
243255 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" ) ,
248260 }
249261 }
250262}
@@ -253,11 +265,11 @@ impl Channel {
253265 pub fn id ( & self ) -> ChannelId {
254266 match self {
255267 Channel :: FixedRate ( fixed_rate) => match fixed_rate. duration ( ) . as_millis ( ) {
256- 1 => channel_ids:: FIXED_RATE_1 ,
257268 50 => channel_ids:: FIXED_RATE_50 ,
258269 200 => channel_ids:: FIXED_RATE_200 ,
259270 _ => panic ! ( "unknown channel: {self:?}" ) ,
260271 } ,
272+ Channel :: RealTime => channel_ids:: REAL_TIME ,
261273 }
262274 }
263275}
@@ -271,7 +283,7 @@ fn id_supports_all_fixed_rates() {
271283
272284fn parse_channel ( value : & str ) -> Option < Channel > {
273285 if value == "real_time" {
274- Some ( Channel :: FixedRate ( FixedRate :: MIN ) )
286+ Some ( Channel :: RealTime )
275287 } else if let Some ( rest) = value. strip_prefix ( "fixed_rate@" ) {
276288 let ms_value = rest. strip_suffix ( "ms" ) ?;
277289 Some ( Channel :: FixedRate ( FixedRate :: from_millis (
@@ -298,9 +310,6 @@ pub struct FixedRate {
298310}
299311
300312impl FixedRate {
301- pub const RATE_1_MS : Self = Self {
302- rate : DurationUs :: from_millis_u32 ( 1 ) ,
303- } ;
304313 pub const RATE_50_MS : Self = Self {
305314 rate : DurationUs :: from_millis_u32 ( 50 ) ,
306315 } ;
@@ -312,7 +321,7 @@ impl FixedRate {
312321 // - Values are sorted.
313322 // - 1 second contains a whole number of each interval.
314323 // - 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 ] ;
316325 pub const MIN : Self = Self :: ALL [ 0 ] ;
317326
318327 pub fn from_millis ( millis : u32 ) -> Option < Self > {
0 commit comments