Skip to content

Commit 8a8d706

Browse files
committed
Constify conversion traits
1 parent a955f1c commit 8a8d706

Some content is hidden

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

88 files changed

+923
-475
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: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ impl<T, A: Allocator> Box<T, A> {
606606
///
607607
/// This conversion does not allocate on the heap and happens in place.
608608
#[unstable(feature = "box_into_boxed_slice", issue = "71582")]
609-
pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
609+
#[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")]
610+
pub const fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
610611
let (raw, alloc) = Box::into_raw_with_allocator(boxed);
611612
unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) }
612613
}
@@ -1268,7 +1269,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12681269
/// [memory layout]: self#memory-layout
12691270
#[unstable(feature = "allocator_api", issue = "32838")]
12701271
#[inline]
1271-
pub unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self {
1272+
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
1273+
pub const unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self {
12721274
Box(unsafe { Unique::new_unchecked(raw) }, alloc)
12731275
}
12741276

@@ -1375,7 +1377,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
13751377
#[must_use = "losing the pointer will leak memory"]
13761378
#[unstable(feature = "allocator_api", issue = "32838")]
13771379
#[inline]
1378-
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
1380+
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
1381+
pub const fn into_raw_with_allocator(b: Self) -> (*mut T, A) {
13791382
let mut b = mem::ManuallyDrop::new(b);
13801383
// We carefully get the raw pointer out in a way that Miri's aliasing model understands what
13811384
// is happening: using the primitive "deref" of `Box`. In case `A` is *not* `Global`, we
@@ -1436,7 +1439,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
14361439
#[unstable(feature = "allocator_api", issue = "32838")]
14371440
// #[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")]
14381441
#[inline]
1439-
pub fn into_non_null_with_allocator(b: Self) -> (NonNull<T>, A) {
1442+
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
1443+
pub const fn into_non_null_with_allocator(b: Self) -> (NonNull<T>, A) {
14401444
let (ptr, alloc) = Box::into_raw_with_allocator(b);
14411445
// SAFETY: `Box` is guaranteed to be non-null.
14421446
unsafe { (NonNull::new_unchecked(ptr), alloc) }
@@ -1449,7 +1453,12 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
14491453
)]
14501454
#[inline]
14511455
#[doc(hidden)]
1452-
pub fn into_unique(b: Self) -> (Unique<T>, A) {
1456+
#[rustc_const_unstable(
1457+
feature = "ptr_internals",
1458+
issue = "none",
1459+
reason = "use `Box::leak(b).into()` or `Unique::from(Box::leak(b))` instead"
1460+
)]
1461+
pub const fn into_unique(b: Self) -> (Unique<T>, A) {
14531462
let (ptr, alloc) = Box::into_raw_with_allocator(b);
14541463
unsafe { (Unique::from(&mut *ptr), alloc) }
14551464
}
@@ -1641,7 +1650,8 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
16411650
/// let bar = Pin::from(foo);
16421651
/// ```
16431652
#[stable(feature = "box_into_pin", since = "1.63.0")]
1644-
pub fn into_pin(boxed: Self) -> Pin<Self>
1653+
#[rustc_const_unstable(feature = "const_convert_methods", issue = "144288")]
1654+
pub const fn into_pin(boxed: Self) -> Pin<Self>
16451655
where
16461656
A: 'static,
16471657
{
@@ -1942,7 +1952,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
19421952
}
19431953

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

19481959
fn deref(&self) -> &T {
@@ -1951,7 +1962,8 @@ impl<T: ?Sized, A: Allocator> Deref for Box<T, A> {
19511962
}
19521963

19531964
#[stable(feature = "rust1", since = "1.0.0")]
1954-
impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A> {
1965+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1966+
impl<T: ?Sized, A: Allocator> const DerefMut for Box<T, A> {
19551967
fn deref_mut(&mut self) -> &mut T {
19561968
&mut **self
19571969
}
@@ -2028,28 +2040,32 @@ unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Box<T, A> {}
20282040
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T, Global> {}
20292041

20302042
#[stable(feature = "box_borrow", since = "1.1.0")]
2031-
impl<T: ?Sized, A: Allocator> Borrow<T> for Box<T, A> {
2043+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2044+
impl<T: ?Sized, A: Allocator> const Borrow<T> for Box<T, A> {
20322045
fn borrow(&self) -> &T {
20332046
&**self
20342047
}
20352048
}
20362049

20372050
#[stable(feature = "box_borrow", since = "1.1.0")]
2038-
impl<T: ?Sized, A: Allocator> BorrowMut<T> for Box<T, A> {
2051+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2052+
impl<T: ?Sized, A: Allocator> const BorrowMut<T> for Box<T, A> {
20392053
fn borrow_mut(&mut self) -> &mut T {
20402054
&mut **self
20412055
}
20422056
}
20432057

20442058
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
2045-
impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
2059+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2060+
impl<T: ?Sized, A: Allocator> const AsRef<T> for Box<T, A> {
20462061
fn as_ref(&self) -> &T {
20472062
&**self
20482063
}
20492064
}
20502065

20512066
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
2052-
impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
2067+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2068+
impl<T: ?Sized, A: Allocator> const AsMut<T> for Box<T, A> {
20532069
fn as_mut(&mut self) -> &mut T {
20542070
&mut **self
20552071
}

library/alloc/src/boxed/convert.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl<T> From<T> for Box<T> {
4040
}
4141

4242
#[stable(feature = "pin", since = "1.33.0")]
43-
impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>>
43+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
44+
impl<T: ?Sized, A: Allocator> const From<Box<T, A>> for Pin<Box<T, A>>
4445
where
4546
A: 'static,
4647
{
@@ -228,7 +229,8 @@ impl From<Cow<'_, str>> for Box<str> {
228229
}
229230

230231
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
231-
impl<A: Allocator> From<Box<str, A>> for Box<[u8], A> {
232+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
233+
impl<A: Allocator> const From<Box<str, A>> for Box<[u8], A> {
232234
/// Converts a `Box<str>` into a `Box<[u8]>`
233235
///
234236
/// This conversion does not allocate on the heap and happens in place.
@@ -247,8 +249,7 @@ impl<A: Allocator> From<Box<str, A>> for Box<[u8], A> {
247249
/// ```
248250
#[inline]
249251
fn from(s: Box<str, A>) -> Self {
250-
let (raw, alloc) = Box::into_raw_with_allocator(s);
251-
unsafe { Box::from_raw_in(raw as *mut [u8], alloc) }
252+
s.into_boxed_bytes()
252253
}
253254
}
254255

@@ -275,10 +276,11 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
275276
/// # Safety
276277
///
277278
/// `boxed_slice.len()` must be exactly `N`.
278-
unsafe fn boxed_slice_as_array_unchecked<T, A: Allocator, const N: usize>(
279+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
280+
const unsafe fn boxed_slice_as_array_unchecked<T, A: Allocator, const N: usize>(
279281
boxed_slice: Box<[T], A>,
280282
) -> Box<[T; N], A> {
281-
debug_assert_eq!(boxed_slice.len(), N);
283+
debug_assert!(boxed_slice.len() == N);
282284

283285
let (ptr, alloc) = Box::into_raw_with_allocator(boxed_slice);
284286
// SAFETY: Pointer and allocator came from an existing box,
@@ -287,6 +289,7 @@ unsafe fn boxed_slice_as_array_unchecked<T, A: Allocator, const N: usize>(
287289
}
288290

289291
#[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
292+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
290293
impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
291294
type Error = Box<[T]>;
292295

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_convert", 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_convert", 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_convert", 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_convert", 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_convert", 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_convert", 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_convert", issue = "143773")]
428+
const fn value(&self) -> *mut u8 {
415429
self.0.as_ptr()
416430
}
417431

0 commit comments

Comments
 (0)