Skip to content

Commit da301ef

Browse files
committed
Add inherent try_from and try_into methods to integer types
This allows these methods to be used without importing the corresponding traits. This causes new `unused-imports` warnings for existing imports used only by method calls. This is a non-trivial amount of churn, maybe worth it? The warnings can be fixed by removing imports (like this PR does in rustc and tests), or by adding `#[allow(unused_import)]` to them until the minimum supported Rust version is incremented to one that has this PR. The new methods are insta-stable in order to avoid causing `unstable-name-collisions` warnings. These would be harder to deal with since they happen at calls sites rather than trait imports.
1 parent 2539b5f commit da301ef

File tree

27 files changed

+171
-152
lines changed

27 files changed

+171
-152
lines changed

src/libcore/convert.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,6 @@ pub trait TryInto<T>: Sized {
451451
/// As described, [`i32`] implements `TryFrom<`[`i64`]`>`:
452452
///
453453
/// ```
454-
/// use std::convert::TryFrom;
455-
///
456454
/// let big_number = 1_000_000_000_000i64;
457455
/// // Silently truncates `big_number`, requires detecting
458456
/// // and handling the truncation after the fact.

src/libcore/iter/range.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::convert::TryFrom;
21
use crate::mem;
32
use crate::ops::{self, Add, Sub, Try};
43
use crate::usize;

src/libcore/num/dec2flt/rawfp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! take the universally-correct slow path (Algorithm M) for very small and very large numbers.
1919
//! That algorithm needs only next_float() which does handle subnormals and zeros.
2020
use crate::cmp::Ordering::{Equal, Greater, Less};
21-
use crate::convert::{TryFrom, TryInto};
21+
use crate::convert::TryFrom;
2222
use crate::fmt::{Debug, LowerExp};
2323
use crate::num::dec2flt::num::{self, Big};
2424
use crate::num::dec2flt::table;

src/libcore/num/mod.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#![stable(feature = "rust1", since = "1.0.0")]
66

7-
use crate::convert::TryFrom;
7+
use crate::convert::{TryFrom, TryInto};
88
use crate::fmt;
99
use crate::intrinsics;
1010
use crate::mem;
@@ -278,6 +278,28 @@ $EndFeature, "
278278
}
279279
}
280280

281+
doc_comment! {
282+
"FIXME docs",
283+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
284+
#[inline]
285+
pub fn try_from<T>(value: T) -> Result<Self, <Self as TryFrom<T>>::Error>
286+
where Self: TryFrom<T>
287+
{
288+
TryFrom::try_from(value)
289+
}
290+
}
291+
292+
doc_comment! {
293+
"FIXME docs",
294+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
295+
#[inline]
296+
pub fn try_into<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
297+
where Self: TryInto<T>
298+
{
299+
TryInto::try_into(self)
300+
}
301+
}
302+
281303
doc_comment! {
282304
concat!("Converts a string slice in a given base to an integer.
283305
@@ -2340,6 +2362,28 @@ stringify!($MaxV), ");", $EndFeature, "
23402362
pub const fn max_value() -> Self { !0 }
23412363
}
23422364

2365+
doc_comment! {
2366+
"FIXME docs",
2367+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
2368+
#[inline]
2369+
pub fn try_from<T>(value: T) -> Result<Self, <Self as TryFrom<T>>::Error>
2370+
where Self: TryFrom<T>
2371+
{
2372+
TryFrom::try_from(value)
2373+
}
2374+
}
2375+
2376+
doc_comment! {
2377+
"FIXME docs",
2378+
#[stable(feature = "int_inherent_try_from_try_into", since = "1.41.0")]
2379+
#[inline]
2380+
pub fn try_into<T>(self) -> Result<T, <Self as TryInto<T>>::Error>
2381+
where Self: TryInto<T>
2382+
{
2383+
TryInto::try_into(self)
2384+
}
2385+
}
2386+
23432387
doc_comment! {
23442388
concat!("Converts a string slice in a given base to an integer.
23452389

src/libcore/tests/iter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::cell::Cell;
2-
use core::convert::TryFrom;
32
use core::iter::*;
43
use core::{i8, i16, isize};
54
use core::usize;

src/libcore/tests/num/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::convert::{TryFrom, TryInto};
1+
use core::convert::TryFrom;
22
use core::cmp::PartialEq;
33
use core::fmt::Debug;
44
use core::marker::Copy;

src/librustc/mir/interpret/pointer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::ty::layout::{self, HasDataLayout, Size};
55

66
use rustc_macros::HashStable;
77

8-
use std::convert::TryFrom;
98
use std::fmt::{self, Display};
109

1110
/// Used by `check_in_alloc` to indicate context of check

src/librustc_apfloat/ieee.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
22
use crate::{Float, FloatConvert, ParseError, Round, Status, StatusAnd};
33

44
use core::cmp::{self, Ordering};
5-
use core::convert::TryFrom;
65
use core::fmt::{self, Write};
76
use core::marker::PhantomData;
87
use core::mem;

src/librustc_metadata/rmeta/table.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::rmeta::*;
22

33
use rustc_index::vec::Idx;
44
use rustc_serialize::{Encodable, opaque::Encoder};
5-
use std::convert::TryInto;
65
use std::marker::PhantomData;
76
use std::num::NonZeroUsize;
87
use log::debug;

src/librustc_mir/build/matches/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ mod simplify;
2626
mod test;
2727
mod util;
2828

29-
use std::convert::TryFrom;
30-
3129
impl<'a, 'tcx> Builder<'a, 'tcx> {
3230
/// Generates MIR for a `match` expression.
3331
///

0 commit comments

Comments
 (0)