Skip to content

Commit

Permalink
add restricting lifetimes to Tag trait to speed up Literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Fogapod committed Mar 4, 2024
1 parent 9601cfd commit 40d27cd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ mod tests {

#[typetag::deserialize]
impl Tag for Increment {
fn generate<'a>(&self, m: &Match<'a>) -> std::borrow::Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(
&'tag self,
m: &Match<'inp>,
) -> std::borrow::Cow<'tag, str> {
let input = m.get_match();

let input_number: i64 = match input.parse() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
//! // `typetag` is only required with `deserialize` crate feature
//! #[typetag::deserialize]
//! impl Tag for StringCase {
//! fn generate<'a>(&self, m: &Match<'a>) -> std::borrow::Cow<'a, str> {
//! fn generate<'tag, 'inp: 'tag>(&self, m: &Match) -> std::borrow::Cow<'_, str> {
//! if self.0 {
//! m.get_match().to_uppercase()
//! } else {
Expand Down
6 changes: 3 additions & 3 deletions src/tag.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::Match;

use std::{borrow::Cow, fmt::Debug};

use crate::Match;

use dyn_clone::{clone_trait_object, DynClone};

/// Receives match and provides replacement
#[cfg_attr(feature = "deserialize", typetag::deserialize)]
pub trait Tag: DynClone + Debug + Send + Sync {
/// Make suitable replacement
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str>;
fn generate<'tag, 'inp: 'tag>(&'tag self, m: &Match<'inp>) -> Cow<'tag, str>;
}

clone_trait_object!(Tag);
Expand Down
14 changes: 7 additions & 7 deletions src/tag_impls/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ impl PrecomputedLiteral {
}

#[inline]
pub(crate) fn handle_mimic_action(&self, action: MimicAction) -> String {
pub(crate) fn handle_mimic_action(&self, action: MimicAction) -> Cow<'_, str> {
match action {
MimicAction::Title => self.body_title.clone(),
MimicAction::Uppercase => self.body_upper.clone(),
MimicAction::Nothing => self.body.clone(),
MimicAction::Title => &self.body_title,
MimicAction::Uppercase => &self.body_upper,
MimicAction::Nothing => &self.body,
}
.into()
}
}

Expand Down Expand Up @@ -80,17 +81,16 @@ impl Literal {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Literal {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&'tag self, m: &Match<'inp>) -> Cow<'tag, str> {
if self.0.has_template {
let interpolated = m.interpolate(&self.0.body);

m.mimic_case(interpolated)
m.mimic_case(interpolated).into()
} else {
let action = self.0.mimic_case_action(m.get_match());

self.0.handle_mimic_action(action)
}
.into()
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/tag_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Original {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Original {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&'tag self, m: &Match<'inp>) -> Cow<'tag, str> {
m.get_match().into()
}
}
Expand All @@ -52,7 +52,7 @@ impl Delete {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Delete {
fn generate<'a>(&self, _: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&self, _: &Match) -> Cow<'_, str> {
Cow::Borrowed("")
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Any {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Any {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&'tag self, m: &Match<'inp>) -> Cow<'tag, str> {
let i = fastrand::usize(..self.0.len());

self.0[i].generate(m)
Expand Down Expand Up @@ -181,7 +181,7 @@ impl Weights {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Weights {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&'tag self, m: &Match<'inp>) -> Cow<'tag, str> {
self.choices[self.random_choice()].generate(m)
}
}
Expand All @@ -207,7 +207,7 @@ impl Upper {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Upper {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&self, m: &Match) -> Cow<'_, str> {
self.0.generate(m).to_uppercase().into()
}
}
Expand All @@ -233,7 +233,7 @@ impl Lower {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Lower {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&self, m: &Match) -> Cow<'_, str> {
self.0.generate(m).to_lowercase().into()
}
}
Expand All @@ -259,7 +259,7 @@ impl Concat {

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Concat {
fn generate<'a>(&self, m: &Match<'a>) -> Cow<'a, str> {
fn generate<'tag, 'inp: 'tag>(&'tag self, m: &Match<'inp>) -> Cow<'_, str> {
self.0 .0.generate(m) + self.0 .1.generate(m)
}
}
Expand All @@ -285,8 +285,10 @@ mod tests {
}
}

fn apply<'a>(tag: &dyn Tag, self_matching_pattern: &'a str) -> Cow<'a, str> {
tag.generate(&make_match(self_matching_pattern)).into()
fn apply<'a, 'b: 'a>(tag: &dyn Tag, self_matching_pattern: &'a str) -> Cow<'a, str> {
tag.generate(&make_match(self_matching_pattern))
.to_string()
.into()
}

#[test]
Expand Down

0 comments on commit 40d27cd

Please sign in to comment.