about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-09-03 23:08:06 +1000
committerGitHub <noreply@github.com>2025-09-03 23:08:06 +1000
commitf4b946a14788df30b693a28e96aa18c9bee618ad (patch)
tree01fa921df7f0c97a99915eb3fc706a60089fc5b8 /library/alloc/src
parent51ff895062ba60a7cba53f57af928c3fb7b0f2f4 (diff)
parent1c64d3e6d1fe6256cb11b8dd455ccad1b5f9848c (diff)
downloadrust-f4b946a14788df30b693a28e96aa18c9bee618ad.tar.gz
rust-f4b946a14788df30b693a28e96aa18c9bee618ad.zip
Rollup merge of #145279 - clarfonthey:const-convert-initial, r=tgross35
Constify conversion traits (part 1)

This is the first part of rust-lang/rust#144289 being split into smaller pieces. It adds/moves constness of several traits under the `const_convert` feature:

* `From`
* `Into`
* `TryFrom`
* `TryInto`
* `FromStr`
* `AsRef`
* `AsMut`
* `Borrow`
* `BorrowMut`
* `Deref`
* `DerefMut`

There are a few methods that are intrinsically tied to these traits which I've included in the feature. Particularly, those which are wrappers over `AsRef`:

* `ByteStr::new` (unstable under `bstr` feature)
* `OsStr::new`
* `Path::new`

Those which directly use `Into`:

* `Result::into_ok`
* `Result::into_err`

And those which use `Deref` and `DerefMut`:

* `Pin::as_ref`
* `Pin::as_mut`
* `Pin::as_deref_mut`
* `Option::as_deref`
* `Option::as_deref_mut`
* `Result::as_deref`
* `Result::as_deref_mut`

(note: the `Option` and `Result` methods were suggested by ``@npmccallum`` initially as rust-lang/rust#146101)

The parts which are missing from this PR are:

* Anything that involves heap-allocated types
* Making any method const than the ones listed above
* Anything that could rely on the above, *or* could rely on system-specific code for `OsStr` or `Path` (note: this mostly makes these methods useless since `str` doesn't implement `AsRef<OsStr>` yet, but it's better to track the method for now and add impls later, IMHO)

r? ``@tgross35`` (who mostly already reviewed this)
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/borrow.rs15
-rw-r--r--library/alloc/src/collections/mod.rs6
-rw-r--r--library/alloc/src/lib.rs3
3 files changed, 17 insertions, 7 deletions
diff --git a/library/alloc/src/borrow.rs b/library/alloc/src/borrow.rs
index 07f51b7614f..cb32896161e 100644
--- a/library/alloc/src/borrow.rs
+++ b/library/alloc/src/borrow.rs
@@ -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
@@ -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;
 
@@ -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
     }
diff --git a/library/alloc/src/collections/mod.rs b/library/alloc/src/collections/mod.rs
index fac4d1a65ab..212d7c8465b 100644
--- a/library/alloc/src/collections/mod.rs
+++ b/library/alloc/src/collections/mod.rs
@@ -128,8 +128,9 @@ 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 }
@@ -137,8 +138,9 @@ impl From<TryReserveErrorKind> for TryReserveError {
 }
 
 #[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 {
diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs
index 711092ae8eb..cba1ce40f75 100644
--- a/library/alloc/src/lib.rs
+++ b/library/alloc/src/lib.rs
@@ -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)]
 #![feature(core_intrinsics)]
 #![feature(deprecated_suggestion)]
 #![feature(deref_pure_trait)]
@@ -168,6 +168,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)]