see_through/
lib.rs

1#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR" ), "/", "README.md"))]
2
3use std::ops;
4
5#[cfg(feature = "macros")]
6pub use see_derive;
7
8///
9/// This trait let's us visit a specific field inside a struct. similar to lens in Haskell
10///
11/// e.g.
12/// Consider a scenerio where, you are passing a generic to a function and incidentally you also want
13/// to get a value inside it. Let's assume that there are multiple structs with the same field
14/// `foo` you might wish to for example view it. But, it would be impossible to do so,
15///
16pub trait See<N> {
17    /// This is the type of the value that we want to [`See`][See]
18    type Inner;
19    /// By using `&self` get the internal value [`Inner`][See::Inner]
20    fn get(&self) -> &Self::Inner;
21    /// If we have access to `&mut self` try and mutate [`&mut Inner`][See::Inner]
22    fn set(&mut self) -> &mut Self::Inner;
23}
24
25///
26/// A trait to provide an additional `ops::Index` functionality over `See`
27///
28/// This trait, is an extension to that See trait, which also allows easier indexing with the help
29/// of `ops::Index`. This allows you to use simple formats like `value[see_t::X]` instead of using
30/// functions to get or set these values. This not only helps with ease of use, but also allows you
31/// to implement multiple Look clause on the generic, unlocking more lookups inside the generic.
32///
33pub trait Look<I>:
34    See<I> + ops::Index<I, Output = Self::Inner> + ops::IndexMut<I, Output = Self::Inner>
35{
36}
37
38/// This implementation is automatically derived when all the conditions are met.
39#[automatically_derived]
40impl<T, I, D> Look<I> for T where
41    T: See<I, Inner = D> + ops::Index<I, Output = D> + ops::IndexMut<I, Output = D>
42{
43}