Skip to content

Commit cabb679

Browse files
committed
Auto merge of #51474 - llogiq:from-docs, r=TimNN
add some docs to `From` conversions This adds a helpful document to the bool → int* conversions as well as to the lossless integer conversions. One of #51430 down, some more to go.
2 parents c5a129e + 553a44a commit cabb679

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/libcore/num/mod.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -4709,30 +4709,56 @@ pub use num::dec2flt::ParseFloatError;
47094709
// Conversions T -> T are covered by a blanket impl and therefore excluded
47104710
// Some conversions from and to usize/isize are not implemented due to portability concerns
47114711
macro_rules! impl_from {
4712-
($Small: ty, $Large: ty, #[$attr:meta]) => {
4712+
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
47134713
#[$attr]
4714+
#[doc = $doc]
47144715
impl From<$Small> for $Large {
47154716
#[inline]
47164717
fn from(small: $Small) -> $Large {
47174718
small as $Large
47184719
}
47194720
}
4721+
};
4722+
($Small: ty, $Large: ty, #[$attr:meta]) => {
4723+
impl_from!($Small,
4724+
$Large,
4725+
#[$attr],
4726+
concat!("Converts `",
4727+
stringify!($Small),
4728+
"` to `",
4729+
stringify!($Large),
4730+
"` losslessly."));
47204731
}
47214732
}
47224733

4734+
macro_rules! impl_from_bool {
4735+
($target: ty, #[$attr:meta]) => {
4736+
impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
4737+
stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
4738+
values.
4739+
4740+
# Examples
4741+
4742+
```
4743+
assert_eq!(", stringify!($target), "::from(true), 1);
4744+
assert_eq!(", stringify!($target), "::from(false), 0);
4745+
```"));
4746+
};
4747+
}
4748+
47234749
// Bool -> Any
4724-
impl_from! { bool, u8, #[stable(feature = "from_bool", since = "1.28.0")] }
4725-
impl_from! { bool, u16, #[stable(feature = "from_bool", since = "1.28.0")] }
4726-
impl_from! { bool, u32, #[stable(feature = "from_bool", since = "1.28.0")] }
4727-
impl_from! { bool, u64, #[stable(feature = "from_bool", since = "1.28.0")] }
4728-
impl_from! { bool, u128, #[stable(feature = "from_bool", since = "1.28.0")] }
4729-
impl_from! { bool, usize, #[stable(feature = "from_bool", since = "1.28.0")] }
4730-
impl_from! { bool, i8, #[stable(feature = "from_bool", since = "1.28.0")] }
4731-
impl_from! { bool, i16, #[stable(feature = "from_bool", since = "1.28.0")] }
4732-
impl_from! { bool, i32, #[stable(feature = "from_bool", since = "1.28.0")] }
4733-
impl_from! { bool, i64, #[stable(feature = "from_bool", since = "1.28.0")] }
4734-
impl_from! { bool, i128, #[stable(feature = "from_bool", since = "1.28.0")] }
4735-
impl_from! { bool, isize, #[stable(feature = "from_bool", since = "1.28.0")] }
4750+
impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
4751+
impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
4752+
impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
4753+
impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
4754+
impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
4755+
impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
4756+
impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
4757+
impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
4758+
impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
4759+
impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
4760+
impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
4761+
impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
47364762

47374763
// Unsigned -> Unsigned
47384764
impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }

0 commit comments

Comments
 (0)