Skip to content

Instantly share code, notes, and snippets.

Created July 22, 2015 08:02
Show Gist options
  • Save anonymous/f0c867f40f832bf2ee6f to your computer and use it in GitHub Desktop.
Save anonymous/f0c867f40f832bf2ee6f to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::slice::Iter;
trait Test {
fn hello(&self);
}
impl Test for i32 {
fn hello(&self) {
println!("i32 {}", self);
}
}
struct TestIter<'a, T: 'a> {
it: Iter<'a, T>,
}
impl<'a, T> Iterator for TestIter<'a, T>
where T: Test + 'a
{
type Item = &'a Test;
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(|i| i as &Test)
}
}
impl<'a, T> TestIter<'a, T> {
fn new(it: Iter<'a, T>) -> TestIter<'a, T> {
TestIter {
it: it
}
}
}
fn test<'b>(test: &'b [i32]) -> Box<Iterator<Item=&'b Test> + 'b> {
let a: Iter<'b, i32> = test.iter();
let b: TestIter<'b, i32> = TestIter::<'b, i32>::new(a);
Box::new(b) as Box<Iterator<Item=&'b Test>>
}
fn main() {
let a = &[1, 2, 3i32];
for t in test(a) {
t.hello();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment