Skip to content

Commit cb3f0a6

Browse files
committed
Constify conversion traits
1 parent 9748d87 commit cb3f0a6

Some content is hidden

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

78 files changed

+673
-345
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/boxed.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
19421942
}
19431943

19441944
#[stable(feature = "rust1", since = "1.0.0")]
1945-
impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
1945+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1946+
impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> {
19461947
type Target = T;
19471948

19481949
fn deref(&self) -> &T {
@@ -1951,7 +1952,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
19511952
}
19521953

19531954
#[stable(feature = "rust1", since = "1.0.0")]
1954-
impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> {
1955+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1956+
impl<T: ?Sized, A: Allocator> const DerefMut for Box<T, A> {
19551957
fn deref_mut(&mut self) -> &mut T {
19561958
&mut **self
19571959
}
@@ -2028,28 +2030,32 @@ unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Box<T, A> {}
20282030
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T, Global> {}
20292031

20302032
#[stable(feature = "box_borrow", since = "1.1.0")]
2031-
impl<T: ?Sized, A: Allocator> Borrow<T> for Box<T, A> {
2033+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2034+
impl<T: ?Sized, A: Allocator> const Borrow<T> for Box<T, A> {
20322035
fn borrow(&self) -> &T {
20332036
&**self
20342037
}
20352038
}
20362039

20372040
#[stable(feature = "box_borrow", since = "1.1.0")]
2038-
impl<T: ?Sized, A: Allocator> BorrowMut<T> for Box<T, A> {
2041+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2042+
impl<T: ?Sized, A: Allocator> const BorrowMut<T> for Box<T, A> {
20392043
fn borrow_mut(&mut self) -> &mut T {
20402044
&mut **self
20412045
}
20422046
}
20432047

20442048
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
2045-
impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
2049+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2050+
impl<T: ?Sized, A: Allocator> const AsRef<T> for Box<T, A> {
20462051
fn as_ref(&self) -> &T {
20472052
&**self
20482053
}
20492054
}
20502055

20512056
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
2052-
impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
2057+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2058+
impl<T: ?Sized, A: Allocator> const AsMut<T> for Box<T, A> {
20532059
fn as_mut(&mut self) -> &mut T {
20542060
&mut **self
20552061
}

library/alloc/src/boxed/thin.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use core::error::Error;
66
use core::fmt::{self, Debug, Display, Formatter};
7+
use core::intrinsics::const_eval_select;
78
#[cfg(not(no_global_oom_handling))]
89
use core::intrinsics::{const_allocate, const_make_global};
910
use core::marker::PhantomData;
@@ -138,7 +139,8 @@ impl<T: ?Sized + Display> Display for ThinBox<T> {
138139
}
139140

140141
#[unstable(feature = "thin_box", issue = "92791")]
141-
impl<T: ?Sized> Deref for ThinBox<T> {
142+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
143+
impl<T: ?Sized> const Deref for ThinBox<T> {
142144
type Target = T;
143145

144146
fn deref(&self) -> &T {
@@ -150,6 +152,7 @@ impl<T: ?Sized> Deref for ThinBox<T> {
150152
}
151153

152154
#[unstable(feature = "thin_box", issue = "92791")]
155+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
153156
impl<T: ?Sized> DerefMut for ThinBox<T> {
154157
fn deref_mut(&mut self) -> &mut T {
155158
let value = self.data();
@@ -172,17 +175,20 @@ impl<T: ?Sized> Drop for ThinBox<T> {
172175

173176
#[unstable(feature = "thin_box", issue = "92791")]
174177
impl<T: ?Sized> ThinBox<T> {
175-
fn meta(&self) -> <T as Pointee>::Metadata {
178+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
179+
const fn meta(&self) -> <T as Pointee>::Metadata {
176180
// Safety:
177181
// - NonNull and valid.
178182
unsafe { *self.with_header().header() }
179183
}
180184

181-
fn data(&self) -> *mut u8 {
185+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
186+
const fn data(&self) -> *mut u8 {
182187
self.with_header().value()
183188
}
184189

185-
fn with_header(&self) -> &WithHeader<<T as Pointee>::Metadata> {
190+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
191+
const fn with_header(&self) -> &WithHeader<<T as Pointee>::Metadata> {
186192
// SAFETY: both types are transparent to `NonNull<u8>`
187193
unsafe { &*((&raw const self.ptr) as *const WithHeader<_>) }
188194
}
@@ -398,7 +404,8 @@ impl<H> WithHeader<H> {
398404
}
399405
}
400406

401-
fn header(&self) -> *mut H {
407+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
408+
const fn header(&self) -> *mut H {
402409
// Safety:
403410
// - At least `size_of::<H>()` bytes are allocated ahead of the pointer.
404411
// - We know that H will be aligned because the middle pointer is aligned to the greater
@@ -407,11 +414,18 @@ impl<H> WithHeader<H> {
407414
// will always result in an aligned header pointer, it just may not point to the
408415
// beginning of the allocation.
409416
let hp = unsafe { self.0.as_ptr().sub(Self::header_size()) as *mut H };
410-
debug_assert!(hp.is_aligned());
417+
418+
const fn ignore_alignment_const<H>(_hp: *mut H) {}
419+
fn check_alignment_rt<H>(hp: *mut H) {
420+
debug_assert!(hp.is_aligned());
421+
}
422+
const_eval_select((hp,), ignore_alignment_const, check_alignment_rt);
423+
411424
hp
412425
}
413426

414-
fn value(&self) -> *mut u8 {
427+
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
428+
const fn value(&self) -> *mut u8 {
415429
self.0.as_ptr()
416430
}
417431

library/alloc/src/bstr.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,27 @@ pub struct ByteString(pub Vec<u8>);
4848

4949
impl ByteString {
5050
#[inline]
51-
pub(crate) fn as_bytes(&self) -> &[u8] {
51+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
52+
pub(crate) const fn as_bytes(&self) -> &[u8] {
5253
&self.0
5354
}
5455

5556
#[inline]
56-
pub(crate) fn as_bytestr(&self) -> &ByteStr {
57+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
58+
pub(crate) const fn as_bytestr(&self) -> &ByteStr {
5759
ByteStr::new(&self.0)
5860
}
5961

6062
#[inline]
61-
pub(crate) fn as_mut_bytestr(&mut self) -> &mut ByteStr {
63+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
64+
pub(crate) const fn as_mut_bytestr(&mut self) -> &mut ByteStr {
6265
ByteStr::from_bytes_mut(&mut self.0)
6366
}
6467
}
6568

6669
#[unstable(feature = "bstr", issue = "134915")]
67-
impl Deref for ByteString {
70+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
71+
impl const Deref for ByteString {
6872
type Target = Vec<u8>;
6973

7074
#[inline]
@@ -74,7 +78,8 @@ impl Deref for ByteString {
7478
}
7579

7680
#[unstable(feature = "bstr", issue = "134915")]
77-
impl DerefMut for ByteString {
81+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
82+
impl const DerefMut for ByteString {
7883
#[inline]
7984
fn deref_mut(&mut self) -> &mut Self::Target {
8085
&mut self.0
@@ -101,47 +106,53 @@ impl fmt::Display for ByteString {
101106
}
102107

103108
#[unstable(feature = "bstr", issue = "134915")]
104-
impl AsRef<[u8]> for ByteString {
109+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
110+
impl const AsRef<[u8]> for ByteString {
105111
#[inline]
106112
fn as_ref(&self) -> &[u8] {
107113
&self.0
108114
}
109115
}
110116

111117
#[unstable(feature = "bstr", issue = "134915")]
112-
impl AsRef<ByteStr> for ByteString {
118+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
119+
impl const AsRef<ByteStr> for ByteString {
113120
#[inline]
114121
fn as_ref(&self) -> &ByteStr {
115122
self.as_bytestr()
116123
}
117124
}
118125

119126
#[unstable(feature = "bstr", issue = "134915")]
120-
impl AsMut<[u8]> for ByteString {
127+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
128+
impl const AsMut<[u8]> for ByteString {
121129
#[inline]
122130
fn as_mut(&mut self) -> &mut [u8] {
123131
&mut self.0
124132
}
125133
}
126134

127135
#[unstable(feature = "bstr", issue = "134915")]
128-
impl AsMut<ByteStr> for ByteString {
136+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
137+
impl const AsMut<ByteStr> for ByteString {
129138
#[inline]
130139
fn as_mut(&mut self) -> &mut ByteStr {
131140
self.as_mut_bytestr()
132141
}
133142
}
134143

135144
#[unstable(feature = "bstr", issue = "134915")]
136-
impl Borrow<[u8]> for ByteString {
145+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
146+
impl const Borrow<[u8]> for ByteString {
137147
#[inline]
138148
fn borrow(&self) -> &[u8] {
139149
&self.0
140150
}
141151
}
142152

143153
#[unstable(feature = "bstr", issue = "134915")]
144-
impl Borrow<ByteStr> for ByteString {
154+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
155+
impl const Borrow<ByteStr> for ByteString {
145156
#[inline]
146157
fn borrow(&self) -> &ByteStr {
147158
self.as_bytestr()
@@ -152,15 +163,17 @@ impl Borrow<ByteStr> for ByteString {
152163
// `impl Borrow<ByteStr> for String` omitted to avoid inference failures
153164

154165
#[unstable(feature = "bstr", issue = "134915")]
155-
impl BorrowMut<[u8]> for ByteString {
166+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
167+
impl const BorrowMut<[u8]> for ByteString {
156168
#[inline]
157169
fn borrow_mut(&mut self) -> &mut [u8] {
158170
&mut self.0
159171
}
160172
}
161173

162174
#[unstable(feature = "bstr", issue = "134915")]
163-
impl BorrowMut<ByteStr> for ByteString {
175+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
176+
impl const BorrowMut<ByteStr> for ByteString {
164177
#[inline]
165178
fn borrow_mut(&mut self) -> &mut ByteStr {
166179
self.as_mut_bytestr()
@@ -211,7 +224,8 @@ impl Default for ByteString {
211224
// }
212225

213226
#[unstable(feature = "bstr", issue = "134915")]
214-
impl From<ByteString> for Vec<u8> {
227+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
228+
impl const From<ByteString> for Vec<u8> {
215229
#[inline]
216230
fn from(s: ByteString) -> Self {
217231
s.0
@@ -245,15 +259,17 @@ impl<'a> From<&'a ByteStr> for ByteString {
245259
}
246260

247261
#[unstable(feature = "bstr", issue = "134915")]
248-
impl<'a> From<ByteString> for Cow<'a, ByteStr> {
262+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
263+
impl<'a> const From<ByteString> for Cow<'a, ByteStr> {
249264
#[inline]
250265
fn from(s: ByteString) -> Self {
251266
Cow::Owned(s)
252267
}
253268
}
254269

255270
#[unstable(feature = "bstr", issue = "134915")]
256-
impl<'a> From<&'a ByteString> for Cow<'a, ByteStr> {
271+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
272+
impl<'a> const From<&'a ByteString> for Cow<'a, ByteStr> {
257273
#[inline]
258274
fn from(s: &'a ByteString) -> Self {
259275
Cow::Borrowed(s.as_bytestr())
@@ -578,7 +594,8 @@ impl TryFrom<ByteString> for String {
578594
}
579595

580596
#[unstable(feature = "bstr", issue = "134915")]
581-
impl<'a> TryFrom<&'a ByteString> for &'a str {
597+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
598+
impl<'a> const TryFrom<&'a ByteString> for &'a str {
582599
type Error = crate::str::Utf8Error;
583600

584601
#[inline]
@@ -598,7 +615,8 @@ impl Clone for Box<ByteStr> {
598615
}
599616

600617
#[unstable(feature = "bstr", issue = "134915")]
601-
impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> {
618+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
619+
impl<'a> const From<&'a ByteStr> for Cow<'a, ByteStr> {
602620
#[inline]
603621
fn from(s: &'a ByteStr) -> Self {
604622
Cow::Borrowed(s)

0 commit comments

Comments
 (0)