1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use super::{Compose, Identity, Invert, Lenticuloid};

#[cfg(not(feature = "nightly"))]
/// The identity lenticuloid (function form).
#[inline]
pub fn identity<S, T>() -> Identity<S, T> {
    Identity::mk()
}

#[cfg(feature = "nightly")]
/// The identity lenticuloid (constant function form).
#[inline]
pub const fn identity<S, T>() -> Identity<S, T> {
    Identity::mk()
}

/// Extension `trait` for lenticuloid composition in categorical order.
pub trait ComposeExt<Other>: Lenticuloid + Sized
    where Other: Lenticuloid<InitialTarget = Self::InitialSource, FinalTarget = Self::FinalSource>
{
    fn compose(self, other: Other) -> Compose<Self, Other>;
}

impl<This, Other> ComposeExt<Other> for This
    where This: Lenticuloid,
          Other: Lenticuloid<InitialTarget = This::InitialSource, FinalTarget = This::FinalSource>
{
    #[inline]
    fn compose(self, other: Other) -> Compose<Self, Other> {
        Compose::of(self, other)
    }
}

/// Compose all the provided lenticuloids in categorical order.
#[macro_export]
macro_rules! chain_compose {
    () => { $crate::Identity::mk() };
    ($l:expr) => { $l };
    ($lf:expr, $($ls:expr),+) => { $crate::Compose::of($lf, chain_compose!($($ls),+)) };
}

/// Extension `trait` for lenticuloid composition in intuitive order.
pub trait AndThenExt<Other: Lenticuloid>
    : Lenticuloid<InitialTarget = Other::InitialSource, FinalTarget = Other::FinalSource> + Sized
    where Other: Lenticuloid
{
    fn and_then(self, other: Other) -> Compose<Other, Self>;
}

impl<This, Other> AndThenExt<Other> for This
    where Other: Lenticuloid,
          This: Lenticuloid<InitialTarget = Other::InitialSource, FinalTarget = Other::FinalSource>
{
    #[inline]
    fn and_then(self, other: Other) -> Compose<Other, Self> {
        Compose::of(other, self)
    }
}

/// Compose all the provided lenticuloids in intuitive order.
#[macro_export]
macro_rules! chain_and_then {
    () => { $crate::Identity::mk() };
    ($l:expr) => { $l };
    ($lf:expr, $($ls:expr),+) => { $crate::Compose::of(chain_and_then!($($ls),+), $lf) };
}

/// Extension `trait` for lenticuloid inversion.
pub trait InvertExt: Lenticuloid + Sized {
    fn invert(self) -> Invert<Self>;
}

impl<This: Lenticuloid> InvertExt for This {
    #[inline]
    fn invert(self) -> Invert<Self> {
        Invert::of(self)
    }
}

/// Create a simple lens inline to address a specific (possibly nested) field
/// of a type.
#[macro_export]
macro_rules! field_lens {
    ($source:ty => $($field_name:tt).*: $target:ty) => {
        {
            #[derive(Copy,Clone,Debug,Default)]
            struct __FieldLens__;
            impl $crate::Lenticuloid for __FieldLens__ {
                type InitialSource = $source;
                type InitialTarget = $target;
                type FinalSource = $source;
                type FinalTarget = $target;
                type AtInitial = Self;
                #[inline]
                fn at_initial(&self) -> Self::AtInitial {
                    *self
                }
                type AtFinal = Self;
                #[inline]
                fn at_final(&self) -> Self::AtFinal {
                    *self
                }
            }
            impl $crate::PartialLens for __FieldLens__ {
                #[inline]
                fn try_get(&self, v: Self::InitialSource) ->
                    $crate::std::result::Result<Self::InitialTarget, Self::FinalSource>
                {
                    $crate::std::result::Result::Ok(v$(.$field_name)*)
                }
                #[inline]
                fn try_get_inject(&self, mut v: Self::InitialSource) ->
                    $crate::std::result::Result<(Self::InitialTarget,
                                                 $crate::Injector<Self::FinalTarget,
                                                 Self::FinalSource>), Self::FinalSource>
                {
                    // this is safe because we fully own `v` and can NoDrop-wrap it
                    let x = $crate::std::mem::replace(&mut v$(.$field_name)*, unsafe {
                        $crate::std::mem::uninitialized()
                    });
                    let v_no_drop = $crate::nodrop::NoDrop::new(v);
                    $crate::std::result::Result::Ok((
                        x,
                        $crate::util::once_to_mut(move |y| {
                            let mut v_final = v_no_drop.into_inner();
                            $crate::std::mem::forget($crate::std::mem::replace(
                                &mut v_final$(.$field_name)*,
                                y
                            ));
                            v_final
                        })
                    ))
                }
                #[inline]
                fn set(&self, mut v: Self::InitialSource, x: Self::FinalTarget) ->
                    Self::FinalSource
                {
                    v$(.$field_name)* = x;
                    v
                }
                #[inline]
                fn exchange(&self,
                            mut v: Self::InitialSource,
                            mut x: Self::FinalTarget) ->
                    ($crate::std::option::Option<Self::InitialTarget>,
                     Self::FinalSource)
                {
                    $crate::std::mem::swap(&mut v$(.$field_name)*, &mut x);
                    ($crate::std::option::Option::Some(x), v)
                }
                #[inline]
                fn modify<F>(&self, mut v: Self::InitialSource, f: F) -> Self::FinalSource
                    where F: FnOnce(Self::InitialTarget) -> Self::FinalTarget
                {
                    v$(.$field_name)* = f(v$(.$field_name)*);
                    v
                }
                #[inline]
                fn modify_with<F, X>(&self, mut v: Self::InitialSource, f: F) ->
                    (Self::FinalSource, $crate::std::option::Option<X>)
                    where F: FnOnce(Self::InitialTarget) -> (Self::FinalTarget, X)
                {
                    let (x, aux) = f(v$(.$field_name)*);
                    v$(.$field_name)* = x;
                    (v, $crate::std::option::Option::Some(aux))
                }
            }
            impl $crate::Lens for __FieldLens__ {
                #[inline]
                fn get(&self, v: Self::InitialSource) -> Self::InitialTarget
                {
                    v$(.$field_name)*
                }
            }
            __FieldLens__
        }
    }
}

#[cfg(test)]
mod test {
    use ::PartialLens;
    #[test]
    fn test_field_lens() {
        struct TestInner(String);
        struct TestOuter {
            pub test_field: TestInner,
        }
        let l = field_lens!(TestOuter => test_field.0: String);
        let v = TestOuter { test_field: TestInner("  hello\n".to_string()) };
        let w = l.modify(v, |x| x.trim().to_string());
        assert_eq!(w.test_field.0, "hello")
    }
}