33extern crate alloc;
44
55use core:: {
6+ convert:: { AsRef , From } ,
67 fmt:: Debug ,
78 mem:: { self , ManuallyDrop , MaybeUninit } ,
89 ops:: { Deref , DerefMut , Index , IndexMut } ,
9- convert:: { From , AsRef } ,
1010 ptr,
1111 ptr:: NonNull ,
1212 slice:: SliceIndex ,
1313} ;
1414
1515#[ cfg( feature = "std" ) ]
1616use std:: {
17- borrow:: Cow
1817} ;
1918
2019mod weak;
@@ -539,6 +538,14 @@ impl<H, T> HeaderVec<H, T> {
539538}
540539
541540impl < H , T : Clone > HeaderVec < H , T > {
541+ /// Creates a new `HeaderVec` with the given header from some data.
542+ pub fn from_header_slice ( header : H , slice : impl AsRef < [ T ] > ) -> Self {
543+ let slice = slice. as_ref ( ) ;
544+ let mut hv = Self :: with_capacity ( slice. len ( ) , header) ;
545+ hv. extend_from_slice_intern ( slice, None ) ;
546+ hv
547+ }
548+
542549 /// Adds items from a slice to the end of the list.
543550 pub fn extend_from_slice ( & mut self , slice : impl AsRef < [ T ] > ) {
544551 self . extend_from_slice_intern ( slice. as_ref ( ) , None )
@@ -547,7 +554,11 @@ impl<H, T: Clone> HeaderVec<H, T> {
547554 /// Adds items from a slice to the end of the list.
548555 /// This method must be used when `HeaderVecWeak` are used. It takes a closure that is responsible for
549556 /// updating the weak references as additional parameter.
550- pub fn extend_from_slice_with_weakfix ( & mut self , slice : impl AsRef < [ T ] > , weak_fixup : WeakFixupFn ) {
557+ pub fn extend_from_slice_with_weakfix (
558+ & mut self ,
559+ slice : impl AsRef < [ T ] > ,
560+ weak_fixup : WeakFixupFn ,
561+ ) {
551562 self . extend_from_slice_intern ( slice. as_ref ( ) , Some ( weak_fixup) ) ;
552563 }
553564
@@ -762,59 +773,12 @@ where
762773 }
763774}
764775
765- /// A helper struct for using the `HeaderVec::from(WithHeader(H, T))`
766- pub struct WithHeader < H , T > ( pub H , pub T ) ;
767-
768- xmacro:: xmacro! {
769- // Generates a lot `impl From` for `HeaderVec<(), T>` and `HeaderVec<H, T>`
770- // The later variant is initialized from a tuple (H,T).
771- $[
772- attr:
773- from: lt: generics: where :
774- ( ) ( & [ T ] ) ( ) ( ) ( )
775- ( ) ( & mut [ T ] ) ( ) ( ) ( )
776- ( ) ( & [ T ; N ] ) ( ) ( const N : usize ) ( )
777- ( ) ( & mut [ T ; N ] ) ( ) ( const N : usize ) ( )
778- ( ) ( [ T ; N ] ) ( ) ( const N : usize ) ( )
779- ( #[ cfg( feature = "std" ) ] )
780- ( Cow <' a, [ T ] >) ( ' a, ) ( ) ( where [ T ] : ToOwned )
781- ( #[ cfg( feature = "std" ) ] )
782- ( Box <[ T ] >) ( ) ( ) ( )
783- ( #[ cfg( feature = "std" ) ] )
784- ( Vec <T >) ( ) ( ) ( )
785- ]
786-
787- $attr
788- impl <$lt T : Clone , $generics> From <$from> for HeaderVec <( ) , T > $where {
789- fn from( from: $from) -> Self {
790- let mut hv = HeaderVec :: new( ( ) ) ;
791- hv. extend_from_slice( from) ;
792- hv
793- }
794- }
795-
796- $attr
797- impl <$lt H , T : Clone , $generics> From <WithHeader <H , $from>> for HeaderVec <H , T > $where {
798- fn from( from: WithHeader <H , $from>) -> Self {
799- let mut hv = HeaderVec :: new( from. 0 ) ;
800- hv. extend_from_slice( from. 1 ) ;
801- hv
802- }
803- }
804- }
805-
806- impl From < & str > for HeaderVec < ( ) , u8 > {
807- fn from ( from : & str ) -> Self {
808- let mut hv = HeaderVec :: new ( ( ) ) ;
809- hv. extend_from_slice ( from. as_bytes ( ) ) ;
810- hv
776+ impl < H : Default , T : Clone , U > From < U > for HeaderVec < H , T >
777+ where
778+ U : AsRef < [ T ] > ,
779+ {
780+ fn from ( from : U ) -> Self {
781+ HeaderVec :: from_header_slice ( H :: default ( ) , from)
811782 }
812783}
813784
814- impl < H > From < WithHeader < H , & str > > for HeaderVec < H , u8 > {
815- fn from ( from : WithHeader < H , & str > ) -> Self {
816- let mut hv = HeaderVec :: new ( from. 0 ) ;
817- hv. extend_from_slice ( from. 1 . as_bytes ( ) ) ;
818- hv
819- }
820- }
0 commit comments