From 16a37f409776775341c5de8a8812eaeb8b1fe7f0 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Wed, 20 Jul 2022 15:38:30 +0200 Subject: Remove support for deprecated `_parent` field The support for the magic `_parent` field is deprecated since v0.8.0 or issue #180. It's bothersome to keep this feature alive, when no-one should be using it for 3 years. --- testing/tests/inheritance.rs | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'testing/tests/inheritance.rs') diff --git a/testing/tests/inheritance.rs b/testing/tests/inheritance.rs index 2dac362..5d8aefb 100644 --- a/testing/tests/inheritance.rs +++ b/testing/tests/inheritance.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use askama::Template; #[derive(Template)] @@ -9,7 +11,15 @@ struct BaseTemplate<'a> { #[derive(Template)] #[template(path = "child.html")] struct ChildTemplate<'a> { - _parent: BaseTemplate<'a>, + _parent: &'a BaseTemplate<'a>, +} + +impl<'a> Deref for ChildTemplate<'a> { + type Target = BaseTemplate<'a>; + + fn deref(&self) -> &Self::Target { + self._parent + } } #[test] @@ -21,7 +31,7 @@ fn test_use_base_directly() { #[test] fn test_simple_extends() { let t = ChildTemplate { - _parent: BaseTemplate { title: "Bar" }, + _parent: &BaseTemplate { title: "Bar" }, }; assert_eq!( t.render().unwrap(), @@ -43,6 +53,7 @@ fn test_empty_child() { pub mod parent { use askama::Template; + #[derive(Template)] #[template(path = "base.html")] pub struct BaseTemplate<'a> { @@ -53,17 +64,26 @@ pub mod parent { pub mod child { use super::parent::*; use askama::Template; + #[derive(Template)] #[template(path = "child.html")] pub struct ChildTemplate<'a> { - pub _parent: BaseTemplate<'a>, + pub _parent: &'a BaseTemplate<'a>, + } + + impl<'a> std::ops::Deref for ChildTemplate<'a> { + type Target = BaseTemplate<'a>; + + fn deref(&self) -> &Self::Target { + self._parent + } } } #[test] fn test_different_module() { let t = child::ChildTemplate { - _parent: parent::BaseTemplate { title: "a" }, + _parent: &parent::BaseTemplate { title: "a" }, }; assert_eq!( t.render().unwrap(), @@ -81,6 +101,14 @@ struct NestedChildTemplate { _parent: NestedBaseTemplate, } +impl Deref for NestedChildTemplate { + type Target = NestedBaseTemplate; + + fn deref(&self) -> &Self::Target { + &self._parent + } +} + #[test] fn test_nested_blocks() { let t = NestedChildTemplate { @@ -109,6 +137,14 @@ struct DeepKidTemplate { item: String, } +impl Deref for DeepKidTemplate { + type Target = DeepMidTemplate; + + fn deref(&self) -> &Self::Target { + &self._parent + } +} + #[test] fn test_deep() { let t = DeepKidTemplate { -- cgit