Skip to content

Commit 6e8e26c

Browse files
committed
add grow_exact + docs
1 parent adee3a0 commit 6e8e26c

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/pool/singleton/arc.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@
4747
//! // ..
4848
//! }
4949
//! ```
50+
//!
51+
//! The `grow_exact` API is also available on the "Arc pool". It requires using
52+
//! `Node<ArcInner<Type>>` as the array element type. Example below:
53+
//!
54+
//! ``` ignore
55+
//! use heapless::pool::{singleton::arc::ArcInner, Node};
56+
//!
57+
//! pub struct BigStruct { /* .. */ }
58+
//!
59+
//! arc_pool!(P: BigStruct);
60+
//!
61+
//! #[cortex_m_rt::entry]
62+
//! fn main() -> ! {
63+
//! static mut MEMORY: MaybeUninit<[Node<ArcInner<BigStruct>>; 2]> = MaybeUninit::uninit();
64+
//!
65+
//! P::grow_exact(MEMORY);
66+
//!
67+
//! // 2 allocations are guaranteed to work
68+
//! let x = P::alloc(BigStruct::new()).ok().expect("OOM");
69+
//! let y = P::alloc(BigStruct::new()).ok().expect("OOM");
70+
//!
71+
//! // ..
72+
//! }
73+
//! ```
5074
5175
use core::{
5276
cmp, fmt,
@@ -114,6 +138,16 @@ macro_rules! arc_pool {
114138
pub fn grow(memory: &'static mut [u8]) -> usize {
115139
<Self as $crate::pool::singleton::arc::Pool>::ptr().grow(memory)
116140
}
141+
142+
/// Increases the capacity of the pool
143+
///
144+
/// Unlike `grow`, this method fully utilizes the given memory block
145+
pub fn grow_exact<A>(memory: &'static mut MaybeUninit<A>) -> usize
146+
where
147+
A: AsMut<[$crate::pool::Node<$crate::pool::singleton::arc::ArcInner<$ty>>]>,
148+
{
149+
<Self as $crate::pool::singleton::arc::Pool>::ptr().grow_exact(memory)
150+
}
117151
}
118152
};
119153
}
@@ -348,7 +382,7 @@ where
348382

349383
impl<P> Unpin for Arc<P> where P: Pool {}
350384

351-
#[doc(hidden)]
385+
/// Unfortunate implementation detail required to use the `grow_exact` API
352386
pub struct ArcInner<T> {
353387
data: T,
354388
strong: AtomicUsize,

0 commit comments

Comments
 (0)