Git Product home page Git Product logo

iter_view's Introduction

iter_view

Rust has IntoIterator trait, to create Iterator from a type. IntoIterator consumes the type, many types provides iter() method to create Iterator from immutable reference without consuming the type. But no trait for iter() method.

In most cases, such trait is not needed, because iter() method returns Iterator which implements IntoIterator trait. But consider the following case:

trait Inspector<T> {
    fn inspect(&self, v: &T);
}

inspect() method takes immutable reference of T, and T is not Copy type. If impl Inspector for any object can convert into Iterator, we can write code like this:

impl<T, I> Inspector<T> for I
    where
        I: IntoIterator<Item = T>,
        T: std::fmt::Debug,
{
    fn inspect(&self, v: &T) {
        for item in self {
            println!("{:?}", item);
        }
    }
}

But IntoIterator trait consumes the type, compiler won't let it go.

If we impl Inspector on slice:

impl<T: std::fmt::Debug> Inspector<T> for [T] {
    fn inspect(&self, v: &T) {
        for item in self {
            println!("{:?}", item);
        }
    }
}

Won't work, unless we change Inspector trait to allow unsized type:

trait Inspector<T: ?Sized> {
    fn inspect(&self, v: &T);
}

Unsized types are poison, cause much trouble to use, we don't want to use it.

Vec<T> support convert to slice very easily, but many other types don't support it, such as HashMap, LinkedList, slice version can not cover all cases.

We can not pass Iterator to inspect() method, because Iterator is a mutable object, and inspect() requires immutable reference, make it impossible to work.

iter_view crate provides IterView trait, which is similar to IntoIterator, but it doesn't consume the type,

pub trait IterView<'a> {
    type Item: 'a;
    type Iter: Iterator<Item = &'a Self::Item>;
    fn iter(&'a self) -> Self::Iter;
}

Use IterView trait, we can impl Inspector for any type which implements IterView:

impl<T, I> Inspector<T> for I
    where
        I: IterView<Item = T>,
        T: std::fmt::Debug,
{
    fn inspect(&self, v: &T) {
        for item in self.iter() {
            println!("{:?}", item);
        }
    }
}

License: MIT OR Apache-2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.