11use core:: ffi:: c_void;
22use core:: fmt;
3- use core:: marker :: PhantomData ;
3+ use core:: slice :: from_raw_parts ;
44use core:: str:: FromStr ;
55
66use crate :: core:: * ;
@@ -424,11 +424,20 @@ impl fmt::Debug for Request {
424424///
425425/// Implementes the core::iter::Iterator trait.
426426pub struct NgxListIterator < ' a > {
427- done : bool ,
428- part : * const ngx_list_part_t ,
429- h : * const ngx_table_elt_t ,
427+ part : Option < ListPart < ' a > > ,
430428 i : ngx_uint_t ,
431- __ : PhantomData < & ' a ( ) > ,
429+ }
430+ struct ListPart < ' a > {
431+ raw : & ' a ngx_list_part_t ,
432+ arr : & ' a [ ngx_table_elt_t ] ,
433+ }
434+ impl < ' a > From < & ' a ngx_list_part_t > for ListPart < ' a > {
435+ fn from ( raw : & ' a ngx_list_part_t ) -> Self {
436+ Self {
437+ raw,
438+ arr : & unsafe { from_raw_parts ( raw. elts . cast :: < ngx_table_elt_t > ( ) , raw. nelts ) } ,
439+ }
440+ }
432441}
433442
434443/// Creates new HTTP header iterator
@@ -437,14 +446,9 @@ pub struct NgxListIterator<'a> {
437446///
438447/// The caller has provided a valid [`ngx_str_t`] which can be dereferenced validly.
439448pub unsafe fn list_iterator ( list : & ngx_list_t ) -> NgxListIterator {
440- let part: * const ngx_list_part_t = & list. part ;
441-
442449 NgxListIterator {
443- done : false ,
444- part,
445- h : ( * part) . elts as * const ngx_table_elt_t ,
450+ part : Some ( ( & list. part ) . into ( ) ) ,
446451 i : 0 ,
447- __ : PhantomData ,
448452 }
449453}
450454
@@ -457,29 +461,19 @@ impl<'a> Iterator for NgxListIterator<'a> {
457461 type Item = ( & ' a str , & ' a str ) ;
458462
459463 fn next ( & mut self ) -> Option < Self :: Item > {
460- unsafe {
461- if self . done {
462- None
464+ let part = self . part . as_ref ( ) ?;
465+ let header = & part. arr [ self . i ] ;
466+ self . i += 1 ;
467+ if self . i >= part. arr . len ( ) {
468+ if let Some ( next_part_raw) = unsafe { part. raw . next . as_ref ( ) } {
469+ // loop back
470+ self . part = Some ( next_part_raw. into ( ) ) ;
471+ self . i = 0 ;
463472 } else {
464- if self . i >= ( * self . part ) . nelts {
465- if ( * self . part ) . next . is_null ( ) {
466- self . done = true ;
467- return None ;
468- }
469-
470- // loop back
471- self . part = ( * self . part ) . next ;
472- self . h = ( * self . part ) . elts as * mut ngx_table_elt_t ;
473- self . i = 0 ;
474- }
475-
476- let header: * const ngx_table_elt_t = self . h . add ( self . i ) ;
477- let header_name: & ngx_str_t = & ( * header) . key ;
478- let header_value: & ngx_str_t = & ( * header) . value ;
479- self . i += 1 ;
480- Some ( ( header_name. to_str ( ) , header_value. to_str ( ) ) )
473+ self . part = None ;
481474 }
482475 }
476+ Some ( ( header. key . to_str ( ) , header. value . to_str ( ) ) )
483477 }
484478}
485479
0 commit comments