cross: list: remove special access to Node::{head,tail}

This commit is contained in:
2022-08-15 23:00:49 -07:00
parent a2939a7807
commit 7704eb623a
2 changed files with 17 additions and 4 deletions

View File

@@ -14,8 +14,8 @@ use serde::{Serialize, Deserialize};
#[cfg_attr(feature = "fmt", derive(Debug))] #[cfg_attr(feature = "fmt", derive(Debug))]
#[derive(Copy, Clone, Default, PartialEq)] #[derive(Copy, Clone, Default, PartialEq)]
pub struct Node<H, T: ?Sized> { pub struct Node<H, T: ?Sized> {
pub(crate) head: H, head: H,
pub(crate) tail: T, tail: T,
} }
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "fmt", derive(Debug))] #[cfg_attr(feature = "fmt", derive(Debug))]
@@ -305,6 +305,19 @@ impl<H, T: Meta> Meta for Node<H, T> {
type Length = PNext<T::Length>; type Length = PNext<T::Length>;
} }
pub trait SplitHead {
type Head;
type Tail;
fn split(self) -> (Self::Head, Self::Tail);
}
impl<H, T> SplitHead for Node<H, T> {
type Head = H;
type Tail = T;
fn split(self) -> (Self::Head, Self::Tail) {
(self.head, self.tail)
}
}
/// these are exported for the convenience of potential consumers: not needed internally /// these are exported for the convenience of potential consumers: not needed internally
pub(crate) mod exports { pub(crate) mod exports {
#![allow(dead_code)] #![allow(dead_code)]

View File

@@ -7,7 +7,7 @@ mod flat;
// pub use linked::*; // pub use linked::*;
pub use flat::{IntoList, Appendable, Appended, Prependable, Prepended}; pub use flat::{IntoList, Appendable, Appended, Prependable, Prepended};
pub use flat::exports::*; pub use flat::exports::*;
use flat::Node; use flat::{Node, SplitHead};
pub type Empty = flat::Null; pub type Empty = flat::Null;
@@ -128,7 +128,7 @@ where
type Output = <FoldImpl<Op, Op::Output> as ListConsumer<T>>::Output; type Output = <FoldImpl<Op, Op::Output> as ListConsumer<T>>::Output;
fn consume(self, l: Node<H, T>) -> Self::Output { fn consume(self, l: Node<H, T>) -> Self::Output {
let FoldImpl(op, state) = self; let FoldImpl(op, state) = self;
let (head, tail) = (l.head, l.tail); // TODO: hide behind a SplitHead trait let (head, tail) = l.split();
let next_state = op.feed(state, head); let next_state = op.feed(state, head);
FoldImpl(op, next_state).consume(tail) FoldImpl(op, next_state).consume(tail)
} }