Skip to content

Commit f4b946a

Browse files
authored
Rollup merge of #145279 - clarfonthey:const-convert-initial, r=tgross35
Constify conversion traits (part 1) This is the first part of #144289 being split into smaller pieces. It adds/moves constness of several traits under the `const_convert` feature: * `From` * `Into` * `TryFrom` * `TryInto` * `FromStr` * `AsRef` * `AsMut` * `Borrow` * `BorrowMut` * `Deref` * `DerefMut` There are a few methods that are intrinsically tied to these traits which I've included in the feature. Particularly, those which are wrappers over `AsRef`: * `ByteStr::new` (unstable under `bstr` feature) * `OsStr::new` * `Path::new` Those which directly use `Into`: * `Result::into_ok` * `Result::into_err` And those which use `Deref` and `DerefMut`: * `Pin::as_ref` * `Pin::as_mut` * `Pin::as_deref_mut` * `Option::as_deref` * `Option::as_deref_mut` * `Result::as_deref` * `Result::as_deref_mut` (note: the `Option` and `Result` methods were suggested by ``@npmccallum`` initially as #146101) The parts which are missing from this PR are: * Anything that involves heap-allocated types * Making any method const than the ones listed above * Anything that could rely on the above, *or* could rely on system-specific code for `OsStr` or `Path` (note: this mostly makes these methods useless since `str` doesn't implement `AsRef<OsStr>` yet, but it's better to track the method for now and add impls later, IMHO) r? ``@tgross35`` (who mostly already reviewed this)
2 parents 51ff895 + 1c64d3e commit f4b946a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+278
-171
lines changed

library/alloc/src/borrow.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use crate::fmt;
1717
use crate::string::String;
1818

