@@ -57,26 +57,20 @@ impl Item {
5757 match self . typed . get ( tid) {
5858 Some ( val) => Some ( val) ,
5959 None => {
60- match parse :: < H > ( self . raw . as_ref ( ) . expect ( "item.raw must exist" ) ) {
61- Ok ( typed) => {
62- unsafe { self . typed . insert ( tid, typed) ; }
63- self . typed . get ( tid)
64- } ,
65- Err ( _) => None
66- }
60+ parse :: < H > ( self . raw . as_ref ( ) . expect ( "item.raw must exist" ) ) . and_then ( |typed| {
61+ unsafe { self . typed . insert ( tid, typed) ; }
62+ self . typed . get ( tid)
63+ } )
6764 }
6865 } . map ( |typed| unsafe { typed. downcast_ref_unchecked ( ) } )
6966 }
7067
7168 pub fn typed_mut < H : Header > ( & mut self ) -> Option < & mut H > {
7269 let tid = TypeId :: of :: < H > ( ) ;
7370 if self . typed . get_mut ( tid) . is_none ( ) {
74- match parse :: < H > ( self . raw . as_ref ( ) . expect ( "item.raw must exist" ) ) {
75- Ok ( typed) => {
76- unsafe { self . typed . insert ( tid, typed) ; }
77- } ,
78- Err ( _) => ( )
79- }
71+ parse :: < H > ( self . raw . as_ref ( ) . expect ( "item.raw must exist" ) ) . map ( |typed| {
72+ unsafe { self . typed . insert ( tid, typed) ; }
73+ } ) ;
8074 }
8175 if self . raw . is_some ( ) && self . typed . get_mut ( tid) . is_some ( ) {
8276 self . raw = OptCell :: new ( None ) ;
@@ -86,10 +80,10 @@ impl Item {
8680
8781 pub fn into_typed < H : Header > ( self ) -> Option < H > {
8882 let tid = TypeId :: of :: < H > ( ) ;
89- match self . typed . into_value ( tid ) {
90- Some ( val ) => Some ( val ) ,
91- None => parse :: < H > ( self . raw . as_ref ( ) . expect ( "item.raw must exist" ) ) . ok ( )
92- } . map ( |typed| unsafe { typed. downcast_unchecked ( ) } )
83+ let Item { typed , raw } = self ;
84+ typed . into_value ( tid )
85+ . or_else ( || raw. as_ref ( ) . and_then ( parse :: < H > ) )
86+ . map ( |typed| unsafe { typed. downcast_unchecked ( ) } )
9387 }
9488
9589 pub fn write_h1 ( & self , f : & mut Formatter ) -> fmt:: Result {
@@ -117,9 +111,9 @@ impl Item {
117111}
118112
119113#[ inline]
120- fn parse < H : Header > ( raw : & Raw ) -> :: Result < Box < Header + Send + Sync > > {
114+ fn parse < H : Header > ( raw : & Raw ) -> Option < Box < Header + Send + Sync > > {
121115 H :: parse_header ( raw) . map ( |h| {
122116 let h: Box < Header + Send + Sync > = Box :: new ( h) ;
123117 h
124- } )
118+ } ) . ok ( )
125119}
0 commit comments