pub struct Compact(/* private fields */);Available on crate features
alloc or std only.Expand description
A strategy that aggressively inlines data to minimize heap allocations and memory usage.
§Overview
The Compact strategy prioritizes memory efficiency over conversion speed. It
automatically converts heap-allocated data back to inline storage whenever possible
(when data size ≤62 bytes) after operations like advance(), truncate(), or
split_to/off().
This makes Compact ideal when:
- Memory footprint is critical
- You want to minimize heap allocations
- You’re working with small, frequently-modified buffers
- Conversions to
Bytesare rare
§Behavior
- Inline → Inline: Small data stays inline (≤62 bytes)
- Heap → Inline: Automatically inlines when data shrinks ≤62 bytes
- Smart optimization: Operations like
advance(),truncate(), andsplit_to/off()can trigger heap→inline conversion
§Example
use smol_bytes::compact::Bytes;
use bytes::Buf;
// Create heap-allocated bytes (>62 bytes)
let mut data = Bytes::from(vec![1u8; 100]);
assert!(data.is_heap());
// Advance past most data
data.advance(70); // Only 30 bytes remain
// Automatically converted to inline! (Compact strategy saves memory)
assert!(!data.is_heap());§Comparison with Shared
| Operation | Compact | Shared |
|---|---|---|
| Heap→Inline on shrink | ✅ Yes | ❌ No |
| Bytes conversion | 📋 May copy | ⚡ Zero-copy |
| Memory usage | 💾 Lower | 💾 Higher |
| Best for | Memory efficiency | Speed, Bytes interop |
Trait Implementations§
impl Copy for Compact
impl Eq for Compact
Source§impl Ord for Compact
impl Ord for Compact
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialOrd for Compact
impl PartialOrd for Compact
impl StructuralPartialEq for Compact
Auto Trait Implementations§
impl Freeze for Compact
impl RefUnwindSafe for Compact
impl Send for Compact
impl Sync for Compact
impl Unpin for Compact
impl UnsafeUnpin for Compact
impl UnwindSafe for Compact
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more