Skip to main content

ChunkTypeSet

Trait ChunkTypeSet 

Source
pub trait ChunkTypeSet<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>:
    Send
    + Sync
    + 'static {
    const BODY_SIZE: usize = BODY_SIZE;

    // Required methods
    fn supports(type_id: ChunkTypeId) -> bool;
    fn deserialize(bytes: &[u8]) -> Result<AnyChunk<BODY_SIZE>>;
    fn supported_types() -> &'static [ChunkTypeId];

    // Provided method
    fn format_supported_types() -> String { ... }
}
Expand description

Trait defining a set of supported chunk types with configurable body size.

This trait is implemented by marker types that define which chunk types a system supports. It enables compile-time configuration of valid chunk types while providing runtime polymorphism through AnyChunk.

§Design Rationale

This trait uses associated functions (not methods) because the supported types are determined at compile time, not per-instance. This allows:

  • Zero-cost type checking at compile time
  • Generic programming over chunk type sets
  • Runtime dispatch only when necessary (deserialization)

§Example

use nectar_primitives::{ChunkTypeSet, ChunkTypeId, AnyChunk, StandardChunkSet};

// Check if a type is supported
assert!(StandardChunkSet::supports(ChunkTypeId::CONTENT));
assert!(StandardChunkSet::supports(ChunkTypeId::SINGLE_OWNER));
assert!(!StandardChunkSet::supports(ChunkTypeId::custom(200)));

// Get supported types
let types = StandardChunkSet::supported_types();
assert_eq!(types.len(), 2);

Provided Associated Constants§

Source

const BODY_SIZE: usize = BODY_SIZE

The chunk body size in bytes for this set.

This is exposed as an associated const so consumers can access the body size at compile time through the type system.

Required Methods§

Source

fn supports(type_id: ChunkTypeId) -> bool

Check if a chunk type ID is supported by this set.

Returns true if chunks with the given type ID can be deserialized and processed by this set.

Source

fn deserialize(bytes: &[u8]) -> Result<AnyChunk<BODY_SIZE>>

Deserialize bytes into the appropriate chunk type.

The first byte of the input should be the chunk type ID. Returns an error if the type is not supported or deserialization fails.

§Errors

Returns ChunkError::UnsupportedType if the type ID is not in this set. May return other errors from the underlying chunk deserialization.

Source

fn supported_types() -> &'static [ChunkTypeId]

Get the list of all supported type IDs.

This returns a static slice for efficiency in const contexts.

Provided Methods§

Source

fn format_supported_types() -> String

Format the supported chunk types as a human-readable string.

Returns a comma-separated list with abbreviations and hex codes, e.g., “CAC (0x00), SOC (0x01)”.

§Example
use nectar_primitives::{ChunkTypeSet, StandardChunkSet, DEFAULT_BODY_SIZE};

let formatted = <StandardChunkSet as ChunkTypeSet<DEFAULT_BODY_SIZE>>::format_supported_types();
assert!(formatted.contains("CAC"));
assert!(formatted.contains("SOC"));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<const BODY_SIZE: usize> ChunkTypeSet<BODY_SIZE> for ContentOnlyChunkSet

Source§

impl<const BODY_SIZE: usize> ChunkTypeSet<BODY_SIZE> for StandardChunkSet