Drgabble
Drgabble

Reputation: 628

Can a function take both IntoIterator<T> and IntoIterator<&T>?

I want a function that takes two arguments, both of which can be turned into an iterator of Foo. The snag is that I'd like to accept things which are both IntoIterator<Foo> and also IntoIterator<&Foo>. Importantly Foo is Copy so I can cheaply create an owned copy from it's reference.

The solution I currently have is:

use std::borrow::Cow;
use std::iter::IntoIterator;

fn main() {
    let foos = [Foo {}, Foo {}, Foo {}];
    let references = || foos.iter();
    let owned = || foos.iter().cloned();
    bar(references(), references());
    bar(references(), owned());
    bar(owned(), references());
    bar(owned(), owned());
}

#[derive(Copy, Clone)]
struct Foo {
    // code ommitted here
}

impl<'a> From<Foo> for Cow<'a, Foo> {
    fn from(foo: Foo) -> Self {
        Self::Owned(foo)
    }
}

impl<'a> From<&'a Foo> for Cow<'a, Foo> {
    fn from(foo: &'a Foo) -> Self {
        Self::Borrowed(foo)
    }
}

fn bar<'a, AIter, A, BIter, B>(alpha_iter: AIter, beta_iter: BIter)
  where 
    AIter: IntoIterator<Item=A>,
    A: Into<Cow<'a, Foo>>,
    BIter: IntoIterator<Item=B>,
    B: Into<Cow<'a, Foo>>
{
    for (alpha, beta) in alpha_iter.into_iter().zip(beta_iter.into_iter()) {
       some_foo_specific_thing(*alpha.into(), *beta.into());
    }
}

fn some_foo_specific_thing(alpha: Foo, beta: Foo) {
    // code ommitted here
}

playground

Is there a way to do this in less lines / without Cow?

Upvotes: 1

Views: 311

Answers (1)

Dmitry
Dmitry

Reputation: 1647

First of all, you don't need exactly IntoIterator bound here. It's just enough for Iterator<Item = Foo>.

use std::borrow::Cow;
use std::iter::IntoIterator;

fn main() {
    let foos = [Foo {}, Foo {}, Foo {}];
    let references = || foos.iter(); // it is iterator itself
    let owned = || foos.iter().cloned(); // it is iterator itself
    bar(references(), references());
    bar(references(), owned());
    bar(owned(), references());
    bar(owned(), owned());
}
fn bar<'a, AIter, A, BIter, B>(alpha_iter: AIter, beta_iter: BIter)
  where 
    AIter: Iterator<Item = A>,
    A: Into<Cow<'a, Foo>>,
    BIter: Iterator<Item = B>,
    B: Into<Cow<'a, Foo>>
{
    for (alpha, beta) in alpha_iter.zip(beta_iter) {
       some_foo_specific_thing(*alpha.into(), *beta.into());
    }
}

Secondly, I think it is better to use Borrow trait:

fn bar<'a, AIter, A, BIter, B>(alpha_iter: AIter, beta_iter: BIter)
  where 
    AIter: Iterator<Item = A>,
    A: std::borrow::Borrow<Foo> + Copy,
    BIter: Iterator<Item = B>,
    B: std::borrow::Borrow<Foo> + Copy,
{
    let alpha_iter = alpha_iter.map(|item| *item.borrow()); // here we  get a reference to an item and then make a copy to own it
    let beta_iter = beta_iter.map(|item| *item.borrow()); // here we  get a reference to an item and then make a copy to own it
    
    for (alpha, beta) in alpha_iter.zip(beta_iter) {
       some_foo_specific_thing(alpha, beta);
    }
}

Upvotes: 5

Related Questions