pub struct DataBuilder { /* private fields */ }Expand description
Configurable Data encoder with optional signing.
let wire = DataBuilder::new("/test", b"hello")
.freshness(Duration::from_secs(10))
.build();Implementations§
Source§impl DataBuilder
impl DataBuilder
pub fn new(name: impl Into<Name>, content: &[u8]) -> DataBuilder
pub fn freshness(self, d: Duration) -> DataBuilder
Sourcepub fn final_block_id(self, component_bytes: Bytes) -> DataBuilder
pub fn final_block_id(self, component_bytes: Bytes) -> DataBuilder
Set the FinalBlockId from a raw NameComponent TLV value.
Sourcepub fn final_block_id_seg(self, last_seg: usize) -> DataBuilder
pub fn final_block_id_seg(self, last_seg: usize) -> DataBuilder
Encode the last segment index as a GenericNameComponent and set as FinalBlockId.
This matches the ASCII-string segment encoding used by ndn-put and ndn-peek.
let wire = DataBuilder::new("/test/0", b"hello")
.final_block_id_seg(5) // segments 0..=5
.build();Sourcepub fn final_block_id_typed_seg(self, last_seg: u64) -> DataBuilder
pub fn final_block_id_typed_seg(self, last_seg: u64) -> DataBuilder
Encode the last segment index as a SegmentNameComponent (TLV type 0x32, big-endian non-negative integer encoding) and set as FinalBlockId.
This matches the segment encoding used by ndn-cxx’s ndnputchunks.
Use DataBuilder::final_block_id_seg for ASCII-decimal encoding instead.
Sourcepub fn sign_digest_sha256(self) -> Bytes
pub fn sign_digest_sha256(self) -> Bytes
Build and sign the Data with DigestSha256.
Single-buffer fast path: pre-computes all TLV sizes, allocates one
BytesMut, writes Name + MetaInfo + Content + SignatureInfo directly,
then hashes in-place — 1 allocation, 0 copies of the signed region.
~6× fewer allocations than sign_sync with a lambda; use this for
all high-throughput DigestSha256 production.
§Limitations
- Requires the
stdfeature (transitively requiresring).no_stdcallers must usebuild()+ out-of-band signing. - The hardcoded
SignatureInfocontains no KeyLocator. DigestSha256 packets that carry a KeyLocator must be built viasign_sync/sign. debug_assertguards validate the size pre-computation but are elided in release builds. The math is deterministic once the name/content are fixed; no runtime variability can trigger them in correct code.
Sourcepub fn sign_digest_blake3(self) -> Bytes
pub fn sign_digest_blake3(self) -> Bytes
Build a Data packet signed with a BLAKE3 digest (experimental).
Single-buffer fast path with 1 allocation, 0 copies of the signed region,
identical structure to sign_digest_sha256 but using BLAKE3.
Produces the same 32-byte output as SHA-256, so encoded packet size is identical.
§Performance characteristics
BLAKE3 uses SIMD (NEON on ARM, AVX2/AVX-512 on x86) and tree parallelism for
large inputs. However, for the small per-packet payloads typical of NDN iperf
(< a few KB), tree parallelism never activates. On hardware with dedicated SHA
accelerators — ARM crypto extensions (Apple Silicon, Cortex-A) or Intel SHA-NI —
ring’s SHA-256 implementation uses single-cycle hardware instructions and will
match or beat BLAKE3 at these payload sizes. BLAKE3’s advantage over SHA-256
is most visible on hardware without such extensions (older x86, RISC-V, embedded).
§Note
Uses signature type code 6 (DigestBlake3), which is an experimental
NDA extension not yet in the NDN Packet Format specification.
Sourcepub fn sign_none(self) -> Bytes
pub fn sign_none(self) -> Bytes
Build a Data packet with no signature fields (no SignatureInfo, no SignatureValue).
Single-buffer fast path with 1 allocation, 0 crypto overhead.
§⚠ Non-conformant NDN
All conformant NDN Data packets must carry a signature. Packets produced
by this method will be rejected by validators unless validation is
explicitly bypassed (e.g., FlowSignMode::None in iperf for benchmarking).
Do not use in production data planes.
Sourcepub async fn sign<F, Fut>(
self,
sig_type: SignatureType,
key_locator: Option<&Name>,
sign_fn: F,
) -> Bytes
pub async fn sign<F, Fut>( self, sig_type: SignatureType, key_locator: Option<&Name>, sign_fn: F, ) -> Bytes
Encode and sign the Data packet.
sig_type and key_locator describe the signature algorithm and
optional KeyLocator name (for SignatureInfo). sign_fn receives the
signed region (Name + MetaInfo + Content + SignatureInfo) and returns
the raw signature value bytes.
Sourcepub fn sign_sync<F>(
self,
sig_type: SignatureType,
key_locator: Option<&Name>,
sign_fn: F,
) -> Bytes
pub fn sign_sync<F>( self, sig_type: SignatureType, key_locator: Option<&Name>, sign_fn: F, ) -> Bytes
Synchronous encode-and-sign using a single pre-sized buffer.
Avoids the three intermediate allocations of the async sign() path.
sign_fn receives the signed region (Name + MetaInfo + Content +
SignatureInfo) and must return the raw signature bytes.