pub struct LayerCollection(/* private fields */);
Expand description

Collection of layers with some meta-information.

When a map is rendered, it draws all visible layers in the order they are stored in the collection. Any layer can be temporary hidden with the LayerCollection::hide or LayerCollection::show_by methods. These layers will be ignored by the renderer, but retain their place in the collection.

Since a map should be able to render anything implementing the Layer trait, this collection stores layers as trait objects. You can use downcasting through Any trait to obtain a concrete layer type you work with.

use galileo::layer::{RasterTileLayer, VectorTileLayer};
use galileo::layer::data_provider::FileCacheController;
use galileo::layer::data_provider::UrlDataProvider;
use galileo::layer::vector_tile_layer::tile_provider::ThreadedProvider;
use galileo::layer::vector_tile_layer::tile_provider::VtProcessor;
use galileo::LayerCollection;
use galileo::MapBuilder;
use galileo::tile_scheme::TileIndex;

let raster_tiles = MapBuilder::create_raster_tile_layer(|index| format!("url from {index:?}"), todo!());
let vector_tiles = MapBuilder::create_vector_tile_layer(|index| format!("url from {index:?}"), todo!(), todo!());

let mut collection = LayerCollection::default();
collection.push(raster_tiles);
collection.push(vector_tiles);

assert!(collection[1].as_any().downcast_ref::<VectorTileLayer<ThreadedProvider<UrlDataProvider<TileIndex, VtProcessor, FileCacheController>>>>().is_some());

Implementations§

source§

impl LayerCollection

source

pub fn truncate(&mut self, length: usize)

Shortens the collection, keeping the first length layers and dropping the rest. If the length of the collection is less than length does nothing.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

collection.truncate(3);
assert_eq!(collection.len(), 2);
collection.truncate(1);
assert_eq!(collection.len(), 1);
assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer A")));
source

pub fn clear(&mut self)

Removes all layers from the collection.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

collection.clear();
assert_eq!(collection.len(), 0);
source

pub fn swap_remove(&mut self, index: usize) -> Box<dyn Layer>

Removes a layer from the collection and returns it. The removed element is replaced by the last layer in the collection.

§Panics

Panics if index equals or greater then collection length.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

let removed = collection.swap_remove(0);
assert_eq!(removed.as_any().downcast_ref(), Some(&TestLayer("Layer A")));
assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
source

