Skip to content

Commit 5ab4174

Browse files
committed
document how to do array initialization
1 parent b7942b9 commit 5ab4174

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/pool/arc.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@
3838
//!
3939
//! assert!(res.is_ok());
4040
//! ```
41+
//!
42+
//! # Array block initialization
43+
//!
44+
//! You can create a static variable that contains an array of memory blocks and give all the blocks
45+
//! to the `ArcPool`. This requires an intermediate `const` value as shown below:
46+
//!
47+
//! ```
48+
//! use heapless::{arc_pool, pool::arc::ArcBlock};
49+
//!
50+
//! arc_pool!(P: u128);
51+
//!
52+
//! const POOL_CAPACITY: usize = 8;
53+
//!
54+
//! let blocks: &'static mut [ArcBlock<u128>] = {
55+
//! const BLOCK: ArcBlock<u128> = ArcBlock::new(); // <=
56+
//! static mut BLOCKS: [ArcBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
57+
//! unsafe { &mut BLOCKS }
58+
//! };
59+
//!
60+
//! for block in blocks {
61+
//! P.manage(block);
62+
//! }
63+
//! ```
4164
4265
// reference counting logic is based on version 1.63.0 of the Rust standard library (`alloc` crate)
4366
// which is licensed under 'MIT or APACHE-2.0'

src/pool/boxed.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@
5353
//!
5454
//! assert!(res.is_ok());
5555
//! ```
56+
//!
57+
//! # Array block initialization
58+
//!
59+
//! You can create a static variable that contains an array of memory blocks and give all the blocks
60+
//! to the `BoxPool`. This requires an intermediate `const` value as shown below:
61+
//!
62+
//! ```
63+
//! use heapless::{box_pool, pool::boxed::BoxBlock};
64+
//!
65+
//! box_pool!(P: u128);
66+
//!
67+
//! const POOL_CAPACITY: usize = 8;
68+
//!
69+
//! let blocks: &'static mut [BoxBlock<u128>] = {
70+
//! const BLOCK: BoxBlock<u128> = BoxBlock::new(); // <=
71+
//! static mut BLOCKS: [BoxBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
72+
//! unsafe { &mut BLOCKS }
73+
//! };
74+
//!
75+
//! for block in blocks {
76+
//! P.manage(block);
77+
//! }
78+
//! ```
5679
5780
use core::{
5881
fmt,

src/pool/object.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@
3939
//!
4040
//! assert!(res.is_some());
4141
//! ```
42+
//!
43+
//! # Array block initialization
44+
//!
45+
//! You can create a static variable that contains an array of memory blocks and give all the blocks
46+
//! to the `ObjectPool`. This requires an intermediate `const` value as shown below:
47+
//!
48+
//! ```
49+
//! use heapless::{object_pool, pool::object::ObjectBlock};
50+
//!
51+
//! object_pool!(P: [u8; 128]);
52+
//!
53+
//! const POOL_CAPACITY: usize = 8;
54+
//!
55+
//! let blocks: &'static mut [ObjectBlock<[u8; 128]>] = {
56+
//! const BLOCK: ObjectBlock<[u8; 128]> = ObjectBlock::new([0; 128]); // <=
57+
//! static mut BLOCKS: [ObjectBlock<[u8; 128]>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
58+
//! unsafe { &mut BLOCKS }
59+
//! };
60+
//!
61+
//! for block in blocks {
62+
//! P.manage(block);
63+
//! }
64+
//! ```
4265
4366
use core::{
4467
cmp::Ordering,

0 commit comments

Comments
 (0)