nectar_primitives/chunk/chunk_type.rs
1//! Chunk type trait
2//!
3//! This module provides the [`ChunkType`] trait which adds compile-time type
4//! information to chunk implementations.
5
6use super::traits::Chunk;
7use super::type_id::ChunkTypeId;
8
9/// Trait for chunk types with compile-time type information.
10///
11/// This trait extends [`Chunk`] with static type metadata, enabling:
12/// - Compile-time type identification via [`TYPE_ID`](ChunkType::TYPE_ID)
13/// - Type-safe serialization/deserialization
14/// - Generic programming over chunk types
15///
16/// # Implementing ChunkType
17///
18/// All implementations must also implement:
19/// - [`Chunk`] trait
20/// - [`TryFrom<Bytes>`] for deserialization
21/// - [`Into<Bytes>`] for serialization
22///
23/// # Example
24///
25/// ```ignore
26/// use nectar_primitives::{Chunk, ChunkType, ChunkTypeId};
27///
28/// struct MyCustomChunk { /* ... */ }
29///
30/// impl ChunkType for MyCustomChunk {
31/// const TYPE_ID: ChunkTypeId = ChunkTypeId::custom(200);
32/// const TYPE_NAME: &'static str = "my_custom";
33/// }
34/// ```
35pub trait ChunkType: Chunk + Sized {
36 /// The wire-level type identifier for this chunk type.
37 ///
38 /// This ID is used in chunk headers for serialization and must be unique
39 /// across all chunk types in a system.
40 const TYPE_ID: ChunkTypeId;
41
42 /// Human-readable name for this chunk type.
43 ///
44 /// Used for logging, debugging, and error messages.
45 const TYPE_NAME: &'static str;
46}