detect_targets/
desired_targets.rsuse crate::detect_targets;
use std::sync::Arc;
use tokio::sync::OnceCell;
#[derive(Debug)]
enum DesiredTargetsInner {
AutoDetect(Arc<OnceCell<Vec<String>>>),
Initialized(Vec<String>),
}
#[derive(Debug)]
pub struct DesiredTargets(DesiredTargetsInner);
impl DesiredTargets {
fn initialized(targets: Vec<String>) -> Self {
Self(DesiredTargetsInner::Initialized(targets))
}
fn auto_detect() -> Self {
let arc = Arc::new(OnceCell::new());
let once_cell = arc.clone();
tokio::spawn(async move {
once_cell.get_or_init(detect_targets).await;
});
Self(DesiredTargetsInner::AutoDetect(arc))
}
pub async fn get(&self) -> &[String] {
use DesiredTargetsInner::*;
match &self.0 {
Initialized(targets) => targets,
AutoDetect(once_cell) => once_cell.get_or_init(detect_targets).await,
}
}
pub fn get_initialized(&self) -> Option<&[String]> {
use DesiredTargetsInner::*;
match &self.0 {
Initialized(targets) => Some(targets),
AutoDetect(..) => None,
}
}
}
pub fn get_desired_targets(opts_targets: Option<Vec<String>>) -> DesiredTargets {
if let Some(targets) = opts_targets {
DesiredTargets::initialized(targets)
} else {
DesiredTargets::auto_detect()
}
}