pub struct Writer<A, W: Monoid> { /* private fields */ }Expand description
Writer helps capture the pattern of writing to a pure log or accumulated
value, handling the book-keeping for you.
This is often used for loggers, but could be anything as long as the hidden value
is a Monoid.
There are many applications of Writers, but as an illustrative point,
one could use it for logging across processes and time, since the log
is carried around with the result in a pure fashion. The monadic DSL
helps make using these feel more natural.
Implementations§
Source§impl<A, W: Monoid> Writer<A, W>
impl<A, W: Monoid> Writer<A, W>
Sourcepub fn new(value: A, log: W) -> Self
pub fn new(value: A, log: W) -> Self
Construct a Writer struct from a starting value and log.
Examples found in repository?
examples/turtle/model/helpers.rs (lines 14-17)
8pub fn lift_op<'a, A: 'a>(
9 op_fun: impl Fn(Turtle, A) -> Turtle + 'static,
10) -> impl FnOnce(A) -> StateT<'a, Turtle, ResultT<Writer<Result<((), Turtle), TurtleError>, String>>>
11{
12 move |p: A| {
13 StateT::new(move |s| {
14 ResultT::lift(Writer::new(
15 ((), op_fun(s, p)),
16 <String as Monoid>::mempty(),
17 ))
18 })
19 }
20}
21
22pub fn lift_validation<'a, A: 'a>(
23 validate_fun: impl Fn(String) -> Result<A, TurtleError>,
24) -> impl Fn(&str) -> StateT<'a, Turtle, ResultT<Writer<Result<(A, Turtle), TurtleError>, String>>>
25{
26 move |p: &str| {
27 StateT::<'a, Turtle, ResultT<Writer<Result<(A, Turtle), TurtleError>, String>>>::lift(
28 ResultT::new(Writer::new(
29 validate_fun(p.to_string()),
30 <String as Monoid>::mempty(),
31 )),
32 )
33 }
34}Trait Implementations§
Source§impl<'a, A: 'a, W: Monoid> Applicative<'a> for Writer<A, W>
impl<'a, A: 'a, W: Monoid> Applicative<'a> for Writer<A, W>
Source§impl<'a, A, W: Monoid> Apply<'a> for Writer<A, W>
impl<'a, A, W: Monoid> Apply<'a> for Writer<A, W>
Source§impl<'a, A, W: Monoid> Functor<'a> for Writer<A, W>
impl<'a, A, W: Monoid> Functor<'a> for Writer<A, W>
Auto Trait Implementations§
impl<A, W> Freeze for Writer<A, W>
impl<A, W> RefUnwindSafe for Writer<A, W>where
A: RefUnwindSafe,
W: RefUnwindSafe,
impl<A, W> Send for Writer<A, W>
impl<A, W> Sync for Writer<A, W>
impl<A, W> Unpin for Writer<A, W>
impl<A, W> UnwindSafe for Writer<A, W>where
A: UnwindSafe,
W: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more