Compare commits
No commits in common. "5af0d4eb2daf02c4718e84f21581e341249ad224" and "76eb80517f4c1c448a72ca925da389841189ec8d" have entirely different histories.
5af0d4eb2d
...
76eb80517f
7 changed files with 0 additions and 112 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +0,0 @@
|
||||||
/target
|
|
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -1,7 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "generic"
|
|
||||||
version = "0.1.0"
|
|
|
@ -1,6 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "generic"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
|
@ -1,28 +0,0 @@
|
||||||
use crate::generic::{Field, Sum, Generic};
|
|
||||||
|
|
||||||
|
|
||||||
struct IntPair {
|
|
||||||
a: i32,
|
|
||||||
b: i32
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Generic for IntPair {
|
|
||||||
type Representation = Sum<Field<i32>, Field<i32>>;
|
|
||||||
fn generalize(self) -> Self::Representation {
|
|
||||||
Sum {
|
|
||||||
left: Field { value: self.a },
|
|
||||||
right: Field { value: self.b }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn specialize(rep: Self::Representation) -> Self {
|
|
||||||
let Sum {
|
|
||||||
left: Field { value: a },
|
|
||||||
right: Field { value: b },
|
|
||||||
} = rep;
|
|
||||||
IntPair { a, b }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn convert(x: IntPair) -> (i32, i32) {
|
|
||||||
Generic::specialize(x.generalize())
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
pub struct Sum<Con, Rhs> where Con: IsConType, Rhs: IsRepType {
|
|
||||||
pub left: Con,
|
|
||||||
pub right: Rhs,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Product<T, Rhs> where Rhs: IsConType {
|
|
||||||
pub left: Field<T>,
|
|
||||||
pub right: Rhs
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Field<T> {
|
|
||||||
pub value: T
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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<T> IsRepType for Field<T> {}
|
|
||||||
|
|
||||||
// types that represent constructors
|
|
||||||
pub trait IsConType {}
|
|
||||||
impl<T, Rhs: IsConType> IsConType for Product<T, Rhs> {}
|
|
||||||
impl<T> IsConType for Field<T> {}
|
|
||||||
|
|
||||||
|
|
||||||
pub trait Generic {
|
|
||||||
type Representation: IsRepType;
|
|
||||||
fn generalize(self) -> Self::Representation;
|
|
||||||
fn specialize(rep: Self::Representation) -> Self;
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
use crate::generic::{Field, Generic, Sum};
|
|
||||||
|
|
||||||
impl<A, B> Generic for (A, B) {
|
|
||||||
type Representation = Sum<Field<A>, Field<B>>;
|
|
||||||
|
|
||||||
fn generalize(self) -> Self::Representation {
|
|
||||||
let (l, r) = self;
|
|
||||||
Sum {
|
|
||||||
left: Field { value: l },
|
|
||||||
right: Field { value: r }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn specialize(rep: Self::Representation) -> Self {
|
|
||||||
let Sum {
|
|
||||||
left: Field { value: a },
|
|
||||||
right: Field { value: b },
|
|
||||||
} = rep;
|
|
||||||
(a, b)
|
|
||||||
}
|
|
||||||
}
|
|
18
src/lib.rs
18
src/lib.rs
|
@ -1,18 +0,0 @@
|
||||||
pub mod generic;
|
|
||||||
mod example;
|
|
||||||
pub mod instances;
|
|
||||||
|
|
||||||
pub fn add(left: u64, right: u64) -> u64 {
|
|
||||||
left + right
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let result = add(2, 2);
|
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue