From a56391e70c888e7fddd71ed7217aa9b680df47a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domen=20Ko=C5=BEar?= Date: Sat, 21 Feb 2026 10:51:51 +0000 Subject: [PATCH] feat: add `CommandWrap::spawn_with` for custom spawner functions Allows callers to provide their own spawning closure instead of the default `command.spawn()`, useful for platform-specific or custom spawning mechanisms while preserving the full wrapper lifecycle. Co-Authored-By: Claude Opus 4.6 --- src/generic_wrap.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/generic_wrap.rs b/src/generic_wrap.rs index d6e5ed4..3c4048f 100644 --- a/src/generic_wrap.rs +++ b/src/generic_wrap.rs @@ -80,6 +80,7 @@ macro_rules! Wrap { &self, command: &mut $command, wrappers: &mut ::indexmap::IndexMap<::std::any::TypeId, Box>, + spawner: impl FnOnce(&mut $command) -> ::std::io::Result<$child>, ) -> ::std::io::Result> { for (id, wrapper) in wrappers.iter_mut() { #[cfg(feature = "tracing")] @@ -87,7 +88,7 @@ macro_rules! Wrap { wrapper.pre_spawn(command, self)?; } - let mut child = command.spawn()?; + let mut child = spawner(command)?; for (id, wrapper) in wrappers.iter_mut() { #[cfg(feature = "tracing")] ::tracing::debug!(?id, "post_spawn"); @@ -115,10 +116,25 @@ macro_rules! Wrap { /// trait object, only the methods from the trait are available directly; however you /// may downcast to the concrete type of the last applied wrapper if you need to. pub fn spawn(&mut self) -> ::std::io::Result> { + self.spawn_with(|command| command.spawn()) + } + + /// Spawn the command using a custom spawner function. + /// + /// This is like [`spawn`](Self::spawn), but instead of calling `command.spawn()` + /// directly, it calls the provided closure to create the child process. This is + /// useful when you need to use a platform-specific or custom spawning mechanism. + /// + /// The lifecycle is the same as `spawn`: all `pre_spawn` hooks run first, then + /// the provided closure is called, then `post_spawn` hooks, then `wrap_child`. + pub fn spawn_with( + &mut self, + spawner: impl FnOnce(&mut $command) -> ::std::io::Result<$child>, + ) -> ::std::io::Result> { let mut command = ::std::mem::replace(&mut self.command, <$command>::new("")); let mut wrappers = ::std::mem::take(&mut self.wrappers); - let res = self.spawn_inner(&mut command, &mut wrappers); + let res = self.spawn_inner(&mut command, &mut wrappers, spawner); self.command = command; self.wrappers = wrappers;