1
0
forked from ROMEO/nexosim

Changes after review.

This commit is contained in:
Jaŭhien Piatlicki 2025-01-28 11:08:17 +01:00
parent 8a1a6cf354
commit 7ff6f4c6c3
3 changed files with 24 additions and 21 deletions

View File

@ -71,7 +71,7 @@ pub struct Counter {
}
impl Counter {
/// Creates new `Counter` model.
/// Creates a new `Counter` model.
fn new() -> Self {
let mode = Output::default();
let count = Output::default();
@ -122,7 +122,7 @@ pub struct Detector {
}
impl Detector {
/// Creates new `Detector` model.
/// Creates a new `Detector` model.
pub fn new() -> Self {
Self {
pulse: Output::default(),
@ -140,7 +140,7 @@ impl Detector {
self.next = None;
}
/// Generates pulse.
/// Generates a pulse.
///
/// Note: self-scheduling async methods must be for now defined with an
/// 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>) {
let next = {
let mut rng = rand::thread_rng();
@ -204,7 +204,7 @@ fn main() -> Result<(), SimulationError> {
counter
.count
.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).
let t0 = MonotonicTime::EPOCH;

View File

@ -9,20 +9,21 @@ use nexosim::model::{Context, InitializedModel, 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 {
/// Tick period in milliseconds.
/// Tick period.
tick: Duration,
}
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 {
Self { tick }
}
/// Self-scheduled function.
pub async fn tick(&mut self) {}
async fn tick(&mut self) {}
}
impl Model for Ticker {

View File

@ -8,9 +8,11 @@ use super::{EventSink, EventSinkStream, EventSinkWriter};
/// A blocking event queue with an unbounded size.
///
/// Implements [`EventSink`], while [`EventSinkStream`] is implemented by
/// [`BlockingEventQueueReader`] available through the
/// [`BlockingEventQueue::reader`] method.
/// Implements [`EventSink`].
///
/// Note that [`EventSinkStream`] is implemented by
/// [`BlockingEventQueueReader`], created with the
/// [`BlockingEventQueue::into_reader`] method.
pub struct BlockingEventQueue<T> {
is_open: Arc<AtomicBool>,
sender: Sender<T>,
@ -20,24 +22,24 @@ pub struct BlockingEventQueue<T> {
impl<T> BlockingEventQueue<T> {
/// Creates an open `BlockingEventQueue`.
pub fn new() -> Self {
Self::new_with_open_state(true)
Self::new_with_state(true)
}
/// Creates a closed `BlockingEventQueue`.
pub fn new_closed() -> Self {
Self::new_with_open_state(false)
Self::new_with_state(false)
}
/// Returns consumer handle.
pub fn reader(self) -> BlockingEventQueueReader<T> {
/// Returns a consumer handle.
pub fn into_reader(self) -> BlockingEventQueueReader<T> {
BlockingEventQueueReader {
is_open: self.is_open,
receiver: self.receiver,
}
}
/// Creates new `BlockingEventQueue` in a given state.
fn new_with_open_state(is_open: bool) -> Self {
/// Creates a new `BlockingEventQueue` in the specified state.
fn new_with_state(is_open: bool) -> Self {
let (sender, receiver) = channel();
Self {
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
/// blocking. `None` is returned when simulation is closed.
/// Implements [`EventSinkStream`]. Calls to the iterator's `next` method are
/// blocking. `None` is returned when all writer handles have been dropped.
pub struct BlockingEventQueueReader<T> {
is_open: Arc<AtomicBool>,
receiver: Receiver<T>,