@@ -247,14 +247,7 @@ impl ScalarInt {
247247 }
248248
249249 #[ inline]
250- pub fn assert_bits ( self , target_size : Size ) -> u128 {
251- self . to_bits ( target_size) . unwrap_or_else ( |size| {
252- bug ! ( "expected int of size {}, but got size {}" , target_size. bytes( ) , size. bytes( ) )
253- } )
254- }
255-
256- #[ inline]
257- pub fn to_bits ( self , target_size : Size ) -> Result < u128 , Size > {
250+ pub fn try_to_bits ( self , target_size : Size ) -> Result < u128 , Size > {
258251 assert_ne ! ( target_size. bytes( ) , 0 , "you should never look at the bits of a ZST" ) ;
259252 if target_size. bytes ( ) == u64:: from ( self . size . get ( ) ) {
260253 self . check_data ( ) ;
@@ -264,48 +257,60 @@ impl ScalarInt {
264257 }
265258 }
266259
260+ #[ inline]
261+ pub fn assert_bits ( self , target_size : Size ) -> u128 {
262+ self . try_to_bits ( target_size) . unwrap_or_else ( |size| {
263+ bug ! ( "expected int of size {}, but got size {}" , target_size. bytes( ) , size. bytes( ) )
264+ } )
265+ }
266+
267267 /// Tries to convert the `ScalarInt` to an unsigned integer of the given size.
268268 /// Fails if the size of the `ScalarInt` is not equal to `size` and returns the
269269 /// `ScalarInt`s size in that case.
270270 #[ inline]
271271 pub fn try_to_uint ( self , size : Size ) -> Result < u128 , Size > {
272- self . to_bits ( size)
272+ self . try_to_bits ( size)
273+ }
274+
275+ #[ inline]
276+ pub fn assert_uint ( self , size : Size ) -> u128 {
277+ self . assert_bits ( size)
273278 }
274279
275280 // Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
276- // in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
281+ // in not equal to 1 byte and returns the `size` value of the `ScalarInt` in
277282 // that case.
278283 #[ inline]
279284 pub fn try_to_u8 ( self ) -> Result < u8 , Size > {
280285 self . try_to_uint ( Size :: from_bits ( 8 ) ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
281286 }
282287
283288 /// Tries to convert the `ScalarInt` to `u16`. Fails if the size of the `ScalarInt`
284- /// in not equal to `Size { raw: 2 }` and returns the `size` value of the `ScalarInt` in
289+ /// in not equal to 2 bytes and returns the `size` value of the `ScalarInt` in
285290 /// that case.
286291 #[ inline]
287292 pub fn try_to_u16 ( self ) -> Result < u16 , Size > {
288293 self . try_to_uint ( Size :: from_bits ( 16 ) ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
289294 }
290295
291296 /// Tries to convert the `ScalarInt` to `u32`. Fails if the `size` of the `ScalarInt`
292- /// in not equal to `Size { raw: 4 }` and returns the `size` value of the `ScalarInt` in
297+ /// in not equal to 4 bytes and returns the `size` value of the `ScalarInt` in
293298 /// that case.
294299 #[ inline]
295300 pub fn try_to_u32 ( self ) -> Result < u32 , Size > {
296301 self . try_to_uint ( Size :: from_bits ( 32 ) ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
297302 }
298303
299304 /// Tries to convert the `ScalarInt` to `u64`. Fails if the `size` of the `ScalarInt`
300- /// in not equal to `Size { raw: 8 }` and returns the `size` value of the `ScalarInt` in
305+ /// in not equal to 8 bytes and returns the `size` value of the `ScalarInt` in
301306 /// that case.
302307 #[ inline]
303308 pub fn try_to_u64 ( self ) -> Result < u64 , Size > {
304309 self . try_to_uint ( Size :: from_bits ( 64 ) ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
305310 }
306311
307312 /// Tries to convert the `ScalarInt` to `u128`. Fails if the `size` of the `ScalarInt`
308- /// in not equal to `Size { raw: 16 }` and returns the `size` value of the `ScalarInt` in
313+ /// in not equal to 16 bytes and returns the `size` value of the `ScalarInt` in
309314 /// that case.
310315 #[ inline]
311316 pub fn try_to_u128 ( self ) -> Result < u128 , Size > {
@@ -318,7 +323,7 @@ impl ScalarInt {
318323 }
319324
320325 // Tries to convert the `ScalarInt` to `bool`. Fails if the `size` of the `ScalarInt`
321- // in not equal to `Size { raw: 1 }` or if the value is not 0 or 1 and returns the `size`
326+ // in not equal to 1 byte or if the value is not 0 or 1 and returns the `size`
322327 // value of the `ScalarInt` in that case.
323328 #[ inline]
324329 pub fn try_to_bool ( self ) -> Result < bool , Size > {
@@ -334,40 +339,46 @@ impl ScalarInt {
334339 /// `ScalarInt`s size in that case.
335340 #[ inline]
336341 pub fn try_to_int ( self , size : Size ) -> Result < i128 , Size > {
337- let b = self . to_bits ( size) ?;
342+ let b = self . try_to_bits ( size) ?;
338343 Ok ( size. sign_extend ( b) as i128 )
339344 }
340345
346+ #[ inline]
347+ pub fn assert_int ( self , size : Size ) -> i128 {
348+ let b = self . assert_bits ( size) ;
349+ size. sign_extend ( b) as i128
350+ }
351+
341352 /// Tries to convert the `ScalarInt` to i8.
342- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 1 }`
353+ /// Fails if the size of the `ScalarInt` is not equal to 1 byte
343354 /// and returns the `ScalarInt`s size in that case.
344355 pub fn try_to_i8 ( self ) -> Result < i8 , Size > {
345356 self . try_to_int ( Size :: from_bits ( 8 ) ) . map ( |v| i8:: try_from ( v) . unwrap ( ) )
346357 }
347358
348359 /// Tries to convert the `ScalarInt` to i16.
349- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 2 }`
360+ /// Fails if the size of the `ScalarInt` is not equal to 2 bytes
350361 /// and returns the `ScalarInt`s size in that case.
351362 pub fn try_to_i16 ( self ) -> Result < i16 , Size > {
352363 self . try_to_int ( Size :: from_bits ( 16 ) ) . map ( |v| i16:: try_from ( v) . unwrap ( ) )
353364 }
354365
355366 /// Tries to convert the `ScalarInt` to i32.
356- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 4 }`
367+ /// Fails if the size of the `ScalarInt` is not equal to 4 bytes
357368 /// and returns the `ScalarInt`s size in that case.
358369 pub fn try_to_i32 ( self ) -> Result < i32 , Size > {
359370 self . try_to_int ( Size :: from_bits ( 32 ) ) . map ( |v| i32:: try_from ( v) . unwrap ( ) )
360371 }
361372
362373 /// Tries to convert the `ScalarInt` to i64.
363- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 8 }`
374+ /// Fails if the size of the `ScalarInt` is not equal to 8 bytes
364375 /// and returns the `ScalarInt`s size in that case.
365376 pub fn try_to_i64 ( self ) -> Result < i64 , Size > {
366377 self . try_to_int ( Size :: from_bits ( 64 ) ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
367378 }
368379
369380 /// Tries to convert the `ScalarInt` to i128.
370- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 16 }`
381+ /// Fails if the size of the `ScalarInt` is not equal to 16 bytes
371382 /// and returns the `ScalarInt`s size in that case.
372383 pub fn try_to_i128 ( self ) -> Result < i128 , Size > {
373384 self . try_to_int ( Size :: from_bits ( 128 ) )
@@ -381,7 +392,7 @@ impl ScalarInt {
381392 #[ inline]
382393 pub fn try_to_float < F : Float > ( self ) -> Result < F , Size > {
383394 // Going through `to_uint` to check size and truncation.
384- Ok ( F :: from_bits ( self . to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
395+ Ok ( F :: from_bits ( self . try_to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
385396 }
386397
387398 #[ inline]
@@ -430,7 +441,7 @@ macro_rules! try_from {
430441 fn try_from( int: ScalarInt ) -> Result <Self , Size > {
431442 // The `unwrap` cannot fail because to_bits (if it succeeds)
432443 // is guaranteed to return a value that fits into the size.
433- int. to_bits ( Size :: from_bytes( std:: mem:: size_of:: <$ty>( ) ) )
444+ int. try_to_bits ( Size :: from_bytes( std:: mem:: size_of:: <$ty>( ) ) )
434445 . map( |u| u. try_into( ) . unwrap( ) )
435446 }
436447 }
@@ -465,7 +476,7 @@ impl TryFrom<ScalarInt> for char {
465476
466477 #[ inline]
467478 fn try_from ( int : ScalarInt ) -> Result < Self , Self :: Error > {
468- let Ok ( bits) = int. to_bits ( Size :: from_bytes ( std:: mem:: size_of :: < char > ( ) ) ) else {
479+ let Ok ( bits) = int. try_to_bits ( Size :: from_bytes ( std:: mem:: size_of :: < char > ( ) ) ) else {
469480 return Err ( CharTryFromScalarInt ) ;
470481 } ;
471482 match char:: from_u32 ( bits. try_into ( ) . unwrap ( ) ) {
@@ -487,7 +498,7 @@ impl TryFrom<ScalarInt> for Half {
487498 type Error = Size ;
488499 #[ inline]
489500 fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
490- int. to_bits ( Size :: from_bytes ( 2 ) ) . map ( Self :: from_bits)
501+ int. try_to_bits ( Size :: from_bytes ( 2 ) ) . map ( Self :: from_bits)
491502 }
492503}
493504
@@ -503,7 +514,7 @@ impl TryFrom<ScalarInt> for Single {
503514 type Error = Size ;
504515 #[ inline]
505516 fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
506- int. to_bits ( Size :: from_bytes ( 4 ) ) . map ( Self :: from_bits)
517+ int. try_to_bits ( Size :: from_bytes ( 4 ) ) . map ( Self :: from_bits)
507518 }
508519}
509520
@@ -519,7 +530,7 @@ impl TryFrom<ScalarInt> for Double {
519530 type Error = Size ;
520531 #[ inline]
521532 fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
522- int. to_bits ( Size :: from_bytes ( 8 ) ) . map ( Self :: from_bits)
533+ int. try_to_bits ( Size :: from_bytes ( 8 ) ) . map ( Self :: from_bits)
523534 }
524535}
525536
@@ -535,7 +546,7 @@ impl TryFrom<ScalarInt> for Quad {
535546 type Error = Size ;
536547 #[ inline]
537548 fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
538- int. to_bits ( Size :: from_bytes ( 16 ) ) . map ( Self :: from_bits)
549+ int. try_to_bits ( Size :: from_bytes ( 16 ) ) . map ( Self :: from_bits)
539550 }
540551}
541552
0 commit comments