Skip to content

Commit e37da48

Browse files
authored
refactor(spawn): use Pid.t in Pgid.t (#15474)
More type safety Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 625278e commit e37da48

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

otherlibs/stdune/src/spawn.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,16 @@ end
7878
module Env : Env = (val if Sys.win32 then (module Env_win32) else (module Env_unix) : Env)
7979

8080
module Pgid = struct
81-
type t = int
81+
type t =
82+
| New
83+
| Pid of Pid.t
8284

83-
let new_process_group = 0
85+
let new_process_group = New
86+
let of_pid p = Pid p
8487

85-
let of_pid = function
86-
| 0 -> raise (Invalid_argument "bad pid: 0 (hint: use [Pgid.new_process_group])")
87-
| t -> if t < 0 then raise (Invalid_argument ("bad pid: " ^ string_of_int t)) else t
88+
let to_int = function
89+
| New -> 0
90+
| Pid p -> Pid.to_int p
8891
;;
8992
end
9093

@@ -114,6 +117,7 @@ let spawn_unix
114117
~setpgid
115118
~sigprocmask
116119
=
120+
let setpgid = Option.map ~f:Pgid.to_int setpgid in
117121
spawn_unix ~env ~cwd ~prog ~argv ~stdin ~stdout ~stderr ~use_vfork ~setpgid ~sigprocmask
118122
|> Pid.of_int_exn
119123
;;

otherlibs/stdune/src/spawn.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ module Pgid : sig
4242
[setpgid(0, 0)]. *)
4343
val new_process_group : t
4444

45-
(** Raises [Invalid_arg] if the value is not strictly positive. *)
46-
val of_pid : int -> t
45+
val of_pid : Pid.t -> t
4746
end
4847

4948
(** Spawn a sub-command and return its PID. This function is low-level and

0 commit comments

Comments
 (0)