@@ -351,65 +351,69 @@ pub trait Float: Real
351
351
/// A generic trait for converting a value to a number.
352
352
pub trait ToPrimitive {
353
353
/// Converts the value of `self` to an `int`.
354
- fn to_int ( & self ) -> Option < int > ;
354
+ #[ inline]
355
+ fn to_int ( & self ) -> Option < int > {
356
+ // XXX: Check for range.
357
+ self . to_i64 ( ) . and_then ( |x| Some ( x as int ) )
358
+ }
355
359
356
360
/// Converts the value of `self` to an `i8`.
357
361
#[ inline]
358
362
fn to_i8 ( & self ) -> Option < i8 > {
359
363
// XXX: Check for range.
360
- self . to_int ( ) . and_then ( |x| Some ( x as i8 ) )
364
+ self . to_i64 ( ) . and_then ( |x| Some ( x as i8 ) )
361
365
}
362
366
363
367
/// Converts the value of `self` to an `i16`.
364
368
#[ inline]
365
369
fn to_i16 ( & self ) -> Option < i16 > {
366
370
// XXX: Check for range.
367
- self . to_int ( ) . and_then ( |x| Some ( x as i16 ) )
371
+ self . to_i64 ( ) . and_then ( |x| Some ( x as i16 ) )
368
372
}
369
373
370
374
/// Converts the value of `self` to an `i32`.
371
375
#[ inline]
372
376
fn to_i32 ( & self ) -> Option < i32 > {
373
377
// XXX: Check for range.
374
- self . to_int ( ) . and_then ( |x| Some ( x as i32 ) )
378
+ self . to_i64 ( ) . and_then ( |x| Some ( x as i32 ) )
375
379
}
376
380
377
381
/// Converts the value of `self` to an `i64`.
382
+ fn to_i64 ( & self ) -> Option < i64 > ;
383
+
384
+ /// Converts the value of `self` to an `uint`.
378
385
#[ inline]
379
- fn to_i64 ( & self ) -> Option < i64 > {
386
+ fn to_uint ( & self ) -> Option < uint > {
380
387
// XXX: Check for range.
381
- self . to_int ( ) . and_then ( |x| Some ( x as i64 ) )
388
+ self . to_u64 ( ) . and_then ( |x| Some ( x as uint ) )
382
389
}
383
390
384
- /// Converts the value of `self` to an `uint`.
385
- fn to_uint ( & self ) -> Option < uint > ;
386
-
387
391
/// Converts the value of `self` to an `u8`.
388
392
#[ inline]
389
393
fn to_u8 ( & self ) -> Option < u8 > {
390
394
// XXX: Check for range.
391
- self . to_uint ( ) . and_then ( |x| Some ( x as u8 ) )
395
+ self . to_u64 ( ) . and_then ( |x| Some ( x as u8 ) )
392
396
}
393
397
394
398
/// Converts the value of `self` to an `u16`.
395
399
#[ inline]
396
400
fn to_u16 ( & self ) -> Option < u16 > {
397
401
// XXX: Check for range.
398
- self . to_uint ( ) . and_then ( |x| Some ( x as u16 ) )
402
+ self . to_u64 ( ) . and_then ( |x| Some ( x as u16 ) )
399
403
}
400
404
401
405
/// Converts the value of `self` to an `u32`.
402
406
#[ inline]
403
407
fn to_u32 ( & self ) -> Option < u32 > {
404
408
// XXX: Check for range.
405
- self . to_uint ( ) . and_then ( |x| Some ( x as u32 ) )
409
+ self . to_u64 ( ) . and_then ( |x| Some ( x as u32 ) )
406
410
}
407
411
408
412
/// Converts the value of `self` to an `u64`.
409
413
#[ inline]
410
414
fn to_u64 ( & self ) -> Option < u64 > {
411
415
// XXX: Check for range.
412
- self . to_uint ( ) . and_then ( |x| Some ( x as u64 ) )
416
+ self . to_u64 ( ) . and_then ( |x| Some ( x as u64 ) )
413
417
}
414
418
415
419
/// Converts the value of `self` to an `f32`.
@@ -423,7 +427,7 @@ pub trait ToPrimitive {
423
427
#[ inline]
424
428
fn to_f64 ( & self ) -> Option < f64 > {
425
429
// XXX: Check for range.
426
- self . to_float ( ) . and_then ( |x| Some ( x as f64 ) )
430
+ self . to_i64 ( ) . and_then ( |x| Some ( x as f64 ) )
427
431
}
428
432
}
429
433
@@ -467,80 +471,80 @@ impl_to_primitive!(float)
467
471
pub trait FromPrimitive {
468
472
/// Convert an `int` to return an optional value of this type. If the
469
473
/// value cannot be represented by this value, the `None` is returned.
470
- fn from_int ( n : int ) -> Option < Self > ;
474
+ #[ inline]
475
+ fn from_int ( n : int ) -> Option < Self > {
476
+ FromPrimitive :: from_i64 ( n as i64 )
477
+ }
471
478
472
479
/// Convert an `i8` to return an optional value of this type. If the
473
480
/// type cannot be represented by this value, the `None` is returned.
474
481
#[ inline]
475
482
fn from_i8 ( n : i8 ) -> Option < Self > {
476
- FromPrimitive :: from_int ( n as int )
483
+ FromPrimitive :: from_i64 ( n as i64 )
477
484
}
478
485
479
486
/// Convert an `i16` to return an optional value of this type. If the
480
487
/// type cannot be represented by this value, the `None` is returned.
481
488
#[ inline]
482
489
fn from_i16 ( n : i16 ) -> Option < Self > {
483
- FromPrimitive :: from_int ( n as int )
490
+ FromPrimitive :: from_i64 ( n as i64 )
484
491
}
485
492
486
493
/// Convert an `i32` to return an optional value of this type. If the
487
494
/// type cannot be represented by this value, the `None` is returned.
488
495
#[ inline]
489
496
fn from_i32 ( n : i32 ) -> Option < Self > {
490
- FromPrimitive :: from_int ( n as int )
497
+ FromPrimitive :: from_i64 ( n as i64 )
491
498
}
492
499
493
500
/// Convert an `i64` to return an optional value of this type. If the
494
501
/// type cannot be represented by this value, the `None` is returned.
495
- #[ inline]
496
- fn from_i64 ( n : i64 ) -> Option < Self > {
497
- FromPrimitive :: from_int ( n as int )
498
- }
502
+ fn from_i64 ( n : i64 ) -> Option < Self > ;
499
503
500
504
/// Convert an `uint` to return an optional value of this type. If the
501
505
/// type cannot be represented by this value, the `None` is returned.
502
- fn from_uint ( n : uint ) -> Option < Self > ;
506
+ #[ inline]
507
+ fn from_uint ( n : uint ) -> Option < Self > {
508
+ FromPrimitive :: from_u64 ( n as u64 )
509
+ }
503
510
504
511
/// Convert an `u8` to return an optional value of this type. If the
505
512
/// type cannot be represented by this value, the `None` is returned.
506
513
#[ inline]
507
514
fn from_u8 ( n : u8 ) -> Option < Self > {
508
- FromPrimitive :: from_uint ( n as uint )
515
+ FromPrimitive :: from_u64 ( n as u64 )
509
516
}
510
517
511
518
/// Convert an `u16` to return an optional value of this type. If the
512
519
/// type cannot be represented by this value, the `None` is returned.
513
520
#[ inline]
514
521
fn from_u16 ( n : u16 ) -> Option < Self > {
515
- FromPrimitive :: from_uint ( n as uint )
522
+ FromPrimitive :: from_u64 ( n as u64 )
516
523
}
517
524
518
525
/// Convert an `u32` to return an optional value of this type. If the
519
526
/// type cannot be represented by this value, the `None` is returned.
520
527
#[ inline]
521
528
fn from_u32 ( n : u32 ) -> Option < Self > {
522
- FromPrimitive :: from_uint ( n as uint )
529
+ FromPrimitive :: from_u64 ( n as u64 )
523
530
}
524
531
525
532
/// Convert an `u64` to return an optional value of this type. If the
526
533
/// type cannot be represented by this value, the `None` is returned.
527
- #[ inline]
528
- fn from_u64 ( n : u64 ) -> Option < Self > {
529
- FromPrimitive :: from_uint ( n as uint )
530
- }
534
+ fn from_u64 ( n : u64 ) -> Option < Self > ;
531
535
532
536
/// Convert a `f32` to return an optional value of this type. If the
533
537
/// type cannot be represented by this value, the `None` is returned.
534
538
#[ inline]
535
539
fn from_f32 ( n : f32 ) -> Option < Self > {
536
- FromPrimitive :: from_float ( n as float )
540
+ FromPrimitive :: from_f64 ( n as f64 )
537
541
}
538
542
539
543
/// Convert a `f64` to return an optional value of this type. If the
540
544
/// type cannot be represented by this value, the `None` is returned.
541
545
#[ inline]
542
546
fn from_f64 ( n : f64 ) -> Option < Self > {
543
- FromPrimitive :: from_float ( n as float )
547
+ FromPrimitive :: from_i64 ( n as i64 )
544
548
}
545
549
}
546
550
0 commit comments