Skip to content

Constify conversion traits #144289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions library/alloc/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ use crate::fmt;
use crate::string::String;

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B>
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<'a, B: ?Sized> const Borrow<B> for Cow<'a, B>
where
B: ToOwned,
B::Owned: ~const Borrow<B>,
{
fn borrow(&self) -> &B {
&**self
Expand Down Expand Up @@ -326,9 +328,10 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<B: ?Sized + ToOwned> Deref for Cow<'_, B>
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<B: ?Sized + ToOwned> const Deref for Cow<'_, B>
where
B::Owned: Borrow<B>,
B::Owned: ~const Borrow<B>,
{
type Target = B;

Expand Down Expand Up @@ -439,7 +442,11 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + ToOwned> AsRef<T> for Cow<'_, T> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized + ToOwned> const AsRef<T> for Cow<'_, T>
where
T::Owned: ~const Borrow<T>,
{
fn as_ref(&self) -> &T {
self
}
Expand Down
18 changes: 12 additions & 6 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,8 @@ impl<T: ?Sized, A: Allocator> fmt::Pointer for Box<T, A> {
}

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

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

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

#[stable(feature = "box_borrow", since = "1.1.0")]
impl<T: ?Sized, A: Allocator> Borrow<T> for Box<T, A> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized, A: Allocator> const Borrow<T> for Box<T, A> {
fn borrow(&self) -> &T {
&**self
}
}

#[stable(feature = "box_borrow", since = "1.1.0")]
impl<T: ?Sized, A: Allocator> BorrowMut<T> for Box<T, A> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized, A: Allocator> const BorrowMut<T> for Box<T, A> {
fn borrow_mut(&mut self) -> &mut T {
&mut **self
}
}

#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
impl<T: ?Sized, A: Allocator> AsRef<T> for Box<T, A> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized, A: Allocator> const AsRef<T> for Box<T, A> {
fn as_ref(&self) -> &T {
&**self
}
}

#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized, A: Allocator> const AsMut<T> for Box<T, A> {
fn as_mut(&mut self) -> &mut T {
&mut **self
}
Expand Down
28 changes: 21 additions & 7 deletions library/alloc/src/boxed/thin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use core::error::Error;
use core::fmt::{self, Debug, Display, Formatter};
use core::intrinsics::const_eval_select;
#[cfg(not(no_global_oom_handling))]
use core::intrinsics::{const_allocate, const_make_global};
use core::marker::PhantomData;
Expand Down Expand Up @@ -138,7 +139,8 @@ impl<T: ?Sized + Display> Display for ThinBox<T> {
}

#[unstable(feature = "thin_box", issue = "92791")]
impl<T: ?Sized> Deref for ThinBox<T> {
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
impl<T: ?Sized> const Deref for ThinBox<T> {
type Target = T;

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

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

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

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

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

fn header(&self) -> *mut H {
#[rustc_const_unstable(feature = "const_from", issue = "143773")]
const fn header(&self) -> *mut H {
// Safety:
// - At least `size_of::<H>()` bytes are allocated ahead of the pointer.
// - We know that H will be aligned because the middle pointer is aligned to the greater
Expand All @@ -407,11 +414,18 @@ impl<H> WithHeader<H> {
// will always result in an aligned header pointer, it just may not point to the
// beginning of the allocation.
let hp = unsafe { self.0.as_ptr().sub(Self::header_size()) as *mut H };
debug_assert!(hp.is_aligned());

const fn ignore_alignment_const<H>(_hp: *mut H) {}
fn check_alignment_rt<H>(hp: *mut H) {
debug_assert!(hp.is_aligned());
}
const_eval_select((hp,), ignore_alignment_const, check_alignment_rt);

hp
}

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

Expand Down
54 changes: 36 additions & 18 deletions library/alloc/src/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,27 @@ pub struct ByteString(pub Vec<u8>);

impl ByteString {
#[inline]
pub(crate) fn as_bytes(&self) -> &[u8] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub(crate) const fn as_bytes(&self) -> &[u8] {
&self.0
}

#[inline]
pub(crate) fn as_bytestr(&self) -> &ByteStr {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub(crate) const fn as_bytestr(&self) -> &ByteStr {
ByteStr::new(&self.0)
}

#[inline]
pub(crate) fn as_mut_bytestr(&mut self) -> &mut ByteStr {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub(crate) const fn as_mut_bytestr(&mut self) -> &mut ByteStr {
ByteStr::from_bytes_mut(&mut self.0)
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl Deref for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const Deref for ByteString {
type Target = Vec<u8>;

#[inline]
Expand All @@ -74,7 +78,8 @@ impl Deref for ByteString {
}

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

#[unstable(feature = "bstr", issue = "134915")]
impl AsRef<[u8]> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const AsRef<[u8]> for ByteString {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl AsRef<ByteStr> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const AsRef<ByteStr> for ByteString {
#[inline]
fn as_ref(&self) -> &ByteStr {
self.as_bytestr()
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl AsMut<[u8]> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const AsMut<[u8]> for ByteString {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl AsMut<ByteStr> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const AsMut<ByteStr> for ByteString {
#[inline]
fn as_mut(&mut self) -> &mut ByteStr {
self.as_mut_bytestr()
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl Borrow<[u8]> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const Borrow<[u8]> for ByteString {
#[inline]
fn borrow(&self) -> &[u8] {
&self.0
}
}

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

#[unstable(feature = "bstr", issue = "134915")]
impl BorrowMut<[u8]> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const BorrowMut<[u8]> for ByteString {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}

#[unstable(feature = "bstr", issue = "134915")]
impl BorrowMut<ByteStr> for ByteString {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const BorrowMut<ByteStr> for ByteString {
#[inline]
fn borrow_mut(&mut self) -> &mut ByteStr {
self.as_mut_bytestr()
Expand Down Expand Up @@ -211,7 +224,8 @@ impl Default for ByteString {
// }

#[unstable(feature = "bstr", issue = "134915")]
impl From<ByteString> for Vec<u8> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const From<ByteString> for Vec<u8> {
#[inline]
fn from(s: ByteString) -> Self {
s.0
Expand Down Expand Up @@ -245,15 +259,17 @@ impl<'a> From<&'a ByteStr> for ByteString {
}

#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<ByteString> for Cow<'a, ByteStr> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<'a> const From<ByteString> for Cow<'a, ByteStr> {
#[inline]
fn from(s: ByteString) -> Self {
Cow::Owned(s)
}
}

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

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

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

#[unstable(feature = "bstr", issue = "134915")]
impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<'a> const From<&'a ByteStr> for Cow<'a, ByteStr> {
#[inline]
fn from(s: &'a ByteStr) -> Self {
Cow::Borrowed(s)
Expand Down
Loading
Loading