diff options
Diffstat (limited to 'crates/stdx/src/lib.rs')
-rw-r--r-- | crates/stdx/src/lib.rs | 31 |
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. |
2 | use std::{cmp::Ordering, ops, process, time::Instant}; | 2 | use std::{cmp::Ordering, ops, time::Instant}; |
3 | 3 | ||
4 | mod macros; | 4 | mod macros; |
5 | pub mod process; | ||
5 | pub mod panic_context; | 6 | pub mod panic_context; |
6 | 7 | ||
7 | pub use always_assert::{always, never}; | 8 | pub use always_assert::{always, never}; |
@@ -178,17 +179,30 @@ where | |||
178 | start..start + len | 179 | start..start + len |
179 | } | 180 | } |
180 | 181 | ||
181 | pub struct JodChild(pub process::Child); | 182 | pub 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)] | ||
195 | pub struct JodChild(pub std::process::Child); | ||
182 | 196 | ||
183 | impl ops::Deref for JodChild { | 197 | impl 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 | ||
190 | impl ops::DerefMut for JodChild { | 204 | impl 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 | ||
217 | impl 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)] |
204 | mod tests { | 225 | mod tests { |
205 | use super::*; | 226 | use super::*; |