DataBuilder

Struct DataBuilder 

Source
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

Source

pub fn new(name: impl Into<Name>, content: &[u8]) -> Self

Source

pub fn freshness(self, d: Duration) -> Self

Source

pub fn final_block_id(self, component_bytes: Bytes) -> Self

Set the FinalBlockId from a raw NameComponent TLV value.

Source

pub fn final_block_id_seg(self, last_seg: usize) -> Self

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();
Source

pub fn final_block_id_typed_seg(self, last_seg: u64) -> Self

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.

Source

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 std feature (transitively requires ring). no_std callers must use build() + out-of-band signing.
  • The hardcoded SignatureInfo contains no KeyLocator. DigestSha256 packets that carry a KeyLocator must be built via sign_sync/sign.
  • debug_assert guards 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.
Source

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.

Source

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.

Source

pub fn build(self) -> Bytes

Build unsigned Data with a DigestSha256 placeholder signature.

Source

pub async fn sign<F, Fut>( self, sig_type: SignatureType, key_locator: Option<&Name>, sign_fn: F, ) -> Bytes
where F: FnOnce(&[u8]) -> Fut, Fut: Future<Output = 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.

Source

pub fn sign_sync<F>( self, sig_type: SignatureType, key_locator: Option<&Name>, sign_fn: F, ) -> Bytes
where F: FnOnce(&[u8]) -> 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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.