From 9ffc07dedd802f844f28be1c322ccb73104e8ae1 Mon Sep 17 00:00:00 2001 From: Patrick Ferris Date: Thu, 13 Nov 2025 08:50:11 +0000 Subject: [PATCH] Add Eio_unix.Stdenv.with_env for updating environments --- lib_eio/unix/eio_unix.ml | 21 +++++++++++++++++++++ lib_eio/unix/eio_unix.mli | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib_eio/unix/eio_unix.ml b/lib_eio/unix/eio_unix.ml index 14f818193..dadcf14d0 100644 --- a/lib_eio/unix/eio_unix.ml +++ b/lib_eio/unix/eio_unix.ml @@ -47,4 +47,25 @@ module Stdenv = struct debug : Eio.Debug.t; backend_id: string; > + + let with_env + ?stdin ?stdout ?stderr ?net ?domain_mgr + ?process_mgr ?clock ?mono_clock ?fs ?cwd + ?secure_random ?debug ?backend_id (env : base) : base + = + object + method stdin = Option.value ~default:env#stdin stdin + method stdout = Option.value ~default:env#stdout stdout + method stderr = Option.value ~default:env#stderr stderr + method net = Option.value ~default:env#net net + method domain_mgr = Option.value ~default:env#domain_mgr domain_mgr + method process_mgr = Option.value ~default:env#process_mgr process_mgr + method clock = Option.value ~default:env#clock clock + method mono_clock = Option.value ~default:env#mono_clock mono_clock + method fs = Option.value ~default:env#fs fs + method cwd = Option.value ~default:env#cwd cwd + method secure_random = Option.value ~default:env#secure_random secure_random + method debug = Option.value ~default:env#debug debug + method backend_id = Option.value ~default:env#backend_id backend_id + end end diff --git a/lib_eio/unix/eio_unix.mli b/lib_eio/unix/eio_unix.mli index 05dfcb99e..80ab1c754 100644 --- a/lib_eio/unix/eio_unix.mli +++ b/lib_eio/unix/eio_unix.mli @@ -89,6 +89,26 @@ module Stdenv : sig (** The common set of features provided by all traditional operating systems (BSDs, Linux, Mac, Windows). You can use the functions in {!Eio.Stdenv} to access these fields if you prefer. *) + + val with_env : + ?stdin:source_ty r -> + ?stdout:sink_ty r -> + ?stderr:sink_ty r -> + ?net:[ `Generic | `Unix ] Eio.Net.ty r -> + ?domain_mgr:Eio.Domain_manager.ty r -> + ?process_mgr:Process.mgr_ty r -> + ?clock:float Eio.Time.clock_ty r -> + ?mono_clock:Eio.Time.Mono.ty r -> + ?fs:Eio.Fs.dir_ty Eio.Path.t -> + ?cwd:Eio.Fs.dir_ty Eio.Path.t -> + ?secure_random:Eio.Flow.source_ty r -> + ?debug:Eio.Debug.t -> + ?backend_id:string -> + base -> + base + (** [with_env env] allows you to replace parts of [env] with new resources. + + By default it will select the resource from [env]. *) end (** API for Eio backends only. *)