1
1
//! Slab traits.
2
- use core:: marker:: PhantomData ;
2
+ use core:: { default :: Default , marker:: PhantomData } ;
3
3
pub use crabslab_derive:: SlabItem ;
4
4
5
5
use crate :: { array:: Array , id:: Id } ;
@@ -457,73 +457,12 @@ pub trait GrowableSlab: Slab {
457
457
///
458
458
/// Returns the previous length.
459
459
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
- }
502
460
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
- }
522
461
523
462
/// Expands the slab to fit the given number of `T`s, if necessary.
524
463
fn maybe_expand_to_fit < T : SlabItem > ( & mut self , len : usize ) {
525
464
let size = T :: slab_size ( ) ;
526
- let capacity = self . slab . capacity ( ) ;
465
+ let capacity = self . capacity ( ) ;
527
466
//log::trace!(
528
467
// "append_slice: {size} * {ts_len} + {len} ({}) >= {capacity}",
529
468
// size * ts_len + len
@@ -545,7 +484,7 @@ impl<B: GrowableSlab> CpuSlab<B> {
545
484
///
546
485
/// NOTE: This changes the next available buffer index and may change the
547
486
/// buffer capacity.
548
- pub fn allocate < T : SlabItem > ( & mut self ) -> Id < T > {
487
+ fn allocate < T : SlabItem > ( & mut self ) -> Id < T > {
549
488
self . maybe_expand_to_fit :: < T > ( 1 ) ;
550
489
let index = self . increment_len ( T :: slab_size ( ) ) ;
551
490
Id :: from ( index)
@@ -558,7 +497,7 @@ impl<B: GrowableSlab> CpuSlab<B> {
558
497
/// written later with [`Self::write_array`].
559
498
///
560
499
/// 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 > {
562
501
if len == 0 {
563
502
return Array :: default ( ) ;
564
503
}
@@ -570,25 +509,87 @@ impl<B: GrowableSlab> CpuSlab<B> {
570
509
/// Append to the end of the buffer.
571
510
///
572
511
/// 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 > {
574
513
let id = self . allocate :: < T > ( ) ;
575
514
// IGNORED: safe because we just allocated the id
576
- let _ = self . slab . write ( id, t) ;
515
+ let _ = self . write ( id, t) ;
577
516
id
578
517
}
579
518
580
519
/// Append a slice to the end of the buffer, resizing if necessary
581
520
/// and returning a slabbed array.
582
521
///
583
522
/// 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 > {
585
524
let array = self . allocate_array :: < T > ( ts. len ( ) ) ;
586
525
// IGNORED: safe because we just allocated the array
587
526
let _ = self . write_array ( array, ts) ;
588
527
array
589
528
}
590
529
}
591
530
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
+
592
593
#[ cfg( not( target_arch = "spirv" ) ) ]
593
594
impl GrowableSlab for Vec < u32 > {
594
595
fn capacity ( & self ) -> usize {
0 commit comments