Module zng::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§

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

Structs§

Functions§

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