Crate haz

source · []
Expand description

A thin abstraction over polymorphic environments.

We might want to a polymorphic when:

  • we want access to a set of types smaller than what a concrete environment may expose, or
  • we do not care about any concrete environment as long as it exposes all required types

Example

A procedure responsible for spawning a TCP server from a config might only require some type, from which it can retrieve host and port without looking at anything else.

use haz::{Has, access_from};

struct Config {
  host: Host,
  port: Port,
  abort_on_error: bool,
  debug: bool,
}

#[derive(Debug)]
struct Host(String);

#[derive(Debug)]
struct Port(u16);

impl Has<Host> for Config {
  fn access(&self) -> &Host {
    &self.host
  }
}

impl Has<Port> for Config {
  fn access(&self) -> &Port {
    &self.port
  }
}


fn run_with<C>(cfg: &C)
where C: Has<Host> + Has<Port> {
  let host: &Host = cfg.access();
  let port = access_from::<Port, _>(cfg);

  println!("host: {:?}, port: {:?}", host, port);
}

Macros

Implements Has for a container which can give access to a component.

Structs

Helper to give access to a component via a turbofish-friendly, infix syntax.

Traits

A representation of a type which can give access to some Component.

Functions

Constructs a proxy from which one may access a component from its container via a turbofish-friendly, infix syntax.

Accesses a component from its container via a turbofish-friendly syntax.