pub struct Shared(/* private fields */);Available on crate features
alloc or std only.Expand description
A strategy that preserves heap allocations for fast, zero-copy conversions with bytes::Bytes.
§Overview
The Shared strategy prioritizes fast conversions and allocation sharing with
bytes::Bytes. When data is heap-allocated, it remains on the heap even after operations
like advance(), truncate(), or split_to/off() that reduce the size below the inline
capacity.
This makes Shared ideal when:
- You frequently convert between
BytesandBytes - You want to share heap allocations (cheap clones via reference counting)
- Performance is more important than memory overhead
§Behavior
- Inline → Inline: Inline source slices stay inline
- Heap → Heap: Every non-empty heap source slice stays shared and heap-allocated, even at or below the inline capacity; empty slices use the canonical empty value
- Conversions: Zero-cost
From/IntowithByteswhen heap-allocated
§Example
use smol_bytes::shared::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
// Still heap-allocated! (Shared strategy preserves heap)
assert!(data.is_heap());
// Fast, zero-copy conversion to Bytes
let bytes: bytes::Bytes = data.into();
assert_eq!(bytes.len(), 30);§Comparison with Compact
| Operation | Shared | Compact |
|---|---|---|
| Heap→Inline on shrink | ❌ No | ✅ Yes |
| Bytes conversion | ⚡ Zero-copy | 📋 May copy |
| Memory usage | 💾 Higher | 💾 Lower |
| Best for | Speed, Bytes interop | Memory efficiency |
Trait Implementations§
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
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Auto Trait Implementations§
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