Skip to content

Commit 89f4adb

Browse files
committed
constify Cow::Owned types
`Cow` already has a `Deref<Target=B>` implementation that is `const` when the `Borrow` impl from the owned type is also `const`. However, none of the major types actually implement this, so the `Deref` impl doesn't really work. This change constifies the two major `Cow::Owned` types so that the `Deref` impl on `Cow` will work for those two types.
1 parent 28d0a4a commit 89f4adb

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

library/alloc/src/slice.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,10 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
784784
////////////////////////////////////////////////////////////////////////////////
785785

786786
#[stable(feature = "rust1", since = "1.0.0")]
787-
impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> {
787+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
788+
impl<T, A: Allocator> const Borrow<[T]> for Vec<T, A> {
788789
fn borrow(&self) -> &[T] {
789-
&self[..]
790+
self.as_slice()
790791
}
791792
}
792793

library/alloc/src/str.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ where
186186
}
187187

188188
#[stable(feature = "rust1", since = "1.0.0")]
189-
impl Borrow<str> for String {
189+
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
190+
impl const Borrow<str> for String {
190191
#[inline]
191192
fn borrow(&self) -> &str {
192-
&self[..]
193+
self.as_str()
193194
}
194195
}
195196

library/alloctests/tests/borrow.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ fn cow_const() {
5858
const IS_OWNED: bool = COW.is_owned();
5959
assert!(!IS_OWNED);
6060
}
61+
62+
#[test]
63+
fn test_const_deref_cow_str() {
64+
// Test Cow<str> - both borrowed and owned variants
65+
const COW_STR_BORROWED: Cow<'_, str> = Cow::Borrowed("hello");
66+
const COW_STR_OWNED: Cow<'_, str> = Cow::Owned(String::new());
67+
const _: &str = &*COW_STR_BORROWED;
68+
const _: &str = &*COW_STR_OWNED;
69+
}
70+
71+
#[test]
72+
fn test_const_deref_cow_slice() {
73+
// Test Cow<[T]> - both borrowed and owned variants
74+
const COW_SLICE_BORROWED: Cow<'_, [i32]> = Cow::Borrowed(&[1, 2, 3]);
75+
const COW_SLICE_OWNED: Cow<'_, [i32]> = Cow::Owned(Vec::new());
76+
const _: &[i32] = &*COW_SLICE_BORROWED;
77+
const _: &[i32] = &*COW_SLICE_OWNED;
78+
}

library/alloctests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![feature(string_remove_matches)]
2929
#![feature(const_btree_len)]
3030
#![feature(const_trait_impl)]
31+
#![feature(const_convert)]
3132
#![feature(panic_update_hook)]
3233
#![feature(pointer_is_aligned_to)]
3334
#![feature(test)]

0 commit comments

Comments
 (0)