From 331739105d1483017ed77291ce0d6d22753000e7 Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Sat, 5 Apr 2025 21:19:39 -0700 Subject: [PATCH] Implement AsRef for [u8] This impl was omitted from #135073 due to inference failures. Add it separately to judge its impact. --- library/alloc/src/str.rs | 4 ++-- library/core/src/bstr/mod.rs | 8 +++++++- src/tools/clippy/tests/ui/useful_asref.rs | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 0664f2c3cf2..999776fd904 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -90,7 +90,7 @@ macro_rules! specialize_for_lengths { $num => { for s in iter { copy_slice_and_advance!(target, sep_bytes); - let content_bytes = s.borrow().as_ref(); + let content_bytes: &[_] = s.borrow().as_ref(); copy_slice_and_advance!(target, content_bytes); } }, @@ -99,7 +99,7 @@ macro_rules! specialize_for_lengths { // arbitrary non-zero size fallback for s in iter { copy_slice_and_advance!(target, sep_bytes); - let content_bytes = s.borrow().as_ref(); + let content_bytes: &[_] = s.borrow().as_ref(); copy_slice_and_advance!(target, content_bytes); } } diff --git a/library/core/src/bstr/mod.rs b/library/core/src/bstr/mod.rs index c8d0c701ba8..9c292f2b7c4 100644 --- a/library/core/src/bstr/mod.rs +++ b/library/core/src/bstr/mod.rs @@ -201,7 +201,13 @@ impl AsRef for ByteStr { } } -// `impl AsRef for [u8]` omitted to avoid widespread inference failures +#[unstable(feature = "bstr", issue = "134915")] +impl AsRef for [u8] { + #[inline] + fn as_ref(&self) -> &ByteStr { + ByteStr::new(self) + } +} #[unstable(feature = "bstr", issue = "134915")] impl AsRef for str { diff --git a/src/tools/clippy/tests/ui/useful_asref.rs b/src/tools/clippy/tests/ui/useful_asref.rs index a37c2785bde..fa548e4cd7b 100644 --- a/src/tools/clippy/tests/ui/useful_asref.rs +++ b/src/tools/clippy/tests/ui/useful_asref.rs @@ -7,7 +7,7 @@ trait Trait { fn as_ptr(&self); } -impl<'a> Trait for &'a [u8] { +impl<'a> Trait for &'a [u32] { fn as_ptr(&self) { self.as_ref().as_ptr(); }