Skip to content

Commit 3973bab

Browse files
bors[bot]Enet4
andauthored
Merge #176
176: Document cast traits about possible precision loss and truncation r=cuviper a=Enet4 Resolves #175. Document the traits `ToPrimitive`, `FromPrimitive` and `NumCast` that value can be represented by the target type when they are within the range admitted by the target type, and so truncation or precision loss may happen. One question worth reflecting on is whether this documentation be normalized somewhere at module level, and whether it should be replicated to all methods or simply stay at each trait. I would also be open to adding examples with doctests if deemed useful. Co-authored-by: Eduardo Pinho <[email protected]>
2 parents f6852e0 + 4090814 commit 3973bab

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/cast.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ use core::{u16, u32, u64, u8, usize};
99
use float::FloatCore;
1010

1111
/// A generic trait for converting a value to a number.
12+
///
13+
/// A value can be represented by the target type when it lies within
14+
/// the range of scalars supported by the target type.
15+
/// For example, a negative integer cannot be represented by an unsigned
16+
/// integer type, and an `f64` with a very high magnitude might not be
17+
/// convertible to an `f32`.
18+
/// On the other hand, conversions with possible precision loss or truncation
19+
/// (e.g. an `f32` with a decimal part to an integer type) are admitted.
1220
pub trait ToPrimitive {
1321
/// Converts the value of `self` to an `isize`. If the value cannot be
1422
/// represented by an `isize`, then `None` is returned.
@@ -392,6 +400,14 @@ impl_to_primitive_float!(f32);
392400
impl_to_primitive_float!(f64);
393401

394402
/// A generic trait for converting a number to a value.
403+
///
404+
/// A value can be represented by the target type when it lies within
405+
/// the range of scalars supported by the target type.
406+
/// For example, a negative integer cannot be represented by an unsigned
407+
/// integer type, and an `f64` with a very high magnitude might not be
408+
/// convertible to an `f32`.
409+
/// On the other hand, conversions with possible precision loss or truncation
410+
/// (e.g. an `f32` with a decimal part to an integer type) are admitted.
395411
pub trait FromPrimitive: Sized {
396412
/// Converts an `isize` to return an optional value of this type. If the
397413
/// value cannot be represented by this type, then `None` is returned.
@@ -672,6 +688,14 @@ pub trait NumCast: Sized + ToPrimitive {
672688
/// Creates a number from another value that can be converted into
673689
/// a primitive via the `ToPrimitive` trait. If the source value cannot be
674690
/// represented by the target type, then `None` is returned.
691+
///
692+
/// A value can be represented by the target type when it lies within
693+
/// the range of scalars supported by the target type.
694+
/// For example, a negative integer cannot be represented by an unsigned
695+
/// integer type, and an `f64` with a very high magnitude might not be
696+
/// convertible to an `f32`.
697+
/// On the other hand, conversions with possible precision loss or truncation
698+
/// (e.g. an `f32` with a decimal part to an integer type) are admitted.
675699
fn from<T: ToPrimitive>(n: T) -> Option<Self>;
676700
}
677701

0 commit comments

Comments
 (0)