diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-10-04 15:02:04 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-10-04 15:02:04 +0200 |
commit | 82d9d7629bf897719bc1d9e28f750c4cdecb0b5a (patch) | |
tree | 8b00c7bcf39ad8ed154b8d45e454bf7c9c4556a8 | |
parent | e8d87630e4cd6960244fcd4dc15447648b596a33 (diff) | |
download | rebel-82d9d7629bf897719bc1d9e28f750c4cdecb0b5a.tar rebel-82d9d7629bf897719bc1d9e28f750c4cdecb0b5a.zip |
recipe: used scoped TLS to base recipe name to deserializer
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/recipe.rs | 27 |
3 files changed, 14 insertions, 21 deletions
@@ -811,6 +811,7 @@ dependencies = [ "oci-spec", "olpc-cjson", "rm_rf", + "scoped-tls-hkt", "serde", "serde_json", "serde_yaml", @@ -864,6 +865,12 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -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<Option<String>> = 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<TaskID, D::Error> @@ -97,20 +90,12 @@ pub fn read_recipes<P: AsRef<Path>>(path: P) -> Result<HashMap<TaskID, TaskDef>> 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() { |