Skip to main content

Dfir

Struct Dfir 

Source
pub struct Dfir<Tick: TickClosure> { /* private fields */ }
Expand description

An executable DFIR dataflow, as created by dfir_syntax!. Provides the Self::run, Self::run_available, and Self::run_tick family of methods to execute the dataflow.

§Design

The inline codegen generates an async move |df: &mut Context<M>| closure that captures dataflow-specific state (handoff buffers, source iterators) and receives the Context (tick counter, metrics) by reference each tick. Dfir owns both the closure and the context, and coordinates tick lifecycle and idle/wake behavior.

We use a single opaque closure rather than generating a bespoke struct per dataflow because:

  • The closure naturally captures exactly the state it needs with correct lifetimes
  • No codegen needed for struct definitions, field accessors, or initialization
  • Rust’s async closure machinery handles the complex state machine (suspend/resume across .await points) that would be very difficult to replicate in a generated struct

The Tick type parameter is bounded by TickClosure (not AsyncFnMut directly) so that the runners can be split by what the closure supports (TickClosureSync / TickClosureAsync) and to support type erasure ([TickClosureErasedSync], [TickClosureErasedAsync], [TickClosureErased]) for storing graphs of different concrete types in one place (a struct field, or the sim runtime’s Vec of locations). The concrete (non-erased) path used by trybuild and embedded has zero overhead.

§Metrics

Whether this Dfir tracks runtime metrics is determined statically by TickClosure::Metrics (see MetricsState). The Self::metrics and Self::metrics_intervals accessors only exist when Tick::Metrics = DfirMetrics. When returning a Dfir from a function via impl TickClosureBoth, use Dfir<impl TickClosureBoth<Metrics = DfirMetrics>> if callers need to access the metrics.

Implementations§

Source§

impl<Tick: TickClosure> Dfir<Tick>

Source

pub fn meta_graph(&self) -> Option<DfirGraph>

Available on crate feature meta only.

Returns the meta graph, if set.

The graph is deserialized from its JSON representation on each call, so callers which need it multiple times should store the result.

Source

pub fn diagnostics(&self) -> Option<&[Diagnostic<SerdeSpan>]>

Available on crate feature meta only.

Returns any diagnostics generated by the surface syntax macro.

Source

pub fn current_tick(&self) -> TickInstant

Gets the current tick (local time) count.

Source§

impl<Tick: TickClosure<Metrics = DfirMetrics>> Dfir<Tick>

Metrics accessors, only available when metrics tracking is enabled (Tick::Metrics = DfirMetrics).

Source

pub fn metrics(&self) -> Arc<DfirMetrics> ⓘ

Returns a reference-counted handle to the continually-updated runtime metrics for this DFIR instance.

Only available when metrics tracking is enabled, see MetricsState.

Source

pub fn metrics_intervals(&self) -> DfirMetricsIntervals

Returns a DfirMetricsIntervals handle where each call to DfirMetricsIntervals::take_interval ends the current interval and returns its metrics.

The first call to take_interval returns metrics since this DFIR instance was created. Each subsequent call to take_interval returns metrics since the previous call.

Cloning the handle “forks” it from the original, as afterwards each interval may return different metrics depending on when exactly take_interval is called.

Only available when metrics tracking is enabled, see MetricsState.

Source§

impl<Tick: TickClosureAsync> Dfir<Tick>

The async runners. Available when the tick closure is TickClosureAsync.

Source

pub async fn run_tick(&mut self) -> bool

Run a single tick. Returns true if any subgraph received input data.

Checks both handoff buffers (via work_done flag set in generated recv port code) and external events (via can_start_tick set by waker/schedule_subgraph).

Source

pub async fn run_available(&mut self)

Available on crate feature tokio only.

Run ticks as long as work is available, then return.

Source

pub async fn run(&mut self) -> Never

Available on crate feature tokio only.

Run forever, processing ticks when work is available and yielding when idle.

Source§

impl<Tick: TickClosureSync> Dfir<Tick>

The sync runners. Available when the tick closure is TickClosureSync.

Source

