can_reborrow

Function can_reborrow 

Source
pub fn can_reborrow<T: ?Sized>(obj: &T) -> bool
Expand description

Returns true if reborrowing is possible.

This can be used to make an object conditionally reborrow. If this method returns true, then reborrow will not panic.

use minijinja::value::{Value, StructObject};
use minijinja_stack_ref::{reborrow, can_reborrow, scope};

struct MyObject {
    values: Vec<u32>,
}

impl StructObject for MyObject {
    fn get_field(&self, field: &str) -> Option<Value> {
        match field {
            "values" => if can_reborrow(self) {
                Some(reborrow(self, |slf, scope| {
                    scope.seq_object_ref(&slf.values[..])
                }))
            } else {
                Some(Value::from_serialize(&self.values))
            },
            _ => None
        }
    }
}