Please, can someone help me? I'm already tired of fighting with the compiler. There is a certain trait that has a method that returns an iterator (everything works here). Next, I want to implement this trait on an array of elements, each of which implements this trait. It is necessary that the returned iterator be flattened. But it doesn't work:
use core::iter::{Flatten, Map};
use core::slice::Iter;
use std::collections;
use core::borrow::Borrow;
use core::hash::Hash;
pub trait Container<E: ?Sized> {
type Key;
type Value;
type Keys<'a>: Iterator<Item = &'a Self::Key>
where
Self: 'a,
Self::Key: 'a,
Self::Value: 'a;
fn keys<'a>(&'a self) -> Self::Keys<'a>;
}
impl<K, V, Q> Container<Q> for collections::HashMap<K, V>
where
K: Hash + Eq + Borrow<Q>,
Q: Hash + Eq,
{
type Key = K;
type Value = V;
type Keys<'a> = collections::hash_map::Keys<'a, K, V> where Self: 'a, K: 'a, V: 'a;
fn keys<'a>(&'a self) -> Self::Keys<'a> {
self.keys()
}
}
impl<E, T: Container<E>, const N: usize> Container<E> for [T; N] {
type Key = T::Key;
type Value = T::Value;
type Keys<'a> = Flatten<Map<Iter<'a, T>, fn(&T) -> T::Keys<'a>>> //Flatten<Map<Iter<'a, T>, for<'b> fn(&'b T) -> T::Keys<'b>>>
where
Self: 'a,
Self::Key: 'a,
Self::Value: 'a;
fn keys<'a>(&'a self) -> Self::Keys<'a> {
fn keys_iter<'a, E1, T1: Container<E1>>(item: &'a T1) -> T1::Keys<'a> {
item.keys()
}
self.iter().map(keys_iter).flatten()
}
}
Here is the compilation error:
error[E0308]: mismatched types
--> src\lib.rs:488:9
|
484 | fn keys<'a>(&'a self) -> Self::Keys<'a> {
| -------------- expected `Flatten<Map<std::slice::Iter<'a, T>, for<'b> fn(&'b T) -> <T as Container<E>>::Keys<'a>>>` because of return
type
...
488 | self.iter().map(keys_iter).flatten()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found fn item
|
= note: expected struct `Flatten<Map<std::slice::Iter<'a, _>, for<'b> fn(&'b T) -> <T as Container<E>>::Keys<'a>>>`
found struct `Flatten<Map<std::slice::Iter<'_, _>, for<'a> fn(&'a T) -> <T as Container<E>>::Keys<'a> {keys_iter::<E, T>}>>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `container` due to previous error
Doesn't work with closures or function pointers