//! TTL 记录定长时间戳编解码(8 字节大端 u64 绝对毫秒时间戳)
/// TTL 载荷定长字节数(8 字节大端 u64 绝对毫秒时间戳,无需额外容器包装)
pub const TTL_VAL_LEN: usize = 8;
/// TTL 记录载荷快速编解码器
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TtlCodec;
impl TtlCodec {
/// 将绝对毫秒时间戳编码为 8 字节大端字节数组 (const fn, 零堆分配)
#[inline(always)]
pub const fn encode(expire_at_ms: u64) -> [u8; TTL_VAL_LEN] {
expire_at_ms.to_be_bytes()
}
/// 从字节切片中解码绝对毫秒时间戳 (const fn)
///
/// 若切片长度不足 8 字节返回 None
#[inline(always)]
pub const fn decode(bytes: &[u8]) -> Option<u64> {
if let Some((arr, _)) = bytes.split_first_chunk::<TTL_VAL_LEN>() {
Some(u64::from_be_bytes(*arr))
} else {
None
}
}
}