cross: list: add IntoVec trait

This commit is contained in:
2022-08-18 04:00:41 -07:00
parent a3b15bd7e7
commit 07fa4042a3
4 changed files with 48 additions and 3 deletions

View File

@@ -240,6 +240,33 @@ where
}
}
#[cfg(feature = "std")]
mod into_vec {
use super::*;
pub struct IntoVecOp;
impl<Next> FoldOp<Vec<Next>, Next> for IntoVecOp {
type Output = Vec<Next>;
fn feed(&mut self, mut prev: Vec<Next>, next: Next) -> Self::Output {
prev.push(next);
prev
}
}
pub trait IntoVec<T> {
fn into_vec(self) -> Vec<T>;
}
impl<T, L> IntoVec<T> for L
where
L: Fold<IntoVecOp, Vec<T>, Output=Vec<T>>
{
fn into_vec(self) -> Vec<T> {
self.fold(IntoVecOp, Vec::new())
}
}
}
#[cfg(feature = "std")]
pub use into_vec::{IntoVec, IntoVecOp};
pub trait MapVisitor<V> {
type Output;
fn map(&self, elem: V) -> Self::Output;
@@ -502,6 +529,23 @@ mod test {
assert_eq!(v.0, 24);
}
#[test]
fn into_vec() {
let list = (3i32, 4i32, 5i32).into_list();
assert_eq!(list.into_vec(), vec![3i32, 4, 5]);
}
#[test]
fn into_vec_ref() {
let list = &(3i32, 4i32, 5i32).into_list();
assert_eq!(list.into_vec(), vec![&3i32, &4, &5]);
}
#[test]
fn into_vec_empty() {
assert_eq!(IntoVec::<u32>::into_vec(Empty::default()), vec![]);
}
#[test]
fn sum() {
let list = (3i32, 4i32, 5i32).into_list();

View File

@@ -1,5 +1,5 @@
#![no_std]
#![feature(core_intrinsics)]
#![cfg_attr(not(feature = "std"), no_std)]
pub mod compound;
pub mod dim;