Module env

Module env 

Source
Expand description

Process events, external directories and metadata.

This module contains functions and macros that operate on the executable level, not the app level. Zng apps can have multiple process instances, most common a view-process and app-process pair, plus the crash handler.

The app-process is the normal execution, the other processes use on_process_start! to takeover the process if specific environment variables are set. The process start handlers are called on init!, if a process takeover it exits without returning, so only the normal app-process code executes after init!().

fn main() {
    println!("print in all processes");
    zng::env::init!();
    println!("print only in the app-process");

    // get a path in the app config dir, the config dir is created if needed.
    let my_config = zng::env::config("my-config.txt");

    // read a config file, or create it
    if let Ok(c) = std::fs::read_to_string(&my_config) {
        println!("{c}");
    } else {
        std::fs::write(zng::env::config("my-config.txt"), b"Hello!").unwrap();
    }
}

Note that init must be called in main, it must be called in main to define the lifetime of the processes, this is needed to properly call on_process_exit handlers.

Also see init! docs for details init in "wasm32" and "android" target builds.

§Full API

See zng_env for the full API.

Macros§

init
Inits process metadata, calls process start handlers and defines the process lifetime in main.
on_process_start
Register a FnOnce(&ProcessStartArgs) closure to be called on init!.

Structs§

About
Metadata about the app and main crate.
ProcessExitArgs
Arguments for on_process_exit handlers.
ProcessStartArgs
Arguments for on_process_start handlers.

Functions§

about
Gets metadata about the application.
bin
Gets a path relative to the package binaries.
cache
Gets a path relative to the cache directory for the app.
clear_cache
Removes all cache files possible.
config
Gets a path relative to the user config directory for the app.
exit
Terminates the current process with the specified exit code.
init_built_res
Sets a custom path for the “built resources” override checked by res in debug builds.
init_cache
Sets a custom cache path.
init_config
Sets a custom original_config path.
init_res
Sets a custom res path.
migrate_cache
Save new_path as the new cache path and make a best effort to move existing cache files.
migrate_config
Copies all config to new_path and saves it as the config path.
on_process_exit
Register a handler to run once when the current process exits.
process_name
Gets a process runtime name.
res
Gets a path relative to the package resources.