running_process_platform_internal/
spawn_admission.rs1use std::{any::Any, fmt, io, sync::Arc};
4
5#[derive(Clone)]
10pub struct SpawnAdmission {
11 acquire: Arc<dyn Fn() -> io::Result<Box<dyn Any>> + Send + Sync>,
12}
13
14impl fmt::Debug for SpawnAdmission {
15 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
16 formatter.write_str("SpawnAdmission { .. }")
17 }
18}
19
20impl SpawnAdmission {
21 pub fn new<F, G>(acquire: F) -> Self
24 where
25 F: Fn() -> io::Result<G> + Send + Sync + 'static,
26 G: 'static,
27 {
28 Self {
29 acquire: Arc::new(move || acquire().map(|permit| Box::new(permit) as Box<dyn Any>)),
30 }
31 }
32
33 pub(crate) fn run<T>(&self, spawn: impl FnOnce() -> io::Result<T>) -> io::Result<T> {
34 let _permit = (self.acquire)()?;
35 spawn()
36 }
37}