1- use crate :: { Error , ErrorExt } ;
21use std:: any:: Any ;
32use std:: collections:: HashMap ;
43
4+ #[ derive( thiserror:: Error , Debug ) ]
5+ pub enum TableError {
6+ #[ error( "table has no free keys" ) ]
7+ Full ,
8+ #[ error( "value not present" ) ]
9+ NotPresent ,
10+ #[ error( "value is of another type" ) ]
11+ WrongType ,
12+ }
13+
514/// The `Table` type is designed to map u32 handles to resources. The table is now part of the
615/// public interface to a `WasiCtx` - it is reference counted so that it can be shared beyond a
716/// `WasiCtx` with other WASI proposals (e.g. `wasi-crypto` and `wasi-nn`) to manage their
@@ -24,11 +33,11 @@ impl Table {
2433 }
2534
2635 /// Insert a resource at the next available index.
27- pub fn push ( & mut self , a : Box < dyn Any + Send + Sync > ) -> Result < u32 , Error > {
36+ pub fn push ( & mut self , a : Box < dyn Any + Send + Sync > ) -> Result < u32 , TableError > {
2837 // NOTE: The performance of this new key calculation could be very bad once keys wrap
2938 // around.
3039 if self . map . len ( ) == u32:: MAX as usize {
31- return Err ( Error :: trap ( anyhow :: Error :: msg ( "table has no free keys" ) ) ) ;
40+ return Err ( TableError :: Full ) ;
3241 }
3342 loop {
3443 let key = self . next_key ;
@@ -41,25 +50,25 @@ impl Table {
4150 }
4251 }
4352
44- pub fn push_file ( & mut self , file : Box < dyn crate :: WasiFile > ) -> Result < u32 , Error > {
53+ pub fn push_file ( & mut self , file : Box < dyn crate :: WasiFile > ) -> Result < u32 , TableError > {
4554 self . push ( Box :: new ( file) )
4655 }
4756
48- pub fn push_dir ( & mut self , dir : Box < dyn crate :: WasiDir > ) -> Result < u32 , Error > {
57+ pub fn push_dir ( & mut self , dir : Box < dyn crate :: WasiDir > ) -> Result < u32 , TableError > {
4958 self . push ( Box :: new ( dir) )
5059 }
5160
5261 pub fn push_input_stream (
5362 & mut self ,
5463 istream : Box < dyn crate :: InputStream > ,
55- ) -> Result < u32 , Error > {
64+ ) -> Result < u32 , TableError > {
5665 self . push ( Box :: new ( istream) )
5766 }
5867
5968 pub fn push_output_stream (
6069 & mut self ,
6170 ostream : Box < dyn crate :: OutputStream > ,
62- ) -> Result < u32 , Error > {
71+ ) -> Result < u32 , TableError > {
6372 self . push ( Box :: new ( ostream) )
6473 }
6574
@@ -81,35 +90,33 @@ impl Table {
8190 /// Get an immutable reference to a resource of a given type at a given index. Multiple
8291 /// immutable references can be borrowed at any given time. Borrow failure
8392 /// results in a trapping error.
84- pub fn get < T : Any + Sized > ( & self , key : u32 ) -> Result < & T , Error > {
93+ pub fn get < T : Any + Sized > ( & self , key : u32 ) -> Result < & T , TableError > {
8594 if let Some ( r) = self . map . get ( & key) {
86- r. downcast_ref :: < T > ( )
87- . ok_or_else ( || Error :: badf ( ) . context ( "element is a different type" ) )
95+ r. downcast_ref :: < T > ( ) . ok_or_else ( || TableError :: WrongType )
8896 } else {
89- Err ( Error :: badf ( ) . context ( "key not in table" ) )
97+ Err ( TableError :: NotPresent )
9098 }
9199 }
92100
93101 /// Get a mutable reference to a resource of a given type at a given index. Only one mutable
94102 /// reference can be borrowed at any given time. Borrow failure results in a trapping error.
95- pub fn get_mut < T : Any + Sized > ( & mut self , key : u32 ) -> Result < & mut T , Error > {
103+ pub fn get_mut < T : Any + Sized > ( & mut self , key : u32 ) -> Result < & mut T , TableError > {
96104 if let Some ( r) = self . map . get_mut ( & key) {
97- r. downcast_mut :: < T > ( )
98- . ok_or_else ( || Error :: badf ( ) . context ( "element is a different type" ) )
105+ r. downcast_mut :: < T > ( ) . ok_or_else ( || TableError :: WrongType )
99106 } else {
100- Err ( Error :: badf ( ) . context ( "key not in table" ) )
107+ Err ( TableError :: NotPresent )
101108 }
102109 }
103110
104111 /// Remove a resource at a given index from the table. Returns the resource
105112 /// if it was present.
106- pub fn delete < T : Any + Sized > ( & mut self , key : u32 ) -> Result < Option < T > , Error > {
113+ pub fn delete < T : Any + Sized > ( & mut self , key : u32 ) -> Result < Option < T > , TableError > {
107114 self . map
108115 . remove ( & key)
109116 . map ( |r| {
110117 r. downcast :: < T > ( )
111118 . map ( |r| * r)
112- . map_err ( |_| Error :: badf ( ) . context ( "element is a different type" ) )
119+ . map_err ( |_| TableError :: WrongType )
113120 } )
114121 . transpose ( )
115122 }
0 commit comments