FakeNetwork

Struct FakeNetwork 

Source
pub struct FakeNetwork<Spec: FakeNetworkSpec, CtxId, Links> { /* private fields */ }
Expand description

A fake network, composed of many FakeCoreCtxs.

Provides a utility to have many contexts keyed by CtxId that can exchange frames.

Implementations§

Source§

impl<Spec, CtxId, Links> FakeNetwork<Spec, CtxId, Links>
where CtxId: Eq + Hash, Spec: FakeNetworkSpec,

Source

pub fn context<K: Into<CtxId>>(&mut self, context: K) -> &mut Spec::Context

Retrieves a context named context.

Source

pub fn with_context<K: Into<CtxId>, O, F: FnOnce(&mut Spec::Context) -> O>( &mut self, context: K, f: F, ) -> O

Calls f with a mutable reference to the context named context.

Source§

impl<Spec, CtxId, Links> FakeNetwork<Spec, CtxId, Links>
where Spec: FakeNetworkSpec, CtxId: Eq + Hash + Copy + Debug, Links: FakeNetworkLinks<Spec::SendMeta, Spec::RecvMeta, CtxId>,

Source

pub fn new<I: IntoIterator<Item = (CtxId, Spec::Context)>>( contexts: I, links: Links, ) -> Self

Creates a new FakeNetwork.

Creates a new FakeNetwork with the collection of FakeCoreCtxs in contexts. Ctxs are named by type parameter CtxId.

§Panics

Calls to new will panic if given a FakeCoreCtx with timer events. FakeCoreCtxs given to FakeNetwork must not have any timer events already attached to them, because FakeNetwork maintains all the internal timers in dispatchers in sync to enable synchronous simulation steps.

Source

pub fn iter_pending_frames( &self, ) -> impl Iterator<Item = &PendingFrame<CtxId, Spec::RecvMeta>>

Iterates over pending frames in an arbitrary order.

Source

pub fn assert_no_pending_frames(&self)
where Spec::RecvMeta: Debug,

Asserts no pending frames exist.

Source

pub fn drop_pending_frames(&mut self)

Drops all pending frames; they will not be delivered.

Source

pub fn step(&mut self) -> StepResult
where Spec::TimerId: Debug,

Performs a single step in network simulation.

step performs a single logical step in the collection of Ctxs held by this FakeNetwork. A single step consists of the following operations:

  • All pending frames, kept in each FakeCoreCtx, are mapped to their destination context/device pairs and moved to an internal collection of pending frames.
  • The collection of pending timers and scheduled frames is inspected and a simulation time step is retrieved, which will cause a next event to trigger. The simulation time is updated to the new time.
  • All scheduled frames whose deadline is less than or equal to the new simulation time are sent to their destinations, handled using handle_frame.
  • All timer events whose deadline is less than or equal to the new simulation time are fired, handled using handle_timer.

If any new events are created during the operation of frames or timers, they will not be taken into account in the current step. That is, step collects all the pending events before dispatching them, ensuring that an infinite loop can’t be created as a side effect of calling step.

The return value of step indicates which of the operations were performed.

§Panics

If FakeNetwork was set up with a bad links, calls to step may panic when trying to route frames to their context/device destinations.

Source

pub fn step_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, ) -> StepResult
where Spec::TimerId: Debug,

Like FakeNetwork::step, but receives a function filter_map_frame that can modify the an inbound frame before delivery or drop it altogether by returning None.

Source

pub fn step_deliver_frames(&mut self) -> StepResult
where Spec::TimerId: Debug,

Collects and dispatches all pending frames without advancing time or triggering any timers.

§Panics

Panics under the same conditions as [dispatch_pending_frames].

Source

pub fn step_deliver_frames_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, ) -> StepResult
where Spec::TimerId: Debug,

Like FakeNetwork::step_deliver_frames, but receives a function filter_map_frame that can modify frames, or drop them if it returns None.

Source

pub fn run_until_idle(&mut self)
where Spec::TimerId: Debug,

Runs the network until it is starved of events.

§Panics

Panics if 1,000,000 steps are performed without becoming idle. Also panics under the same conditions as [step].

Source

pub fn run_until_idle_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, )
where Spec::TimerId: Debug,

Like FakeNetwork::run_until_idle but receives a function filter_map_frame that can modify the an inbound frame before delivery or drop it altogether by returning None.

Source

pub fn collect_frames(&mut self)

Collects all queued frames.

Collects all pending frames and schedules them for delivery to the destination context/device based on the result of links. The collected frames are queued for dispatching in the FakeNetwork, ordered by their scheduled delivery time given by the latency result provided by links.

