package ambient-context

  1. Overview
  2. Docs
Abstraction over thread-local / continuation-local storage mechanisms for communication with transitive dependencies

Install

dune-project
 Dependency

Authors

Maintainers

Sources

ambient-context-0.2.tbz
sha256=f451f4ed467ef0c15f472cda1021a89e96afdf3a32970ea047faf057a01c72d3
sha512=3115fd7b45ac171f7a971013dd6f32ec317fc274d800a86870a0dd43574e27b404090fbc51dae5e4c8af4961d706f84abf3843a6cf8dc57f711211cd2b547780

doc/src/ambient-context.core/storage.ml.html

Source file storage.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(** Storage implementation.

    There is a singleton storage for a given program, responsible for providing
    ambient context to the rest of the program. *)

type t = {
  name: string;
  get_context: unit -> Context.t;
  with_context: 'a. Context.t -> (unit -> 'a) -> 'a;
}
(** Storage type *)

(** Name of the storage implementation. *)
let[@inline] name self = self.name

(** Get the context from the current storage, or [Hmap.empty] if there is no
    ambient context. *)
let[@inline] get_context self = self.get_context ()

(** [with_context storage ctx f] calls [f()] in an ambient context in which
    [get_context()] will return [ctx]. Once [f()] returns, the storage is reset
    to its previous value. *)
let[@inline] with_context self ctx f = self.with_context ctx f

(** Get the ambient context and then look up [k] in it *)
let[@inline] get self (k : 'a Context.key) : 'a option =
  Hmap.find k (get_context self)

(** [with_key_bound_to storage k v f] calls [f()] in a context updated to have
    [k] map to [v]. *)
let with_key_bound_to self k v f =
  let ctx = get_context self in
  let new_ctx = Hmap.add k v ctx in
  self.with_context new_ctx f

(** [with_key_unbound storage k f] calls [f()] in a context updated to have [k]
    bound to no value. *)
let with_key_unbound self k f =
  let ctx = get_context self in
  if Hmap.mem k ctx then (
    let new_ctx = Hmap.rem k ctx in
    self.with_context new_ctx f
  ) else
    f ()