aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/stdx/src/lib.rs')
-rw-r--r--crates/stdx/src/lib.rs31
1 files changed, 26 insertions, 5 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index d26be4853..857567a85 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -1,7 +1,8 @@
1//! Missing batteries for standard libraries. 1//! Missing batteries for standard libraries.
2use std::{cmp::Ordering, ops, process, time::Instant}; 2use std::{cmp::Ordering, ops, time::Instant};
3 3
4mod macros; 4mod macros;
5pub mod process;
5pub mod panic_context; 6pub mod panic_context;
6 7
7pub use always_assert::{always, never}; 8pub use always_assert::{always, never};
@@ -178,17 +179,30 @@ where
178 start..start + len 179 start..start + len
179} 180}
180 181
181pub struct JodChild(pub process::Child); 182pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
183 struct D<F: FnOnce()>(Option<F>);
184 impl<F: FnOnce()> Drop for D<F> {
185 fn drop(&mut self) {
186 if let Some(f) = self.0.take() {
187 f()
188 }
189 }
190 }
191 D(Some(f))
192}
193
194#[repr(transparent)]
195pub struct JodChild(pub std::process::Child);
182 196
183impl ops::Deref for JodChild { 197impl ops::Deref for JodChild {
184 type Target = process::Child; 198 type Target = std::process::Child;
185 fn deref(&self) -> &process::Child { 199 fn deref(&self) -> &std::process::Child {
186 &self.0 200 &self.0
187 } 201 }
188} 202}
189 203
190impl ops::DerefMut for JodChild { 204impl ops::DerefMut for JodChild {
191 fn deref_mut(&mut self) -> &mut process::Child { 205 fn deref_mut(&mut self) -> &mut std::process::Child {
192 &mut self.0 206 &mut self.0
193 } 207 }
194} 208}
@@ -200,6 +214,13 @@ impl Drop for JodChild {
200 } 214 }
201} 215}
202 216
217impl JodChild {
218 pub fn into_inner(self) -> std::process::Child {
219 // SAFETY: repr transparent
220 unsafe { std::mem::transmute::<JodChild, std::process::Child>(self) }
221 }
222}
223
203#[cfg(test)] 224#[cfg(test)]
204mod tests { 225mod tests {
205 use super::*; 226 use super::*;