[−][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<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: Clone, R: Clone + Reactor<S>> Clone for Store<S, R>
[src]
fn clone(&self) -> Store<S, R>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<S: Default, R: Default + Reactor<S>> Default for Store<S, R>
[src]
impl<S: PartialEq, R: PartialEq + Reactor<S>> PartialEq<Store<S, R>> for Store<S, R>
[src]
impl<S: Copy, R: Copy + Reactor<S>> Copy for Store<S, R>
[src]
impl<S: Unpin, R: Reactor<S> + Unpin> Unpin for Store<S, R>
[src]
impl<S: Eq, R: Eq + Reactor<S>> Eq for Store<S, R>
[src]
impl<S: Debug, R: Debug + Reactor<S>> Debug 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<S: Hash, R: Hash + Reactor<S>> Hash for Store<S, R>
[src]
fn hash<__HSR: Hasher>(&self, state: &mut __HSR)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<A, S, R, E> Sink<A> for Store<S, R> where
S: Reducer<A> + Unpin + Clone,
R: Reactor<S, Error = E> + Sink<S, Error = E>,
[src]
S: Reducer<A> + Unpin + 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>>
Auto Trait Implementations
impl<S, R> Sync for Store<S, R> where
R: Sync,
S: Sync,
R: Sync,
S: Sync,
impl<S, R> Send for Store<S, R> where
R: Send,
S: Send,
R: Send,
S: Send,
impl<S, R> RefUnwindSafe for Store<S, R> where
R: RefUnwindSafe,
S: RefUnwindSafe,
R: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, R> UnwindSafe for Store<S, R> where
R: UnwindSafe,
S: UnwindSafe,
R: UnwindSafe,
S: UnwindSafe,
Blanket Implementations
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> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
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>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
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>>,
Composes a function in front of the sink. Read more
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>>,
Composes a function in front of the sink. Read more
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F> where
F: FnOnce(Self::Error) -> E,
[src]
F: FnOnce(Self::Error) -> E,
Transforms the error returned by the sink.
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E> where
Self::Error: Into<E>,
[src]
Self::Error: Into<E>,
Map this sink's error to a different error type using the Into
trait. Read more
fn buffer(self, capacity: usize) -> Buffer<Self, Item>
[src]
Adds a fixed-size buffer to the current sink. Read more
fn close(&mut self) -> Close<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
Close the sink.
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>,
Fanout items to multiple sinks. Read more
fn flush(&mut self) -> Flush<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
Flush the sync, processing all pending items. Read more
fn send(&mut self, item: Item) -> Send<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
A future that completes after the given item has been fully processed into the sink, including flushing. Read more
fn send_all<St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St> where
Self: Unpin,
St: Stream<Item = Item> + Unpin,
[src]
Self: Unpin,
St: Stream<Item = Item> + Unpin,
A future that completes after the given stream has been fully processed into the sink, including flushing. Read more
fn left_sink<Si2>(self) -> Either<Self, Si2> where
Si2: Sink<Item, Error = Self::Error>,
[src]
Si2: Sink<Item, Error = Self::Error>,
Wrap this sink in an Either
sink, making it the left-hand variant of that Either
. Read more
fn right_sink<Si1>(self) -> Either<Si1, Self> where
Si1: Sink<Item, Error = Self::Error>,
[src]
Si1: Sink<Item, Error = Self::Error>,
Wrap this stream in an Either
stream, making it the right-hand variant of that Either
. Read more