Skip to content

Commit 30430e6

Browse files
committed
Make extemd_from_slice() generic over AsRef<[T]>
This simplifies the `From` impls and should generally be more useful.
1 parent 0fd6d9a commit 30430e6

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

src/lib.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use core::{
66
fmt::Debug,
77
mem::{self, ManuallyDrop, MaybeUninit},
88
ops::{Deref, DerefMut, Index, IndexMut},
9+
convert::{From, AsRef},
910
ptr,
1011
ptr::NonNull,
1112
slice::SliceIndex,
@@ -539,15 +540,15 @@ impl<H, T> HeaderVec<H, T> {
539540

540541
impl<H, T: Clone> HeaderVec<H, T> {
541542
/// Adds items from a slice to the end of the list.
542-
pub fn extend_from_slice(&mut self, slice: &[T]) {
543-
self.extend_from_slice_intern(slice, None)
543+
pub fn extend_from_slice(&mut self, slice: impl AsRef<[T]>) {
544+
self.extend_from_slice_intern(slice.as_ref(), None)
544545
}
545546

546547
/// Adds items from a slice to the end of the list.
547548
/// This method must be used when `HeaderVecWeak` are used. It takes a closure that is responsible for
548549
/// updating the weak references as additional parameter.
549-
pub fn extend_from_slice_with_weakfix(&mut self, slice: &[T], weak_fixup: WeakFixupFn) {
550-
self.extend_from_slice_intern(slice, Some(weak_fixup));
550+
pub fn extend_from_slice_with_weakfix(&mut self, slice: impl AsRef<[T]>, weak_fixup: WeakFixupFn) {
551+
self.extend_from_slice_intern(slice.as_ref(), Some(weak_fixup));
551552
}
552553

553554
#[inline(always)]
@@ -768,29 +769,29 @@ xmacro::xmacro! {
768769
// Generates a lot `impl From` for `HeaderVec<(), T>` and `HeaderVec<H, T>`
769770
// The later variant is initialized from a tuple (H,T).
770771
$[
771-
from: lt: generics: where: conv:
772-
(&[T]) () () () ()
773-
(&mut [T]) () () () ()
774-
(&[T; N]) () (const N: usize) () ()
775-
(&mut[T; N]) () (const N: usize) () ()
776-
([T; N]) () (const N: usize) () (.as_ref())
777-
(Cow<'a, [T]>) ('a,) () (where [T]: ToOwned) (.as_ref())
778-
(Box<[T]>) () () () (.as_ref())
779-
(Vec<T>) () () () (.as_ref())
772+
from: lt: generics: where:
773+
(&[T]) () () ()
774+
(&mut [T]) () () ()
775+
(&[T; N]) () (const N: usize) ()
776+
(&mut[T; N]) () (const N: usize) ()
777+
([T; N]) () (const N: usize) ()
778+
(Cow<'a, [T]>) ('a,) () (where [T]: ToOwned)
779+
(Box<[T]>) () () ()
780+
(Vec<T>) () () ()
780781
]
781782

782783
impl<$lt T: Clone, $generics> From<$from> for HeaderVec<(), T> $where {
783784
fn from(from: $from) -> Self {
784785
let mut hv = HeaderVec::new(());
785-
hv.extend_from_slice(from $conv);
786+
hv.extend_from_slice(from);
786787
hv
787788
}
788789
}
789790

790-
impl<$lt H, T: Clone, $generics> From<WithHeader<H,$from>> for HeaderVec<H, T> $where {
791-
fn from(from: WithHeader<H,$from>) -> Self {
791+
impl<$lt H, T: Clone, $generics> From<WithHeader<H, $from>> for HeaderVec<H, T> $where {
792+
fn from(from: WithHeader<H, $from>) -> Self {
792793
let mut hv = HeaderVec::new(from.0);
793-
hv.extend_from_slice(from.1 $conv);
794+
hv.extend_from_slice(from.1);
794795
hv
795796
}
796797
}

0 commit comments

Comments
 (0)