Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can create iterator adapter that fulfills Iterator<Item=T> and Iterator<Item=Option<T>> #20346

Closed
japaric opened this issue Dec 30, 2014 · 0 comments
Assignees

Comments

@japaric
Copy link
Member

japaric commented Dec 30, 2014

STR

Example from libcore

#![crate_type = "lib"]
#![feature(associated_types)]
#![no_implicit_prelude]

use std::option::Option::{None, Some, mod};
use std::vec::Vec;

trait Iterator {
    type Item;

    fn next(&mut self) -> Option<Self::Item>;
}

fn is_iterator_of<A, I: Iterator<Item=A>>(_: &I) {}

struct Adapter<I> {
    iter: I,
    found_none: bool,
}

impl<T, I> Iterator for Adapter<I> where I: Iterator<Item=Option<T>> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        loop {}
    }
}

fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
    is_iterator_of::<Option<T>, _>(&it);  // Sanity check
    let adapter = Adapter { iter: it, found_none: false };
    is_iterator_of::<T, _>(&adapter); // OK
    is_iterator_of::<Option<T>, _>(&adapter); // <-- This shouldn't be accepted
}

Version

84f5ad8

cc @nikomatsakis

@nikomatsakis nikomatsakis self-assigned this Dec 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants