Skip to content

Commit 5b62992

Browse files
committed
Document relaxed requirements for Num::from_str_radix
1 parent 37e7658 commit 5b62992

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

RELEASES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
- [Casts from large `f64` values to `f32` now saturate to infinity][186]. They
88
previously returned `None` because that was once thought to be undefined
99
behavior, but [rust#15536] resolved that such casts are fine.
10+
- [`Num::from_str_radix` documents requirements for radix support][192], which
11+
are now more relaxed than previously implied. It is suggested to accept at
12+
least `2..=36` without panicking, but `Err` may be returned otherwise.
1013

1114
**Contributors**: @cuviper, @Enet4, @KaczuH, @martin-t, @newpavlov
1215

1316
[180]: https://github.com/rust-num/num-traits/pull/180
1417
[185]: https://github.com/rust-num/num-traits/pull/185
1518
[186]: https://github.com/rust-num/num-traits/pull/186
19+
[192]: https://github.com/rust-num/num-traits/issues/192
1620
[rust#15536]: https://github.com/rust-lang/rust/issues/15536
1721

1822
# Release 0.2.12 (2020-06-11)

src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub mod sign;
6767
pub trait Num: PartialEq + Zero + One + NumOps {
6868
type FromStrRadixErr;
6969

70-
/// Convert from a string and radix <= 36.
70+
/// Convert from a string and radix (typically `2..=36`).
7171
///
7272
/// # Examples
7373
///
@@ -80,6 +80,18 @@ pub trait Num: PartialEq + Zero + One + NumOps {
8080
/// let result = <i32 as Num>::from_str_radix("foo", 10);
8181
/// assert!(result.is_err());
8282
/// ```
83+
///
84+
/// # Supported radices
85+
///
86+
/// The exact range of supported radices is at the discretion of each type implementation. For
87+
/// primitive integers, this is implemented by the inherent `from_str_radix` methods in the
88+
/// standard library, which **panic** if the radix is not in the range from 2 to 36. The
89+
/// implementation in this crate for primitive floats is similar.
90+
///
91+
/// For third-party types, it is suggested that implementations should follow suit and at least
92+
/// accept `2..=36` without panicking, but an `Err` may be returned for any unsupported radix.
93+
/// It's possible that a type might not even support the common radix 10, nor any, if string
94+
/// parsing doesn't make sense for that type.
8395
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>;
8496
}
8597

0 commit comments

Comments
 (0)