add support for ambient-context

optional library to provide ambient-context implem using
Moonpool.Task_local_storage
This commit is contained in:
Simon Cruanes 2026-04-07 21:42:52 -04:00
parent 471feb96d6
commit d681231d9d
5 changed files with 29 additions and 1 deletions

View file

@ -27,7 +27,7 @@ jobs:
- run: opam exec -- dune build @install - run: opam exec -- dune build @install
# install some depopts # install some depopts
- run: opam install thread-local-storage trace hmap - run: opam install thread-local-storage trace hmap ambient-context.0.2
- run: opam exec -- dune build --profile=release --force @install @runtest - run: opam exec -- dune build --profile=release --force @install @runtest
compat: compat:

View file

@ -60,6 +60,7 @@
:with-test))) :with-test)))
(depopts (depopts
hmap hmap
(ambient-context (>= 0.2))
(trace (trace
(>= 0.6))) (>= 0.6)))
(tags (tags

View file

@ -24,6 +24,7 @@ depends: [
] ]
depopts: [ depopts: [
"hmap" "hmap"
"ambient-context" {>= "0.2"}
"trace" {>= "0.6"} "trace" {>= "0.6"}
] ]
build: [ build: [

5
src/ambient-context/dune Normal file
View file

@ -0,0 +1,5 @@
(library
(name moonpool_ambient_context)
(public_name moonpool.ambient-context)
(optional) ; ambient-context
(libraries moonpool hmap ambient-context))

View file

@ -0,0 +1,21 @@
open struct
module TLS = Moonpool.Task_local_storage
end
let storage : Ambient_context.Storage.t =
{
name = "moonpool";
get_context = TLS.get_local_hmap;
with_context =
(fun new_hmap f ->
let old = TLS.get_local_hmap () in
TLS.set_local_hmap new_hmap;
match f () with
| x ->
TLS.set_local_hmap old;
x
| exception exn ->
let bt = Printexc.get_raw_backtrace () in
TLS.set_local_hmap old;
Printexc.raise_with_backtrace exn bt);
}