Skip to content
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
5 changes: 3 additions & 2 deletions library/alloc/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,10 @@ impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
////////////////////////////////////////////////////////////////////////////////

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

Expand Down
5 changes: 3 additions & 2 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Borrow<str> for String {
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl const Borrow<str> for String {
#[inline]
fn borrow(&self) -> &str {
&self[..]
self.as_str()
}
}

Expand Down
18 changes: 18 additions & 0 deletions library/alloctests/tests/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ fn cow_const() {
const IS_OWNED: bool = COW.is_owned();
assert!(!IS_OWNED);
}

#[test]
fn test_const_deref_cow_str() {
// Test Cow<str> - both borrowed and owned variants
const COW_STR_BORROWED: Cow<'_, str> = Cow::Borrowed("hello");
const COW_STR_OWNED: Cow<'_, str> = Cow::Owned(String::new());
const _: &str = &*COW_STR_BORROWED;
const _: &str = &*COW_STR_OWNED;
}

#[test]
fn test_const_deref_cow_slice() {
// Test Cow<[T]> - both borrowed and owned variants
const COW_SLICE_BORROWED: Cow<'_, [i32]> = Cow::Borrowed(&[1, 2, 3]);
const COW_SLICE_OWNED: Cow<'_, [i32]> = Cow::Owned(Vec::new());
const _: &[i32] = &*COW_SLICE_BORROWED;
const _: &[i32] = &*COW_SLICE_OWNED;
}
1 change: 1 addition & 0 deletions library/alloctests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#![feature(string_remove_matches)]
#![feature(const_btree_len)]
#![feature(const_trait_impl)]
#![feature(const_convert)]
#![feature(panic_update_hook)]
#![feature(pointer_is_aligned_to)]
#![feature(test)]
Expand Down
Loading