mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-28 21:10:28 +00:00
document how to do array initialization
This commit is contained in:
parent
b7942b9c3b
commit
5ab41741fa
@ -38,6 +38,29 @@
|
||||
//!
|
||||
//! assert!(res.is_ok());
|
||||
//! ```
|
||||
//!
|
||||
//! # Array block initialization
|
||||
//!
|
||||
//! You can create a static variable that contains an array of memory blocks and give all the blocks
|
||||
//! to the `ArcPool`. This requires an intermediate `const` value as shown below:
|
||||
//!
|
||||
//! ```
|
||||
//! use heapless::{arc_pool, pool::arc::ArcBlock};
|
||||
//!
|
||||
//! arc_pool!(P: u128);
|
||||
//!
|
||||
//! const POOL_CAPACITY: usize = 8;
|
||||
//!
|
||||
//! let blocks: &'static mut [ArcBlock<u128>] = {
|
||||
//! const BLOCK: ArcBlock<u128> = ArcBlock::new(); // <=
|
||||
//! static mut BLOCKS: [ArcBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
||||
//! unsafe { &mut BLOCKS }
|
||||
//! };
|
||||
//!
|
||||
//! for block in blocks {
|
||||
//! P.manage(block);
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
// reference counting logic is based on version 1.63.0 of the Rust standard library (`alloc` crate)
|
||||
// which is licensed under 'MIT or APACHE-2.0'
|
||||
|
@ -53,6 +53,29 @@
|
||||
//!
|
||||
//! assert!(res.is_ok());
|
||||
//! ```
|
||||
//!
|
||||
//! # Array block initialization
|
||||
//!
|
||||
//! You can create a static variable that contains an array of memory blocks and give all the blocks
|
||||
//! to the `BoxPool`. This requires an intermediate `const` value as shown below:
|
||||
//!
|
||||
//! ```
|
||||
//! use heapless::{box_pool, pool::boxed::BoxBlock};
|
||||
//!
|
||||
//! box_pool!(P: u128);
|
||||
//!
|
||||
//! const POOL_CAPACITY: usize = 8;
|
||||
//!
|
||||
//! let blocks: &'static mut [BoxBlock<u128>] = {
|
||||
//! const BLOCK: BoxBlock<u128> = BoxBlock::new(); // <=
|
||||
//! static mut BLOCKS: [BoxBlock<u128>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
||||
//! unsafe { &mut BLOCKS }
|
||||
//! };
|
||||
//!
|
||||
//! for block in blocks {
|
||||
//! P.manage(block);
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use core::{
|
||||
fmt,
|
||||
|
@ -39,6 +39,29 @@
|
||||
//!
|
||||
//! assert!(res.is_some());
|
||||
//! ```
|
||||
//!
|
||||
//! # Array block initialization
|
||||
//!
|
||||
//! You can create a static variable that contains an array of memory blocks and give all the blocks
|
||||
//! to the `ObjectPool`. This requires an intermediate `const` value as shown below:
|
||||
//!
|
||||
//! ```
|
||||
//! use heapless::{object_pool, pool::object::ObjectBlock};
|
||||
//!
|
||||
//! object_pool!(P: [u8; 128]);
|
||||
//!
|
||||
//! const POOL_CAPACITY: usize = 8;
|
||||
//!
|
||||
//! let blocks: &'static mut [ObjectBlock<[u8; 128]>] = {
|
||||
//! const BLOCK: ObjectBlock<[u8; 128]> = ObjectBlock::new([0; 128]); // <=
|
||||
//! static mut BLOCKS: [ObjectBlock<[u8; 128]>; POOL_CAPACITY] = [BLOCK; POOL_CAPACITY];
|
||||
//! unsafe { &mut BLOCKS }
|
||||
//! };
|
||||
//!
|
||||
//! for block in blocks {
|
||||
//! P.manage(block);
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use core::{
|
||||
cmp::Ordering,
|
||||
|
Loading…
x
Reference in New Issue
Block a user