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" ) ]
16- use std:: {
17- borrow:: Cow
18- } ;
16+ use std:: { } ;
1917
2018mod weak;
2119pub use weak:: HeaderVecWeak ;
@@ -539,6 +537,14 @@ impl<H, T> HeaderVec<H, T> {
539537}
540538
541539impl < H , T : Clone > HeaderVec < H , T > {
540+ /// Creates a new `HeaderVec` with the given header from some data.
541+ pub fn from_header_slice ( header : H , slice : impl AsRef < [ T ] > ) -> Self {
542+ let slice = slice. as_ref ( ) ;
543+ let mut hv = Self :: with_capacity ( slice. len ( ) , header) ;
544+ hv. extend_from_slice_intern ( slice, None ) ;
545+ hv
546+ }
547+
542548 /// Adds items from a slice to the end of the list.
543549 pub fn extend_from_slice ( & mut self , slice : impl AsRef < [ T ] > ) {
544550 self . extend_from_slice_intern ( slice. as_ref ( ) , None )
@@ -547,7 +553,11 @@ impl<H, T: Clone> HeaderVec<H, T> {
547553 /// Adds items from a slice to the end of the list.
548554 /// This method must be used when `HeaderVecWeak` are used. It takes a closure that is responsible for
549555 /// updating the weak references as additional parameter.
550- pub fn extend_from_slice_with_weakfix ( & mut self , slice : impl AsRef < [ T ] > , weak_fixup : WeakFixupFn ) {
556+ pub fn extend_from_slice_with_weakfix (
557+ & mut self ,
558+ slice : impl AsRef < [ T ] > ,
559+ weak_fixup : WeakFixupFn ,
560+ ) {
551561 self . extend_from_slice_intern ( slice. as_ref ( ) , Some ( weak_fixup) ) ;
552562 }
553563
@@ -762,59 +772,12 @@ where
762772 }
763773}
764774
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
775+ impl < H : Default , T : Clone , U > From < U > for HeaderVec < H , T >
776+ where
777+ U : AsRef < [ T ] > ,
778+ {
779+ fn from ( from : U ) -> Self {
780+ HeaderVec :: from_header_slice ( H :: default ( ) , from)
811781 }
812782}
813783
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