From 2784327c1c8c4a656d3d2d2039b954e38fdbcd08 Mon Sep 17 00:00:00 2001 From: Nathaniel McCallum Date: Sun, 31 Aug 2025 20:28:45 -0400 Subject: [PATCH] alloc: make Cow From impls const --- library/alloc/src/bstr.rs | 6 ++++-- library/alloc/src/ffi/c_str.rs | 6 ++++-- library/alloc/src/lib.rs | 1 + library/alloc/src/string.rs | 9 ++++++--- library/alloc/src/vec/cow.rs | 12 ++++++++---- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/bstr.rs b/library/alloc/src/bstr.rs index 338c7ac7f8876..5e21ec5334c42 100644 --- a/library/alloc/src/bstr.rs +++ b/library/alloc/src/bstr.rs @@ -245,7 +245,8 @@ impl<'a> From<&'a ByteStr> for ByteString { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> From for Cow<'a, ByteStr> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From for Cow<'a, ByteStr> { #[inline] fn from(s: ByteString) -> Self { Cow::Owned(s) @@ -598,7 +599,8 @@ impl Clone for Box { } #[unstable(feature = "bstr", issue = "134915")] -impl<'a> From<&'a ByteStr> for Cow<'a, ByteStr> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From<&'a ByteStr> for Cow<'a, ByteStr> { #[inline] fn from(s: &'a ByteStr) -> Self { Cow::Borrowed(s) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index b0c8c4b1ca4a7..fec9a0c3f1ba8 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -869,7 +869,8 @@ impl From for Box { } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From for Cow<'a, CStr> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From for Cow<'a, CStr> { /// Converts a [`CString`] into an owned [`Cow`] without copying or allocating. #[inline] fn from(s: CString) -> Cow<'a, CStr> { @@ -878,7 +879,8 @@ impl<'a> From for Cow<'a, CStr> { } #[stable(feature = "cow_from_cstr", since = "1.28.0")] -impl<'a> From<&'a CStr> for Cow<'a, CStr> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From<&'a CStr> for Cow<'a, CStr> { /// Converts a [`CStr`] into a borrowed [`Cow`] without copying or allocating. #[inline] fn from(s: &'a CStr) -> Cow<'a, CStr> { diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 711092ae8eb25..00603bad8eee8 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -109,6 +109,7 @@ #![feature(coerce_unsized)] #![feature(const_default)] #![feature(const_eval_select)] +#![feature(const_from)] #![feature(const_heap)] #![feature(const_trait_impl)] #![feature(core_intrinsics)] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 1d0dd4be1b66a..a93777f32e426 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -3109,7 +3109,8 @@ impl<'a> From> for String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From<&'a str> for Cow<'a, str> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From<&'a str> for Cow<'a, str> { /// Converts a string slice into a [`Borrowed`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -3130,7 +3131,8 @@ impl<'a> From<&'a str> for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] -impl<'a> From for Cow<'a, str> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From for Cow<'a, str> { /// Converts a [`String`] into an [`Owned`] variant. /// No heap allocation is performed, and the string /// is not copied. @@ -3153,7 +3155,8 @@ impl<'a> From for Cow<'a, str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "cow_from_string_ref", since = "1.28.0")] -impl<'a> From<&'a String> for Cow<'a, str> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a> const From<&'a String> for Cow<'a, str> { /// Converts a [`String`] reference into a [`Borrowed`] variant. /// No heap allocation is performed, and the string /// is not copied. diff --git a/library/alloc/src/vec/cow.rs b/library/alloc/src/vec/cow.rs index 4deb35efffc14..ac752fc72dc9b 100644 --- a/library/alloc/src/vec/cow.rs +++ b/library/alloc/src/vec/cow.rs @@ -2,7 +2,8 @@ use super::Vec; use crate::borrow::Cow; #[stable(feature = "cow_from_vec", since = "1.8.0")] -impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a, T: Clone> const From<&'a [T]> for Cow<'a, [T]> { /// Creates a [`Borrowed`] variant of [`Cow`] /// from a slice. /// @@ -15,7 +16,8 @@ impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> { } #[stable(feature = "cow_from_array_ref", since = "1.77.0")] -impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a, T: Clone, const N: usize> const From<&'a [T; N]> for Cow<'a, [T]> { /// Creates a [`Borrowed`] variant of [`Cow`] /// from a reference to an array. /// @@ -28,7 +30,8 @@ impl<'a, T: Clone, const N: usize> From<&'a [T; N]> for Cow<'a, [T]> { } #[stable(feature = "cow_from_vec", since = "1.8.0")] -impl<'a, T: Clone> From> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a, T: Clone> const From> for Cow<'a, [T]> { /// Creates an [`Owned`] variant of [`Cow`] /// from an owned instance of [`Vec`]. /// @@ -41,7 +44,8 @@ impl<'a, T: Clone> From> for Cow<'a, [T]> { } #[stable(feature = "cow_from_vec_ref", since = "1.28.0")] -impl<'a, T: Clone> From<&'a Vec> for Cow<'a, [T]> { +#[rustc_const_unstable(feature = "const_from", issue = "143773")] +impl<'a, T: Clone> const From<&'a Vec> for Cow<'a, [T]> { /// Creates a [`Borrowed`] variant of [`Cow`] /// from a reference to [`Vec`]. ///