@@ -27,7 +27,11 @@ use {crate::BoxedUint, alloc::borrow::ToOwned};
2727/// thus provides an abstraction for writing shared implementations.
2828#[ repr( transparent) ]
2929#[ derive( PartialEq , Eq ) ]
30- pub struct UintRef ( pub ( crate ) [ Limb ] ) ;
30+ pub struct UintRef {
31+ /// Inner limb array. Stored from least significant to most significant.
32+ // TODO(tarcieri): maintain an invariant of at least one limb?
33+ pub ( crate ) limbs : [ Limb ] ,
34+ }
3135
3236impl UintRef {
3337 /// Create a [`UintRef`] reference type from a [`Limb`] slice.
@@ -70,55 +74,55 @@ impl UintRef {
7074 #[ inline]
7175 #[ must_use]
7276 pub const fn as_limbs ( & self ) -> & [ Limb ] {
73- & self . 0
77+ & self . limbs
7478 }
7579
7680 /// Mutably borrow the inner `&mut [Limb]` slice.
7781 #[ inline]
7882 pub const fn as_mut_limbs ( & mut self ) -> & mut [ Limb ] {
79- & mut self . 0
83+ & mut self . limbs
8084 }
8185
8286 /// Borrow the inner limbs as a slice of [`Word`]s.
8387 #[ inline]
8488 #[ must_use]
8589 pub const fn as_words ( & self ) -> & [ Word ] {
86- Limb :: slice_as_words ( & self . 0 )
90+ Limb :: slice_as_words ( & self . limbs )
8791 }
8892
8993 /// Borrow the inner limbs as a mutable slice of [`Word`]s.
9094 #[ inline]
9195 pub const fn as_mut_words ( & mut self ) -> & mut [ Word ] {
92- Limb :: slice_as_mut_words ( & mut self . 0 )
96+ Limb :: slice_as_mut_words ( & mut self . limbs )
9397 }
9498
9599 /// Get an iterator over the inner limbs.
96100 #[ inline]
97101 #[ must_use]
98102 pub fn iter ( & self ) -> impl DoubleEndedIterator < Item = & Limb > {
99- self . 0 . iter ( )
103+ self . limbs . iter ( )
100104 }
101105
102106 /// Get a mutable iterator over the inner limbs.
103107 #[ inline]
104108 #[ allow( dead_code) ] // TODO(tarcieri): use this
105109 pub fn iter_mut ( & mut self ) -> impl DoubleEndedIterator < Item = & mut Limb > {
106- self . 0 . iter_mut ( )
110+ self . limbs . iter_mut ( )
107111 }
108112
109113 /// Access the number of limbs.
110114 #[ inline]
111115 #[ must_use]
112116 pub const fn nlimbs ( & self ) -> usize {
113- self . 0 . len ( )
117+ self . limbs . len ( )
114118 }
115119
116120 /// Conditionally assign all of the limbs to zero.
117121 #[ inline( always) ]
118122 pub const fn conditional_set_zero ( & mut self , choice : Choice ) {
119123 let mut i = 0 ;
120124 while i < self . nlimbs ( ) {
121- self . 0 [ i] = Limb :: select ( self . 0 [ i] , Limb :: ZERO , choice) ;
125+ self . limbs [ i] = Limb :: select ( self . limbs [ i] , Limb :: ZERO , choice) ;
122126 i += 1 ;
123127 }
124128 }
@@ -128,7 +132,7 @@ impl UintRef {
128132 pub const fn conditional_set_max ( & mut self , choice : Choice ) {
129133 let mut i = 0 ;
130134 while i < self . nlimbs ( ) {
131- self . 0 [ i] = Limb :: select ( self . 0 [ i] , Limb :: MAX , choice) ;
135+ self . limbs [ i] = Limb :: select ( self . limbs [ i] , Limb :: MAX , choice) ;
132136 i += 1 ;
133137 }
134138 }
@@ -144,7 +148,7 @@ impl UintRef {
144148 } ;
145149 let mut i = 0 ;
146150 while i < len {
147- out. limbs [ i] = self . 0 [ i] ;
151+ out. limbs [ i] = self . limbs [ i] ;
148152 i += 1 ;
149153 }
150154 out
@@ -164,11 +168,11 @@ impl UintRef {
164168 #[ inline]
165169 #[ must_use]
166170 pub const fn to_mut_nz_ref ( & mut self ) -> CtOption < & mut NonZero < Self > > {
167- assert ! ( !self . 0 . is_empty( ) , "cannot be used with empty slices" ) ;
171+ assert ! ( !self . limbs . is_empty( ) , "cannot be used with empty slices" ) ;
168172 let is_nz = self . is_nonzero ( ) ;
169173
170174 // If `self` is zero, set the first limb to `1` to make it non-zero.
171- self . 0 [ 0 ] = Limb :: select ( Limb :: ONE , self . 0 [ 0 ] , is_nz) ;
175+ self . limbs [ 0 ] = Limb :: select ( Limb :: ONE , self . limbs [ 0 ] , is_nz) ;
172176
173177 CtOption :: new ( self . as_mut_nz_unchecked ( ) , is_nz)
174178 }
@@ -234,11 +238,11 @@ impl UintRef {
234238 #[ inline]
235239 #[ must_use]
236240 pub const fn to_mut_odd_ref ( & mut self ) -> CtOption < & mut Odd < Self > > {
237- assert ! ( !self . 0 . is_empty( ) , "cannot be used with empty slices" ) ;
241+ assert ! ( !self . limbs . is_empty( ) , "cannot be used with empty slices" ) ;
238242 let is_odd = self . is_odd ( ) ;
239243
240244 // If `self` is even, set the first limb to `1` to make it odd.
241- self . 0 [ 0 ] = Limb :: select ( Limb :: ONE , self . 0 [ 0 ] , is_odd) ;
245+ self . limbs [ 0 ] = Limb :: select ( Limb :: ONE , self . limbs [ 0 ] , is_odd) ;
242246
243247 CtOption :: new ( self . as_mut_odd_unchecked ( ) , is_odd)
244248 }
@@ -296,18 +300,18 @@ impl UintRef {
296300 #[ cfg( target_pointer_width = "32" ) ]
297301 {
298302 debug_assert ! ( self . nlimbs( ) >= 1 ) ;
299- let mut ret = self . 0 [ 0 ] . 0 as u64 ;
303+ let mut ret = self . limbs [ 0 ] . 0 as u64 ;
300304
301305 if self . nlimbs ( ) >= 2 {
302- ret |= ( self . 0 [ 1 ] . 0 as u64 ) << 32 ;
306+ ret |= ( self . limbs [ 1 ] . 0 as u64 ) << 32 ;
303307 }
304308
305309 ret
306310 }
307311
308312 #[ cfg( target_pointer_width = "64" ) ]
309313 {
310- self . 0 [ 0 ] . 0
314+ self . limbs [ 0 ] . 0
311315 }
312316 }
313317}
@@ -331,14 +335,14 @@ impl Index<usize> for UintRef {
331335
332336 #[ inline]
333337 fn index ( & self , index : usize ) -> & Limb {
334- self . 0 . index ( index)
338+ self . limbs . index ( index)
335339 }
336340}
337341
338342impl IndexMut < usize > for UintRef {
339343 #[ inline]
340344 fn index_mut ( & mut self , index : usize ) -> & mut Limb {
341- self . 0 . index_mut ( index)
345+ self . limbs . index_mut ( index)
342346 }
343347}
344348
0 commit comments