grr-cli 2.0.0

Google tools from the terminal, at maximum performance: 401 methods across 14 Google APIs in one generated command tree, zero-config, over HTTP/3
Documentation
use tokio::sync::OnceCell;
use tracing::warn;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RuntimeFeatures {
    pub io_uring: bool,
    pub num_cpus: usize,
}

static RUNTIME_FEATURES: OnceCell<RuntimeFeatures> = OnceCell::const_new();

pub async fn detect_runtime_features() -> RuntimeFeatures {
    *RUNTIME_FEATURES
        .get_or_init(|| async {
            let io_uring = match tokio::task::spawn_blocking(detect_io_uring).await {
                Ok(value) => value,
                Err(error) => {
                    warn!("runtime feature probe failed: {error}");
                    false
                }
            };
            RuntimeFeatures {
                io_uring,
                // std since 1.59; honors affinity and cgroup limits like
                // num_cpus did, and drops a dependency.
                num_cpus: std::thread::available_parallelism()
                    .map_or(1, std::num::NonZeroUsize::get),
            }
        })
        .await
}

fn detect_io_uring() -> bool {
    #[cfg(target_os = "linux")]
    {
        if let Ok(release) = std::fs::read_to_string("/proc/sys/kernel/osrelease")
            && let Some(major) = release.split('.').next()
            && let Ok(major) = major.parse::<u32>()
            && major < 5
        {
            return false;
        }
        io_uring::IoUring::new(1).is_ok()
    }

    #[cfg(not(target_os = "linux"))]
    {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn io_uring_report_matches_target_gate() {
        let features = detect_runtime_features().await;
        assert!(!features.io_uring || cfg!(target_os = "linux"));
    }
}