46
46
//!
47
47
//! # Note
48
48
//!
49
- //! If persisting [`Scorer`], it must be restored using the same [`Time`] parameterization. Using a
50
- //! different type results in undefined behavior. Specifically, persisting when built with feature
51
- //! `no-std` and restoring without it, or vice versa, uses different types and thus is undefined.
49
+ //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
50
+ //! different types and thus is undefined.
52
51
//!
53
52
//! [`find_route`]: crate::routing::router::find_route
54
53
@@ -59,9 +58,10 @@ use util::ser::{Readable, Writeable, Writer};
59
58
60
59
use prelude:: * ;
61
60
use core:: cell:: { RefCell , RefMut } ;
62
- use core:: ops:: { DerefMut , Sub } ;
61
+ use core:: ops:: DerefMut ;
63
62
use core:: time:: Duration ;
64
- use io:: { self , Read } ; use sync:: { Mutex , MutexGuard } ;
63
+ use io:: { self , Read } ;
64
+ use sync:: { Mutex , MutexGuard } ;
65
65
66
66
/// We define Score ever-so-slightly differently based on whether we are being build for C bindings
67
67
/// or not. For users, `LockableScore` must somehow be writeable to disk. For Rust users, this is
@@ -185,23 +185,25 @@ impl<'a, S: Writeable> Writeable for MutexGuard<'a, S> {
185
185
/// See [module-level documentation] for usage.
186
186
///
187
187
/// [module-level documentation]: crate::routing::scoring
188
- pub type Scorer = ScorerUsingTime :: < DefaultTime > ;
189
-
190
- /// Time used by [`Scorer`].
191
188
#[ cfg( not( feature = "no-std" ) ) ]
192
- pub type DefaultTime = std:: time:: Instant ;
193
-
194
- /// Time used by [`Scorer`].
189
+ pub type Scorer = ScorerUsingTime :: < std:: time:: Instant > ;
195
190
#[ cfg( feature = "no-std" ) ]
196
- pub type DefaultTime = Eternity ;
191
+ pub type Scorer = ScorerUsingTime :: < time :: Eternity > ;
197
192
198
- /// [`Score`] implementation parameterized by [`Time`].
193
+ // Note that ideally we'd hide ScorerUsingTime from public view by sealing it as well, but rustdoc
194
+ // doesn't handle this well - instead exposing a `Scorer` which has no trait implementation(s) or
195
+ // methods at all.
196
+
197
+ /// [`Score`] implementation.
199
198
///
200
199
/// See [`Scorer`] for details.
201
200
///
202
201
/// # Note
203
202
///
204
- /// Mixing [`Time`] types between serialization and deserialization results in undefined behavior.
203
+ /// Mixing the `no-std` feature between serialization and deserialization results in undefined
204
+ /// behavior.
205
+ ///
206
+ /// (C-not exported) generally all users should use the [`Scorer`] type alias.
205
207
pub struct ScorerUsingTime < T : Time > {
206
208
params : ScoringParameters ,
207
209
// TODO: Remove entries of closed channels.
@@ -247,8 +249,8 @@ pub struct ScoringParameters {
247
249
///
248
250
/// # Note
249
251
///
250
- /// When time is an [`Eternity`], as is default when enabling feature `no-std`, it will never
251
- /// elapse. Therefore, this penalty will never decay.
252
+ /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
253
+ /// never decay.
252
254
///
253
255
/// [`failure_penalty_msat`]: Self::failure_penalty_msat
254
256
pub failure_penalty_half_life : Duration ,
@@ -273,20 +275,6 @@ struct ChannelFailure<T: Time> {
273
275
last_failed : T ,
274
276
}
275
277
276
- /// A measurement of time.
277
- pub trait Time : Sub < Duration , Output = Self > where Self : Sized {
278
- /// Returns an instance corresponding to the current moment.
279
- fn now ( ) -> Self ;
280
-
281
- /// Returns the amount of time elapsed since `self` was created.
282
- fn elapsed ( & self ) -> Duration ;
283
-
284
- /// Returns the amount of time passed since the beginning of [`Time`].
285
- ///
286
- /// Used during (de-)serialization.
287
- fn duration_since_epoch ( ) -> Duration ;
288
- }
289
-
290
278
impl < T : Time > ScorerUsingTime < T > {
291
279
/// Creates a new scorer using the given scoring parameters.
292
280
pub fn new ( params : ScoringParameters ) -> Self {
@@ -383,48 +371,6 @@ impl<T: Time> Score for ScorerUsingTime<T> {
383
371
}
384
372
}
385
373
386
- #[ cfg( not( feature = "no-std" ) ) ]
387
- impl Time for std:: time:: Instant {
388
- fn now ( ) -> Self {
389
- std:: time:: Instant :: now ( )
390
- }
391
-
392
- fn duration_since_epoch ( ) -> Duration {
393
- use std:: time:: SystemTime ;
394
- SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
395
- }
396
-
397
- fn elapsed ( & self ) -> Duration {
398
- std:: time:: Instant :: elapsed ( self )
399
- }
400
- }
401
-
402
- /// A state in which time has no meaning.
403
- #[ derive( Debug , PartialEq , Eq ) ]
404
- pub struct Eternity ;
405
-
406
- impl Time for Eternity {
407
- fn now ( ) -> Self {
408
- Self
409
- }
410
-
411
- fn duration_since_epoch ( ) -> Duration {
412
- Duration :: from_secs ( 0 )
413
- }
414
-
415
- fn elapsed ( & self ) -> Duration {
416
- Duration :: from_secs ( 0 )
417
- }
418
- }
419
-
420
- impl Sub < Duration > for Eternity {
421
- type Output = Self ;
422
-
423
- fn sub ( self , _other : Duration ) -> Self {
424
- self
425
- }
426
- }
427
-
428
374
impl < T : Time > Writeable for ScorerUsingTime < T > {
429
375
#[ inline]
430
376
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
@@ -475,9 +421,72 @@ impl<T: Time> Readable for ChannelFailure<T> {
475
421
}
476
422
}
477
423
424
+ pub ( crate ) mod time {
425
+ use core:: ops:: Sub ;
426
+ use core:: time:: Duration ;
427
+ /// A measurement of time.
428
+ pub trait Time : Sub < Duration , Output = Self > where Self : Sized {
429
+ /// Returns an instance corresponding to the current moment.
430
+ fn now ( ) -> Self ;
431
+
432
+ /// Returns the amount of time elapsed since `self` was created.
433
+ fn elapsed ( & self ) -> Duration ;
434
+
435
+ /// Returns the amount of time passed since the beginning of [`Time`].
436
+ ///
437
+ /// Used during (de-)serialization.
438
+ fn duration_since_epoch ( ) -> Duration ;
439
+ }
440
+
441
+ /// A state in which time has no meaning.
442
+ #[ derive( Debug , PartialEq , Eq ) ]
443
+ pub struct Eternity ;
444
+
445
+ #[ cfg( not( feature = "no-std" ) ) ]
446
+ impl Time for std:: time:: Instant {
447
+ fn now ( ) -> Self {
448
+ std:: time:: Instant :: now ( )
449
+ }
450
+
451
+ fn duration_since_epoch ( ) -> Duration {
452
+ use std:: time:: SystemTime ;
453
+ SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( )
454
+ }
455
+
456
+ fn elapsed ( & self ) -> Duration {
457
+ std:: time:: Instant :: elapsed ( self )
458
+ }
459
+ }
460
+
461
+ impl Time for Eternity {
462
+ fn now ( ) -> Self {
463
+ Self
464
+ }
465
+
466
+ fn duration_since_epoch ( ) -> Duration {
467
+ Duration :: from_secs ( 0 )
468
+ }
469
+
470
+ fn elapsed ( & self ) -> Duration {
471
+ Duration :: from_secs ( 0 )
472
+ }
473
+ }
474
+
475
+ impl Sub < Duration > for Eternity {
476
+ type Output = Self ;
477
+
478
+ fn sub ( self , _other : Duration ) -> Self {
479
+ self
480
+ }
481
+ }
482
+ }
483
+
484
+ pub ( crate ) use self :: time:: Time ;
485
+
478
486
#[ cfg( test) ]
479
487
mod tests {
480
- use super :: { Eternity , ScoringParameters , ScorerUsingTime , Time } ;
488
+ use super :: { ScoringParameters , ScorerUsingTime , Time } ;
489
+ use super :: time:: Eternity ;
481
490
482
491
use routing:: scoring:: Score ;
483
492
use routing:: network_graph:: NodeId ;
0 commit comments