Skip to content

Commit 66dcd76

Browse files
committed
Constify conversion traits
1 parent 41f2b6b commit 66dcd76

Some content is hidden

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

43 files changed

+261
-158
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
@@ -192,38 +192,42 @@ impl fmt::Display for TryFromSliceError {
192192
impl Error for TryFromSliceError {}
193193

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

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

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

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

225228
#[stable(feature = "array_borrow", since = "1.4.0")]
226-
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
229+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
230+
impl<T, const N: usize> const BorrowMut<[T]> for [T; N] {
227231
fn borrow_mut(&mut self) -> &mut [T] {
228232
self
229233
}
@@ -242,7 +246,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
242246
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
243247
/// ```
244248
#[stable(feature = "try_from", since = "1.34.0")]
245-
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
249+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
250+
impl<T, const N: usize> const TryFrom<&[T]> for [T; N]
246251
where
247252
T: Copy,
248253
{
@@ -267,7 +272,8 @@ where
267272
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
268273
/// ```
269274
#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
270-
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
275+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
276+
impl<T, const N: usize> const TryFrom<&mut [T]> for [T; N]
271277
where
272278
T: Copy,
273279
{
@@ -292,7 +298,8 @@ where
292298
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
293299
/// ```
294300
#[stable(feature = "try_from", since = "1.34.0")]
295-
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
301+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
302+
impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] {
296303
type Error = TryFromSliceError;
297304

298305
#[inline]
@@ -314,7 +321,8 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
314321
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
315322
/// ```
316323
#[stable(feature = "try_from", since = "1.34.0")]
317-
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
324+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
325+
impl<'a, T, const N: usize> const TryFrom<&'a mut [T]> for &'a mut [T; N] {
318326
type Error = TryFromSliceError;
319327

320328
#[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)