pub fn insert(&mut self, index: usize, layer: impl Layer + 'static)

Inserts a layer at position index, shifting all layers after it to the right.

§Panics

Panics if index > len

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

collection.insert(1, TestLayer("Layer C"));
assert_eq!(collection.len(), 3);
assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
assert_eq!(collection[2].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
source

pub fn remove(&mut self, index: usize) -> Box<dyn Layer>

Removes a layer at index, shifting all layers after it to the left and returning the removed layer.

§Panics

Panics if index is out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

let removed = collection.remove(1);
assert_eq!(removed.as_any().downcast_ref(), Some(&TestLayer("Layer B")));
assert_eq!(collection.len(), 2);
assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&dyn Layer) -> bool,

Retains only the layers specified by the predicate. In other words, remove all layers l for which f(&l) returns false.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

collection.retain(|layer| !layer.as_any().downcast_ref::<TestLayer>().is_some_and(|l| l.0.ends_with("A")));

assert_eq!(collection.len(), 2);
assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
source

pub fn push(&mut self, layer: impl Layer + 'static)

Adds the layer to the end of the collection.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

collection.push(TestLayer("Layer C"));

assert_eq!(collection.len(), 3);
assert_eq!(collection[2].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
source

pub fn pop(&mut self) -> Option<Box<dyn Layer>>

Removes the last layer from the collection and returns it. Returns None if the collection is empty.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

let removed = collection.pop();

assert_eq!(collection.len(), 2);
assert_eq!(removed.unwrap().as_any().downcast_ref(), Some(&TestLayer("Layer C")));
source

pub fn drain<R>( &mut self, range: R ) -> impl Iterator<Item = Box<dyn Layer>> + '_
where R: RangeBounds<usize>,

Removes the specified range of layers from the collection in bulk, returning all removed layers in an iterator. If the iterator is dropped before being fully consumed, it drops the remaining removed layers.

§Panics

Panics if the starting point is greater than the end point and if the end point is greater that the length of the collection.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

let drained: Vec<_> = collection.drain(0..2).collect();
assert_eq!(drained.len(), 2);
assert_eq!(drained[1].as_any().downcast_ref(), Some(&TestLayer("Layer B")));

assert_eq!(collection.len(), 1);
assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
source

pub fn len(&self) -> usize

Returns the count of layers in the collection.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

assert_eq!(collection.len(), 2);
source

pub fn is_empty(&self) -> bool

Returns true if the collection contains zero layers.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::default();
assert!(collection.is_empty());

collection.push(TestLayer("Layer A"));
assert!(!collection.is_empty());
source

pub fn get(&self, index: usize) -> Option<&dyn Layer>

Returns a layer at index, or None if index is out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

assert_eq!(collection.get(1).and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer B")));
assert!(collection.get(2).is_none());
source

pub fn get_mut(&mut self, index: usize) -> Option<&mut Box<dyn Layer>>

Returns a mutable reference to a layer at index, or None if index is out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

assert_eq!(collection.get_mut(1).and_then(|layer| layer.as_any_mut().downcast_ref()), Some(&TestLayer("Layer B")));
assert!(collection.get(2).is_none());
source

pub fn swap(&mut self, a: usize, b: usize)

Swaps two layers in the collection.

§Panics

Panics if a or b are out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

collection.swap(1, 2);

assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
assert_eq!(collection[2].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
source

pub fn iter(&self) -> impl Iterator<Item = &dyn Layer> + '_

Iterates over all layers in the collection.

use galileo::LayerCollection;
use galileo::layer::TestLayer;

let collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

let mut iterator = collection.iter();
assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer A")));
assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer B")));
assert!(iterator.next().is_none());
source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Box<dyn Layer>> + '_

Iterates over mutable references to all layers in the collection.

use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

let mut iterator = collection.iter_mut();
assert_eq!(iterator.next().and_then(|layer| layer.as_any_mut().downcast_ref()), Some(&TestLayer("Layer A")));
assert_eq!(iterator.next().and_then(|layer| layer.as_any_mut().downcast_ref()), Some(&TestLayer("Layer B")));
assert!(iterator.next().is_none());
source

pub fn hide(&mut self, index: usize)

Sets the layer at index as invisible. The hidden layer can be later shown with LayerCollection::show.

Hidden layers are stored in the layer collection, but are not rendered to a map.

§Panics

Panics if index is out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

collection.hide(1);
assert!(!collection.is_visible(1));
source

pub fn show(&mut self, index: usize)

Sets the layer at index as visible.

Hidden layers are stored in the layer collection, but are not rendered to a map.

§Panics

Panics if index is out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

collection.hide(1);
collection.show(1);
assert!(collection.is_visible(1));
source

pub fn show_by<F>(&mut self, f: F)
where F: FnMut(&dyn Layer) -> bool,

Sets all layers for which the predicate returns true as visible. The rest of layers are set as hidden.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

collection.show_by(|layer| layer.as_any().downcast_ref::<TestLayer>().unwrap().0.ends_with("B"));

assert!(!collection.is_visible(0));
assert!(collection.is_visible(1));
assert!(!collection.is_visible(2));
source

pub fn is_visible(&self, index: usize) -> bool

Returns true, if the layer at index is not hidden.

Hidden layers are stored in the layer collection, but are not rendered to a map.

§Panics

Panics if index is out of bounds.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
]);

assert!(collection.is_visible(1));
collection.hide(1);
assert!(!collection.is_visible(1));
collection.show(1);
assert!(collection.is_visible(1));
source

pub fn iter_visible(&self) -> impl Iterator<Item = &dyn Layer> + '_

Iterates over all visible layers in the collection.

§Examples
use galileo::LayerCollection;
use galileo::layer::TestLayer;

let mut collection = LayerCollection::from(vec![
    TestLayer("Layer A"),
    TestLayer("Layer B"),
    TestLayer("Layer C"),
]);

collection.hide(1);

let mut iterator = collection.iter_visible();
assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer A")));
assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer C")));
assert!(iterator.next().is_none());

Trait Implementations§

source§

impl Default for LayerCollection

source§

fn default() -> LayerCollection

Returns the “default value” for a type. Read more
source§

impl<L: Into<LayerEntry>, T: IntoIterator<Item = L>> From<T> for LayerCollection

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl Index<usize> for LayerCollection

§

type Output = dyn Layer

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<usize> for LayerCollection

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CoordinateMetadata for T
where T: ?Sized,

§

fn crs_id(&self) -> Option<MdIdentifier>

§

fn crs(&self) -> Option<Crs>

§

fn coordinate_epoch(&self) -> Option<DataEpoch>

§

fn is_valid(&self) -> bool

§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSync for T
where T: Sync,