@@ -57,26 +57,20 @@ impl Item {
57
57
match self . typed . get ( tid) {
58
58
Some ( val) => Some ( val) ,
59
59
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
+ } )
67
64
}
68
65
} . map ( |typed| unsafe { typed. downcast_ref_unchecked ( ) } )
69
66
}
70
67
71
68
pub fn typed_mut < H : Header > ( & mut self ) -> Option < & mut H > {
72
69
let tid = TypeId :: of :: < H > ( ) ;
73
70
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
+ } ) ;
80
74
}
81
75
if self . raw . is_some ( ) && self . typed . get_mut ( tid) . is_some ( ) {
82
76
self . raw = OptCell :: new ( None ) ;
@@ -86,10 +80,10 @@ impl Item {
86
80
87
81
pub fn into_typed < H : Header > ( self ) -> Option < H > {
88
82
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 ( ) } )
93
87
}
94
88
95
89
pub fn write_h1 ( & self , f : & mut Formatter ) -> fmt:: Result {
@@ -117,9 +111,9 @@ impl Item {
117
111
}
118
112
119
113
#[ 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 > > {
121
115
H :: parse_header ( raw) . map ( |h| {
122
116
let h: Box < Header + Send + Sync > = Box :: new ( h) ;
123
117
h
124
- } )
118
+ } ) . ok ( )
125
119
}
0 commit comments