@@ -28,7 +28,9 @@ impl Subseconds {
28
28
///
29
29
/// To obtain that representation in nanoseconds, see [`Subseconds::nanos`].
30
30
///
31
- /// To approximate a [`Subseconds`] from nanoseconds, see [`Subseconds::new_from_nanos`]
31
+ /// To approximate a [`Subseconds`] from nanoseconds, see [`Subseconds::new_from_nanos`].
32
+ ///
33
+ /// Returns `None` if `value > SUBSECONDS_PER_SECOND`. (See [`SUBSECONDS_PER_SECOND`]).
32
34
pub const fn new ( value : u32 ) -> Option < Self > {
33
35
if value > SUBSECONDS_PER_SECOND as u32 {
34
36
None
@@ -45,12 +47,14 @@ impl Subseconds {
45
47
/// To obtain that representation in nanoseconds, see [`Subseconds::nanos`].
46
48
///
47
49
/// To approximate a [`Subseconds`] from nanoseconds, see [`Subseconds::new_from_nanos`]
48
- pub const fn new_unchecked ( value : u32 ) -> Self {
50
+ pub ( crate ) const fn new_unchecked ( value : u32 ) -> Self {
49
51
Self ( value)
50
52
}
51
53
52
54
/// Create a new [`Subseconds`] from the given amount of nanoseconds,
53
- /// using a round-to-closest method.
55
+ /// using a round-to-nearest method.
56
+ ///
57
+ /// Returns [`None`] if `nanos >= NANOS_PER_SECOND`. (See [`NANOS_PER_SECOND`])
54
58
pub const fn new_from_nanos ( nanos : u32 ) -> Option < Self > {
55
59
if nanos >= NANOS_PER_SECOND as u32 {
56
60
return None ;
@@ -61,7 +65,7 @@ impl Subseconds {
61
65
Some ( Subseconds :: new_unchecked ( subseconds as u32 ) )
62
66
}
63
67
64
- /// Convert this [`Subseconds`] to nanoseconds, using a round-to-closest method.
68
+ /// Convert this [`Subseconds`] to nanoseconds, using a round-to-nearest method.
65
69
pub const fn nanos ( & self ) -> u32 {
66
70
let nanos = ( ( self . 0 as u64 * NS_PER_S ) + ( SUBS_PER_S / 2 ) ) / SUBS_PER_S ;
67
71
@@ -118,7 +122,7 @@ mod test {
118
122
119
123
use super :: * ;
120
124
121
- // Assert that values produced by [`Subseconds::nearest_increment`] for all
125
+ // Assert that values produced by [`Subseconds::nearest_increment`] for some
122
126
// valid frequencies are within the correct span for `stssi`
123
127
#[ test]
124
128
fn correct_subsecond_increment ( ) {
@@ -128,6 +132,17 @@ mod test {
128
132
}
129
133
}
130
134
135
+ #[ test]
136
+ fn from_nanos ( ) {
137
+ for i in [ 0 , 1 , 2 , 3 , NANOS_PER_SECOND - 1 ] {
138
+ let subseconds = Subseconds :: new_from_nanos ( i) . unwrap ( ) ;
139
+ assert ! ( subseconds. raw( ) < SUBSECONDS_PER_SECOND ) ;
140
+ }
141
+
142
+ assert ! ( Subseconds :: new_from_nanos( NANOS_PER_SECOND ) . is_none( ) ) ;
143
+ assert ! ( Subseconds :: new_from_nanos( u32 :: MAX ) . is_none( ) ) ;
144
+ }
145
+
131
146
#[ test]
132
147
fn subsecond_math ( ) {
133
148
let one = Subseconds :: new ( 1 ) . unwrap ( ) ;
0 commit comments