maplibre/tcs/system/
function.rs

1use std::{any::type_name, borrow::Cow};
2
3use crate::{
4    context::MapContext,
5    tcs::system::{System, SystemResult},
6};
7
8/// Conversion trait to turn something into a [`System`].
9///
10/// Use this to get a system from a function. Also note that every system implements this trait as
11/// well.
12pub trait IntoSystem: Sized {
13    type System: System;
14    /// Turns this value into its corresponding [`System`].
15    fn into_system(self) -> Self::System;
16}
17
18pub struct FunctionSystem<F> {
19    func: F,
20}
21
22impl<F> System for FunctionSystem<F>
23where
24    F: FnMut(&mut MapContext) -> SystemResult + 'static,
25{
26    fn name(&self) -> Cow<'static, str> {
27        type_name::<F>().into()
28    }
29
30    fn run(&mut self, context: &mut MapContext) -> SystemResult {
31        (self.func)(context)
32    }
33}
34
35impl<F> IntoSystem for F
36where
37    F: FnMut(&mut MapContext) -> SystemResult + 'static,
38{
39    type System = FunctionSystem<F>;
40
41    fn into_system(self) -> Self::System {
42        FunctionSystem { func: self }
43    }
44}