Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context: add inspect layer #33

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/indicator/src/context/layer/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::context::{ContextOperator, Value, ValueRef};

use super::Layer;

/// Layer that used to inspect the context.
pub struct Inspect<F>(pub F);

impl<T, P, F> Layer<T, P> for Inspect<F>
where
P: ContextOperator<T>,
F: Fn(ValueRef<'_, T>) + Clone,
{
type Output = InspectOperator<P, F>;

#[inline]
fn layer(&self, operator: P) -> Self::Output {
InspectOperator {
inner: operator,
inspect: self.0.clone(),
}
}
}

/// Operator that used to inspect the context.
pub struct InspectOperator<P, F> {
inner: P,
inspect: F,
}

impl<T, P, F> ContextOperator<T> for InspectOperator<P, F>
where
P: ContextOperator<T>,
F: Fn(ValueRef<'_, T>),
{
type Output = P::Output;

#[inline]
fn next(&mut self, input: Value<T>) -> Self::Output {
(self.inspect)(input.as_ref());
self.inner.next(input)
}
}
3 changes: 3 additions & 0 deletions crates/indicator/src/context/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub mod cache;
/// Layer that inserts a value into the context.
pub mod insert;

/// Layer that used to inspect the context.
pub mod inspect;

/// Layer.
/// Convert an `In`-operator to another `In`-operator.
pub trait Layer<T, P>
Expand Down
13 changes: 11 additions & 2 deletions crates/indicator/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub mod output;

use crate::Operator;

use self::layer::{cache::CacheOperator, insert::InsertOperator};
use self::layer::{cache::CacheOperator, insert::InsertOperator, inspect::InspectOperator};

pub use self::{
anymap::Context,
layer::{cache::Cache, insert::Insert, layer_fn, Layer},
layer::{cache::Cache, insert::Insert, inspect::Inspect, layer_fn, Layer},
output::{output, output_with},
value::{input, IntoValue, Value, ValueRef},
};
Expand Down Expand Up @@ -88,6 +88,15 @@ pub trait ContextOperatorExt<T>: ContextOperator<T> {
{
self.with(Insert(f))
}

/// Add an inspect layer with the given closure.
fn inspect<F>(self, f: F) -> InspectOperator<Self, F>
where
F: Fn(ValueRef<'_, T>) + Clone,
Self: Sized,
{
self.with(Inspect(f))
}
}

impl<T, P> ContextOperatorExt<T> for P where P: ContextOperator<T> {}
Expand Down
16 changes: 11 additions & 5 deletions examples/context/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![no_std]
extern crate alloc;

use alloc::vec::Vec;
use indicator::{prelude::*, IndicatorIteratorExt};
use num::Num;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;

struct AddTwo<T>(T);
Expand Down Expand Up @@ -37,7 +34,16 @@ where
}

fn main() -> anyhow::Result<()> {
let op = output_with(ma).insert(add_two).cache(1).finish();
let op = output_with(ma)
.inspect(|value| {
println!("input: {}", value.value);
if let Some(AddTwo(x)) = value.context.get::<AddTwo<Decimal>>() {
println!("AddTwo: {x}");
}
})
.insert(add_two)
.cache(1)
.finish();
let data = [dec!(1), dec!(2), dec!(3)];

assert_eq!(
Expand Down