[−][src]Struct reducer::Store
A reactive state container.
The only way to mutate the internal state managed by Store
is by
dispatching actions on it.
The associated Reactor
is notified upon every state transition.
Example
use reducer::*; use std::error::Error; use std::io::{self, Write}; // The state of your app. struct Calculator(i32); // Actions the user can trigger. struct Add(i32); struct Sub(i32); struct Mul(i32); struct Div(i32); impl Reducer<Add> for Calculator { fn reduce(&mut self, Add(x): Add) { self.0 += x; } } impl Reducer<Sub> for Calculator { fn reduce(&mut self, Sub(x): Sub) { self.0 -= x; } } impl Reducer<Mul> for Calculator { fn reduce(&mut self, Mul(x): Mul) { self.0 *= x; } } impl Reducer<Div> for Calculator { fn reduce(&mut self, Div(x): Div) { self.0 /= x; } } // The user interface. struct Console; impl Reactor<Calculator> for Console { type Error = io::Error; fn react(&mut self, state: &Calculator) -> io::Result<()> { io::stdout().write_fmt(format_args!("{}\n", state.0)) } } fn main() -> Result<(), Box<dyn Error>> { let mut store = Store::new(Calculator(0), Console); store.dispatch(Add(5))?; // displays "5" store.dispatch(Mul(3))?; // displays "15" store.dispatch(Sub(1))?; // displays "14" store.dispatch(Div(7))?; // displays "2" Ok(()) }
Methods
impl<S, R: Reactor<S>> Store<S, R>
[src]
pub fn new(state: S, reactor: R) -> Self
[src]
Constructs the Store given the initial state and a Reactor
.
pub fn subscribe(&mut self, reactor: impl Into<R>) -> R
[src]
Replaces the Reactor
and returns the previous one.
Trait Implementations
impl<S: Clone, R: Clone + Reactor<S>> Clone for Store<S, R>
[src]
impl<S: Copy, R: Copy + Reactor<S>> Copy for Store<S, R>
[src]
impl<S: Debug, R: Debug + Reactor<S>> Debug for Store<S, R>
[src]
impl<S: Default, R: Default + Reactor<S>> Default for Store<S, R>
[src]
impl<S, R: Reactor<S>> Deref for Store<S, R>
[src]
View Store as a read-only owning smart pointer to the state.
type Target = S
The resulting type after dereferencing.
fn deref(&self) -> &Self::Target
[src]
Grants read access to the current state.
impl<A, S, R> Dispatcher<A> for Store<S, R> where
S: Reducer<A>,
R: Reactor<S>,
[src]
S: Reducer<A>,
R: Reactor<S>,
type Output = Result<(), R::Error>
fn dispatch(&mut self, action: A) -> Self::Output
[src]
Updates the state via Reducer::reduce
and notifies the Reactor
,
returning the result of calling Reactor::react
with a reference
to the new state.
impl<S: Eq, R: Eq + Reactor<S>> Eq for Store<S, R>
[src]
impl<S: Hash, R: Hash + Reactor<S>> Hash for Store<S, R>
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<S: PartialEq, R: PartialEq + Reactor<S>> PartialEq<Store<S, R>> for Store<S, R>
[src]
impl<A, S, R, E> Sink<A> for Store<S, R> where
S: Reducer<A> + Clone,
R: Reactor<S, Error = E> + Sink<S, Error = E>,
[src]
S: Reducer<A> + Clone,
R: Reactor<S, Error = E> + Sink<S, Error = E>,
View Store as a Sink of actions (requires async
).
type Error = E
The type of value produced by the sink when an error occurs.
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
fn start_send(self: Pin<&mut Self>, action: A) -> Result<(), Self::Error>
[src]
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
impl<S, R: Reactor<S>> StructuralEq for Store<S, R>
[src]
impl<S, R: Reactor<S>> StructuralPartialEq for Store<S, R>
[src]
impl<'pin, S, R: Reactor<S>> Unpin for Store<S, R> where
__Store<'pin, S, R>: Unpin,
[src]
__Store<'pin, S, R>: Unpin,
Auto Trait Implementations
impl<S, R> RefUnwindSafe for Store<S, R> where
R: RefUnwindSafe,
S: RefUnwindSafe,
R: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, R> Send for Store<S, R> where
R: Send,
S: Send,
R: Send,
S: Send,
impl<S, R> Sync for Store<S, R> where
R: Sync,
S: Sync,
R: Sync,
S: Sync,
impl<S, R> UnwindSafe for Store<S, R> where
R: UnwindSafe,
S: UnwindSafe,
R: UnwindSafe,
S: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, Item> SinkExt<Item> for T where
T: Sink<Item> + ?Sized,
[src]
T: Sink<Item> + ?Sized,
fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F> where
E: From<Self::Error>,
F: FnMut(U) -> Fut,
Fut: Future<Output = Result<Item, E>>,
[src]
E: From<Self::Error>,
F: FnMut(U) -> Fut,
Fut: Future<Output = Result<Item, E>>,
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F> where
F: FnMut(U) -> St,
St: Stream<Item = Result<Item, Self::Error>>,
[src]
F: FnMut(U) -> St,
St: Stream<Item = Result<Item, Self::Error>>,
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F> where
F: FnOnce(Self::Error) -> E,
[src]
F: FnOnce(Self::Error) -> E,
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E> where
Self::Error: Into<E>,
[src]
Self::Error: Into<E>,
fn buffer(self, capacity: usize) -> Buffer<Self, Item>
[src]
fn close(&mut self) -> Close<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
fn fanout<Si>(self, other: Si) -> Fanout<Self, Si> where
Item: Clone,
Si: Sink<Item, Error = Self::Error>,
[src]
Item: Clone,
Si: Sink<Item, Error = Self::Error>,
fn flush(&mut self) -> Flush<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
fn send(&mut self, item: Item) -> Send<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
fn send_all<St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St> where
Self: Unpin,
St: TryStream<Ok = Item, Error = Self::Error> + Stream + Unpin + ?Sized,
[src]
Self: Unpin,
St: TryStream<Ok = Item, Error = Self::Error> + Stream + Unpin + ?Sized,
fn left_sink<Si2>(self) -> Either<Self, Si2> where
Si2: Sink<Item, Error = Self::Error>,
[src]
Si2: Sink<Item, Error = Self::Error>,
fn right_sink<Si1>(self) -> Either<Si1, Self> where
Si1: Sink<Item, Error = Self::Error>,
[src]
Si1: Sink<Item, Error = Self::Error>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,