Document panicking cases for integer division

The panic on division by zero is expected, but the panic on overflow
is somewhat surprising (since most arithmetic operations panic on
overflow only when `debug_assertions` is enabled). As a result, it's
helpful to document this behavior.
This commit is contained in:
Jim Turner 2021-03-01 16:09:41 -05:00
parent 5233edcf1c
commit 9278b4c672

View file

@ -456,9 +456,13 @@ pub trait Div<Rhs = Self> {
}
macro_rules! div_impl_integer {
($($t:ty)*) => ($(
($(($($t:ty)*) => $panic:expr),*) => ($($(
/// This operation rounds towards zero, truncating any
/// fractional part of the exact result.
///
/// # Panics
///
#[doc = $panic]
#[stable(feature = "rust1", since = "1.0.0")]
impl Div for $t {
type Output = $t;
@ -468,10 +472,13 @@ macro_rules! div_impl_integer {
}
forward_ref_binop! { impl Div, div for $t, $t }
)*)
)*)*)
}
div_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
div_impl_integer! {
(usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
(isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or the division results in overflow."
}
macro_rules! div_impl_float {
($($t:ty)*) => ($(