1414//! # Example
1515//!
1616//! ```
17+ //! # fn foo() -> wasmtime_internal_core::error::Result<()> {
1718//! use wasmtime_internal_core::slab::Slab;
1819//!
1920//! let mut slab = Slab::new();
2021//!
2122//! // Insert some values into the slab.
22- //! let rza = slab.alloc("Robert Fitzgerald Diggs");
23- //! let gza = slab.alloc("Gary Grice");
24- //! let bill = slab.alloc("Bill Gates");
23+ //! let rza = slab.alloc("Robert Fitzgerald Diggs")? ;
24+ //! let gza = slab.alloc("Gary Grice")? ;
25+ //! let bill = slab.alloc("Bill Gates")? ;
2526//!
2627//! // Allocated elements can be accessed infallibly via indexing (and missing and
2728//! // deallocated entries will panic).
3940//! slab.dealloc(bill);
4041//!
4142//! // Allocate a new entry.
42- //! let bill = slab.alloc("Bill Murray");
43+ //! let bill = slab.alloc("Bill Murray")?;
44+ //! # Ok(())
45+ //! # }
46+ //! # foo().unwrap();
4347//! ```
4448//!
4549//! # Using `Id`s with the Wrong `Slab`
7882//! the following:
7983//!
8084//! ```rust
85+ //! use wasmtime_internal_core::error::OutOfMemory;
86+ //!
8187//! pub struct GenerationalId {
8288//! id: wasmtime_internal_core::slab::Id,
8389//! generation: u32,
94100//! }
95101//!
96102//! impl<T> GenerationalSlab<T> {
97- //! pub fn alloc(&mut self, value: T) -> GenerationalId {
103+ //! pub fn alloc(&mut self, value: T) -> Result< GenerationalId, OutOfMemory> {
98104//! let generation = self.generation;
99- //! let id = self.slab.alloc(GenerationalEntry { value, generation });
100- //! GenerationalId { id, generation }
105+ //! let id = self.slab.alloc(GenerationalEntry { value, generation })? ;
106+ //! Ok( GenerationalId { id, generation })
101107//! }
102108//!
103109//! pub fn get(&self, id: GenerationalId) -> Option<&T> {
127133//! }
128134//! ```
129135
136+ use crate :: alloc:: Vec ;
137+ use crate :: error:: OutOfMemory ;
130138use core:: fmt;
131139use core:: num:: NonZeroU32 ;
132- use std_alloc:: vec:: Vec ;
133140
134141/// An identifier for an allocated value inside a `slab`.
135142#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -255,10 +262,10 @@ impl<T> Slab<T> {
255262 /// Construct a new, empty slab, pre-reserving space for at least `capacity`
256263 /// elements.
257264 #[ inline]
258- pub fn with_capacity ( capacity : usize ) -> Self {
265+ pub fn with_capacity ( capacity : usize ) -> Result < Self , OutOfMemory > {
259266 let mut slab = Self :: new ( ) ;
260- slab. reserve ( capacity) ;
261- slab
267+ slab. reserve ( capacity) ? ;
268+ Ok ( slab)
262269 }
263270
264271 /// Ensure that there is space for at least `additional` elements in this
@@ -267,29 +274,31 @@ impl<T> Slab<T> {
267274 /// # Panics
268275 ///
269276 /// Panics if the new capacity exceeds `Self::MAX_CAPACITY`.
270- pub fn reserve ( & mut self , additional : usize ) {
277+ pub fn reserve ( & mut self , additional : usize ) -> Result < ( ) , OutOfMemory > {
271278 let cap = self . capacity ( ) ;
272279 let len = self . len ( ) ;
273280 assert ! ( cap >= len) ;
274281 if cap - len >= additional {
275282 // Already have `additional` capacity available.
276- return ;
283+ return Ok ( ( ) ) ;
277284 }
278285
279- self . entries . reserve ( additional) ;
286+ self . entries . reserve ( additional) ? ;
280287
281288 // Maintain the invariant that `i <= MAX_CAPACITY` for all indices `i`
282289 // in `self.entries`.
283290 assert ! ( self . entries. capacity( ) <= Self :: MAX_CAPACITY ) ;
291+
292+ Ok ( ( ) )
284293 }
285294
286- fn double_capacity ( & mut self ) {
295+ fn double_capacity ( & mut self ) -> Result < ( ) , OutOfMemory > {
287296 // Double our capacity to amortize the cost of resizing. But make sure
288297 // we add some amount of minimum additional capacity, since doubling
289298 // zero capacity isn't useful.
290299 const MIN_CAPACITY : usize = 16 ;
291300 let additional = core:: cmp:: max ( self . entries . capacity ( ) , MIN_CAPACITY ) ;
292- self . reserve ( additional) ;
301+ self . reserve ( additional)
293302 }
294303
295304 /// What is the capacity of this slab? That is, how many entries can it
@@ -336,7 +345,9 @@ impl<T> Slab<T> {
336345 self . free . take ( ) . or_else ( || {
337346 if self . entries . len ( ) < self . entries . capacity ( ) {
338347 let index = EntryIndex :: new ( self . entries . len ( ) ) ;
339- self . entries . push ( Entry :: Free { next_free : None } ) ;
348+ self . entries
349+ . push ( Entry :: Free { next_free : None } )
350+ . expect ( "have capacity" ) ;
340351 Some ( index)
341352 } else {
342353 None
@@ -352,9 +363,9 @@ impl<T> Slab<T> {
352363 /// Panics if allocating this value requires reallocating the underlying
353364 /// storage, and the new capacity exceeds `Slab::MAX_CAPACITY`.
354365 #[ inline]
355- pub fn alloc ( & mut self , value : T ) -> Id {
366+ pub fn alloc ( & mut self , value : T ) -> Result < Id , OutOfMemory > {
356367 self . try_alloc ( value)
357- . unwrap_or_else ( |value| self . alloc_slow ( value) )
368+ . or_else ( |value| self . alloc_slow ( value) )
358369 }
359370
360371 /// Get the `Id` that will be returned for the next allocation in this slab.
@@ -366,12 +377,13 @@ impl<T> Slab<T> {
366377
367378 #[ inline( never) ]
368379 #[ cold]
369- fn alloc_slow ( & mut self , value : T ) -> Id {
380+ fn alloc_slow ( & mut self , value : T ) -> Result < Id , OutOfMemory > {
370381 // Reserve additional capacity, since we didn't have space for the
371382 // allocation.
372- self . double_capacity ( ) ;
383+ self . double_capacity ( ) ? ;
373384 // After which the allocation will succeed.
374- self . try_alloc ( value) . ok ( ) . unwrap ( )
385+ let id = self . try_alloc ( value) . ok ( ) . unwrap ( ) ;
386+ Ok ( id)
375387 }
376388
377389 /// Get a shared borrow of the value associated with `id`.
0 commit comments