Skip to content

Add doc to ToPrimitive about conversion semantics #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,17 @@ prim_int_impl!(i32, i32, u32);
prim_int_impl!(i64, i64, u64);
prim_int_impl!(isize, isize, usize);

/// A generic trait for converting a value to a number.
/// A generic trait for converting a value to a primitive number type.
///
/// Return `Some` with the value if the new type can represent the value,
/// otherwise return `None`.
///
/// Conversion details:
///
/// - Integers are converted to integers with the `as` operator if the
/// result can be represented in the new type without wrapping.
/// - Floats to and from integer conversions simply use `as` with
/// no error cases. (May change in the future.)
pub trait ToPrimitive {
/// Converts the value of `self` to an `isize`.
#[inline]
Expand Down Expand Up @@ -1393,6 +1403,9 @@ pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
}

/// An interface for casting between machine scalars.
///
/// Return `Some` with the value if the conversion succeeded, otherwise `None`.
/// See `ToPrimitive` for more information.
pub trait NumCast: Sized + ToPrimitive {
/// Creates a number from another value that can be converted into
/// a primitive via the `ToPrimitive` trait.
Expand Down