From eadd2827a55398069eee0d95e4aa42ce30ca1e1b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 8 Apr 2024 16:23:59 +0200 Subject: [PATCH] refact: Accept &str in eval_from_string (cherry picked from commit 0ce86f66801ce0a052316a742ded241c773099dd) --- rust/nix-expr/src/eval_state.rs | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/rust/nix-expr/src/eval_state.rs b/rust/nix-expr/src/eval_state.rs index 15e2e6a..30baa34 100644 --- a/rust/nix-expr/src/eval_state.rs +++ b/rust/nix-expr/src/eval_state.rs @@ -68,7 +68,7 @@ impl EvalState { pub fn store(&self) -> &Store { &self.store } - pub fn eval_from_string(&self, expr: String, path: String) -> Result { + pub fn eval_from_string(&self, expr: &str, path: &str) -> Result { let expr_ptr = CString::new(expr).with_context(|| "eval_from_string: expr contains null byte")?; let path_ptr = @@ -214,9 +214,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); - let v = es - .eval_from_string("1".to_string(), "".to_string()) - .unwrap(); + let v = es.eval_from_string("1", "").unwrap(); let v2 = v.clone(); es.force(&v).unwrap(); let t = es.value_type(&v).unwrap(); @@ -233,9 +231,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); - let v = es - .eval_from_string("true".to_string(), "".to_string()) - .unwrap(); + let v = es.eval_from_string("true", "").unwrap(); es.force(&v).unwrap(); let t = es.value_type(&v).unwrap(); assert!(t == ValueType::Bool); @@ -248,9 +244,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); - let v = es - .eval_from_string("\"hello\"".to_string(), "".to_string()) - .unwrap(); + let v = es.eval_from_string("\"hello\"", "").unwrap(); es.force(&v).unwrap(); let t = es.value_type(&v).unwrap(); assert!(t == ValueType::String); @@ -265,9 +259,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); - let v = es - .eval_from_string("true".to_string(), "".to_string()) - .unwrap(); + let v = es.eval_from_string("true", "").unwrap(); es.force(&v).unwrap(); let r = es.require_string(&v); assert!(r.is_err()); @@ -285,9 +277,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); - let v = es - .eval_from_string("/foo".to_string(), "".to_string()) - .unwrap(); + let v = es.eval_from_string("/foo", "").unwrap(); es.force(&v).unwrap(); let r = es.require_string(&v); assert!(r.is_err()); @@ -305,10 +295,7 @@ mod tests { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); let v = es - .eval_from_string( - "builtins.substring 0 1 \"ü\"".to_string(), - "".to_string(), - ) + .eval_from_string("builtins.substring 0 1 \"ü\"", "") .unwrap(); es.force(&v).unwrap(); let t = es.value_type(&v).unwrap(); @@ -329,7 +316,7 @@ mod tests { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); let v = es - .eval_from_string("(derivation { name = \"hello\"; system = \"dummy\"; builder = \"cmd.exe\"; }).outPath".to_string(), "".to_string()) + .eval_from_string("(derivation { name = \"hello\"; system = \"dummy\"; builder = \"cmd.exe\"; }).outPath", "") .unwrap(); es.force(&v).unwrap(); let t = es.value_type(&v).unwrap(); @@ -347,9 +334,7 @@ mod tests { gc_registering_current_thread(|| { let store = Store::open("auto").unwrap(); let es = EvalState::new(store).unwrap(); - let v = es - .eval_from_string("{ }".to_string(), "".to_string()) - .unwrap(); + let v = es.eval_from_string("{ }", "").unwrap(); es.force(&v).unwrap(); let t = es.value_type(&v).unwrap(); assert!(t == ValueType::AttrSet);