1919
#[stable(feature = "rust1", since = "1.0.0")]
20-
impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B>
20+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
21+
impl<'a, B: ?Sized> const Borrow<B> for Cow<'a, B>
2122
where
2223
B: ToOwned,
24+
B::Owned: [const] Borrow<B>,
2325
{
2426
fn borrow(&self) -> &B {
2527
&**self
@@ -326,9 +328,10 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
326328
}
327329

328330
#[stable(feature = "rust1", since = "1.0.0")]
329-
impl<B: ?Sized + ToOwned> Deref for Cow<'_, B>
331+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
332+
impl<B: ?Sized + ToOwned> const Deref for Cow<'_, B>
330333
where
331-
B::Owned: Borrow<B>,
334+
B::Owned: [const] Borrow<B>,
332335
{
333336
type Target = B;
334337

@@ -439,7 +442,11 @@ where
439442
}
440443

441444
#[stable(feature = "rust1", since = "1.0.0")]
442-
impl<T: ?Sized + ToOwned> AsRef<T> for Cow<'_, T> {
445+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
446+
impl<T: ?Sized + ToOwned> const AsRef<T> for Cow<'_, T>
447+
where
448+
T::Owned: [const] Borrow<T>,
449+
{
443450
fn as_ref(&self) -> &T {
444451
self
445452
}

library/alloc/src/collections/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,19 @@ pub use realalloc::collections::TryReserveErrorKind;
128128
reason = "Uncertain how much info should be exposed",
129129
issue = "48043"
130130
)]
131+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
131132
#[cfg(not(test))]
132-
impl From<TryReserveErrorKind> for TryReserveError {
133+
impl const From<TryReserveErrorKind> for TryReserveError {
133134
#[inline]
134135
fn from(kind: TryReserveErrorKind) -> Self {
135136
Self { kind }
136137
}
137138
}
138139

139140
#[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")]
141+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
140142
#[cfg(not(test))]
141-
impl From<LayoutError> for TryReserveErrorKind {
143+
impl const From<LayoutError> for TryReserveErrorKind {
142144
/// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`].
143145
#[inline]
144146
fn from(_: LayoutError) -> Self {

library/alloc/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@
107107
#![feature(char_max_len)]
108108
#![feature(clone_to_uninit)]
109109
#![feature(coerce_unsized)]
110+
#![feature(const_convert)]
110111
#![feature(const_default)]
111112
#![feature(const_eval_select)]
112113
#![feature(const_heap)]
113-
#![feature(const_trait_impl)]
114114
#![feature(core_intrinsics)]
115115
#![feature(deprecated_suggestion)]
116116
#![feature(deref_pure_trait)]
@@ -168,6 +168,7 @@
168168
#![feature(allow_internal_unstable)]
169169
#![feature(cfg_sanitize)]
170170
#![feature(const_precise_live_drops)]
171+
#![feature(const_trait_impl)]
171172
#![feature(coroutine_trait)]
172173
#![feature(decl_macro)]
173174
#![feature(dropck_eyepatch)]

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
// Language features:
5050
// tidy-alphabetical-start
5151
#![feature(cfg_sanitize)]
52+
#![feature(const_trait_impl)]
5253
#![feature(dropck_eyepatch)]
5354
#![feature(lang_items)]
5455
#![feature(min_specialization)]

library/core/src/array/mod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,38 +190,42 @@ impl fmt::Display for TryFromSliceError {
190190
impl Error for TryFromSliceError {}
191191

192192
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
193-
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
193+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
194194
impl const From<Infallible> for TryFromSliceError {
195195
fn from(x: Infallible) -> TryFromSliceError {
196196
match x {}
197197
}
198198
}
199199

200200
#[stable(feature = "rust1", since = "1.0.0")]
201-
impl<T, const N: usize> AsRef<[T]> for [T; N] {
201+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
202+
impl<T, const N: usize> const AsRef<[T]> for [T; N] {
202203
#[inline]
203204
fn as_ref(&self) -> &[T] {
204205
&self[..]
205206
}
206207
}
207208

208209
#[stable(feature = "rust1", since = "1.0.0")]
209-
impl<T, const N: usize> AsMut<[T]> for [T; N] {
210+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
211+
impl<T, const N: usize> const AsMut<[T]> for [T; N] {
210212
#[inline]
211213
fn as_mut(&mut self) -> &mut [T] {
212214
&mut self[..]
213215
}
214216
}
215217

216218
#[stable(feature = "array_borrow", since = "1.4.0")]
217-
impl<T, const N: usize> Borrow<[T]> for [T; N] {
219+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
220+
impl<T, const N: usize> const Borrow<[T]> for [T; N] {
218221
fn borrow(&self) -> &[T] {
219222
self
220223
}
221224
}
222225

223226
#[stable(feature = "array_borrow", since = "1.4.0")]
224-
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
227+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
228+
impl<T, const N: usize> const BorrowMut<[T]> for [T; N] {
225229
fn borrow_mut(&mut self) -> &mut [T] {
226230
self
227231
}
@@ -240,7 +244,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
240244
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
241245
/// ```
242246
#[stable(feature = "try_from", since = "1.34.0")]
243-
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
247+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
248+
impl<T, const N: usize> const TryFrom<&[T]> for [T; N]
244249
where
245250
T: Copy,
246251
{
@@ -265,7 +270,8 @@ where
265270
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
266271
/// ```
267272
#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
268-
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
273+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
274+
impl<T, const N: usize> const TryFrom<&mut [T]> for [T; N]
269275
where
270276
T: Copy,
271277
{
@@ -290,7 +296,8 @@ where
290296
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
291297
/// ```
292298
#[stable(feature = "try_from", since = "1.34.0")]
293-
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
299+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
300+
impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] {
294301
type Error = TryFromSliceError;
295302

296303
#[inline]
@@ -312,7 +319,8 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
312319
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
313320
/// ```
314321
#[stable(feature = "try_from", since = "1.34.0")]
315-
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
322+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
323+
impl<'a, T, const N: usize> const TryFrom<&'a mut [T]> for &'a mut [T; N] {
316324
type Error = TryFromSliceError;
317325

318326
#[inline]

library/core/src/ascii/ascii_char.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ macro_rules! into_int_impl {
11561156
($($ty:ty)*) => {
11571157
$(
11581158
#[unstable(feature = "ascii_char", issue = "110998")]
1159-
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
1159+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
11601160
impl const From<AsciiChar> for $ty {
11611161
#[inline]
11621162
fn from(chr: AsciiChar) -> $ty {

library/core/src/borrow.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
/// [`String`]: ../../std/string/struct.String.html
155155
#[stable(feature = "rust1", since = "1.0.0")]
156156
#[rustc_diagnostic_item = "Borrow"]
157+
#[const_trait]
158+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
157159
pub trait Borrow<Borrowed: ?Sized> {
158160
/// Immutably borrows from an owned value.
159161
///
@@ -185,6 +187,8 @@ pub trait Borrow<Borrowed: ?Sized> {
185187
/// for more information on borrowing as another type.
186188
#[stable(feature = "rust1", since = "1.0.0")]
187189
#[rustc_diagnostic_item = "BorrowMut"]
190+
#[const_trait]
191+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
188192
pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
189193
/// Mutably borrows from an owned value.
190194
///
@@ -206,36 +210,41 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
206210
}
207211

208212
#[stable(feature = "rust1", since = "1.0.0")]
209-
impl<T: ?Sized> Borrow<T> for T {
213+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
214+
impl<T: ?Sized> const Borrow<T> for T {
210215
#[rustc_diagnostic_item = "noop_method_borrow"]
211216
fn borrow(&self) -> &T {
212217
self
213218
}
214219
}
215220

216221
#[stable(feature = "rust1", since = "1.0.0")]
217-
impl<T: ?Sized> BorrowMut<T> for T {
222+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
223+
impl<T: ?Sized> const BorrowMut<T> for T {
218224
fn borrow_mut(&mut self) -> &mut T {
219225
self
220226
}
221227
}
222228

223229
#[stable(feature = "rust1", since = "1.0.0")]
224-
impl<T: ?Sized> Borrow<T> for &T {
230+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
231+
impl<T: ?Sized> const Borrow<T> for &T {
225232
fn borrow(&self) -> &T {
226233
self
227234
}
228235
}
229236

230237
#[stable(feature = "rust1", since = "1.0.0")]
231-
impl<T: ?Sized> Borrow<T> for &mut T {
238+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
239+
impl<T: ?Sized> const Borrow<T> for &mut T {
232240
fn borrow(&self) -> &T {
233241
self
234242
}
235243
}
236244

237245
#[stable(feature = "rust1", since = "1.0.0")]
238-
impl<T: ?Sized> BorrowMut<T> for &mut T {
246+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
247+
impl<T: ?Sized> const BorrowMut<T> for &mut T {
239248
fn borrow_mut(&mut self) -> &mut T {
240249
self
241250
}

library/core/src/bstr/mod.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ impl ByteStr {
6363
/// ```
6464
#[inline]
6565
#[unstable(feature = "bstr", issue = "134915")]
66-
pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &Self {
66+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
67+
pub const fn new<B: ?Sized + [const] AsRef<[u8]>>(bytes: &B) -> &Self {
6768
ByteStr::from_bytes(bytes.as_ref())
6869
}
6970

7071
#[doc(hidden)]
7172
#[unstable(feature = "bstr_internals", issue = "none")]
7273
#[inline]
73-
pub fn from_bytes(slice: &[u8]) -> &Self {
74+
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
75+
pub const fn from_bytes(slice: &[u8]) -> &Self {
7476
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`, so we can turn a reference to
7577
// the wrapped type into a reference to the wrapper type.
7678
unsafe { &*(slice as *const [u8] as *const Self) }
@@ -79,7 +81,8 @@ impl ByteStr {
7981
#[doc(hidden)]
8082
#[unstable(feature = "bstr_internals", issue = "none")]
8183
#[inline]
82-
pub fn from_bytes_mut(slice: &mut [u8]) -> &mut Self {
84+
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
85+
pub const fn from_bytes_mut(slice: &mut [u8]) -> &mut Self {
8386
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`, so we can turn a reference to
8487
// the wrapped type into a reference to the wrapper type.
8588
unsafe { &mut *(slice as *mut [u8] as *mut Self) }
@@ -88,20 +91,23 @@ impl ByteStr {
8891
#[doc(hidden)]
8992
#[unstable(feature = "bstr_internals", issue = "none")]
9093
#[inline]
91-
pub fn as_bytes(&self) -> &[u8] {
94+
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
95+
pub const fn as_bytes(&self) -> &[u8] {
9296
&self.0
9397
}
9498

9599
#[doc(hidden)]
96100
#[unstable(feature = "bstr_internals", issue = "none")]
97101
#[inline]
98-
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
102+
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
103+
pub const fn as_bytes_mut(&mut self) -> &mut [u8] {
99104
&mut self.0
100105
}
101106
}
102107

103108
#[unstable(feature = "bstr", issue = "134915")]
104-
impl Deref for ByteStr {
109+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
110+
impl const Deref for ByteStr {
105111
type Target = [u8];
106112

107113
#[inline]
@@ -111,7 +117,8 @@ impl Deref for ByteStr {
111117
}
112118

113119
#[unstable(feature = "bstr", issue = "134915")]
114-
impl DerefMut for ByteStr {
120+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
121+
impl const DerefMut for ByteStr {
115122
#[inline]
116123
fn deref_mut(&mut self) -> &mut [u8] {
117124
&mut self.0
@@ -185,15 +192,17 @@ impl fmt::Display for ByteStr {
185192
}
186193

187194
#[unstable(feature = "bstr", issue = "134915")]
188-
impl AsRef<[u8]> for ByteStr {
195+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
196+
impl const AsRef<[u8]> for ByteStr {
189197
#[inline]
190198
fn as_ref(&self) -> &[u8] {
191199
&self.0
192200
}
193201
}
194202

195203
#[unstable(feature = "bstr", issue = "134915")]
196-
impl AsRef<ByteStr> for ByteStr {
204+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
205+
impl const AsRef<ByteStr> for ByteStr {
197206
#[inline]
198207
fn as_ref(&self) -> &ByteStr {
199208
self
@@ -203,15 +212,17 @@ impl AsRef<ByteStr> for ByteStr {
203212
// `impl AsRef<ByteStr> for [u8]` omitted to avoid widespread inference failures
204213

205214
#[unstable(feature = "bstr", issue = "134915")]
206-
impl AsRef<ByteStr> for str {
215+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
216+
impl const AsRef<ByteStr> for str {
207217
#[inline]
208218
fn as_ref(&self) -> &ByteStr {
209219
ByteStr::new(self)
210220
}
211221
}
212222

213223
#[unstable(feature = "bstr", issue = "134915")]
214-
impl AsMut<[u8]> for ByteStr {
224+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
225+
impl const AsMut<[u8]> for ByteStr {
215226
#[inline]
216227
fn as_mut(&mut self) -> &mut [u8] {
217228
&mut self.0
@@ -225,7 +236,8 @@ impl AsMut<[u8]> for ByteStr {
225236
// `impl Borrow<ByteStr> for str` omitted to avoid widespread inference failures
226237

227238
#[unstable(feature = "bstr", issue = "134915")]
228-
impl Borrow<[u8]> for ByteStr {
239+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
240+
impl const Borrow<[u8]> for ByteStr {
229241
#[inline]
230242
fn borrow(&self) -> &[u8] {
231243
&self.0
@@ -235,7 +247,8 @@ impl Borrow<[u8]> for ByteStr {
235247
// `impl BorrowMut<ByteStr> for [u8]` omitted to avoid widespread inference failures
236248

237249
#[unstable(feature = "bstr", issue = "134915")]
238-
impl BorrowMut<[u8]> for ByteStr {
250+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
251+
impl const BorrowMut<[u8]> for ByteStr {
239252
#[inline]
240253
fn borrow_mut(&mut self) -> &mut [u8] {
241254
&mut self.0
@@ -303,7 +316,8 @@ impl<'a> Default for &'a mut ByteStr {
303316
// }
304317

305318
#[unstable(feature = "bstr", issue = "134915")]
306-
impl<'a> TryFrom<&'a ByteStr> for &'a str {
319+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
320+
impl<'a> const TryFrom<&'a ByteStr> for &'a str {
307321
type Error = crate::str::Utf8Error;
308322

309323
#[inline]
@@ -313,7 +327,8 @@ impl<'a> TryFrom<&'a ByteStr> for &'a str {
313327
}
314328

315329
#[unstable(feature = "bstr", issue = "134915")]
316-
impl<'a> TryFrom<&'a mut ByteStr> for &'a mut str {
330+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
331+
impl<'a> const TryFrom<&'a mut ByteStr> for &'a mut str {
317332
type Error = crate::str::Utf8Error;
318333

319334
#[inline]

0 commit comments

Comments
 (0)