Skip to content

Commit 1253227

Browse files
icefoxenSimonSapin
authored andcommitted
Add basic docs to integer TryFrom impl macros.
They're not as good as `From` 'cause they don't stringify the types and generate examples and so on, but it's a start.
1 parent d2b1212 commit 1253227

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcore/num/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,6 +4544,9 @@ macro_rules! try_from_unbounded {
45444544
impl TryFrom<$source> for $target {
45454545
type Error = TryFromIntError;
45464546

4547+
/// Try to create the target type from the source type.
4548+
/// This particular variant will never fail, but is included
4549+
/// for completeness's sake.
45474550
#[inline]
45484551
fn try_from(value: $source) -> Result<Self, Self::Error> {
45494552
Ok(value as $target)
@@ -4559,6 +4562,10 @@ macro_rules! try_from_lower_bounded {
45594562
impl TryFrom<$source> for $target {
45604563
type Error = TryFromIntError;
45614564

4565+
/// Try to create a target number type from a
4566+
/// source type that has `source::MIN > dest::MIN`.
4567+
/// Will return an error if `source` is less than
4568+
/// `dest::MIN`.
45624569
#[inline]
45634570
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
45644571
if u >= 0 {
@@ -4578,6 +4585,10 @@ macro_rules! try_from_upper_bounded {
45784585
impl TryFrom<$source> for $target {
45794586
type Error = TryFromIntError;
45804587

4588+
/// Try to create a target number type from a
4589+
/// source type that has `source::MAX > dest::MAX`.
4590+
/// Will return an error if `source` is greater than
4591+
/// `dest::MAX`.
45814592
#[inline]
45824593
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
45834594
if u > (<$target>::max_value() as $source) {
@@ -4597,6 +4608,11 @@ macro_rules! try_from_both_bounded {
45974608
impl TryFrom<$source> for $target {
45984609
type Error = TryFromIntError;
45994610

4611+
/// Try to "narrow" a number from the source type
4612+
/// to the target type. Will return an error if
4613+
/// the source value is either larger than the
4614+
/// `MAX` value for the target type or smaller
4615+
/// than the `MIN` value for it.
46004616
#[inline]
46014617
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
46024618
let min = <$target>::min_value() as $source;

0 commit comments

Comments
 (0)