@@ -54,40 +54,43 @@ pub enum HandleKind {
5454 } ,
5555}
5656
57- enum ResourceKind {
57+ /// Return value from [`HandleTable::remove_resource`].
58+ pub enum RemovedResource {
59+ /// An `own` resource was removed with the specified `rep`
60+ Own { rep : u32 } ,
61+ /// A `borrow` resource was removed originally created within `scope`.
62+ Borrow { scope : usize } ,
63+ }
64+
65+ enum Slot {
66+ Free {
67+ next : u32 ,
68+ } ,
69+ Handle {
70+ rep : u32 ,
71+ kind : HandleKind ,
72+ } ,
73+
5874 /// Represents an owned resource handle with the listed representation.
5975 ///
6076 /// The `lend_count` tracks how many times this has been lent out as a
6177 /// `borrow` and if nonzero this can't be removed.
62- Own {
78+ ResourceOwn {
6379 resource : TypedResource ,
6480 lend_count : u32 ,
6581 } ,
82+
6683 /// Represents a borrowed resource handle connected to the `scope`
6784 /// provided.
6885 ///
6986 /// The `rep` is listed and dropping this borrow will decrement the borrow
7087 /// count of the `scope`.
71- Borrow {
88+ ResourceBorrow {
7289 resource : TypedResource ,
7390 scope : usize ,
7491 } ,
7592}
7693
77- /// Return value from [`HandleTable::remove_resource`].
78- pub enum RemovedResource {
79- /// An `own` resource was removed with the specified `rep`
80- Own { rep : u32 } ,
81- /// A `borrow` resource was removed originally created within `scope`.
82- Borrow { scope : usize } ,
83- }
84-
85- enum Slot {
86- Free { next : u32 } ,
87- Handle { rep : u32 , kind : HandleKind } ,
88- Resource { kind : ResourceKind } ,
89- }
90-
9194pub struct HandleTable {
9295 next : u32 ,
9396 slots : Vec < Slot > ,
@@ -160,21 +163,17 @@ impl HandleTable {
160163 /// Inserts a new `own` resource into this table whose type/rep are
161164 /// specified by `resource`.
162165 pub fn insert_own_resource ( & mut self , resource : TypedResource ) -> Result < u32 > {
163- self . insert ( Slot :: Resource {
164- kind : ResourceKind :: Own {
165- resource,
166- lend_count : 0 ,
167- } ,
166+ self . insert ( Slot :: ResourceOwn {
167+ resource,
168+ lend_count : 0 ,
168169 } )
169170 }
170171
171172 /// Inserts a new `borrow` resource into this table whose type/rep are
172173 /// specified by `resource`. The `scope` specified is used by
173174 /// `CallContexts` to manage lending information.
174175 pub fn insert_borrow_resource ( & mut self , resource : TypedResource , scope : usize ) -> Result < u32 > {
175- self . insert ( Slot :: Resource {
176- kind : ResourceKind :: Borrow { resource, scope } ,
177- } )
176+ self . insert ( Slot :: ResourceBorrow { resource, scope } )
178177 }
179178
180179 /// Returns the internal "rep" of the resource specified by `idx`.
@@ -190,9 +189,9 @@ impl HandleTable {
190189 Some ( Slot :: Handle { .. } ) => {
191190 bail ! ( "index {} is a handle, not a resource" , idx. raw_index( ) )
192191 }
193- Some ( Slot :: Resource {
194- kind : ResourceKind :: Own { resource, .. } | ResourceKind :: Borrow { resource , .. } ,
195- } ) => resource . rep ( & idx ) ,
192+ Some ( Slot :: ResourceOwn { resource , .. } | Slot :: ResourceBorrow { resource , .. } ) => {
193+ resource. rep ( & idx )
194+ }
196195 }
197196 }
198197
@@ -206,20 +205,15 @@ impl HandleTable {
206205 /// `own` handle.
207206 pub fn resource_lend ( & mut self , idx : TypedResourceIndex ) -> Result < ( u32 , bool ) > {
208207 match self . get_mut ( idx. raw_index ( ) ) ? {
209- Slot :: Resource {
210- kind :
211- ResourceKind :: Own {
212- resource,
213- lend_count,
214- } ,
208+ Slot :: ResourceOwn {
209+ resource,
210+ lend_count,
215211 } => {
216212 let rep = resource. rep ( & idx) ?;
217213 * lend_count = lend_count. checked_add ( 1 ) . unwrap ( ) ;
218214 Ok ( ( rep, true ) )
219215 }
220- Slot :: Resource {
221- kind : ResourceKind :: Borrow { resource, .. } ,
222- } => Ok ( ( resource. rep ( & idx) ?, false ) ) ,
216+ Slot :: ResourceBorrow { resource, .. } => Ok ( ( resource. rep ( & idx) ?, false ) ) ,
223217 _ => bail ! ( "index {} is not a resource" , idx. raw_index( ) ) ,
224218 }
225219 }
@@ -228,9 +222,7 @@ impl HandleTable {
228222 /// lending operation.
229223 pub fn resource_undo_lend ( & mut self , idx : TypedResourceIndex ) -> Result < ( ) > {
230224 match self . get_mut ( idx. raw_index ( ) ) ? {
231- Slot :: Resource {
232- kind : ResourceKind :: Own { lend_count, .. } ,
233- } => {
225+ Slot :: ResourceOwn { lend_count, .. } => {
234226 * lend_count -= 1 ;
235227 Ok ( ( ) )
236228 }
@@ -246,12 +238,9 @@ impl HandleTable {
246238 let to_fill = Slot :: Free { next : self . next } ;
247239 let slot = self . get_mut ( idx. raw_index ( ) ) ?;
248240 let ret = match slot {
249- Slot :: Resource {
250- kind :
251- ResourceKind :: Own {
252- resource,
253- lend_count,
254- } ,
241+ Slot :: ResourceOwn {
242+ resource,
243+ lend_count,
255244 } => {
256245 if * lend_count != 0 {
257246 bail ! ( "cannot remove owned resource while borrowed" )
@@ -260,9 +249,7 @@ impl HandleTable {
260249 rep : resource. rep ( & idx) ?,
261250 }
262251 }
263- Slot :: Resource {
264- kind : ResourceKind :: Borrow { resource, scope } ,
265- } => {
252+ Slot :: ResourceBorrow { resource, scope } => {
266253 // Ensure the drop is done with the right type
267254 resource. rep ( & idx) ?;
268255 RemovedResource :: Borrow { scope : * scope }
@@ -299,7 +286,9 @@ impl HandleTable {
299286 . and_then ( |i| self . slots . get_mut ( i) ) ;
300287 match slot {
301288 None | Some ( Slot :: Free { .. } ) => bail ! ( "unknown handle index {idx}" ) ,
302- Some ( Slot :: Resource { .. } ) => bail ! ( "index {idx} is a resource, not a handle" ) ,
289+ Some ( Slot :: ResourceOwn { .. } | Slot :: ResourceBorrow { .. } ) => {
290+ bail ! ( "index {idx} is a resource, not a handle" )
291+ }
303292 Some ( Slot :: Handle { rep, kind } ) => Ok ( ( * rep, kind) ) ,
304293 }
305294 }
0 commit comments