pub fn run_tick_sync(&mut self) -> bool

Run a single tick synchronously. Panics if the tick yields (async suspension). Returns true if work was done (see Self::run_tick).

Goes through TickClosureSync::call_tick_sync, which stays allocation-free even for the type-erased DfirErasedSync / DfirErased.

Source

pub fn run_available_sync(&mut self)

Self::run_available but panics if any tick yields asynchronously.

Source§

impl<Tick: TickClosure + 'static> Dfir<Tick>

Type erasure. Each option keeps the MetricsState typestate and gives the graph a nameable type, so it can be stored in a struct field or a heterogeneous collection. They differ in which runners the result supports and, accordingly, in what its vtable carries; see TickClosure.

Source

pub fn into_erased_sync(self) -> DfirErasedSync<Tick::Metrics>
where Tick: TickClosureSync,

Type-erase for the sync runners only: the result is DfirErasedSync.

Allocation-free per tick. Use this to store a graph that is driven synchronously, e.g. from an embedding application’s own event loop.

Source

pub fn into_erased_async(self) -> DfirErasedAsync<Tick::Metrics>
where Tick: TickClosureAsync,

Type-erase for the async runners only: the result is DfirErasedAsync.

Boxes the tick’s future, one allocation per tick. Use this for graphs driven by Dfir::run_tick / Dfir::run_available / Dfir::run, e.g. the sim runtime.

Source

pub fn into_erased(self) -> DfirErased<Tick::Metrics>
where Tick: TickClosureBoth,

Type-erase keeping both the sync and the async runners: the result is DfirErased.

Prefer Self::into_erased_sync or Self::into_erased_async when only one kind of runner is used, so the other path is not instantiated.

Auto Trait Implementations§

§

impl<Tick> !RefUnwindSafe for Dfir<Tick>

§

impl<Tick> !Sync for Dfir<Tick>

§

impl<Tick> !UnwindSafe for Dfir<Tick>

§

impl<Tick> Forget for Dfir<Tick>

§

impl<Tick> Freeze for Dfir<Tick>
where Tick: Freeze, *const ArcInner<WakeState> is !null: Freeze, PhantomData<ArcInner<WakeState>>: Freeze, <<Tick as TickClosure>::Metrics as MetricsState>::Handle: Freeze, *const u8 is !null: Freeze, PhantomData<Pin<Box<dyn Future<Output = ()> + Send>>>: Freeze, PhantomData<u8>: Freeze, PhantomData<Diagnostic<SerdeSpan>>: Freeze,

§

impl<Tick> Move for Dfir<Tick>

§

impl<Tick> Send for Dfir<Tick>
where Tick: Send, Arc<WakeState>: Send, <<Tick as TickClosure>::Metrics as MetricsState>::Handle: Send, RawVecInner: Send, BoxRaw<dyn Future<Output = ()> + Send>: Send,

§

impl<Tick> Unpin for Dfir<Tick>
where Tick: Unpin, Arc<WakeState>: Unpin, <<Tick as TickClosure>::Metrics as MetricsState>::Handle: Unpin, *const u8 is !null: Unpin, Box<dyn Future<Output = ()> + Send>: Unpin,

§

impl<Tick> UnsafeUnpin for Dfir<Tick>
where Tick: UnsafeUnpin, *const ArcInner<WakeState> is !null: UnsafeUnpin, PhantomData<ArcInner<WakeState>>: UnsafeUnpin, <<Tick as TickClosure>::Metrics as MetricsState>::Handle: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<Pin<Box<dyn Future<Output = ()> + Send>>>: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, PhantomData<Diagnostic<SerdeSpan>>: UnsafeUnpin,

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> ToSinkBuild for T

§

fn iter_to_sink_build(self) -> SendIterBuild<Self>
where Self: Sized + Iterator,

Starts a [SinkBuild] adaptor chain to send all items from self as an Iterator.
§

fn stream_to_sink_build(self) -> SendStreamBuild<Self>
where Self: Sized + Stream,

Starts a [SinkBuild] adaptor chain to send all items from self as a [Stream].
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more