Created
April 28, 2014 05:23
-
-
Save shadowmint/11362391 to your computer and use it in GitHub Desktop.
Iterator?
This file contains hidden or 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 std::iter::Iterator; | |
struct Foo { | |
offset:uint, | |
x:[int, ..8] | |
} | |
impl Iterator<(* mut Foo, int)> for Foo { | |
fn next(& mut self) -> Option<(* mut Foo, int)> { | |
if self.offset > 0 { | |
self.offset -= 1; | |
return Some((self as * mut Foo, self.x[self.offset])); | |
} | |
return None; | |
} | |
} | |
struct Bar { | |
foo: ~Foo | |
} | |
impl Iterator<(* mut Foo, int)> for Bar { | |
fn next(& mut self) -> Option<(* mut Foo, int)> { | |
return self.foo.next(); | |
} | |
} | |
fn main() { | |
let mut x = Bar { foo: ~Foo { offset: 8, x: [1, 2, 3, 4, 5, 6, 7, 8] } }; | |
println!("Start"); | |
for (y, z) in x { | |
let q:& mut Foo; | |
unsafe { | |
q = & mut (*y); | |
} | |
println!("{} -> {}", q.offset, z); | |
} | |
println!("End"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment