55//! ```
66//! use heapless::{arc_pool, pool::arc::{Arc, ArcBlock}};
77//!
8- //! arc_pool!(P : u128);
8+ //! arc_pool!(MyArcPool : u128);
99//!
1010//! // cannot allocate without first giving memory blocks to the pool
11- //! assert!(P .alloc(42).is_err());
11+ //! assert!(MyArcPool .alloc(42).is_err());
1212//!
1313//! // (some `no_std` runtimes have safe APIs to create `&'static mut` references)
1414//! let block: &'static mut ArcBlock<u128> = unsafe {
15- //! static mut B : ArcBlock<u128> = ArcBlock::new();
16- //! &mut B
15+ //! static mut BLOCK : ArcBlock<u128> = ArcBlock::new();
16+ //! &mut BLOCK
1717//! };
1818//!
19- //! P .manage(block);
19+ //! MyArcPool .manage(block);
2020//!
21- //! let arc = P .alloc(1).unwrap();
21+ //! let arc = MyArcPool .alloc(1).unwrap();
2222//!
2323//! // number of smart pointers is limited to the number of blocks managed by the pool
24- //! let res = P .alloc(2);
24+ //! let res = MyArcPool .alloc(2);
2525//! assert!(res.is_err());
2626//!
2727//! // but cloning does not consume an `ArcBlock`
3434//! drop(arc); // release memory
3535//!
3636//! // it's now possible to allocate a new `Arc` smart pointer
37- //! let res = P .alloc(3);
37+ //! let res = MyArcPool .alloc(3);
3838//!
3939//! assert!(res.is_ok());
4040//! ```
4747//! ```
4848//! use heapless::{arc_pool, pool::arc::ArcBlock};
4949//!
50- //! arc_pool!(P : u128);
50+ //! arc_pool!(MyArcPool : u128);
5151//!
5252//! const POOL_CAPACITY: usize = 8;
5353//!
5858//! };
5959//!
6060//! for block in blocks {
61- //! P .manage(block);
61+ //! MyArcPool .manage(block);
6262//! }
6363//! ```
6464
@@ -83,13 +83,14 @@ use super::treiber::{NonNullPtr, Stack, UnionNode};
8383#[ macro_export]
8484macro_rules! arc_pool {
8585 ( $name: ident: $data_type: ty) => {
86- #[ allow( non_camel_case_types) ]
8786 pub struct $name;
8887
8988 impl $crate:: pool:: arc:: ArcPool for $name {
9089 type Data = $data_type;
9190
9291 fn singleton( ) -> & ' static $crate:: pool:: arc:: ArcPoolImpl <$data_type> {
92+ // Even though the static variable is not exposed to user code, it is
93+ // still useful to have a descriptive symbol name for debugging.
9394 #[ allow( non_upper_case_globals) ]
9495 static $name: $crate:: pool:: arc:: ArcPoolImpl <$data_type> =
9596 $crate:: pool:: arc:: ArcPoolImpl :: new( ) ;
@@ -386,67 +387,67 @@ mod tests {
386387
387388 #[ test]
388389 fn cannot_alloc_if_empty ( ) {
389- arc_pool ! ( P : i32 ) ;
390+ arc_pool ! ( MyArcPool : i32 ) ;
390391
391- assert_eq ! ( Err ( 42 ) , P . alloc( 42 ) , ) ;
392+ assert_eq ! ( Err ( 42 ) , MyArcPool . alloc( 42 ) , ) ;
392393 }
393394
394395 #[ test]
395396 fn can_alloc_if_manages_one_block ( ) {
396- arc_pool ! ( P : i32 ) ;
397+ arc_pool ! ( MyArcPool : i32 ) ;
397398
398399 let block = unsafe {
399- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
400- & mut B
400+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
401+ & mut BLOCK
401402 } ;
402- P . manage ( block) ;
403+ MyArcPool . manage ( block) ;
403404
404- assert_eq ! ( 42 , * P . alloc( 42 ) . unwrap( ) ) ;
405+ assert_eq ! ( 42 , * MyArcPool . alloc( 42 ) . unwrap( ) ) ;
405406 }
406407
407408 #[ test]
408409 fn alloc_drop_alloc ( ) {
409- arc_pool ! ( P : i32 ) ;
410+ arc_pool ! ( MyArcPool : i32 ) ;
410411
411412 let block = unsafe {
412- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
413- & mut B
413+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
414+ & mut BLOCK
414415 } ;
415- P . manage ( block) ;
416+ MyArcPool . manage ( block) ;
416417
417- let arc = P . alloc ( 1 ) . unwrap ( ) ;
418+ let arc = MyArcPool . alloc ( 1 ) . unwrap ( ) ;
418419
419420 drop ( arc) ;
420421
421- assert_eq ! ( 2 , * P . alloc( 2 ) . unwrap( ) ) ;
422+ assert_eq ! ( 2 , * MyArcPool . alloc( 2 ) . unwrap( ) ) ;
422423 }
423424
424425 #[ test]
425426 fn strong_count_starts_at_one ( ) {
426- arc_pool ! ( P : i32 ) ;
427+ arc_pool ! ( MyArcPool : i32 ) ;
427428
428429 let block = unsafe {
429- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
430- & mut B
430+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
431+ & mut BLOCK
431432 } ;
432- P . manage ( block) ;
433+ MyArcPool . manage ( block) ;
433434
434- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
435+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
435436
436437 assert_eq ! ( 1 , arc. inner( ) . strong. load( Ordering :: Relaxed ) ) ;
437438 }
438439
439440 #[ test]
440441 fn clone_increases_strong_count ( ) {
441- arc_pool ! ( P : i32 ) ;
442+ arc_pool ! ( MyArcPool : i32 ) ;
442443
443444 let block = unsafe {
444- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
445- & mut B
445+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
446+ & mut BLOCK
446447 } ;
447- P . manage ( block) ;
448+ MyArcPool . manage ( block) ;
448449
449- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
450+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
450451
451452 let before = arc. inner ( ) . strong . load ( Ordering :: Relaxed ) ;
452453
@@ -459,15 +460,15 @@ mod tests {
459460
460461 #[ test]
461462 fn drop_decreases_strong_count ( ) {
462- arc_pool ! ( P : i32 ) ;
463+ arc_pool ! ( MyArcPool : i32 ) ;
463464
464465 let block = unsafe {
465- static mut B : ArcBlock < i32 > = ArcBlock :: new ( ) ;
466- & mut B
466+ static mut BLOCK : ArcBlock < i32 > = ArcBlock :: new ( ) ;
467+ & mut BLOCK
467468 } ;
468- P . manage ( block) ;
469+ MyArcPool . manage ( block) ;
469470
470- let arc = P . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
471+ let arc = MyArcPool . alloc ( 1 ) . ok ( ) . unwrap ( ) ;
471472 let arc2 = arc. clone ( ) ;
472473
473474 let before = arc. inner ( ) . strong . load ( Ordering :: Relaxed ) ;
@@ -482,23 +483,23 @@ mod tests {
482483 fn runs_destructor_exactly_once_when_strong_count_reaches_zero ( ) {
483484 static COUNT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
484485
485- pub struct S ;
486+ pub struct MyStruct ;
486487
487- impl Drop for S {
488+ impl Drop for MyStruct {
488489 fn drop ( & mut self ) {
489490 COUNT . fetch_add ( 1 , Ordering :: Relaxed ) ;
490491 }
491492 }
492493
493- arc_pool ! ( P : S ) ;
494+ arc_pool ! ( MyArcPool : MyStruct ) ;
494495
495496 let block = unsafe {
496- static mut B : ArcBlock < S > = ArcBlock :: new ( ) ;
497- & mut B
497+ static mut BLOCK : ArcBlock < MyStruct > = ArcBlock :: new ( ) ;
498+ & mut BLOCK
498499 } ;
499- P . manage ( block) ;
500+ MyArcPool . manage ( block) ;
500501
501- let arc = P . alloc ( S ) . ok ( ) . unwrap ( ) ;
502+ let arc = MyArcPool . alloc ( MyStruct ) . ok ( ) . unwrap ( ) ;
502503
503504 assert_eq ! ( 0 , COUNT . load( Ordering :: Relaxed ) ) ;
504505
@@ -512,24 +513,17 @@ mod tests {
512513 #[ repr( align( 4096 ) ) ]
513514 pub struct Zst4096 ;
514515
515- arc_pool ! ( P : Zst4096 ) ;
516+ arc_pool ! ( MyArcPool : Zst4096 ) ;
516517
517518 let block = unsafe {
518- static mut B : ArcBlock < Zst4096 > = ArcBlock :: new ( ) ;
519- & mut B
519+ static mut BLOCK : ArcBlock < Zst4096 > = ArcBlock :: new ( ) ;
520+ & mut BLOCK
520521 } ;
521- P . manage ( block) ;
522+ MyArcPool . manage ( block) ;
522523
523- let arc = P . alloc ( Zst4096 ) . ok ( ) . unwrap ( ) ;
524+ let arc = MyArcPool . alloc ( Zst4096 ) . ok ( ) . unwrap ( ) ;
524525
525526 let raw = & * arc as * const Zst4096 ;
526527 assert_eq ! ( 0 , raw as usize % 4096 ) ;
527528 }
528-
529- #[ test]
530- fn arc_pool_case ( ) {
531- // https://github.com/rust-embedded/heapless/issues/411
532- arc_pool ! ( CamelCaseType : u128 ) ;
533- arc_pool ! ( SCREAMING_SNAKE_CASE_TYPE : u128 ) ;
534- }
535529}
0 commit comments