Skip to main content

ferroui_base/platform/
standard_runtime_platform_services.rs

1use super::{AssetAssembly, IAssetLoader, IRuntimePlatform, StandardAssetLoader, StandardRuntimePlatform};
2use crate::FerroLocator;
3use std::rc::Rc;
4
5/// Registers the services of the standard runtime platform.
6pub struct StandardRuntimePlatformServices;
7
8impl StandardRuntimePlatformServices {
9    /// Registers the standard runtime platform and the standard asset
10    /// loader with the current service locator. `assembly` is the default
11    /// assembly of the asset loader.
12    pub fn register(assembly: Option<&AssetAssembly>) {
13        let asset_loader: Rc<dyn IAssetLoader> = Rc::new(StandardAssetLoader::new(assembly));
14        FerroLocator::current_mutable()
15            .bind::<dyn IRuntimePlatform>()
16            .to_singleton::<StandardRuntimePlatform>(|instance| instance)
17            .bind::<dyn IAssetLoader>()
18            .to_constant(asset_loader);
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use crate::platform::RuntimePlatformInfo;
26    use crate::LocatorExtensions;
27
28    #[test]
29    fn register_binds_the_runtime_platform_and_the_asset_loader() {
30        let scope = FerroLocator::enter_scope();
31        StandardRuntimePlatformServices::register(None);
32
33        let locator = FerroLocator::current();
34        let platform = locator.get_service::<dyn IRuntimePlatform>();
35        let platform = platform.expect("the runtime platform");
36        let again = locator.get_service::<dyn IRuntimePlatform>().expect("the runtime platform");
37        assert!(*platform == *again);
38        assert_eq!(platform.get_runtime_info(), StandardRuntimePlatform::new().get_runtime_info());
39        assert_ne!(platform.get_runtime_info(), RuntimePlatformInfo { is_desktop: true, is_mobile: true, is_tv: true });
40        assert!(locator.get_service::<dyn IAssetLoader>().is_some());
41
42        scope.dispose();
43    }
44}