about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-06-10 18:00:12 +0000
committerbors <bors@rust-lang.org>2018-06-10 18:00:12 +0000
commitcabb679bf1bf37d34b0de3d60f655cf5e4185db6 (patch)
treef691948ccf4ec1a77da775dce1120e9361844773 /src/libcore
parentc5a129e809172462e3f29ab733074630e29b569b (diff)
parent553a44a5cc0aa766d60e878c87d3860f4a2cacdd (diff)
downloadrust-cabb679bf1bf37d34b0de3d60f655cf5e4185db6.tar.gz
rust-cabb679bf1bf37d34b0de3d60f655cf5e4185db6.zip
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.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/num/mod.rs52
1 files changed, 39 insertions, 13 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index c2da9006a8a..1168126c47c 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -4709,30 +4709,56 @@ pub use num::dec2flt::ParseFloatError;
 // Conversions T -> T are covered by a blanket impl and therefore excluded
 // Some conversions from and to usize/isize are not implemented due to portability concerns
 macro_rules! impl_from {
-    ($Small: ty, $Large: ty, #[$attr:meta]) => {
+    ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
         #[$attr]
+        #[doc = $doc]
         impl From<$Small> for $Large {
             #[inline]
             fn from(small: $Small) -> $Large {
                 small as $Large
             }
         }
+    };
+    ($Small: ty, $Large: ty, #[$attr:meta]) => {
+        impl_from!($Small,
+                   $Large,
+                   #[$attr],
+                   concat!("Converts `",
+                           stringify!($Small),
+                           "` to `",
+                           stringify!($Large),
+                           "` losslessly."));
     }
 }
 
+macro_rules! impl_from_bool {
+    ($target: ty, #[$attr:meta]) => {
+        impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
+            stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
+values.
+
+# Examples
+
+```
+assert_eq!(", stringify!($target), "::from(true), 1);
+assert_eq!(", stringify!($target), "::from(false), 0);
+```"));
+    };
+}
+
 // Bool -> Any
-impl_from! { bool, u8, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, u16, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, u32, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, u64, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, u128, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, usize, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, i8, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, i16, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, i32, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, i64, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, i128, #[stable(feature = "from_bool", since = "1.28.0")] }
-impl_from! { bool, isize, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
+impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
 
 // Unsigned -> Unsigned
 impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }