Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::*;
- fn main()
- {
- let iter: MyIterator<f32> = MyIterator::new();
- for (i, val) in iter.take(10).enumerate() {
- println!("{} => {}", i, val);
- }
- }
- struct MyIterator<T: MyType>
- {
- previous: T::InnerType,
- }
- impl<T: MyType> MyIterator<T>
- {
- pub fn new() -> Self {
- MyIterator { previous: T::InnerType::ONE }
- }
- }
- // `Add`, `Shl`, and `From` trait bounds only work when put here...
- impl<T: MyType> Iterator for MyIterator<T>
- {
- type Item = T;
- fn next(&mut self) -> Option<Self::Item> {
- // Add one
- let plus_one = T::InnerType::from(self.previous + T::InnerType::ONE);
- // Multiply by two
- let times_two = T::InnerType::from(plus_one << 1);
- // Save state
- self.previous = times_two;
- // Return value
- Some(T::from_inner(times_two))
- }
- }
- trait MyType
- {
- // Want to put `Add`, `Shl`, and `From` trait bounds on InnerType here...
- type InnerType:
- One + Copy + Add + Shl<usize> +
- From<<Self::InnerType as Add>::Output> +
- From<<Self::InnerType as Shl<usize>>::Output>;
- fn from_inner(val: Self::InnerType) -> Self;
- }
- impl MyType for f32
- {
- type InnerType = u32;
- fn from_inner(val: u32) -> f32 {
- val as f32
- }
- }
- trait One
- {
- const ONE: Self;
- }
- impl One for u32
- {
- const ONE: u32 = 1;
- }
Add Comment
Please, Sign In to add comment