@@ -10,37 +10,44 @@ pub trait Extent: Sized + Default {
1010
1111 fn new ( unit : & Self :: Base , count : & Self :: Multiplier ) -> Self ;
1212
13+ /// # Errors
14+ ///
15+ /// Will return `IntErrorKind` if `add` would overflow the internal `Duration`.
1316 fn increase ( & self , add : Self :: Multiplier ) -> Result < Self , IntErrorKind > ;
17+
18+ /// # Errors
19+ ///
20+ /// Will return `IntErrorKind` if `sub` would underflow the internal `Duration`.
1421 fn decrease ( & self , sub : Self :: Multiplier ) -> Result < Self , IntErrorKind > ;
1522
1623 fn total ( & self ) -> Option < Result < Self :: Product , TryFromIntError > > ;
1724 fn total_next ( & self ) -> Option < Result < Self :: Product , TryFromIntError > > ;
1825}
1926
20- pub type TimeExtentBase = Duration ;
21- pub type TimeExtentMultiplier = u64 ;
22- pub type TimeExtentProduct = TimeExtentBase ;
27+ pub type Base = Duration ;
28+ pub type Multiplier = u64 ;
29+ pub type Product = Base ;
2330
2431#[ derive( Debug , Default , Hash , PartialEq , Eq ) ]
2532pub struct TimeExtent {
26- pub increment : TimeExtentBase ,
27- pub amount : TimeExtentMultiplier ,
33+ pub increment : Base ,
34+ pub amount : Multiplier ,
2835}
2936
3037pub const ZERO : TimeExtent = TimeExtent {
31- increment : TimeExtentBase :: ZERO ,
32- amount : TimeExtentMultiplier :: MIN ,
38+ increment : Base :: ZERO ,
39+ amount : Multiplier :: MIN ,
3340} ;
3441pub const MAX : TimeExtent = TimeExtent {
35- increment : TimeExtentBase :: MAX ,
36- amount : TimeExtentMultiplier :: MAX ,
42+ increment : Base :: MAX ,
43+ amount : Multiplier :: MAX ,
3744} ;
3845
3946impl TimeExtent {
4047 #[ must_use]
41- pub const fn from_sec ( seconds : u64 , amount : & TimeExtentMultiplier ) -> Self {
48+ pub const fn from_sec ( seconds : u64 , amount : & Multiplier ) -> Self {
4249 Self {
43- increment : TimeExtentBase :: from_secs ( seconds) ,
50+ increment : Base :: from_secs ( seconds) ,
4451 amount : * amount,
4552 }
4653 }
@@ -61,9 +68,9 @@ fn checked_duration_from_nanos(time: u128) -> Result<Duration, TryFromIntError>
6168}
6269
6370impl Extent for TimeExtent {
64- type Base = TimeExtentBase ;
65- type Multiplier = TimeExtentMultiplier ;
66- type Product = TimeExtentProduct ;
71+ type Base = Base ;
72+ type Multiplier = Multiplier ;
73+ type Product = Product ;
6774
6875 fn new ( increment : & Self :: Base , amount : & Self :: Multiplier ) -> Self {
6976 Self {
@@ -107,60 +114,58 @@ impl Extent for TimeExtent {
107114 }
108115}
109116
110- pub trait MakeTimeExtent < Clock > : Sized
117+ pub trait Make < Clock > : Sized
111118where
112119 Clock : TimeNow ,
113120{
114121 #[ must_use]
115- fn now ( increment : & TimeExtentBase ) -> Option < Result < TimeExtent , TryFromIntError > > {
122+ fn now ( increment : & Base ) -> Option < Result < TimeExtent , TryFromIntError > > {
116123 Clock :: now ( )
117124 . as_nanos ( )
118125 . checked_div ( ( * increment) . as_nanos ( ) )
119- . map ( |amount| match TimeExtentMultiplier :: try_from ( amount) {
126+ . map ( |amount| match Multiplier :: try_from ( amount) {
120127 Err ( error) => Err ( error) ,
121128 Ok ( amount) => Ok ( TimeExtent :: new ( increment, & amount) ) ,
122129 } )
123130 }
124131
125132 #[ must_use]
126- fn now_after ( increment : & TimeExtentBase , add_time : & Duration ) -> Option < Result < TimeExtent , TryFromIntError > > {
133+ fn now_after ( increment : & Base , add_time : & Duration ) -> Option < Result < TimeExtent , TryFromIntError > > {
127134 match Clock :: add ( add_time) {
128135 None => None ,
129- Some ( time) => {
130- time. as_nanos ( )
131- . checked_div ( increment. as_nanos ( ) )
132- . map ( |amount| match TimeExtentMultiplier :: try_from ( amount) {
133- Err ( error) => Err ( error) ,
134- Ok ( amount) => Ok ( TimeExtent :: new ( increment, & amount) ) ,
135- } )
136- }
136+ Some ( time) => time
137+ . as_nanos ( )
138+ . checked_div ( increment. as_nanos ( ) )
139+ . map ( |amount| match Multiplier :: try_from ( amount) {
140+ Err ( error) => Err ( error) ,
141+ Ok ( amount) => Ok ( TimeExtent :: new ( increment, & amount) ) ,
142+ } ) ,
137143 }
138144 }
139145
140146 #[ must_use]
141- fn now_before ( increment : & TimeExtentBase , sub_time : & Duration ) -> Option < Result < TimeExtent , TryFromIntError > > {
147+ fn now_before ( increment : & Base , sub_time : & Duration ) -> Option < Result < TimeExtent , TryFromIntError > > {
142148 match Clock :: sub ( sub_time) {
143149 None => None ,
144- Some ( time) => {
145- time. as_nanos ( )
146- . checked_div ( increment. as_nanos ( ) )
147- . map ( |amount| match TimeExtentMultiplier :: try_from ( amount) {
148- Err ( error) => Err ( error) ,
149- Ok ( amount) => Ok ( TimeExtent :: new ( increment, & amount) ) ,
150- } )
151- }
150+ Some ( time) => time
151+ . as_nanos ( )
152+ . checked_div ( increment. as_nanos ( ) )
153+ . map ( |amount| match Multiplier :: try_from ( amount) {
154+ Err ( error) => Err ( error) ,
155+ Ok ( amount) => Ok ( TimeExtent :: new ( increment, & amount) ) ,
156+ } ) ,
152157 }
153158 }
154159}
155160
156161#[ derive( Debug ) ]
157- pub struct TimeExtentMaker < const CLOCK_TYPE : usize > { }
162+ pub struct Maker < const CLOCK_TYPE : usize > { }
158163
159- pub type WorkingTimeExtentMaker = TimeExtentMaker < { Type :: WorkingClock as usize } > ;
160- pub type StoppedTimeExtentMaker = TimeExtentMaker < { Type :: StoppedClock as usize } > ;
164+ pub type WorkingTimeExtentMaker = Maker < { Type :: WorkingClock as usize } > ;
165+ pub type StoppedTimeExtentMaker = Maker < { Type :: StoppedClock as usize } > ;
161166
162- impl MakeTimeExtent < Working > for WorkingTimeExtentMaker { }
163- impl MakeTimeExtent < Stopped > for StoppedTimeExtentMaker { }
167+ impl Make < Working > for WorkingTimeExtentMaker { }
168+ impl Make < Stopped > for StoppedTimeExtentMaker { }
164169
165170#[ cfg( not( test) ) ]
166171pub type DefaultTimeExtentMaker = WorkingTimeExtentMaker ;
@@ -172,8 +177,7 @@ pub type DefaultTimeExtentMaker = StoppedTimeExtentMaker;
172177mod test {
173178
174179 use crate :: protocol:: clock:: time_extent:: {
175- checked_duration_from_nanos, DefaultTimeExtentMaker , Extent , MakeTimeExtent , TimeExtent , TimeExtentBase ,
176- TimeExtentMultiplier , TimeExtentProduct , MAX , ZERO ,
180+ checked_duration_from_nanos, Base , DefaultTimeExtentMaker , Extent , Make , Multiplier , Product , TimeExtent , MAX , ZERO ,
177181 } ;
178182 use crate :: protocol:: clock:: { Current , DurationSinceUnixEpoch , StoppedTime } ;
179183
@@ -238,7 +242,7 @@ mod test {
238242
239243 #[ test]
240244 fn it_should_make_empty_for_zero ( ) {
241- assert_eq ! ( TimeExtent :: from_sec( u64 :: MIN , & TimeExtentMultiplier :: MIN ) , ZERO ) ;
245+ assert_eq ! ( TimeExtent :: from_sec( u64 :: MIN , & Multiplier :: MIN ) , ZERO ) ;
242246 }
243247 #[ test]
244248 fn it_should_make_from_seconds ( ) {
@@ -254,15 +258,15 @@ mod test {
254258
255259 #[ test]
256260 fn it_should_make_empty_for_zero ( ) {
257- assert_eq ! ( TimeExtent :: new( & TimeExtentBase :: ZERO , & TimeExtentMultiplier :: MIN ) , ZERO ) ;
261+ assert_eq ! ( TimeExtent :: new( & Base :: ZERO , & Multiplier :: MIN ) , ZERO ) ;
258262 }
259263
260264 #[ test]
261265 fn it_should_make_new ( ) {
262266 assert_eq ! (
263- TimeExtent :: new( & TimeExtentBase :: from_millis( 2 ) , & TIME_EXTENT_VAL . amount) ,
267+ TimeExtent :: new( & Base :: from_millis( 2 ) , & TIME_EXTENT_VAL . amount) ,
264268 TimeExtent {
265- increment: TimeExtentBase :: from_millis( 2 ) ,
269+ increment: Base :: from_millis( 2 ) ,
266270 amount: TIME_EXTENT_VAL . amount
267271 }
268272 ) ;
@@ -328,30 +332,27 @@ mod test {
328332
329333 #[ test]
330334 fn it_should_be_zero_for_zero ( ) {
331- assert_eq ! ( ZERO . total( ) . unwrap( ) . unwrap( ) , TimeExtentProduct :: ZERO ) ;
335+ assert_eq ! ( ZERO . total( ) . unwrap( ) . unwrap( ) , Product :: ZERO ) ;
332336 }
333337
334338 #[ test]
335339 fn it_should_give_a_total ( ) {
336340 assert_eq ! (
337341 TIME_EXTENT_VAL . total( ) . unwrap( ) . unwrap( ) ,
338- TimeExtentProduct :: from_secs( TIME_EXTENT_VAL . increment. as_secs( ) * TIME_EXTENT_VAL . amount)
342+ Product :: from_secs( TIME_EXTENT_VAL . increment. as_secs( ) * TIME_EXTENT_VAL . amount)
339343 ) ;
340344
341345 assert_eq ! (
342- TimeExtent :: new( & TimeExtentBase :: from_millis( 2 ) , & ( TIME_EXTENT_VAL . amount * 1000 ) )
346+ TimeExtent :: new( & Base :: from_millis( 2 ) , & ( TIME_EXTENT_VAL . amount * 1000 ) )
343347 . total( )
344348 . unwrap( )
345349 . unwrap( ) ,
346- TimeExtentProduct :: from_secs( TIME_EXTENT_VAL . increment. as_secs( ) * TIME_EXTENT_VAL . amount)
350+ Product :: from_secs( TIME_EXTENT_VAL . increment. as_secs( ) * TIME_EXTENT_VAL . amount)
347351 ) ;
348352
349353 assert_eq ! (
350- TimeExtent :: new( & TimeExtentBase :: from_secs( 1 ) , & ( u64 :: MAX ) )
351- . total( )
352- . unwrap( )
353- . unwrap( ) ,
354- TimeExtentProduct :: from_secs( u64 :: MAX )
354+ TimeExtent :: new( & Base :: from_secs( 1 ) , & ( u64 :: MAX ) ) . total( ) . unwrap( ) . unwrap( ) ,
355+ Product :: from_secs( u64 :: MAX )
355356 ) ;
356357 }
357358
@@ -378,33 +379,33 @@ mod test {
378379
379380 #[ test]
380381 fn it_should_be_zero_for_zero ( ) {
381- assert_eq ! ( ZERO . total_next( ) . unwrap( ) . unwrap( ) , TimeExtentProduct :: ZERO ) ;
382+ assert_eq ! ( ZERO . total_next( ) . unwrap( ) . unwrap( ) , Product :: ZERO ) ;
382383 }
383384
384385 #[ test]
385386 fn it_should_give_a_total ( ) {
386387 assert_eq ! (
387388 TIME_EXTENT_VAL . total_next( ) . unwrap( ) . unwrap( ) ,
388- TimeExtentProduct :: from_secs( TIME_EXTENT_VAL . increment. as_secs( ) * ( TIME_EXTENT_VAL . amount + 1 ) )
389+ Product :: from_secs( TIME_EXTENT_VAL . increment. as_secs( ) * ( TIME_EXTENT_VAL . amount + 1 ) )
389390 ) ;
390391
391392 assert_eq ! (
392- TimeExtent :: new( & TimeExtentBase :: from_millis( 2 ) , & ( TIME_EXTENT_VAL . amount * 1000 ) )
393+ TimeExtent :: new( & Base :: from_millis( 2 ) , & ( TIME_EXTENT_VAL . amount * 1000 ) )
393394 . total_next( )
394395 . unwrap( )
395396 . unwrap( ) ,
396- TimeExtentProduct :: new(
397+ Product :: new(
397398 TIME_EXTENT_VAL . increment. as_secs( ) * ( TIME_EXTENT_VAL . amount) ,
398- TimeExtentBase :: from_millis( 2 ) . as_nanos( ) . try_into( ) . unwrap( )
399+ Base :: from_millis( 2 ) . as_nanos( ) . try_into( ) . unwrap( )
399400 )
400401 ) ;
401402
402403 assert_eq ! (
403- TimeExtent :: new( & TimeExtentBase :: from_secs( 1 ) , & ( u64 :: MAX - 1 ) )
404+ TimeExtent :: new( & Base :: from_secs( 1 ) , & ( u64 :: MAX - 1 ) )
404405 . total_next( )
405406 . unwrap( )
406407 . unwrap( ) ,
407- TimeExtentProduct :: from_secs( u64 :: MAX )
408+ Product :: from_secs( u64 :: MAX )
408409 ) ;
409410 }
410411
@@ -453,16 +454,14 @@ mod test {
453454
454455 #[ test]
455456 fn it_should_fail_for_zero ( ) {
456- assert_eq ! ( DefaultTimeExtentMaker :: now( & TimeExtentBase :: ZERO ) , None ) ;
457+ assert_eq ! ( DefaultTimeExtentMaker :: now( & Base :: ZERO ) , None ) ;
457458 }
458459
459460 #[ test]
460461 fn it_should_fail_if_amount_exceeds_bounds ( ) {
461462 Current :: local_set ( & DurationSinceUnixEpoch :: MAX ) ;
462463 assert_eq ! (
463- DefaultTimeExtentMaker :: now( & TimeExtentBase :: from_millis( 1 ) )
464- . unwrap( )
465- . unwrap_err( ) ,
464+ DefaultTimeExtentMaker :: now( & Base :: from_millis( 1 ) ) . unwrap( ) . unwrap_err( ) ,
466465 u64 :: try_from( u128 :: MAX ) . unwrap_err( )
467466 ) ;
468467 }
@@ -488,20 +487,17 @@ mod test {
488487
489488 #[ test]
490489 fn it_should_fail_for_zero ( ) {
491- assert_eq ! (
492- DefaultTimeExtentMaker :: now_after( & TimeExtentBase :: ZERO , & Duration :: ZERO ) ,
493- None
494- ) ;
490+ assert_eq ! ( DefaultTimeExtentMaker :: now_after( & Base :: ZERO , & Duration :: ZERO ) , None ) ;
495491
496492 Current :: local_set ( & DurationSinceUnixEpoch :: MAX ) ;
497- assert_eq ! ( DefaultTimeExtentMaker :: now_after( & TimeExtentBase :: ZERO , & Duration :: MAX ) , None ) ;
493+ assert_eq ! ( DefaultTimeExtentMaker :: now_after( & Base :: ZERO , & Duration :: MAX ) , None ) ;
498494 }
499495
500496 #[ test]
501497 fn it_should_fail_if_amount_exceeds_bounds ( ) {
502498 Current :: local_set ( & DurationSinceUnixEpoch :: MAX ) ;
503499 assert_eq ! (
504- DefaultTimeExtentMaker :: now_after( & TimeExtentBase :: from_millis( 1 ) , & Duration :: ZERO )
500+ DefaultTimeExtentMaker :: now_after( & Base :: from_millis( 1 ) , & Duration :: ZERO )
505501 . unwrap( )
506502 . unwrap_err( ) ,
507503 u64 :: try_from( u128 :: MAX ) . unwrap_err( )
@@ -519,36 +515,30 @@ mod test {
519515
520516 assert_eq ! (
521517 DefaultTimeExtentMaker :: now_before(
522- & TimeExtentBase :: from_secs( u64 :: from( u32 :: MAX ) ) ,
518+ & Base :: from_secs( u64 :: from( u32 :: MAX ) ) ,
523519 & Duration :: from_secs( u64 :: from( u32 :: MAX ) )
524520 )
525521 . unwrap( )
526522 . unwrap( ) ,
527523 TimeExtent {
528- increment: TimeExtentBase :: from_secs( u64 :: from( u32 :: MAX ) ) ,
524+ increment: Base :: from_secs( u64 :: from( u32 :: MAX ) ) ,
529525 amount: 4_294_967_296
530526 }
531527 ) ;
532528 }
533529
534530 #[ test]
535531 fn it_should_fail_for_zero ( ) {
536- assert_eq ! (
537- DefaultTimeExtentMaker :: now_before( & TimeExtentBase :: ZERO , & Duration :: ZERO ) ,
538- None
539- ) ;
532+ assert_eq ! ( DefaultTimeExtentMaker :: now_before( & Base :: ZERO , & Duration :: ZERO ) , None ) ;
540533
541- assert_eq ! (
542- DefaultTimeExtentMaker :: now_before( & TimeExtentBase :: ZERO , & Duration :: MAX ) ,
543- None
544- ) ;
534+ assert_eq ! ( DefaultTimeExtentMaker :: now_before( & Base :: ZERO , & Duration :: MAX ) , None ) ;
545535 }
546536
547537 #[ test]
548538 fn it_should_fail_if_amount_exceeds_bounds ( ) {
549539 Current :: local_set ( & DurationSinceUnixEpoch :: MAX ) ;
550540 assert_eq ! (
551- DefaultTimeExtentMaker :: now_before( & TimeExtentBase :: from_millis( 1 ) , & Duration :: ZERO )
541+ DefaultTimeExtentMaker :: now_before( & Base :: from_millis( 1 ) , & Duration :: ZERO )
552542 . unwrap( )
553543 . unwrap_err( ) ,
554544 u64 :: try_from( u128 :: MAX ) . unwrap_err( )
0 commit comments