Source

pub fn dispatch_pending_frames<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, ) -> usize

Dispatches scheduled frames that were previously collected with [collect_frames].

Only frames for which the current deadline is less than or equal to the current simulation time are delivered.

§Panics

If FakeNetwork was set up with a bad links, calls may panic when trying to route frames to their context/device destinations.

Source

pub fn next_step(&self) -> Option<FakeInstant>

Calculates the next FakeInstant when events are available.

Returns the smallest FakeInstant greater than or equal to the current time for which an event is available. If no events are available, returns None.

Auto Trait Implementations§

§

impl<Spec, CtxId, Links> Freeze for FakeNetwork<Spec, CtxId, Links>
where Links: Freeze,

§

impl<Spec, CtxId, Links> RefUnwindSafe for FakeNetwork<Spec, CtxId, Links>

§

impl<Spec, CtxId, Links> Send for FakeNetwork<Spec, CtxId, Links>
where Links: Send, CtxId: Send, <Spec as FakeNetworkSpec>::Context: Send, <Spec as FakeNetworkSpec>::RecvMeta: Send,

§

impl<Spec, CtxId, Links> Sync for FakeNetwork<Spec, CtxId, Links>
where Links: Sync, CtxId: Sync, <Spec as FakeNetworkSpec>::Context: Sync, <Spec as FakeNetworkSpec>::RecvMeta: Sync,

§

impl<Spec, CtxId, Links> Unpin for FakeNetwork<Spec, CtxId, Links>
where Links: Unpin, CtxId: Unpin, <Spec as FakeNetworkSpec>::Context: Unpin, <Spec as FakeNetworkSpec>::RecvMeta: Unpin,

§

impl<Spec, CtxId, Links> UnwindSafe for FakeNetwork<Spec, CtxId, Links>

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
§

impl<T, D> Encode<Ambiguous1, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
§

impl<T, D> Encode<Ambiguous2, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. 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.

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> 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<CC, BC, Meta> RecvFrameContext<Meta, BC> for CC
where Meta: ReceivableFrameMeta<CC, BC>,

Source§

fn receive_frame<B>(&mut self, bindings_ctx: &mut BC, metadata: Meta, frame: B)
where B: BufferMut + Debug,

Receive a frame. Read more
Source§

impl<CC, BC, Meta> SendFrameContext<BC, Meta> for CC
where Meta: SendableFrameMeta<CC, BC>,

Source§

fn send_frame<S>( &mut self, bindings_ctx: &mut BC, metadata: Meta, frame: S, ) -> Result<(), ErrorAndSerializer<SendFrameErrorReason, S>>

Send a frame. Read more
Source§

impl<BC, S, Meta, DeviceId> SendableFrameMeta<FakeCoreCtx<S, Meta, DeviceId>, BC> for Meta

Source§

fn send_meta<SS>( self, core_ctx: &mut FakeCoreCtx<S, Meta, DeviceId>, bindings_ctx: &mut BC, frame: SS, ) -> Result<(), ErrorAndSerializer<SendFrameErrorReason, SS>>
where SS: Serializer, <SS as Serializer>::Buffer: BufferMut,

Sends this frame metadata to the provided contexts.
Source§

impl<Meta, BC> SendableFrameMeta<FakeFrameCtx<Meta>, BC> for Meta

Source§

fn send_meta<S>( self, core_ctx: &mut FakeFrameCtx<Meta>, _bindings_ctx: &mut BC, frame: S, ) -> Result<(), ErrorAndSerializer<SendFrameErrorReason, S>>

Sends this frame metadata to the provided contexts.
Source§

impl<Id, CC, BC> TimerHandler<BC, Id> for CC
where BC: TimerBindingsTypes, Id: HandleableTimer<CC, BC>,

Source§

fn handle_timer( &mut self, bindings_ctx: &mut BC, dispatch: Id, timer: <BC as TimerBindingsTypes>::UniqueTimerId, )

Handle a timer firing. 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.
§

impl<L, T> UnlockedAccess<L> for T
where L: UnlockedAccessMarkerFor<T>,

§

type Data = <L as UnlockedAccessMarkerFor<T>>::Data

The type of state being accessed.
§

type Guard<'l> = &'l <L as UnlockedAccessMarkerFor<T>>::Data where T: 'l

A guard providing read access to the data.
§

fn access(&self) -> <T as UnlockedAccess<L>>::Guard<'_>

How to access the state.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<B, A> LockBefore<B> for A
where B: LockAfter<A>,