feat: implemented deriving for structs of all types
This commit is contained in:
parent
5af0d4eb2d
commit
f52b1b5590
9 changed files with 377 additions and 36 deletions
|
@ -1,26 +1,63 @@
|
|||
pub struct Sum<Con, Rhs> where Con: IsConType, Rhs: IsRepType {
|
||||
pub left: Con,
|
||||
pub right: Rhs,
|
||||
use std::ops::Add;
|
||||
|
||||
pub enum Sum<Con, Rhs> where Con: IsConType, Rhs: IsRepType {
|
||||
Left(Con),
|
||||
Right(Rhs),
|
||||
}
|
||||
|
||||
pub struct Product<T, Rhs> where Rhs: IsConType {
|
||||
pub left: Field<T>,
|
||||
pub left: T,
|
||||
pub right: Rhs
|
||||
}
|
||||
|
||||
impl<Field, Rest, RestRhs, FieldRhs> Add<Product<FieldRhs, RestRhs>> for Product<Field, Rest>
|
||||
where
|
||||
Rest: Add<RestRhs> + IsConType,
|
||||
Field: Add<FieldRhs>,
|
||||
RestRhs: IsConType,
|
||||
Rest::Output: IsConType
|
||||
{
|
||||
type Output = Product<Field::Output, Rest::Output>;
|
||||
|
||||
fn add(self, other: Product<FieldRhs, RestRhs>) -> Self::Output {
|
||||
Product {
|
||||
left: self.left + other.left,
|
||||
right: self.right + other.right
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct Field<T> {
|
||||
pub value: T
|
||||
}
|
||||
|
||||
impl<R, T: Add<R>> Add<Field<R>> for Field<T> {
|
||||
type Output=Field<T::Output>;
|
||||
|
||||
fn add(self, rhs: Field<R>) -> Self::Output {
|
||||
Field {
|
||||
value: self.value + rhs.value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// types used for generic representation
|
||||
pub trait IsRepType {}
|
||||
impl<Lhs: IsConType, Rhs: IsRepType> IsRepType for Sum<Lhs, Rhs> {}
|
||||
impl<T: IsRepType, Rhs: IsConType> IsRepType for Product<T, Rhs> {}
|
||||
|
||||
impl<Lhs, Rhs> IsRepType for Sum<Lhs, Rhs>
|
||||
where Lhs: IsConType, Rhs: IsRepType {}
|
||||
|
||||
impl<T, Rhs> IsRepType for Product<T, Rhs>
|
||||
where Rhs: IsConType {}
|
||||
|
||||
impl<T> IsRepType for Field<T> {}
|
||||
|
||||
// types that represent constructors
|
||||
pub trait IsConType {}
|
||||
impl<T, Rhs: IsConType> IsConType for Product<T, Rhs> {}
|
||||
impl<T, Rhs> IsConType for Product<T, Rhs>
|
||||
where Rhs: IsConType {}
|
||||
impl<T> IsConType for Field<T> {}
|
||||
|
||||
|
||||
|
@ -29,3 +66,4 @@ pub trait Generic {
|
|||
fn generalize(self) -> Self::Representation;
|
||||
fn specialize(rep: Self::Representation) -> Self;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue