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
/*!
Framework for partial function handling (where some functionality is implemented for some but not
all of the data types of fields in a data structure).

A specific piece of partially-implemented functionality consists of a [Func](trait.Func.html)
implementation for all data types where the functionality exists, a
[FuncDefault](trait.FuncDefault.html) implementation for data types where the functionality doesn't
exist, and [IsImplemented](trait.IsImplemented.html) specification for all data types denoting
whether to use the available `Func` or the fall-back `FuncDefault`.

This module should be unnecessary once
[trait specialization](https://github.com/rust-lang/rust/issues/31844) is finalized.
*/
use std::marker::PhantomData;

use access::DataIndex;
use cons::*;
use fieldlist::*;
use label::{LVCons, SelfValued, TypedValue, Valued};
use view::{AssocDataIndexConsOf, DataIndexCons};

/// Marker struct denoting that a [Func](trait.Func.html) is implemented for a particular data type.
#[derive(Debug, Clone)]
pub struct Implemented;
/// Marker struct denoting that a [Func](trait.Func.html) is not implemented for a particular data
/// type.
#[derive(Debug, Clone)]
pub struct Unimplemented;

/// Marker struct for combination of a data type, [Func](trait.Func.html), and whether or not that
/// combination has an implementation.
#[derive(Debug, Clone)]
pub struct Capabilities<DType, Fun, IsImpl> {
    _marker: PhantomData<(DType, Fun, IsImpl)>,
}

/// Structure tracking a field (as accessed through a type that implements
/// [DataIndex](../access/trait.DataIndex.html)) along with its
/// [Capabilities](struct.Capabilities.html) details.
#[derive(Debug, Clone)]
pub struct StorageCapabilities<DType, DI, Fun, IsImpl>
where
    DI: DataIndex<DType = DType>,
{
    _marker: PhantomData<Capabilities<DType, Fun, IsImpl>>,
    data: DI,
}
impl<'a, DType, DI, Fun, IsImpl> SelfValued for StorageCapabilities<DType, DI, Fun, IsImpl> where
    DI: DataIndex<DType = DType>
{
}

/// A cons-list of [StorageCapabilities](struct.StorageCapabilities.html) structs.
pub type StorageCapabilitiesCons<Label, DType, DI, Fun, IsImpl, Tail> =
    LVCons<Label, StorageCapabilities<DType, DI, Fun, IsImpl>, Tail>;

/// Trait denoting whether a particular [Func](trait.Func.html) is implemented for a data type.
pub trait IsImplemented<Fun> {
    /// Marker trait (either [Implemented](struct.Implemented.html) or
    /// [Unimplemented](struct.Unimplemented.html)) specifying the implementation status for this
    /// function / data type combination.
    type IsImpl;
}

/// Trait for applying a partially-implemented function [Func](trait.Func.html) to a cons-list.
pub trait PartialMap<F> {
    /// The output of the function, constructed into a cons-list of function results.
    type Output;
    /// Apply the function `F` to the value in this element of a cons-list, and recurse.
    fn map(&self, f: &mut F) -> Self::Output;
}
impl<'a, F> PartialMap<F> for Nil {
    type Output = Nil;
    fn map(&self, _f: &mut F) -> Nil {
        Nil
    }
}
impl<'a, Label, DType, DI, Fun, Tail, F> PartialMap<F>
    for StorageCapabilitiesCons<Label, DType, DI, Fun, Implemented, Tail>
where
    Tail: PartialMap<F>,
    F: Func<DType>,
    DI: DataIndex<DType = DType>,
{
    type Output = FieldPayloadCons<Label, DType, F::Output, Tail::Output>;

    fn map(&self, f: &mut F) -> Self::Output {
        FieldPayloadCons {
            head: TypedValue::from(f.call(&self.head.value_ref().data)).into(),
            tail: self.tail.map(f),
        }
    }
}
impl<'a, Label, DType, DI, Fun, Tail, F> PartialMap<F>
    for StorageCapabilitiesCons<Label, DType, DI, Fun, Unimplemented, Tail>
where
    Tail: PartialMap<F>,
    DI: DataIndex<DType = DType>,
    F: FuncDefault,
{
    type Output = FieldPayloadCons<Label, DType, F::Output, Tail::Output>;

    fn map(&self, f: &mut F) -> Self::Output {
        FieldPayloadCons {
            head: TypedValue::from(f.call()).into(),
            tail: self.tail.map(f),
        }
    }
}

/// Implementation of a function for a particular data type.
pub trait Func<DType> {
    /// Output of this function.
    type Output;
    /// Method to call this function on field data of data type `DType`.
    fn call<DI>(&mut self, data: &DI) -> Self::Output
    where
        DI: DataIndex<DType = DType>;
}

/// Default function implementation with no valid implementation exists.
pub trait FuncDefault {
    /// Output of this function.
    type Output;
    /// Method called when no [Func](trait.Func.html) implementation exists.
    fn call(&mut self) -> Self::Output;
}

/// Trait that augments a [DataIndexCons](../view/type.DataIndexCons.html) (a cons-list of
/// field access structs) with partial-function capability information as specified by
/// [IsImplemented](trait.IsImplemented.html) definitions.
pub trait DeriveCapabilities<F> {
    /// The augmented cons-list which implements [PartialMap](trait.PartialMap.html), allowing
    /// application of partially-implemented functions to a `DataView`.
    type Output: PartialMap<F>;

    /// Derive the capabilities of this cons-list.
    fn derive(self) -> Self::Output;
}
impl<F> DeriveCapabilities<F> for Nil {
    type Output = Nil;
    fn derive(self) -> Nil {
        Nil
    }
}
impl<Label, DType, DI, Tail, F> DeriveCapabilities<F> for DataIndexCons<Label, DType, DI, Tail>
where
    Tail: DeriveCapabilities<F>,
    DI: DataIndex<DType = DType> + SelfValued,
    DType: IsImplemented<F>,
    StorageCapabilitiesCons<
        Label,
        DType,
        DI,
        F,
        <DType as IsImplemented<F>>::IsImpl,
        <Tail as DeriveCapabilities<F>>::Output,
    >: PartialMap<F>,
{
    type Output = StorageCapabilitiesCons<
        Label,
        DType,
        DI,
        F,
        <DType as IsImplemented<F>>::IsImpl,
        <Tail as DeriveCapabilities<F>>::Output,
    >;
    fn derive(self) -> Self::Output {
        LVCons {
            head: StorageCapabilities {
                data: self.head.value(),
                _marker: PhantomData,
            }
            .into(),
            tail: self.tail.derive(),
        }
    }
}

/// Helper type alias that provides the derived capabilities of the
/// [DataIndexCons](../view/type.DataIndexCons.html) associated with a particular `Labels` /
/// `Frames` pair.
pub type DeriveCapabilitiesOf<Labels, Frames, F> =
    <AssocDataIndexConsOf<Labels, Frames> as DeriveCapabilities<F>>::Output;