3
3
extern crate alloc;
4
4
5
5
use core:: {
6
+ convert:: { AsRef , From } ,
6
7
fmt:: Debug ,
7
8
mem:: { self , ManuallyDrop , MaybeUninit } ,
8
9
ops:: { Deref , DerefMut , Index , IndexMut } ,
9
- convert:: { From , AsRef } ,
10
10
ptr,
11
11
ptr:: NonNull ,
12
12
slice:: SliceIndex ,
13
13
} ;
14
14
15
15
#[ cfg( feature = "std" ) ]
16
- use std:: {
17
- borrow:: Cow
18
- } ;
16
+ use std:: { } ;
19
17
20
18
mod weak;
21
19
pub use weak:: HeaderVecWeak ;
@@ -539,6 +537,14 @@ impl<H, T> HeaderVec<H, T> {
539
537
}
540
538
541
539
impl < 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
+
542
548
/// Adds items from a slice to the end of the list.
543
549
pub fn extend_from_slice ( & mut self , slice : impl AsRef < [ T ] > ) {
544
550
self . extend_from_slice_intern ( slice. as_ref ( ) , None )
@@ -547,7 +553,11 @@ impl<H, T: Clone> HeaderVec<H, T> {
547
553
/// Adds items from a slice to the end of the list.
548
554
/// This method must be used when `HeaderVecWeak` are used. It takes a closure that is responsible for
549
555
/// 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
+ ) {
551
561
self . extend_from_slice_intern ( slice. as_ref ( ) , Some ( weak_fixup) ) ;
552
562
}
553
563
@@ -762,59 +772,12 @@ where
762
772
}
763
773
}
764
774
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)
811
781
}
812
782
}
813
783
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