Skip to content

Commit

Permalink
refactor(fluent-bundle): Combine Scope lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperDeSutter authored and alerque committed May 6, 2024
1 parent 1ee2a7c commit b96f38a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions fluent-bundle/src/resolver/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::resource::FluentResource;
use crate::types::FluentValue;

impl<'bundle> WriteOrResolve<'bundle> for ast::Expression<&'bundle str> {
fn write_or_resolve<'ast, 'args, 'errors, R, M, T>(
&'ast self,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
fn write_or_resolve<'other, R, M, T>(
&'bundle self,
scope: &mut Scope<'bundle, 'other, R, M>,
context: &mut T,
) -> T::Result
where
Expand Down
6 changes: 3 additions & 3 deletions fluent-bundle/src/resolver/inline_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::borrow::{Borrow, Cow};
use std::fmt;

impl<'bundle> WriteOrResolve<'bundle> for ast::InlineExpression<&'bundle str> {
fn write_or_resolve<'ast, 'args, 'errors, R, M, T>(
&'ast self,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
fn write_or_resolve<'other, R, M, T>(
&'bundle self,
scope: &mut Scope<'bundle, 'other, R, M>,
context: &mut T,
) -> T::Result
where
Expand Down
36 changes: 18 additions & 18 deletions fluent-bundle/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ pub trait WriteOrResolveContext<'bundle> {
type Result;

fn unescape(&mut self, s: &'bundle str) -> Self::Result;
fn value<'ast, 'args, 'errors, R, M>(
fn value<'other, R, M>(
&mut self,
scope: &Scope<'bundle, 'ast, 'args, 'errors, R, M>,
scope: &Scope<'bundle, 'other, R, M>,
value: Cow<FluentValue<'bundle>>,
) -> Self::Result
where
R: Borrow<FluentResource>,
M: MemoizerKind;

fn error<E: WriteOrResolve<'bundle>>(&mut self, exp: &E, is_ref: bool) -> Self::Result;
fn resolve_pattern<'ast, 'args, 'errors, R, M>(
fn resolve_pattern<'other, R, M>(
&mut self,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
pattern: &'ast ast::Pattern<&'bundle str>,
scope: &mut Scope<'bundle, 'other, R, M>,
pattern: &'bundle ast::Pattern<&'bundle str>,
) -> Self::Result
where
R: Borrow<FluentResource>,
Expand All @@ -58,9 +58,9 @@ where
unescape_unicode(self, s)
}

fn value<'ast, 'args, 'errors, R, M>(
fn value<'other, R, M>(
&mut self,
scope: &Scope<'bundle, 'ast, 'args, 'errors, R, M>,
scope: &Scope<'bundle, 'other, R, M>,
value: Cow<FluentValue<'bundle>>,
) -> Self::Result
where
Expand All @@ -86,10 +86,10 @@ where
Ok(())
}

fn resolve_pattern<'ast, 'args, 'errors, R, M>(
fn resolve_pattern<'other, R, M>(
&mut self,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
pattern: &'ast ast::Pattern<&'bundle str>,
scope: &mut Scope<'bundle, 'other, R, M>,
pattern: &'bundle ast::Pattern<&'bundle str>,
) -> Self::Result
where
R: Borrow<FluentResource>,
Expand All @@ -109,9 +109,9 @@ impl<'bundle> WriteOrResolveContext<'bundle> for ResolveContext {
unescape_unicode_to_string(s).into()
}

fn value<'ast, 'args, 'errors, R, M>(
fn value<'other, R, M>(
&mut self,
_scope: &Scope<'bundle, 'ast, 'args, 'errors, R, M>,
_scope: &Scope<'bundle, 'other, R, M>,
value: Cow<FluentValue<'bundle>>,
) -> Self::Result
where
Expand All @@ -125,10 +125,10 @@ impl<'bundle> WriteOrResolveContext<'bundle> for ResolveContext {
FluentValue::Error
}

fn resolve_pattern<'ast, 'args, 'errors, R, M>(
fn resolve_pattern<'other, R, M>(
&mut self,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
pattern: &'ast ast::Pattern<&'bundle str>,
scope: &mut Scope<'bundle, 'other, R, M>,
pattern: &'bundle ast::Pattern<&'bundle str>,
) -> Self::Result
where
R: Borrow<FluentResource>,
Expand All @@ -139,9 +139,9 @@ impl<'bundle> WriteOrResolveContext<'bundle> for ResolveContext {
}

pub trait WriteOrResolve<'bundle> {
fn write_or_resolve<'ast, 'args, 'errors, R, M, T>(
&'ast self,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
fn write_or_resolve<'other, R, M, T>(
&'bundle self,
scope: &mut Scope<'bundle, 'other, R, M>,
context: &mut T,
) -> T::Result
where
Expand Down
12 changes: 6 additions & 6 deletions fluent-bundle/src/resolver/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::types::FluentValue;

