@@ -12,6 +12,8 @@ use wasm_encoder::{
1212 GlobalType , InstanceType , MemoryType , ModuleType , TableType , TagKind , TagType , TypeBounds ,
1313} ;
1414
15+ /// A type used to abstract the API differences between a component builder,
16+ /// component type, and instance type from `wasm-encoder`.
1517enum Encodable {
1618 Builder ( ComponentBuilder ) ,
1719 Instance ( InstanceType ) ,
@@ -67,7 +69,7 @@ impl Encodable {
6769 Encodable :: Builder ( b) => {
6870 b. import ( name, ty) ;
6971 }
70- _ => unreachable ! ( "expected a component type" ) ,
72+ _ => panic ! ( "expected a component type" ) ,
7173 }
7274 }
7375
@@ -126,10 +128,10 @@ impl State {
126128 }
127129
128130 pub fn builder ( & mut self ) -> & mut ComponentBuilder {
129- assert ! ( self . scopes. is_empty( ) ) ;
131+ assert ! ( self . scopes. is_empty( ) , "expected scopes to be empty" ) ;
130132 match & mut self . current . encodable {
131133 Encodable :: Builder ( builder) => builder,
132- _ => unreachable ! ( "expected a builder" ) ,
134+ _ => panic ! ( "expected a builder" ) ,
133135 }
134136 }
135137
@@ -148,7 +150,10 @@ impl State {
148150
149151 fn pop ( & mut self ) -> Encodable {
150152 log:: debug!( "popping type scope" ) ;
151- let prev = std:: mem:: replace ( & mut self . current , self . scopes . pop ( ) . unwrap ( ) ) ;
153+ let prev = std:: mem:: replace (
154+ & mut self . current ,
155+ self . scopes . pop ( ) . expect ( "expected a scope to pop" ) ,
156+ ) ;
152157 prev. encodable
153158 }
154159
@@ -194,12 +199,12 @@ impl<'a> TypeEncoder<'a> {
194199 }
195200
196201 let index = match ty {
197- Type :: Resource ( _) => unreachable ! ( ) ,
202+ Type :: Resource ( _) => panic ! ( "cannot encode a resource" ) ,
198203 Type :: Func ( id) => self . func_type ( state, id) ,
199204 Type :: Value ( ValueType :: Primitive ( ty) ) => Self :: primitive ( state, ty) ,
200205 Type :: Value ( ValueType :: Borrow ( id) ) => self . borrow ( state, id) ,
201206 Type :: Value ( ValueType :: Own ( id) ) => self . own ( state, id) ,
202- Type :: Value ( ValueType :: Defined { id , .. } ) => self . defined ( state, id) ,
207+ Type :: Value ( ValueType :: Defined ( id ) ) => self . defined ( state, id) ,
203208 Type :: Interface ( id) => self . instance ( state, id, false ) ,
204209 Type :: World ( id) => self . component ( state, id) ,
205210 Type :: Module ( id) => self . module ( state, id) ,
@@ -260,7 +265,7 @@ impl<'a> TypeEncoder<'a> {
260265 DefinedType :: Alias ( ValueType :: Primitive ( ty) ) => Self :: primitive ( state, * ty) ,
261266 DefinedType :: Alias ( ValueType :: Borrow ( id) ) => self . borrow ( state, * id) ,
262267 DefinedType :: Alias ( ValueType :: Own ( id) ) => self . own ( state, * id) ,
263- DefinedType :: Alias ( ValueType :: Defined { id , .. } ) => self . defined ( state, * id) ,
268+ DefinedType :: Alias ( ValueType :: Defined ( id ) ) => self . defined ( state, * id) ,
264269 } ;
265270
266271 log:: debug!( "defined type {id} encoded to type index {index}" ) ;
@@ -322,7 +327,7 @@ impl<'a> TypeEncoder<'a> {
322327 log:: debug!( "instance {id} encoded to type index {index}" ) ;
323328 index
324329 }
325- _ => unreachable ! ( ) ,
330+ _ => panic ! ( "expected the pushed encodable to be an instance type" ) ,
326331 }
327332 }
328333
@@ -353,7 +358,7 @@ impl<'a> TypeEncoder<'a> {
353358 log:: debug!( "world {id} encoded to type index {index}" ) ;
354359 index
355360 }
356- _ => unreachable ! ( ) ,
361+ _ => panic ! ( "expected the pushed encodable to be a component type" ) ,
357362 }
358363 }
359364
@@ -420,7 +425,7 @@ impl<'a> TypeEncoder<'a> {
420425 ) ;
421426 index
422427 }
423- _ => unreachable ! ( ) ,
428+ _ => panic ! ( "expected the pushed encodable to be a component type" ) ,
424429 }
425430 }
426431
@@ -442,7 +447,7 @@ impl<'a> TypeEncoder<'a> {
442447 log:: debug!( "encoded world definition of `{world_id}` to type index {index}" ) ;
443448 index
444449 }
445- _ => unreachable ! ( ) ,
450+ _ => panic ! ( "expected the push encodable to be a component type" ) ,
446451 }
447452 }
448453
@@ -524,7 +529,7 @@ impl<'a> TypeEncoder<'a> {
524529 ValueType :: Primitive ( ty) => return ComponentValType :: Primitive ( ty. into ( ) ) ,
525530 ValueType :: Borrow ( id) => self . borrow ( state, id) ,
526531 ValueType :: Own ( id) => self . own ( state, id) ,
527- ValueType :: Defined { id , .. } => self . defined ( state, id) ,
532+ ValueType :: Defined ( id ) => self . defined ( state, id) ,
528533 } ;
529534
530535 state. current . type_indexes . insert ( Type :: Value ( ty) , index) ;
@@ -675,7 +680,7 @@ impl<'a> TypeEncoder<'a> {
675680 log:: debug!( "instance {import_index} is available for aliasing as interface {id}" ) ;
676681 state. current . instances . insert ( id, import_index) ;
677682 }
678- _ => unreachable ! ( "expected only types, functions, and instance types" ) ,
683+ _ => panic ! ( "expected only types, functions, and instance types" ) ,
679684 }
680685 }
681686
@@ -744,7 +749,7 @@ impl<'a> TypeEncoder<'a> {
744749 ItemKind :: Type ( _) => ComponentTypeRef :: Type ( TypeBounds :: Eq ( index) ) ,
745750 ItemKind :: Func ( _) => ComponentTypeRef :: Func ( index) ,
746751 ItemKind :: Instance ( _) => ComponentTypeRef :: Instance ( index) ,
747- _ => unreachable ! ( "expected only types, functions, and instance types" ) ,
752+ _ => panic ! ( "expected only types, functions, and instance types" ) ,
748753 } ,
749754 ) ;
750755
@@ -802,7 +807,7 @@ impl<'a> TypeEncoder<'a> {
802807 t. export ( name, ty) ;
803808 index
804809 }
805- Encodable :: Builder ( _) => unreachable ! ( "expected a component or instance type" ) ,
810+ Encodable :: Builder ( _) => panic ! ( "expected a component or instance type" ) ,
806811 }
807812 }
808813}
0 commit comments