cross: list: allow enumerating by u32 instead of Tagged
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::compound::peano::{Peano, P0, PNext};
|
||||
use crate::compound::peano::Peano;
|
||||
|
||||
mod flat;
|
||||
// mod linked;
|
||||
@@ -286,10 +286,34 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapTagToValueOp<T>(T /* unused */);
|
||||
impl<P: Peano, V, T: From<u32>> MapVisitor<Tagged<P, V>> for MapTagToValueOp<T> {
|
||||
type Output = (T, V);
|
||||
fn map(&self, elem: Tagged<P, V>) -> Self::Output {
|
||||
(P::VALUE.into(), elem.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EnumerateU32 {
|
||||
type Output;
|
||||
fn enumerate_u32(self) -> Self::Output;
|
||||
}
|
||||
impl<L> EnumerateU32 for L
|
||||
where
|
||||
L: Enumerate,
|
||||
L::Output: Map<MapTagToValueOp<u32>>,
|
||||
{
|
||||
type Output = <L::Output as Map<MapTagToValueOp<u32>>>::Output;
|
||||
fn enumerate_u32(self) -> Self::Output {
|
||||
self.enumerate().map(MapTagToValueOp(0u32 /* unused */))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::compound::peano::{P0, P1, P2};
|
||||
|
||||
struct SumVal;
|
||||
|
||||
@@ -518,7 +542,6 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn enumerate_multiple() {
|
||||
use crate::compound::peano::{P1, P2};
|
||||
let list = (2i32, (), 4f32).into_list();
|
||||
let expected = (
|
||||
Tagged::<P0, _>::new(2i32),
|
||||
@@ -527,4 +550,15 @@ mod test {
|
||||
).into_list();
|
||||
assert!(list.enumerate() == expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enumerate_u32_multiple() {
|
||||
let list = (2i32, (), 4f32).into_list();
|
||||
let expected = (
|
||||
(0u32, 2i32),
|
||||
(1u32, ()),
|
||||
(2u32, 4f32)
|
||||
).into_list();
|
||||
assert!(list.enumerate_u32() == expected);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user