Created multiplexing publish / subscribe example.
This commit is contained in:
@ -6,13 +6,14 @@ use core::cell::Cell;
|
||||
pub struct CellView<'a, T: Copy>(&'a Cell<T>);
|
||||
|
||||
impl<T: Copy> CellView<'_, T> {
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn get(self) -> T {
|
||||
self.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Copy> From<&'a Cell<T>> for CellView<'a, T> {
|
||||
#[inline(always)]
|
||||
fn from(value: &'a Cell<T>) -> Self {
|
||||
CellView(value)
|
||||
}
|
||||
|
0
src/transducer/conversion/mod.rs
Normal file
0
src/transducer/conversion/mod.rs
Normal file
@ -1,3 +1,4 @@
|
||||
use core::cell::Cell;
|
||||
use crate::cell::CellView;
|
||||
use crate::transducer::Stateful;
|
||||
|
||||
@ -5,19 +6,31 @@ pub trait Poll<T: Copy> {
|
||||
async fn poll() -> T;
|
||||
}
|
||||
|
||||
pub struct StatefulInput<'a, T: Copy> {
|
||||
pub state_cell: CellView<'a, T>,
|
||||
pub struct RawStatefulInput<T: Copy> {
|
||||
pub state_cell: Cell<T>,
|
||||
}
|
||||
|
||||
impl<'a, T: Copy> Stateful for StatefulInput<'a, T> {
|
||||
impl<T: Copy> RawStatefulInput<T> {
|
||||
#[inline(always)]
|
||||
pub fn new(state_cell: Cell<T>) -> Self {
|
||||
Self { state_cell }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn raw_update(&self, value: T) {
|
||||
self.state_cell.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Stateful for RawStatefulInput<T> {
|
||||
type Value = T;
|
||||
|
||||
#[inline(always)]
|
||||
fn state_cell(&self) -> CellView<'a, Self::Value> {
|
||||
self.state_cell
|
||||
fn state_cell(&self) -> CellView<Self::Value> {
|
||||
(&self.state_cell).into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
fn state(&self) -> Self::Value {
|
||||
self.state_cell.get()
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ use crate::cell::CellView;
|
||||
|
||||
pub mod input;
|
||||
pub mod output;
|
||||
mod conversion;
|
||||
|
||||
// Initialisation will always be async and won't complete until a state is available for all
|
||||
// stateful transducers.
|
||||
@ -10,5 +11,8 @@ pub trait Stateful {
|
||||
|
||||
fn state_cell(&self) -> CellView<Self::Value>;
|
||||
|
||||
fn state(&self) -> Self::Value;
|
||||
#[inline(always)]
|
||||
fn state(&self) -> Self::Value {
|
||||
self.state_cell().get()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user