diff options
Diffstat (limited to 'crates/stdx/src/lib.rs')
-rw-r--r-- | crates/stdx/src/lib.rs | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index b0a18d58d..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,18 +179,30 @@ where | |||
178 | start..start + len | 179 | start..start + len |
179 | } | 180 | } |
180 | 181 | ||
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 | |||
181 | #[repr(transparent)] | 194 | #[repr(transparent)] |
182 | pub struct JodChild(pub process::Child); | 195 | pub struct JodChild(pub std::process::Child); |
183 | 196 | ||
184 | impl ops::Deref for JodChild { | 197 | impl ops::Deref for JodChild { |
185 | type Target = process::Child; | 198 | type Target = std::process::Child; |
186 | fn deref(&self) -> &process::Child { | 199 | fn deref(&self) -> &std::process::Child { |
187 | &self.0 | 200 | &self.0 |
188 | } | 201 | } |
189 | } | 202 | } |
190 | 203 | ||
191 | impl ops::DerefMut for JodChild { | 204 | impl ops::DerefMut for JodChild { |
192 | fn deref_mut(&mut self) -> &mut process::Child { | 205 | fn deref_mut(&mut self) -> &mut std::process::Child { |
193 | &mut self.0 | 206 | &mut self.0 |
194 | } | 207 | } |
195 | } | 208 | } |
@@ -202,9 +215,9 @@ impl Drop for JodChild { | |||
202 | } | 215 | } |
203 | 216 | ||
204 | impl JodChild { | 217 | impl JodChild { |
205 | pub fn into_inner(self) -> process::Child { | 218 | pub fn into_inner(self) -> std::process::Child { |
206 | // SAFETY: repr transparent | 219 | // SAFETY: repr transparent |
207 | unsafe { std::mem::transmute::<JodChild, process::Child>(self) } | 220 | unsafe { std::mem::transmute::<JodChild, std::process::Child>(self) } |
208 | } | 221 | } |
209 | } | 222 | } |
210 | 223 | ||