feat: heterogeneous-list with reverse!
This commit is contained in:
commit
313ed45c69
5 changed files with 135 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heterogeneous-list"
|
||||||
|
version = "0.1.0"
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "heterogeneous-list"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
83
src/hlist.rs
Normal file
83
src/hlist.rs
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
use hidden::HList_;
|
||||||
|
|
||||||
|
mod hidden {
|
||||||
|
pub trait HList_ {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Reversed<Cont>
|
||||||
|
{
|
||||||
|
type Output: HList_;
|
||||||
|
fn reversed_prepend(self, cont: Cont) -> Self::Output;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Cont> Reversed<Cont> for Nil
|
||||||
|
where Cont: HList_
|
||||||
|
{
|
||||||
|
type Output = Cont;
|
||||||
|
|
||||||
|
fn reversed_prepend(self, cont: Cont) -> Self::Output {
|
||||||
|
cont
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Cont, Item, Rest> Reversed<Cont> for Cons<Item, Rest>
|
||||||
|
where
|
||||||
|
Cont: HList_,
|
||||||
|
Rest: HList_ + Reversed<Cons<Item, Cont>>,
|
||||||
|
Self: HList_,
|
||||||
|
Cons<Item, Cont>: HList_
|
||||||
|
{
|
||||||
|
type Output = <Rest as Reversed<Cons<Item, Cont>>>::Output;
|
||||||
|
|
||||||
|
fn reversed_prepend(self, cont: Cont) -> Self::Output {
|
||||||
|
self.1.reversed_prepend(Cons(self.0, cont))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait HList
|
||||||
|
where
|
||||||
|
Self: Sized + HList_ + Reversed<Nil> {
|
||||||
|
fn prepend<T>(self, value: T) -> Cons<T, Self> {
|
||||||
|
Cons(value, self)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Reversed;
|
||||||
|
fn reversed(self) -> Self::Reversed;
|
||||||
|
}
|
||||||
|
impl HList for Nil {
|
||||||
|
|
||||||
|
type Reversed = Self::Output;
|
||||||
|
|
||||||
|
fn reversed(self) -> Nil{
|
||||||
|
Nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<Item, Rest> HList for Cons<Item, Rest>
|
||||||
|
where
|
||||||
|
Rest: HList_,
|
||||||
|
Self: Reversed<Nil>
|
||||||
|
{
|
||||||
|
type Reversed = Self::Output;
|
||||||
|
fn reversed(self) -> Self::Output {
|
||||||
|
self.reversed_prepend(Nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)]
|
||||||
|
pub struct Cons<Item, Rest: HList_>(pub Item, pub Rest);
|
||||||
|
impl<Item, Rest> HList_ for Cons<Item , Rest>
|
||||||
|
where Rest: HList_ {}
|
||||||
|
impl<Item, Rest: HList_> Cons<Item, Rest> {
|
||||||
|
pub fn uncons(self) -> (Item, Rest) {
|
||||||
|
(self.0, self.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn head(&self) -> &Item {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Copy)]
|
||||||
|
pub struct Nil;
|
||||||
|
impl HList_ for Nil {}
|
38
src/main.rs
Normal file
38
src/main.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use crate::hlist::HList;
|
||||||
|
use hlist::Nil;
|
||||||
|
|
||||||
|
pub mod hlist;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = Nil
|
||||||
|
.prepend(0)
|
||||||
|
.prepend(1)
|
||||||
|
.prepend(2)
|
||||||
|
.prepend(3)
|
||||||
|
.prepend(4)
|
||||||
|
.prepend(5)
|
||||||
|
.prepend(6)
|
||||||
|
.prepend(7)
|
||||||
|
.prepend(8)
|
||||||
|
.prepend(9)
|
||||||
|
.prepend(10)
|
||||||
|
.prepend(11)
|
||||||
|
.prepend(12)
|
||||||
|
.prepend(13)
|
||||||
|
.prepend(14)
|
||||||
|
.prepend(15)
|
||||||
|
.prepend(16)
|
||||||
|
.prepend(17)
|
||||||
|
.prepend(18)
|
||||||
|
.prepend(19)
|
||||||
|
.prepend(20)
|
||||||
|
.prepend(21)
|
||||||
|
.prepend(22)
|
||||||
|
.prepend(23)
|
||||||
|
.prepend(24)
|
||||||
|
.prepend(25);
|
||||||
|
println!("{x:#?}");
|
||||||
|
let y = x.reversed();
|
||||||
|
println!("{y:#?}");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue