Hello,
I am trying to implement a simple adjacency matrix and now I am stuck with `Iterator' for all existing connections. Below is a minimal example of the problem I am facing:
use fixedbitset::FixedBitSet;
enum Order<T> {
Row(T),
Column(T),
}
struct AdjacencyMatrix {
values: Order<Vec<FixedBitSet>>,
}
pub struct Pivot {
row: usize,
column: usize,
}
impl AdjacencyMatrix {
pub fn connections(&self) -> impl Iterator<Item = Pivot> {
match self.values {
Order::Row(ref values) => values
.iter()
.enumerate()
.flat_map(|(row, values)| values.ones().map(|column| Pivot { row, column })),
Order::Column(ref values) => values
.iter()
.enumerate()
.flat_map(|(column, values)| values.ones().map(|row| Pivot { row, column })),
}
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0308]: `match` arms have incompatible types
--> src/lib.rs:24:42
|
19 | match self.values {
| ----------------- `match` arms have incompatible types
20 | Order::Row(ref values) => values
| _______________________________________-
21 | | .iter()
22 | | .enumerate()
23 | | .flat_map(|(row, values)| values.ones().map(|column| Pivot { row, column })),
| |___________________________---------------___________________--------_______________________- this is found to be of type `FlatMap<Enumerate<std::slice::Iter<'_, FixedBitSet>>, Map<Ones<'_>, [closure@src/lib.rs:23:61: 23:69]>, [closure@src/lib.rs:23:27: 23:42]>`
| | |
| | one of the expected closures
| one of the expected closures
24 | Order::Column(ref values) => values
| __________________________________________^
25 | | .iter()
26 | | .enumerate()
27 | | .flat_map(|(column, values)| values.ones().map(|row| Pivot { row, column })),
| |____________________________________________________________________________________________^ expected closure, found a different closure
|
= note: expected struct `FlatMap<Enumerate<std::slice::Iter<'_, _>>, Map<Ones<'_>, [closure@src/lib.rs:23:61: 23:69]>, [closure@src/lib.rs:23:27: 23:42]>`
found struct `FlatMap<Enumerate<std::slice::Iter<'_, _>>, Map<Ones<'_>, [closure@src/lib.rs:27:64: 27:69]>, [closure@src/lib.rs:27:27: 27:45]>`
= note: no two closures, even if identical, have the same type
= help: consider boxing your closure and/or using it as a trait object
help: you could change the return type to be a boxed trait object
|
18 | pub fn connections(&self) -> Box<dyn Iterator<Item = Pivot>> {
| ~~~~~~~ +
help: if you change the return type to expect trait objects, box the returned expressions
|
20 ~ Order::Row(ref values) => Box::new(values
21 | .iter()
22 | .enumerate()
23 ~ .flat_map(|(row, values)| values.ones().map(|column| Pivot { row, column }))),
24 ~ Order::Column(ref values) => Box::new(values
25 | .iter()
26 | .enumerate()
27 ~ .flat_map(|(column, values)| values.ones().map(|row| Pivot { row, column }))),
|
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
I can put that into the Box
, however first I need to comprehend why compiler consider following types as different:
expected struct `FlatMap<Enumerate<std::slice::Iter<'_, _>>, Map<Ones<'_>, [closure@src/lib.rs:23:61: 23:69]>, [closure@src/lib.rs:23:27: 23:42]>`
found struct `FlatMap<Enumerate<std::slice::Iter<'_, _>>, Map<Ones<'_>, [closure@src/lib.rs:27:64: 27:69]>, [closure@src/lib.rs:27:27: 27:45]>`
It looks weird for me...