Skip to content

Commit 97c330f

Browse files
committed
Constify conversion traits
1 parent 350d0ef commit 97c330f

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
-157
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)]
@@ -167,6 +167,7 @@
167167
#![feature(allow_internal_unstable)]
168168
#![feature(cfg_sanitize)]
169169
#![feature(const_precise_live_drops)]
170+
#![feature(const_trait_impl)]
170171
#![feature(coroutine_trait)]
171172
#![feature(decl_macro)]
172173
#![feature(dropck_eyepatch)]

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
// Language features:
4747
// tidy-alphabetical-start
4848
#![feature(cfg_sanitize)]
49+
#![feature(const_trait_impl)]
4950
#![feature(dropck_eyepatch)]
5051
#![feature(lang_items)]
5152
#![feature(min_specialization)]

library/core/src/array/mod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,38 +198,42 @@ impl Error for TryFromSliceError {
198198
}
199199

200200
#[stable(feature = "try_from_slice_error", since = "1.36.0")]
201-
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
201+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
202202
impl const From<Infallible> for TryFromSliceError {
203203
fn from(x: Infallible) -> TryFromSliceError {
204204
match x {}
205205
}
206206
}
207207

208208
#[stable(feature = "rust1", since = "1.0.0")]
209-
impl<T, const N: usize> AsRef<[T]> for [T; N] {
209+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
210+
impl<T, const N: usize> const AsRef<[T]> for [T; N] {
210211
#[inline]
211212
fn as_ref(&self) -> &[T] {
212213
&self[..]
213214
}
214215
}
215216

216217
#[stable(feature = "rust1", since = "1.0.0")]
217-
impl<T, const N: usize> AsMut<[T]> for [T; N] {
218+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
219+
impl<T, const N: usize> const AsMut<[T]> for [T; N] {
218220
#[inline]
219221
fn as_mut(&mut self) -> &mut [T] {
220222
&mut self[..]
221223
}
222224
}
223225

224226
#[stable(feature = "array_borrow", since = "1.4.0")]
225-
impl<T, const N: usize> Borrow<[T]> for [T; N] {
227+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
228+
impl<T, const N: usize> const Borrow<[T]> for [T; N] {
226229
fn borrow(&self) -> &[T] {
227230
self
228231
}
229232
}
230233

231234
#[stable(feature = "array_borrow", since = "1.4.0")]
232-
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
235+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
236+
impl<T, const N: usize> const BorrowMut<[T]> for [T; N] {
233237
fn borrow_mut(&mut self) -> &mut [T] {
234238
self
235239
}
@@ -248,7 +252,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
248252
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
249253
/// ```
250254
#[stable(feature = "try_from", since = "1.34.0")]
251-
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
255+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
256+
impl<T, const N: usize> const TryFrom<&[T]> for [T; N]
252257
where
253258
T: Copy,
254259
{
@@ -273,7 +278,8 @@ where
273278
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
274279
/// ```
275280
#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
276-
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
281+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
282+
impl<T, const N: usize> const TryFrom<&mut [T]> for [T; N]
277283
where
278284
T: Copy,
279285
{
@@ -298,7 +304,8 @@ where
298304
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
299305
/// ```
300306
#[stable(feature = "try_from", since = "1.34.0")]
301-
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
307+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
308+
impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] {
302309
type Error = TryFromSliceError;
303310

304311
#[inline]
@@ -320,7 +327,8 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
320327
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
321328
/// ```
322329
#[stable(feature = "try_from", since = "1.34.0")]
323-
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
330+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
331+
impl<'a, T, const N: usize> const TryFrom<&'a mut [T]> for &'a mut [T; N] {
324332
type Error = TryFromSliceError;
325333

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