Module apollo_framework::platform[][src]

Expand description

Provides a tiny DI like container to expose all components of the system.

The platform is more or less a simple map which keeps all central components as Arc around. Also this keeps the central is_running flag which is toggled to false once Platform::terminate is invoked.

Not that in common cases Platform::require is a good way of fetching a service which is known to be there. However, be aware, that once the system shutdown is initiated, the internal map is cleared and empty (so that all Dropped handlers run). Therefore if the code might be executed after Platform::terminate was called, you should use Platform::find and gracefully handle the None case. However, in most cases the lookup of services is performed during startup and therefore require can be used.

Examples

struct Service {
    value : i32
}

struct UnknownService;

let platform = Platform::new();
     
// Registers a new service...
platform.register::<Service>(Arc::new(Service { value: 42 }));
     
// Obtains a reference to a previously registered service...
let service = platform.require::<Service>();
assert_eq!(service.value, 42);

// Trying to obtain a service which hasn't been registered yet, returns an empty
// optional...
assert_eq!(platform.find::<UnknownService>().is_none(), true);

// By default the platform is running...
assert_eq!(platform.is_running(), true);
     
// Once terminated...
platform.terminate();
// All services are immediately released so that their "Dropped" handlers run...
assert_eq!(platform.find::<Service>().is_none(), true);

// and the platform is no longer considered active...
assert_eq!(platform.is_running(), false);

Structs

Provides a container to keep all central services in a single place.