Skip to content

Commit 50fde8c

Browse files
committed
std: ToPrimitive's default impls should use .to_*()
This allows the default methods to be properly range checked.
1 parent da145b2 commit 50fde8c

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/libstd/num/num.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -353,29 +353,25 @@ pub trait ToPrimitive {
353353
/// Converts the value of `self` to an `int`.
354354
#[inline]
355355
fn to_int(&self) -> Option<int> {
356-
// XXX: Check for range.
357-
self.to_i64().and_then(|x| Some(x as int))
356+
self.to_i64().and_then(|x| x.to_int())
358357
}
359358

360359
/// Converts the value of `self` to an `i8`.
361360
#[inline]
362361
fn to_i8(&self) -> Option<i8> {
363-
// XXX: Check for range.
364-
self.to_i64().and_then(|x| Some(x as i8))
362+
self.to_i64().and_then(|x| x.to_i8())
365363
}
366364

367365
/// Converts the value of `self` to an `i16`.
368366
#[inline]
369367
fn to_i16(&self) -> Option<i16> {
370-
// XXX: Check for range.
371-
self.to_i64().and_then(|x| Some(x as i16))
368+
self.to_i64().and_then(|x| x.to_i16())
372369
}
373370

374371
/// Converts the value of `self` to an `i32`.
375372
#[inline]
376373
fn to_i32(&self) -> Option<i32> {
377-
// XXX: Check for range.
378-
self.to_i64().and_then(|x| Some(x as i32))
374+
self.to_i64().and_then(|x| x.to_i32())
379375
}
380376

381377
/// Converts the value of `self` to an `i64`.
@@ -384,50 +380,43 @@ pub trait ToPrimitive {
384380
/// Converts the value of `self` to an `uint`.
385381
#[inline]
386382
fn to_uint(&self) -> Option<uint> {
387-
// XXX: Check for range.
388-
self.to_u64().and_then(|x| Some(x as uint))
383+
self.to_u64().and_then(|x| x.to_uint())
389384
}
390385

391386
/// Converts the value of `self` to an `u8`.
392387
#[inline]
393388
fn to_u8(&self) -> Option<u8> {
394-
// XXX: Check for range.
395-
self.to_u64().and_then(|x| Some(x as u8))
389+
self.to_u64().and_then(|x| x.to_u8())
396390
}
397391

398392
/// Converts the value of `self` to an `u16`.
399393
#[inline]
400394
fn to_u16(&self) -> Option<u16> {
401-
// XXX: Check for range.
402-
self.to_u64().and_then(|x| Some(x as u16))
395+
self.to_u64().and_then(|x| x.to_u16())
403396
}
404397

405398
/// Converts the value of `self` to an `u32`.
406399
#[inline]
407400
fn to_u32(&self) -> Option<u32> {
408-
// XXX: Check for range.
409-
self.to_u64().and_then(|x| Some(x as u32))
401+
self.to_u64().and_then(|x| x.to_u32())
410402
}
411403

412404
/// Converts the value of `self` to an `u64`.
413405
#[inline]
414406
fn to_u64(&self) -> Option<u64> {
415-
// XXX: Check for range.
416-
self.to_u64().and_then(|x| Some(x as u64))
407+
self.to_u64().and_then(|x| x.to_u64())
417408
}
418409

419410
/// Converts the value of `self` to an `f32`.
420411
#[inline]
421412
fn to_f32(&self) -> Option<f32> {
422-
// XXX: Check for range.
423-
self.to_float().and_then(|x| Some(x as f32))
413+
self.to_f64().and_then(|x| x.to_f32())
424414
}
425415

426416
/// Converts the value of `self` to an `f64`.
427417
#[inline]
428418
fn to_f64(&self) -> Option<f64> {
429-
// XXX: Check for range.
430-
self.to_i64().and_then(|x| Some(x as f64))
419+
self.to_i64().and_then(|x| x.to_f64())
431420
}
432421
}
433422

0 commit comments

Comments
 (0)