From 82d9d7629bf897719bc1d9e28f750c4cdecb0b5a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 4 Oct 2021 15:02:04 +0200 Subject: recipe: used scoped TLS to base recipe name to deserializer --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/recipe.rs | 27 ++++++--------------------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84877ce..385d88a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -811,6 +811,7 @@ dependencies = [ "oci-spec", "olpc-cjson", "rm_rf", + "scoped-tls-hkt", "serde", "serde_json", "serde_yaml", @@ -863,6 +864,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scoped-tls-hkt" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2e9d7eaddb227e8fbaaa71136ae0e1e913ca159b86c7da82f3e8f0044ad3a63" + [[package]] name = "serde" version = "1.0.130" diff --git a/Cargo.toml b/Cargo.toml index 280c840..b1cb651 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ nix = "0.23.0" oci-spec = "0.5.1" olpc-cjson = "0.1.0" rm_rf = "0.6.1" +scoped-tls-hkt = "0.1.2" serde = { version = "1", features = ["derive"] } serde_json = "1.0.62" serde_yaml = "0.8" diff --git a/src/recipe.rs b/src/recipe.rs index 477a096..5155474 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -1,5 +1,6 @@ -use std::{cell::RefCell, collections::HashMap, fmt, fs::File, io, path::Path, result}; +use std::{collections::HashMap, fmt, fs::File, io, path::Path, result}; +use scoped_tls_hkt::scoped_thread_local; use serde::{Deserialize, Deserializer}; use walkdir::WalkDir; @@ -8,18 +9,10 @@ use crate::{ types::TaskID, }; -thread_local! { - pub static CURRENT_RECIPE: RefCell> = RefCell::new(None); -} +scoped_thread_local!(static CURRENT_RECIPE: str); fn current_recipe() -> String { - CURRENT_RECIPE.with(|current| { - current - .borrow() - .as_ref() - .expect("No current recipe") - .clone() - }) + CURRENT_RECIPE.with(|current| current.to_string()) } pub fn deserialize_task_id<'de, D>(deserializer: D) -> result::Result @@ -97,20 +90,12 @@ pub fn read_recipes>(path: P) -> Result> continue; } - let basename = match path.file_stem().map(|n| n.to_str()) { + let basename: &str = match path.file_stem().map(|n| n.to_str()) { Some(Some(v)) => v, _ => continue, }; - CURRENT_RECIPE.with(|current| { - *current.borrow_mut() = Some(basename.to_string()); - }); - - let recipe = read_recipe(path)?; - - CURRENT_RECIPE.with(|current| { - *current.borrow_mut() = None; - }); + let recipe = CURRENT_RECIPE.set(basename, || read_recipe(path))?; let mut meta = recipe.meta; if meta.name.is_empty() { -- cgit v1.2.3