macro_rules! project_global {
(mut $global:expr => $($path:tt)+) => { ... };
($global:expr => $($path:tt)+) => { ... };
}Expand description
Project a field of a global container (crate::SharedGlobal or
crate::SharedGlobalMut) as a crate::Shared or crate::SharedMut,
in a const context.
project_global!(GLOBAL => path) returns a read-only crate::Shared;
project_global!(mut GLOBAL => path) returns a crate::SharedMut (and
requires the global to be a crate::SharedGlobalMut). Because the
projection is built entirely from function pointers, the result can
initialize another static:
struct App {
name: String,
port: u16,
}
static APP: SharedGlobalMut<App> = SharedGlobalMut::new_lazy(|| App {
name: "server".to_string(),
port: 80,
});
static NAME: SharedMut<String> = project_global!(mut APP => name);
static PORT: Shared<u16> = project_global!(APP => port);
*NAME.write() += "-1";
assert_eq!(APP.read().name, "server-1");
assert_eq!(*PORT.read(), 80);The global must be referred to by its path (e.g. APP or config::APP):
the generated lock functions are non-capturing closures, so a local
variable cannot be used here.