Skip to content

Commit

Permalink
Merge pull request #532 from lovasoa/issue-529-simplify-lifetime-requ…
Browse files Browse the repository at this point in the history
…irements

issue 529: simplify lifetime requirements
  • Loading branch information
sunng87 committed Sep 24, 2022
2 parents 78f343b + 2aacd72 commit 3e803ef
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 152 deletions.
10 changes: 5 additions & 5 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ impl<'reg> BlockParams<'reg> {

/// A data structure holds contextual data for current block scope.
#[derive(Debug, Clone, Default)]
pub struct BlockContext<'reg> {
pub struct BlockContext<'rc> {
/// the base_path of current block scope
base_path: Vec<String>,
/// the base_value of current block scope, when the block is using a
/// constant or derived value as block base
base_value: Option<Json>,
/// current block context variables
block_params: BlockParams<'reg>,
block_params: BlockParams<'rc>,
/// local variables in current context
local_variables: LocalVars,
}

impl<'reg> BlockContext<'reg> {
impl<'rc> BlockContext<'rc> {
/// create a new `BlockContext` with default data
pub fn new() -> BlockContext<'reg> {
pub fn new() -> BlockContext<'rc> {
BlockContext::default()
}

Expand Down Expand Up @@ -120,7 +120,7 @@ impl<'reg> BlockContext<'reg> {
}

/// Set a block parameter into this block.
pub fn set_block_params(&mut self, block_params: BlockParams<'reg>) {
pub fn set_block_params(&mut self, block_params: BlockParams<'rc>) {
self.block_params = block_params;
}
}
4 changes: 2 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Context {
&'rc self,
relative_path: &[PathSeg],
block_contexts: &VecDeque<BlockContext<'reg>>,
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
) -> Result<ScopedJson<'rc>, RenderError> {
// always use absolute at the moment until we get base_value lifetime issue fixed
let resolved_visitor = parse_json_visitor(relative_path, block_contexts, true);

Expand Down Expand Up @@ -239,7 +239,7 @@ mod test {
fn navigate_from_root<'reg, 'rc>(
ctx: &'rc Context,
path: &str,
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
) -> Result<ScopedJson<'rc>, RenderError> {
let relative_path = Path::parse(path).unwrap();
ctx.navigate(relative_path.segs().unwrap(), &VecDeque::new())
}
Expand Down
4 changes: 2 additions & 2 deletions src/decorators/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::render::{Decorator, RenderContext};
#[derive(Clone, Copy)]
pub struct InlineDecorator;

fn get_name<'reg: 'rc, 'rc>(d: &Decorator<'reg, 'rc>) -> Result<String, RenderError> {
fn get_name<'reg: 'rc, 'rc>(d: &Decorator<'rc>) -> Result<String, RenderError> {
d.param(0)
.ok_or_else(|| RenderError::new("Param required for decorator \"inline\""))
.and_then(|v| {
Expand All @@ -21,7 +21,7 @@ fn get_name<'reg: 'rc, 'rc>(d: &Decorator<'reg, 'rc>) -> Result<String, RenderEr
impl DecoratorDef for InlineDecorator {
fn call<'reg: 'rc, 'rc>(
&self,
d: &Decorator<'reg, 'rc>,
d: &Decorator<'rc>,
_: &'reg Registry<'reg>,
_: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down
20 changes: 10 additions & 10 deletions src/decorators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub type DecoratorResult = Result<(), RenderError>;
pub trait DecoratorDef {
fn call<'reg: 'rc, 'rc>(
&'reg self,
d: &Decorator<'reg, 'rc>,
d: &Decorator<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand All @@ -69,7 +69,7 @@ pub trait DecoratorDef {
/// Implement DecoratorDef for bare function so we can use function as decorator
impl<
F: for<'reg, 'rc> Fn(
&Decorator<'reg, 'rc>,
&Decorator<'rc>,
&'reg Registry<'reg>,
&'rc Context,
&mut RenderContext<'reg, 'rc>,
Expand All @@ -78,7 +78,7 @@ impl<
{
fn call<'reg: 'rc, 'rc>(
&'reg self,
d: &Decorator<'reg, 'rc>,
d: &Decorator<'rc>,
reg: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -114,7 +114,7 @@ mod test {
handlebars.register_decorator(
"foo",
Box::new(
|_: &Decorator<'_, '_>,
|_: &Decorator<'_>,
_: &Registry<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>|
Expand All @@ -139,7 +139,7 @@ mod test {
handlebars.register_decorator(
"foo",
Box::new(
|_: &Decorator<'_, '_>,
|_: &Decorator<'_>,
_: &Registry<'_>,
ctx: &Context,
rc: &mut RenderContext<'_, '_>|
Expand Down Expand Up @@ -167,7 +167,7 @@ mod test {
handlebars.register_decorator(
"bar",
Box::new(
|d: &Decorator<'_, '_>,
|d: &Decorator<'_>,
_: &Registry<'_>,
_: &Context,
rc: &mut RenderContext<'_, '_>|
Expand Down Expand Up @@ -224,7 +224,7 @@ mod test {
handlebars.register_helper(
"distance",
Box::new(
|h: &Helper<'_, '_>,
|h: &Helper<'_>,
_: &Registry<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -245,7 +245,7 @@ mod test {
handlebars.register_decorator(
"foo",
Box::new(
|d: &Decorator<'_, '_>,
|d: &Decorator<'_>,
_: &Registry<'_>,
_: &Context,
rc: &mut RenderContext<'_, '_>|
Expand All @@ -256,7 +256,7 @@ mod test {
.and_then(|v| as_string(v.value()))
.unwrap_or("")
.to_owned();
let new_helper = move |h: &Helper<'_, '_>,
let new_helper = move |h: &Helper<'_>,
_: &Registry<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -282,7 +282,7 @@ mod test {
handlebars.register_decorator(
"bar",
Box::new(
|_: &Decorator<'_, '_>,
|_: &Decorator<'_>,
_: &Registry<'_>,
_: &Context,
rc: &mut RenderContext<'_, '_>|
Expand Down
4 changes: 1 addition & 3 deletions src/helpers/block_util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::block::BlockContext;
use crate::json::value::PathAndJson;

pub(crate) fn create_block<'reg: 'rc, 'rc>(
param: &'rc PathAndJson<'reg, 'rc>,
) -> BlockContext<'reg> {
pub(crate) fn create_block<'rc>(param: &PathAndJson<'rc>) -> BlockContext<'rc> {
let mut block = BlockContext::new();

if let Some(new_path) = param.context_path() {
Expand Down
18 changes: 9 additions & 9 deletions src/helpers/helper_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fn update_block_context<'reg>(
}
}

fn set_block_param<'reg: 'rc, 'rc>(
block: &mut BlockContext<'reg>,
h: &Helper<'reg, 'rc>,
fn set_block_param<'rc>(
block: &mut BlockContext<'rc>,
h: &Helper<'rc>,
base_path: Option<&Vec<String>>,
k: &Json,
v: &Json,
Expand Down Expand Up @@ -66,7 +66,7 @@ pub struct EachHelper;
impl HelperDef for EachHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -183,13 +183,13 @@ mod test {
assert!(handlebars
.register_template_string(
"t0",
"{{#each this}}{{@first}}|{{@last}}|{{@index}}:{{this}}|{{/each}}"
"{{#each this}}{{@first}}|{{@last}}|{{@index}}:{{this}}|{{/each}}",
)
.is_ok());
assert!(handlebars
.register_template_string(
"t1",
"{{#each this}}{{@first}}|{{@last}}|{{@key}}:{{this}}|{{/each}}"
"{{#each this}}{{@first}}|{{@last}}|{{@key}}:{{this}}|{{/each}}",
)
.is_ok());

Expand Down Expand Up @@ -238,7 +238,7 @@ mod test {
assert!(handlebars
.register_template_string(
"t0",
"{{#each a}}{{#each b}}{{d}}:{{../c}}{{/each}}{{/each}}"
"{{#each a}}{{#each b}}{{d}}:{{../c}}{{/each}}{{/each}}",
)
.is_ok());

Expand All @@ -256,7 +256,7 @@ mod test {
assert!(handlebars
.register_template_string(
"t0",
"{{#each b}}{{#if ../a}}{{#each this}}{{this}}{{/each}}{{/if}}{{/each}}"
"{{#each b}}{{#if ../a}}{{#each this}}{{this}}{{/each}}{{/if}}{{/each}}",
)
.is_ok());

Expand Down Expand Up @@ -373,7 +373,7 @@ mod test {
assert!(handlebars
.register_template_string(
"t0",
"{{#each a.b}}{{#each c}}{{../../d}}{{/each}}{{/each}}"
"{{#each a.b}}{{#each c}}{{../../d}}{{/each}}{{/each}}",
)
.is_ok());

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helper_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct IfHelper {
impl HelperDef for IfHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/helper_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct LogHelper;
impl HelperDef for LogHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
_: &'reg Registry<'reg>,
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -59,7 +59,7 @@ impl HelperDef for LogHelper {
impl HelperDef for LogHelper {
fn call<'reg: 'rc, 'rc>(
&self,
_: &Helper<'reg, 'rc>,
_: &Helper<'rc>,
_: &Registry<'reg>,
_: &Context,
_: &mut RenderContext<'reg, 'rc>,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/helper_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub struct LookupHelper;
impl HelperDef for LookupHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
) -> Result<ScopedJson<'rc>, RenderError> {
let collection_value = h
.param(0)
.ok_or_else(|| RenderError::new("Param not found for helper \"lookup\""))?;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helper_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct RawHelper;
impl HelperDef for RawHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helper_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct WithHelper;
impl HelperDef for WithHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down
20 changes: 10 additions & 10 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub type HelperResult = Result<(), RenderError>;
/// ```
/// use handlebars::*;
///
/// fn upper(h: &Helper<'_, '_>, _: &Handlebars<'_>, _: &Context, rc:
/// fn upper(h: &Helper< '_>, _: &Handlebars<'_>, _: &Context, rc:
/// &mut RenderContext<'_, '_>, out: &mut dyn Output)
/// -> HelperResult {
/// // get parameter from helper or throw an error
Expand All @@ -50,7 +50,7 @@ pub type HelperResult = Result<(), RenderError>;
/// use handlebars::*;
///
/// fn dummy_block<'reg, 'rc>(
/// h: &Helper<'reg, 'rc>,
/// h: &Helper<'rc>,
/// r: &'reg Handlebars<'reg>,
/// ctx: &'rc Context,
/// rc: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -90,11 +90,11 @@ pub trait HelperDef {
/// helpers like `if` and rendered as empty string.
fn call_inner<'reg: 'rc, 'rc>(
&self,
_: &Helper<'reg, 'rc>,
_: &Helper<'rc>,
_: &'reg Registry<'reg>,
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
) -> Result<ScopedJson<'rc>, RenderError> {
Err(RenderError::unimplemented())
}

Expand All @@ -113,7 +113,7 @@ pub trait HelperDef {
/// So it is not recommended to use these helpers in subexpression.
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -145,7 +145,7 @@ pub trait HelperDef {
/// implement HelperDef for bare function so we can use function as helper
impl<
F: for<'reg, 'rc> Fn(
&Helper<'reg, 'rc>,
&Helper<'rc>,
&'reg Registry<'reg>,
&'rc Context,
&mut RenderContext<'reg, 'rc>,
Expand All @@ -155,7 +155,7 @@ impl<
{
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -200,7 +200,7 @@ mod test {
impl HelperDef for MetaHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
Expand Down Expand Up @@ -248,7 +248,7 @@ mod test {
handlebars.register_helper(
"helperMissing",
Box::new(
|h: &Helper<'_, '_>,
|h: &Helper<'_>,
_: &Registry<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -262,7 +262,7 @@ mod test {
handlebars.register_helper(
"foo",
Box::new(
|h: &Helper<'_, '_>,
|h: &Helper<'_>,
_: &Registry<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand Down

0 comments on commit 3e803ef

Please sign in to comment.