From 8dd79694092a8423fdca4c9ec0e2a3bba5eba400 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 9 Nov 2022 10:13:13 +0100 Subject: [PATCH] Expose a way to hold on to the arc on object creation --- minijinja/src/value/mod.rs | 18 +++++++++++------- minijinja/src/vm/context.rs | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/minijinja/src/value/mod.rs b/minijinja/src/value/mod.rs index 689bc6e1..21506b32 100644 --- a/minijinja/src/value/mod.rs +++ b/minijinja/src/value/mod.rs @@ -423,7 +423,16 @@ impl Value { /// let val = Value::from_object(Thing { id: 42 }); /// ``` pub fn from_object(value: T) -> Value { - Value::from_rc_object(Arc::new(value)) + Value::from_arc_object(Arc::new(value)) + } + + /// Creates a value from a reference counted dynamic object. + /// + /// As values are internally holding dynamic objects in an `Arc` it can be + /// convenient to hold on to it so that the ownership can be shared between + /// the boxed value and outside. + pub fn from_arc_object(value: Arc) -> Value { + ValueRepr::Dynamic(value as Arc).into() } /// Creates a callable value from a function. @@ -764,11 +773,6 @@ impl Value { )) } - /// Creates a value from a reference counted dynamic object. - pub(crate) fn from_rc_object(value: Arc) -> Value { - ValueRepr::Dynamic(value as Arc).into() - } - pub(crate) fn try_into_key(self) -> Result { match self.0 { ValueRepr::Bool(val) => Ok(Key::Bool(val)), @@ -987,7 +991,7 @@ fn test_dynamic_object_roundtrip() { } let x = Arc::new(X(Default::default())); - let x_value = Value::from_rc_object(x.clone()); + let x_value = Value::from_arc_object(x.clone()); x.0.fetch_add(42, atomic::Ordering::Relaxed); let x_clone = Value::from_serializable(&x_value); x.0.fetch_add(23, atomic::Ordering::Relaxed); diff --git a/minijinja/src/vm/context.rs b/minijinja/src/vm/context.rs index 7d5c42e2..b866e959 100644 --- a/minijinja/src/vm/context.rs +++ b/minijinja/src/vm/context.rs @@ -179,7 +179,7 @@ impl<'env> Context<'env> { // if we are a loop, check if we are looking up the special loop var. if let Some(ref l) = frame.current_loop { if l.with_loop_var && key == "loop" { - return Some(Value::from_rc_object(l.object.clone())); + return Some(Value::from_arc_object(l.object.clone())); } }