forked from ROMEO/nexosim
Changes after review.
This commit is contained in:
parent
8a1a6cf354
commit
7ff6f4c6c3
@ -71,7 +71,7 @@ pub struct Counter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Counter {
|
impl Counter {
|
||||||
/// Creates new `Counter` model.
|
/// Creates a new `Counter` model.
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
let mode = Output::default();
|
let mode = Output::default();
|
||||||
let count = Output::default();
|
let count = Output::default();
|
||||||
@ -122,7 +122,7 @@ pub struct Detector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Detector {
|
impl Detector {
|
||||||
/// Creates new `Detector` model.
|
/// Creates a new `Detector` model.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
pulse: Output::default(),
|
pulse: Output::default(),
|
||||||
@ -140,7 +140,7 @@ impl Detector {
|
|||||||
self.next = None;
|
self.next = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates pulse.
|
/// Generates a pulse.
|
||||||
///
|
///
|
||||||
/// Note: self-scheduling async methods must be for now defined with an
|
/// Note: self-scheduling async methods must be for now defined with an
|
||||||
/// explicit signature instead of `async fn` due to a rustc issue.
|
/// explicit signature instead of `async fn` due to a rustc issue.
|
||||||
@ -155,7 +155,7 @@ impl Detector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Schedules next detection.
|
/// Schedules the next detection.
|
||||||
async fn schedule_next(&mut self, cx: &mut Context<Self>) {
|
async fn schedule_next(&mut self, cx: &mut Context<Self>) {
|
||||||
let next = {
|
let next = {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
@ -204,7 +204,7 @@ fn main() -> Result<(), SimulationError> {
|
|||||||
counter
|
counter
|
||||||
.count
|
.count
|
||||||
.map_connect_sink(|c| Event::Count(*c), &observer);
|
.map_connect_sink(|c| Event::Count(*c), &observer);
|
||||||
let mut observer = observer.reader();
|
let mut observer = observer.into_reader();
|
||||||
|
|
||||||
// Start time (arbitrary since models do not depend on absolute time).
|
// Start time (arbitrary since models do not depend on absolute time).
|
||||||
let t0 = MonotonicTime::EPOCH;
|
let t0 = MonotonicTime::EPOCH;
|
||||||
|
@ -9,20 +9,21 @@ use nexosim::model::{Context, InitializedModel, Model};
|
|||||||
|
|
||||||
/// A ticker model.
|
/// A ticker model.
|
||||||
///
|
///
|
||||||
/// This model self-schedules with a provided period keeping simulation alive.
|
/// This model self-schedules at the specified period, which can be used to keep
|
||||||
|
/// the simulation alive.
|
||||||
pub struct Ticker {
|
pub struct Ticker {
|
||||||
/// Tick period in milliseconds.
|
/// Tick period.
|
||||||
tick: Duration,
|
tick: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ticker {
|
impl Ticker {
|
||||||
/// Creates new `Ticker` with provided tick period in milliseconds.
|
/// Creates a new `Ticker` with the specified self-scheduling period.
|
||||||
pub fn new(tick: Duration) -> Self {
|
pub fn new(tick: Duration) -> Self {
|
||||||
Self { tick }
|
Self { tick }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Self-scheduled function.
|
/// Self-scheduled function.
|
||||||
pub async fn tick(&mut self) {}
|
async fn tick(&mut self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Model for Ticker {
|
impl Model for Ticker {
|
||||||
|
@ -8,9 +8,11 @@ use super::{EventSink, EventSinkStream, EventSinkWriter};
|
|||||||
|
|
||||||
/// A blocking event queue with an unbounded size.
|
/// A blocking event queue with an unbounded size.
|
||||||
///
|
///
|
||||||
/// Implements [`EventSink`], while [`EventSinkStream`] is implemented by
|
/// Implements [`EventSink`].
|
||||||
/// [`BlockingEventQueueReader`] available through the
|
///
|
||||||
/// [`BlockingEventQueue::reader`] method.
|
/// Note that [`EventSinkStream`] is implemented by
|
||||||
|
/// [`BlockingEventQueueReader`], created with the
|
||||||
|
/// [`BlockingEventQueue::into_reader`] method.
|
||||||
pub struct BlockingEventQueue<T> {
|
pub struct BlockingEventQueue<T> {
|
||||||
is_open: Arc<AtomicBool>,
|
is_open: Arc<AtomicBool>,
|
||||||
sender: Sender<T>,
|
sender: Sender<T>,
|
||||||
@ -20,24 +22,24 @@ pub struct BlockingEventQueue<T> {
|
|||||||
impl<T> BlockingEventQueue<T> {
|
impl<T> BlockingEventQueue<T> {
|
||||||
/// Creates an open `BlockingEventQueue`.
|
/// Creates an open `BlockingEventQueue`.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::new_with_open_state(true)
|
Self::new_with_state(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a closed `BlockingEventQueue`.
|
/// Creates a closed `BlockingEventQueue`.
|
||||||
pub fn new_closed() -> Self {
|
pub fn new_closed() -> Self {
|
||||||
Self::new_with_open_state(false)
|
Self::new_with_state(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns consumer handle.
|
/// Returns a consumer handle.
|
||||||
pub fn reader(self) -> BlockingEventQueueReader<T> {
|
pub fn into_reader(self) -> BlockingEventQueueReader<T> {
|
||||||
BlockingEventQueueReader {
|
BlockingEventQueueReader {
|
||||||
is_open: self.is_open,
|
is_open: self.is_open,
|
||||||
receiver: self.receiver,
|
receiver: self.receiver,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates new `BlockingEventQueue` in a given state.
|
/// Creates a new `BlockingEventQueue` in the specified state.
|
||||||
fn new_with_open_state(is_open: bool) -> Self {
|
fn new_with_state(is_open: bool) -> Self {
|
||||||
let (sender, receiver) = channel();
|
let (sender, receiver) = channel();
|
||||||
Self {
|
Self {
|
||||||
is_open: Arc::new(AtomicBool::new(is_open)),
|
is_open: Arc::new(AtomicBool::new(is_open)),
|
||||||
@ -70,10 +72,10 @@ impl<T> fmt::Debug for BlockingEventQueue<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A consumer handle to blocking event queue.
|
/// A consumer handle of a `BlockingEventQueue`.
|
||||||
///
|
///
|
||||||
/// Implements [`EventSinkStream`]. Call to iterator's `next` function is
|
/// Implements [`EventSinkStream`]. Calls to the iterator's `next` method are
|
||||||
/// blocking. `None` is returned when simulation is closed.
|
/// blocking. `None` is returned when all writer handles have been dropped.
|
||||||
pub struct BlockingEventQueueReader<T> {
|
pub struct BlockingEventQueueReader<T> {
|
||||||
is_open: Arc<AtomicBool>,
|
is_open: Arc<AtomicBool>,
|
||||||
receiver: Receiver<T>,
|
receiver: Receiver<T>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user