pub fn reborrow<T: ?Sized, R>(
obj: &T,
f: for<'a> fn(&'a T, &'a Scope) -> R,
) -> RExpand description
Reborrows a reference to a dynamic object with the scope’s lifetime.
Within the trait methods of Object, StructObject or SeqObject of a
value that is currently referenced by a StackHandle, this utility can be
used to reborrow &self with the lifetime of the scope.
This lets code return a Value that borrows into a field of &self.
use minijinja::value::{Value, StructObject};
use minijinja_stack_ref::{reborrow, scope};
struct MyObject {
values: Vec<u32>,
}
impl StructObject for MyObject {
fn get_field(&self, field: &str) -> Option<Value> {
match field {
"values" => Some(reborrow(self, |slf, scope| {
scope.seq_object_ref(&slf.values[..])
})),
_ => None
}
}
}
let obj = MyObject { values: (0..100).collect() };
scope(|scope| {
let value = scope.struct_object_ref(&obj);
// do something with value
})§Panics
This function panics if the passed object is not currently interacted with
or not created via the Scope. In other words this function can only be
used within object methods of Object, SeqObject or StructObject
of an object that has been put into a Value via a Scope.
To check if reborrowing is possible, can_reborrow can be used instead.