mapeditor/rs-tiled

Nicer way to iterate over layers of a given type

deifactor opened this issue · 2 comments

Right now if you want to iterate over all layers of a given type, you have to do something like

map
    .layers()
    .filter_map(|layer| match layer.layer_type() {
        tiled::LayerType::TileLayer(TileLayer::Finite(layer)) => Some(layer),
        _ => None,
    })

Maybe it'd make sense to pull those functions into methods on Layer:

impl Layer {
  // name suggestions welcome
  pub fn into_finite_tile_layer(self) -> Option<FiniteTileLayer> {
    match self.layer_type() {
          tiled::LayerType::TileLayer(TileLayer::Finite(layer)) => Some(layer),
          _ => None,
    }
  }
}

(and similar for all the others) so you can just write

map
    .layers()
    .filter_map(Layer::into_finite_tile_layer)

Or, alternately, just have a Map::finite_tile_layers(&self) that does this; less boilerplate to use, more boilerplate to implement.

bjorn commented

Seems very similar to the change proposed in #235. It is currently tagged "indecisive", maybe you can help make a decision. :-)

Oops, yes, this is really a dupe of #235.