const MAX_PLACEABLES: u8 = 100;

pub fn write_pattern<'bundle, 'ast, 'args, 'errors, W, R, M>(
pattern: &'ast ast::Pattern<&'bundle str>,
pub fn write_pattern<'bundle, 'other, W, R, M>(
pattern: &'bundle ast::Pattern<&'bundle str>,
w: &mut W,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
scope: &mut Scope<'bundle, 'other, R, M>,
) -> fmt::Result
where
W: fmt::Write,
Expand Down Expand Up @@ -66,9 +66,9 @@ where
Ok(())
}

pub fn resolve_pattern<'bundle, 'ast, 'args, 'errors, R, M>(
pattern: &'ast ast::Pattern<&'bundle str>,
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
pub fn resolve_pattern<'bundle, 'other, R, M>(
pattern: &'bundle ast::Pattern<&'bundle str>,
scope: &mut Scope<'bundle, 'other, R, M>,
) -> FluentValue<'bundle>
where
R: Borrow<FluentResource>,
Expand Down
24 changes: 12 additions & 12 deletions fluent-bundle/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ use std::borrow::Borrow;
use super::{ResolveContext, ResolverError, WriteOrResolve, WriteOrResolveContext};

/// State for a single `WriteOrResolve::write_or_resolve` call.
pub struct Scope<'bundle, 'ast, 'args, 'errors, R, M> {
pub struct Scope<'bundle, 'other, R, M> {
/// The current `FluentBundle` instance.
pub bundle: &'bundle FluentBundle<R, M>,
/// The current arguments passed by the developer.
pub(super) args: Option<&'args FluentArgs<'args>>,
pub(super) args: Option<&'other FluentArgs<'other>>,
/// Local args
pub(super) local_args: Option<FluentArgs<'bundle>>,
/// The running count of resolved placeables. Used to detect the Billion
/// Laughs and Quadratic Blowup attacks.
pub(super) placeables: u8,
/// Tracks hashes to prevent infinite recursion.
travelled: smallvec::SmallVec<[&'ast ast::Pattern<&'bundle str>; 2]>,
travelled: smallvec::SmallVec<[&'bundle ast::Pattern<&'bundle str>; 2]>,
/// Track errors accumulated during resolving.
pub errors: Option<&'errors mut Vec<FluentError>>,
pub errors: Option<&'other mut Vec<FluentError>>,
/// Makes the resolver bail.
pub dirty: bool,
}

impl<'bundle, 'ast, 'args, 'errors, R, M> Scope<'bundle, 'ast, 'args, 'errors, R, M> {
impl<'bundle, 'other, R, M> Scope<'bundle, 'other, R, M> {
pub fn new(
bundle: &'bundle FluentBundle<R, M>,
args: Option<&'args FluentArgs>,
errors: Option<&'errors mut Vec<FluentError>>,
args: Option<&'other FluentArgs>,
errors: Option<&'other mut Vec<FluentError>>,
) -> Self {
Scope {
bundle,
Expand All @@ -57,8 +57,8 @@ impl<'bundle, 'ast, 'args, 'errors, R, M> Scope<'bundle, 'ast, 'args, 'errors, R
pub fn maybe_track<T>(
&mut self,
context: &mut T,
pattern: &'ast ast::Pattern<&'bundle str>,
exp: &'ast ast::Expression<&'bundle str>,
pattern: &'bundle ast::Pattern<&'bundle str>,
exp: &'bundle ast::Expression<&'bundle str>,
) -> T::Result
where
R: Borrow<FluentResource>,
Expand All @@ -79,8 +79,8 @@ impl<'bundle, 'ast, 'args, 'errors, R, M> Scope<'bundle, 'ast, 'args, 'errors, R
pub fn track<T>(
&mut self,
context: &mut T,
pattern: &'ast ast::Pattern<&'bundle str>,
exp: &'ast ast::InlineExpression<&'bundle str>,
pattern: &'bundle ast::Pattern<&'bundle str>,
exp: &'bundle ast::InlineExpression<&'bundle str>,
) -> T::Result
where
R: Borrow<FluentResource>,
Expand All @@ -100,7 +100,7 @@ impl<'bundle, 'ast, 'args, 'errors, R, M> Scope<'bundle, 'ast, 'args, 'errors, R

pub fn get_arguments(
&mut self,
arguments: Option<&'ast ast::CallArguments<&'bundle str>>,
arguments: Option<&'bundle ast::CallArguments<&'bundle str>>,
) -> (Vec<FluentValue<'bundle>>, FluentArgs<'bundle>)
where
R: Borrow<FluentResource>,
Expand Down

0 comments on commit b96f38a

Please sign in to comment.