Skip to content

Constify conversion traits (part 1) #145279

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
6 changes: 4 additions & 2 deletions library/alloc/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,19 @@ pub use realalloc::collections::TryReserveErrorKind;
reason = "Uncertain how much info should be exposed",
issue = "48043"
)]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[cfg(not(test))]
impl From<TryReserveErrorKind> for TryReserveError {
impl const From<TryReserveErrorKind> for TryReserveError {
#[inline]
fn from(kind: TryReserveErrorKind) -> Self {
Self { kind }
}
}

#[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
#[cfg(not(test))]
impl From<LayoutError> for TryReserveErrorKind {
impl const From<LayoutError> for TryReserveErrorKind {
/// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`].
#[inline]
fn from(_: LayoutError) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@
#![feature(char_max_len)]
#![feature(clone_to_uninit)]
#![feature(coerce_unsized)]
#![feature(const_convert)]
#![feature(const_default)]
#![feature(const_eval_select)]
#![feature(const_heap)]
#![feature(const_trait_impl)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was listed under library features, and is actually a language feature. Since the diff doesn't make that clear.

#![feature(core_intrinsics)]
#![feature(deprecated_suggestion)]
#![feature(deref_pure_trait)]
Expand Down Expand Up @@ -167,6 +167,7 @@
#![feature(allow_internal_unstable)]
#![feature(cfg_sanitize)]
#![feature(const_precise_live_drops)]
#![feature(const_trait_impl)]
#![feature(coroutine_trait)]
#![feature(decl_macro)]
#![feature(dropck_eyepatch)]
Expand Down
1 change: 1 addition & 0 deletions library/alloctests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// Language features:
// tidy-alphabetical-start
#![feature(cfg_sanitize)]
#![feature(const_trait_impl)]
#![feature(dropck_eyepatch)]
#![feature(lang_items)]
#![feature(min_specialization)]
Expand Down
26 changes: 17 additions & 9 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,38 +198,42 @@ impl Error for TryFromSliceError {
}

#[stable(feature = "try_from_slice_error", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const From<Infallible> for TryFromSliceError {
fn from(x: Infallible) -> TryFromSliceError {
match x {}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, const N: usize> AsRef<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, const N: usize> const AsRef<[T]> for [T; N] {
#[inline]
fn as_ref(&self) -> &[T] {
&self[..]
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, const N: usize> AsMut<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, const N: usize> const AsMut<[T]> for [T; N] {
#[inline]
fn as_mut(&mut self) -> &mut [T] {
&mut self[..]
}
}

#[stable(feature = "array_borrow", since = "1.4.0")]
impl<T, const N: usize> Borrow<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, const N: usize> const Borrow<[T]> for [T; N] {
fn borrow(&self) -> &[T] {
self
}
}

#[stable(feature = "array_borrow", since = "1.4.0")]
impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, const N: usize> const BorrowMut<[T]> for [T; N] {
fn borrow_mut(&mut self) -> &mut [T] {
self
}
Expand All @@ -248,7 +252,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
/// ```
#[stable(feature = "try_from", since = "1.34.0")]
impl<T, const N: usize> TryFrom<&[T]> for [T; N]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, const N: usize> const TryFrom<&[T]> for [T; N]
where
T: Copy,
{
Expand All @@ -273,7 +278,8 @@ where
/// assert_eq!(512, u16::from_le_bytes(bytes_tail));
/// ```
#[stable(feature = "try_from_mut_slice_to_array", since = "1.59.0")]
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T, const N: usize> const TryFrom<&mut [T]> for [T; N]
where
T: Copy,
{
Expand All @@ -298,7 +304,8 @@ where
/// assert_eq!(512, u16::from_le_bytes(*bytes_tail));
/// ```
#[stable(feature = "try_from", since = "1.34.0")]
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<'a, T, const N: usize> const TryFrom<&'a [T]> for &'a [T; N] {
type Error = TryFromSliceError;

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

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/ascii/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ macro_rules! into_int_impl {
($($ty:ty)*) => {
$(
#[unstable(feature = "ascii_char", issue = "110998")]
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const From<AsciiChar> for $ty {
#[inline]
fn from(chr: AsciiChar) -> $ty {
Expand Down
19 changes: 14 additions & 5 deletions library/core/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
/// [`String`]: ../../std/string/struct.String.html
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "Borrow"]
#[const_trait]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub trait Borrow<Borrowed: ?Sized> {
/// Immutably borrows from an owned value.
///
Expand Down Expand Up @@ -185,6 +187,8 @@ pub trait Borrow<Borrowed: ?Sized> {
/// for more information on borrowing as another type.
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "BorrowMut"]
#[const_trait]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
/// Mutably borrows from an owned value.
///
Expand All @@ -206,36 +210,41 @@ pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for T {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized> const Borrow<T> for T {
#[rustc_diagnostic_item = "noop_method_borrow"]
fn borrow(&self) -> &T {
self
}
}

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

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

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> Borrow<T> for &mut T {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized> const Borrow<T> for &mut T {
fn borrow(&self) -> &T {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized> BorrowMut<T> for &mut T {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<T: ?Sized> const BorrowMut<T> for &mut T {
fn borrow_mut(&mut self) -> &mut T {
self
}
Expand Down
45 changes: 30 additions & 15 deletions library/core/src/bstr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ impl ByteStr {
/// ```
#[inline]
#[unstable(feature = "bstr", issue = "134915")]
pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &Self {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
pub const fn new<B: ?Sized + [const] AsRef<[u8]>>(bytes: &B) -> &Self {
ByteStr::from_bytes(bytes.as_ref())
}

#[doc(hidden)]
#[unstable(feature = "bstr_internals", issue = "none")]
#[inline]
pub fn from_bytes(slice: &[u8]) -> &Self {
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
pub const fn from_bytes(slice: &[u8]) -> &Self {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`, so we can turn a reference to
// the wrapped type into a reference to the wrapper type.
unsafe { &*(slice as *const [u8] as *const Self) }
Expand All @@ -79,7 +81,8 @@ impl ByteStr {
#[doc(hidden)]
#[unstable(feature = "bstr_internals", issue = "none")]
#[inline]
pub fn from_bytes_mut(slice: &mut [u8]) -> &mut Self {
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
pub const fn from_bytes_mut(slice: &mut [u8]) -> &mut Self {
// SAFETY: `ByteStr` is a transparent wrapper around `[u8]`, so we can turn a reference to
// the wrapped type into a reference to the wrapper type.
unsafe { &mut *(slice as *mut [u8] as *mut Self) }
Expand All @@ -88,20 +91,23 @@ impl ByteStr {
#[doc(hidden)]
#[unstable(feature = "bstr_internals", issue = "none")]
#[inline]
pub fn as_bytes(&self) -> &[u8] {
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
pub const fn as_bytes(&self) -> &[u8] {
&self.0
}

#[doc(hidden)]
#[unstable(feature = "bstr_internals", issue = "none")]
#[inline]
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
#[rustc_const_unstable(feature = "bstr_internals", issue = "none")]
pub const fn as_bytes_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}

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

#[inline]
Expand All @@ -111,7 +117,8 @@ impl Deref for ByteStr {
}

#[unstable(feature = "bstr", issue = "134915")]
impl DerefMut for ByteStr {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const DerefMut for ByteStr {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.0
Expand Down Expand Up @@ -185,15 +192,17 @@ impl fmt::Display for ByteStr {
}

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

#[unstable(feature = "bstr", issue = "134915")]
impl AsRef<ByteStr> for ByteStr {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const AsRef<ByteStr> for ByteStr {
#[inline]
fn as_ref(&self) -> &ByteStr {
self
Expand All @@ -203,15 +212,17 @@ impl AsRef<ByteStr> for ByteStr {
// `impl AsRef<ByteStr> for [u8]` omitted to avoid widespread inference failures

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

#[unstable(feature = "bstr", issue = "134915")]
impl AsMut<[u8]> for ByteStr {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const AsMut<[u8]> for ByteStr {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
Expand All @@ -225,7 +236,8 @@ impl AsMut<[u8]> for ByteStr {
// `impl Borrow<ByteStr> for str` omitted to avoid widespread inference failures

#[unstable(feature = "bstr", issue = "134915")]
impl Borrow<[u8]> for ByteStr {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const Borrow<[u8]> for ByteStr {
#[inline]
fn borrow(&self) -> &[u8] {
&self.0
Expand All @@ -235,7 +247,8 @@ impl Borrow<[u8]> for ByteStr {
// `impl BorrowMut<ByteStr> for [u8]` omitted to avoid widespread inference failures

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

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

#[inline]
Expand All @@ -313,7 +327,8 @@ impl<'a> TryFrom<&'a ByteStr> for &'a str {
}

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

#[inline]
Expand Down
Loading
Loading