11//! Slab traits.
2- use core:: marker:: PhantomData ;
2+ use core:: { default :: Default , marker:: PhantomData } ;
33pub use crabslab_derive:: SlabItem ;
44
55use crate :: { array:: Array , id:: Id } ;
@@ -457,73 +457,12 @@ pub trait GrowableSlab: Slab {
457457 ///
458458 /// Returns the previous length.
459459 fn increment_len ( & mut self , n : usize ) -> usize ;
460- }
461-
462- /// A wrapper around a `GrowableSlab` that provides convenience methods for
463- /// working with CPU-side slabs.
464- ///
465- /// Working with slabs on the CPU is much more convenient because the underlying
466- /// buffer `B` is often a growable type, like `Vec<u32>`. This wrapper provides
467- /// methods for appending to the end of the buffer with automatic resizing and
468- /// for preallocating space for elements that will be written later.
469- pub struct CpuSlab < B > {
470- slab : B ,
471- }
472-
473- impl < B > AsRef < B > for CpuSlab < B > {
474- fn as_ref ( & self ) -> & B {
475- & self . slab
476- }
477- }
478-
479- impl < B > AsMut < B > for CpuSlab < B > {
480- fn as_mut ( & mut self ) -> & mut B {
481- & mut self . slab
482- }
483- }
484-
485- impl < B : Slab > Slab for CpuSlab < B > {
486- fn len ( & self ) -> usize {
487- self . slab . len ( )
488- }
489-
490- fn read < T : SlabItem + Default > ( & self , id : Id < T > ) -> T {
491- self . slab . read ( id)
492- }
493-
494- fn write_indexed < T : SlabItem > ( & mut self , t : & T , index : usize ) -> usize {
495- self . slab . write_indexed ( t, index)
496- }
497-
498- fn write_indexed_slice < T : SlabItem > ( & mut self , t : & [ T ] , index : usize ) -> usize {
499- self . slab . write_indexed_slice ( t, index)
500- }
501- }
502460
503- impl < B : GrowableSlab > GrowableSlab for CpuSlab < B > {
504- fn capacity ( & self ) -> usize {
505- self . slab . capacity ( )
506- }
507-
508- fn reserve_capacity ( & mut self , capacity : usize ) {
509- self . slab . reserve_capacity ( capacity) ;
510- }
511-
512- fn increment_len ( & mut self , n : usize ) -> usize {
513- self . slab . increment_len ( n)
514- }
515- }
516-
517- impl < B : GrowableSlab > CpuSlab < B > {
518- /// Create a new `SlabBuffer` with the given slab.
519- pub fn new ( slab : B ) -> Self {
520- Self { slab }
521- }
522461
523462 /// Expands the slab to fit the given number of `T`s, if necessary.
524463 fn maybe_expand_to_fit < T : SlabItem > ( & mut self , len : usize ) {
525464 let size = T :: slab_size ( ) ;
526- let capacity = self . slab . capacity ( ) ;
465+ let capacity = self . capacity ( ) ;
527466 //log::trace!(
528467 // "append_slice: {size} * {ts_len} + {len} ({}) >= {capacity}",
529468 // size * ts_len + len
@@ -545,7 +484,7 @@ impl<B: GrowableSlab> CpuSlab<B> {
545484 ///
546485 /// NOTE: This changes the next available buffer index and may change the
547486 /// buffer capacity.
548- pub fn allocate < T : SlabItem > ( & mut self ) -> Id < T > {
487+ fn allocate < T : SlabItem > ( & mut self ) -> Id < T > {
549488 self . maybe_expand_to_fit :: < T > ( 1 ) ;
550489 let index = self . increment_len ( T :: slab_size ( ) ) ;
551490 Id :: from ( index)
@@ -558,7 +497,7 @@ impl<B: GrowableSlab> CpuSlab<B> {
558497 /// written later with [`Self::write_array`].
559498 ///
560499 /// NOTE: This changes the length of the buffer and may change the capacity.
561- pub fn allocate_array < T : SlabItem > ( & mut self , len : usize ) -> Array < T > {
500+ fn allocate_array < T : SlabItem > ( & mut self , len : usize ) -> Array < T > {
562501 if len == 0 {
563502 return Array :: default ( ) ;
564503 }
@@ -570,25 +509,87 @@ impl<B: GrowableSlab> CpuSlab<B> {
570509 /// Append to the end of the buffer.
571510 ///
572511 /// Returns the `Id` of the written element.
573- pub fn append < T : SlabItem + Default > ( & mut self , t : & T ) -> Id < T > {
512+ fn append < T : SlabItem + Default > ( & mut self , t : & T ) -> Id < T > {
574513 let id = self . allocate :: < T > ( ) ;
575514 // IGNORED: safe because we just allocated the id
576- let _ = self . slab . write ( id, t) ;
515+ let _ = self . write ( id, t) ;
577516 id
578517 }
579518
580519 /// Append a slice to the end of the buffer, resizing if necessary
581520 /// and returning a slabbed array.
582521 ///
583522 /// Returns the `Array` of the written elements.
584- pub fn append_array < T : SlabItem + Default > ( & mut self , ts : & [ T ] ) -> Array < T > {
523+ fn append_array < T : SlabItem + Default > ( & mut self , ts : & [ T ] ) -> Array < T > {
585524 let array = self . allocate_array :: < T > ( ts. len ( ) ) ;
586525 // IGNORED: safe because we just allocated the array
587526 let _ = self . write_array ( array, ts) ;
588527 array
589528 }
590529}
591530
531+ /// A wrapper around a `GrowableSlab` that provides convenience methods for
532+ /// working with CPU-side slabs.
533+ ///
534+ /// Working with slabs on the CPU is much more convenient because the underlying
535+ /// buffer `B` is often a growable type, like `Vec<u32>`. This wrapper provides
536+ /// methods for appending to the end of the buffer with automatic resizing and
537+ /// for preallocating space for elements that will be written later.
538+ pub struct CpuSlab < B > {
539+ slab : B ,
540+ }
541+
542+ impl < B > AsRef < B > for CpuSlab < B > {
543+ fn as_ref ( & self ) -> & B {
544+ & self . slab
545+ }
546+ }
547+
548+ impl < B > AsMut < B > for CpuSlab < B > {
549+ fn as_mut ( & mut self ) -> & mut B {
550+ & mut self . slab
551+ }
552+ }
553+
554+ impl < B : Slab > Slab for CpuSlab < B > {
555+ fn len ( & self ) -> usize {
556+ self . slab . len ( )
557+ }
558+
559+ fn read < T : SlabItem + Default > ( & self , id : Id < T > ) -> T {
560+ self . slab . read ( id)
561+ }
562+
563+ fn write_indexed < T : SlabItem > ( & mut self , t : & T , index : usize ) -> usize {
564+ self . slab . write_indexed ( t, index)
565+ }
566+
567+ fn write_indexed_slice < T : SlabItem > ( & mut self , t : & [ T ] , index : usize ) -> usize {
568+ self . slab . write_indexed_slice ( t, index)
569+ }
570+ }
571+
572+ impl < B : GrowableSlab > GrowableSlab for CpuSlab < B > {
573+ fn capacity ( & self ) -> usize {
574+ self . slab . capacity ( )
575+ }
576+
577+ fn reserve_capacity ( & mut self , capacity : usize ) {
578+ self . slab . reserve_capacity ( capacity) ;
579+ }
580+
581+ fn increment_len ( & mut self , n : usize ) -> usize {
582+ self . slab . increment_len ( n)
583+ }
584+ }
585+
586+ impl < B : GrowableSlab > CpuSlab < B > {
587+ /// Create a new `SlabBuffer` with the given slab.
588+ pub fn new ( slab : B ) -> Self {
589+ Self { slab }
590+ }
591+ }
592+
592593#[ cfg( not( target_arch = "spirv" ) ) ]
593594impl GrowableSlab for Vec < u32 > {
594595 fn capacity ( & self ) -> usize {
0 commit comments