-
-
Save bczhc/151f300cc000df657eb2f708a89712bb to your computer and use it in GitHub Desktop.
返回一个大迭代器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bigdecimal::num_traits::ops::overflowing::OverflowingAdd; | |
use rand::{thread_rng, RngCore}; | |
use std::iter::{Map, Sum}; | |
use std::ops::Add; | |
pub struct OverflowingAddWrapper<T: OverflowingAdd>(pub T); | |
impl<T: OverflowingAdd> From<T> for OverflowingAddWrapper<T> { | |
fn from(value: T) -> Self { | |
Self(value) | |
} | |
} | |
impl<T: OverflowingAdd> Add for OverflowingAddWrapper<T> { | |
type Output = OverflowingAddWrapper<T>; | |
fn add(self, rhs: Self) -> Self::Output { | |
self.0.overflowing_add(&rhs.0).0.into() | |
} | |
} | |
impl<T: OverflowingAdd + Default> Sum for OverflowingAddWrapper<T> { | |
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { | |
let mut sum = OverflowingAddWrapper(T::default()); | |
for x in iter { | |
sum = sum.add(x) | |
} | |
sum | |
} | |
} | |
fn baz(n: usize) -> Map<impl Iterator<Item = usize>, impl FnMut(usize) -> u32> { | |
let mut rng = [(); 100000].map(|_| thread_rng()); | |
(0..n).map(move |_| { | |
let rng = &mut rng; | |
rng.iter_mut() | |
.map(|x| OverflowingAddWrapper(x.next_u32())) | |
.sum::<OverflowingAddWrapper<u32>>() | |
.0 | |
}) | |
} | |
fn main() { | |
let iter = baz(10); | |
println!("{}", size_of_val(&iter)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment