cross: list: add IntoVec
trait
This commit is contained in:
@@ -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();
|
||||
|
@@ -1,5 +1,5 @@
|
||||
#![no_std]
|
||||
#![feature(core_intrinsics)]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub mod compound;
|
||||
pub mod dim;
|
||||
|
Reference in New Issue
Block a user