Skip to main content

cell_core/
vesicle.rs

1// cell-core/src/vesicle.rs
2// SPDX-License-Identifier: MIT
3
4use alloc::vec;
5use alloc::vec::Vec;
6
7/// The Universal Packet Header (24 Bytes).
8#[repr(C)]
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct VesicleHeader {
11    pub target_id: u64, // Blake3 Hash of target cell name
12    pub source_id: u64, // Blake3 Hash of sender cell name (for replies)
13    pub ttl: u8,        // Hops remaining
14    pub flags: u8,      // Reserved (0x01 = Fragment, 0x02 = Ack...)
15    pub _pad: [u8; 6],  // Alignment to 24 bytes
16}
17
18impl VesicleHeader {
19    pub const SIZE: usize = 24;
20}
21
22/// A wrapper around a data buffer.
23#[derive(Debug, Clone)]
24pub struct Vesicle {
25    data: Vec<u8>,
26}
27
28impl Vesicle {
29    pub fn wrap(data: Vec<u8>) -> Self {
30        Self { data }
31    }
32
33    pub fn with_capacity(capacity: usize) -> Self {
34        Self {
35            data: vec![0; capacity],
36        }
37    }
38
39    pub fn as_slice(&self) -> &[u8] {
40        &self.data
41    }
42
43    pub fn as_mut_slice(&mut self) -> &mut [u8] {
44        &mut self.data
45    }
46
47    pub fn len(&self) -> usize {
48        self.data.len()
49    }
50
51    pub fn is_empty(&self) -> bool {
52        self.data.is_empty()
53    }
54
55    pub fn into_inner(self) -> Vec<u8> {
56        self.data
57    }
58}