pub trait OptionTy {
const EXISTS: bool;
}
impl OptionTy for NoneTy {
const EXISTS: bool = false;
}
impl<T> OptionTy for SomeTy<T> {
const EXISTS: bool = true;
}
pub struct NoneTy;
pub struct SomeTy<T>(pub T);
pub struct SampleParams<B: OptionTy, L: OptionTy, G: OptionTy, S: OptionTy> {
pub bias: B,
pub lod: L,
pub grad: G,
pub sample_index: S,
}
pub fn bias<B>(bias: B) -> SampleParams<SomeTy<B>, NoneTy, NoneTy, NoneTy> {
SampleParams {
bias: SomeTy(bias),
lod: NoneTy,
grad: NoneTy,
sample_index: NoneTy,
}
}
pub fn lod<L>(lod: L) -> SampleParams<NoneTy, SomeTy<L>, NoneTy, NoneTy> {
SampleParams {
bias: NoneTy,
lod: SomeTy(lod),
grad: NoneTy,
sample_index: NoneTy,
}
}
pub fn grad<T>(grad_x: T, grad_y: T) -> SampleParams<NoneTy, NoneTy, SomeTy<(T, T)>, NoneTy> {
SampleParams {
bias: NoneTy,
lod: NoneTy,
grad: SomeTy((grad_x, grad_y)),
sample_index: NoneTy,
}
}
pub fn sample_index<S>(sample_index: S) -> SampleParams<NoneTy, NoneTy, NoneTy, SomeTy<S>> {
SampleParams {
bias: NoneTy,
lod: NoneTy,
grad: NoneTy,
sample_index: SomeTy(sample_index),
}
}
impl<L: OptionTy, G: OptionTy, S: OptionTy> SampleParams<NoneTy, L, G, S> {
pub fn bias<B>(self, bias: B) -> SampleParams<SomeTy<B>, L, G, S> {
SampleParams {
bias: SomeTy(bias),
lod: self.lod,
grad: self.grad,
sample_index: self.sample_index,
}
}
}
impl<B: OptionTy, G: OptionTy, S: OptionTy> SampleParams<B, NoneTy, G, S> {
pub fn lod<L>(self, lod: L) -> SampleParams<B, SomeTy<L>, G, S> {
SampleParams {
bias: self.bias,
lod: SomeTy(lod),
grad: self.grad,
sample_index: self.sample_index,
}
}
}
impl<B: OptionTy, L: OptionTy, S: OptionTy> SampleParams<B, L, NoneTy, S> {
pub fn grad<T>(self, grad_x: T, grad_y: T) -> SampleParams<B, L, SomeTy<(T, T)>, S> {
SampleParams {
bias: self.bias,
lod: self.lod,
grad: SomeTy((grad_x, grad_y)),
sample_index: self.sample_index,
}
}
}
impl<B: OptionTy, L: OptionTy, G: OptionTy> SampleParams<B, L, G, NoneTy> {
pub fn sample_index<S>(self, sample_index: S) -> SampleParams<B, L, G, SomeTy<S>> {
SampleParams {
bias: self.bias,
lod: self.lod,
grad: self.grad,
sample_index: SomeTy(sample_index),
}
}
}