pub enum AnyChunk<const BODY_SIZE: usize = DEFAULT_BODY_SIZE> {
Content(ContentChunk<BODY_SIZE>),
SingleOwner(SingleOwnerChunk<BODY_SIZE>),
Custom {
type_id: ChunkTypeId,
address: ChunkAddress,
data: Bytes,
},
}Expand description
Type-erased chunk for runtime polymorphism with configurable body size.
This enum provides dynamic dispatch for chunks without requiring object-safe traits. Use this when you need to store heterogeneous chunk types in collections or pass chunks through interfaces that can’t be generic.
§Why an enum instead of Box<dyn Chunk>?
The Chunk trait has an associated type (type Header) which makes it not
object-safe. This enum provides the same functionality while maintaining type safety.
§Examples
use nectar_primitives::{AnyChunk, Chunk, ContentChunk, ChunkTypeId};
// Create a content chunk
let content = ContentChunk::new(&b"hello world"[..]).unwrap();
let any: AnyChunk = content.clone().into();
// Access common properties
assert_eq!(any.type_id(), ChunkTypeId::CONTENT);
// Get the concrete type back
if let Some(recovered) = any.as_content() {
assert_eq!(recovered.address(), content.address());
}Variants§
Content(ContentChunk<BODY_SIZE>)
A content-addressed chunk (CAC).
SingleOwner(SingleOwnerChunk<BODY_SIZE>)
A single-owner chunk (SOC).
Custom
A custom chunk type (for extensibility).
This variant allows storing chunks of types not known at compile time. The raw bytes are preserved for potential later processing.
Implementations§
Source§impl<const BODY_SIZE: usize> AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> AnyChunk<BODY_SIZE>
Sourcepub fn address(&self) -> &ChunkAddress
pub fn address(&self) -> &ChunkAddress
Get the address of this chunk.
Sourcepub const fn type_id(&self) -> ChunkTypeId
pub const fn type_id(&self) -> ChunkTypeId
Get the type ID of this chunk.
Sourcepub fn span(&self) -> u64
pub fn span(&self) -> u64
Get the span (logical data length) of this chunk.
For content chunks and single-owner chunks, this returns the BMT span. For custom chunks, the span is not available (returns 0).
Sourcepub fn verify(&self, expected: &ChunkAddress) -> Result<()>
pub fn verify(&self, expected: &ChunkAddress) -> Result<()>
Verify that this chunk’s address matches an expected address.
Sourcepub fn into_bytes(self) -> Bytes
pub fn into_bytes(self) -> Bytes
Convert this chunk into its serialized bytes representation.
Sourcepub const fn is_content(&self) -> bool
pub const fn is_content(&self) -> bool
Check if this is a content chunk.
Sourcepub const fn is_single_owner(&self) -> bool
pub const fn is_single_owner(&self) -> bool
Check if this is a single-owner chunk.
Sourcepub const fn as_content(&self) -> Option<&ContentChunk<BODY_SIZE>>
pub const fn as_content(&self) -> Option<&ContentChunk<BODY_SIZE>>
Get a reference to the contained ContentChunk, if this is one.
Sourcepub const fn as_single_owner(&self) -> Option<&SingleOwnerChunk<BODY_SIZE>>
pub const fn as_single_owner(&self) -> Option<&SingleOwnerChunk<BODY_SIZE>>
Get a reference to the contained SingleOwnerChunk, if this is one.
Sourcepub fn into_content(self) -> Option<ContentChunk<BODY_SIZE>>
pub fn into_content(self) -> Option<ContentChunk<BODY_SIZE>>
Convert into the contained ContentChunk, if this is one.
Sourcepub fn into_single_owner(self) -> Option<SingleOwnerChunk<BODY_SIZE>>
pub fn into_single_owner(self) -> Option<SingleOwnerChunk<BODY_SIZE>>
Convert into the contained SingleOwnerChunk, if this is one.
Trait Implementations§
impl<const BODY_SIZE: usize> Eq for AnyChunk<BODY_SIZE>
Source§impl<const BODY_SIZE: usize> From<ContentChunk<BODY_SIZE>> for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> From<ContentChunk<BODY_SIZE>> for AnyChunk<BODY_SIZE>
Source§fn from(chunk: ContentChunk<BODY_SIZE>) -> Self
fn from(chunk: ContentChunk<BODY_SIZE>) -> Self
Source§impl<const BODY_SIZE: usize> From<SingleOwnerChunk<BODY_SIZE>> for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> From<SingleOwnerChunk<BODY_SIZE>> for AnyChunk<BODY_SIZE>
Source§fn from(chunk: SingleOwnerChunk<BODY_SIZE>) -> Self
fn from(chunk: SingleOwnerChunk<BODY_SIZE>) -> Self
Auto Trait Implementations§
impl<const BODY_SIZE: usize = DEFAULT_BODY_SIZE> !Freeze for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> RefUnwindSafe for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> Send for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> Sync for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> Unpin for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> UnsafeUnpin for AnyChunk<BODY_SIZE>
impl<const BODY_SIZE: usize> UnwindSafe for AnyChunk<BODY_SIZE>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more