Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RRuleSetIter: removed lifetimes and added from_str example #114

Merged
merged 2 commits into from
Jun 3, 2024

Conversation

AlbanWS
Copy link
Contributor

@AlbanWS AlbanWS commented May 23, 2024

This is just between a feature and a fix.

In my use-case, i have to store a RRuleSetIter inside an object up to keep the current state of the iterator and not recreate the iterator again and again

Therefore, it was not possible due to the lifetime of RRuleSetIter, currently :

pub struct Recurrent<'a> {
    rrule_iter: RRuleSetIter<'a>,
}

impl<'a> FromStr for Recurrent<'a> {
    type Err = RRuleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let rrule = s.parse::<RRuleSet>()?;
        let iter: RRuleSetIter<'a> = rrule.into_iter();
        return Ok(Self {
            rrule_iter: iter,
        });
    }
}
 

Gives this error :

error[E0597]: `rrule` does not live long enough
  --> src/programs/module/recurrent.rs:17:38
   |
12 | impl<'a> FromStr for Recurrent<'a> {
   |      -- lifetime `'a` defined here
...
16 |         let rrule = s.parse::<RRuleSet>()?;
   |             ----- binding `rrule` declared here
17 |         let iter: RRuleSetIter<'a> = rrule.into_iter();
   |                   ----------------   ^^^^^ borrowed value does not live long enough
   |                   |
   |                   type annotation requires that `rrule` is borrowed for `'a`
18 |         return Ok(Self { rrule_iter: iter });
19 |     }
   |     - `rrule` dropped here while still borrowed

So, I removed the lifetimes, and the only real change is the in the struct IterInfo where the field rrule have to be a value and not a reference, and it has to be cloned on IterInfo::new.

Since it is also possible now, I implemented FromStr for RRuleSetIter in addition.

@AlbanWS AlbanWS changed the title removed lifetimes and added from_str example RRuleSetIter: removed lifetimes and added from_str example May 23, 2024
@fmeringdal
Copy link
Owner

Thanks for the contribution. LGTM!

Once the CI issues are resolved we can merge this

@AlbanWS
Copy link
Contributor Author

AlbanWS commented Jun 3, 2024

Thanks for review, I reformatted the file who broke the CI.

@fmeringdal fmeringdal merged commit 3fc78d1 into fmeringdal:main Jun 3, 2024
8 checks passed
@AlbanWS AlbanWS deleted the remove-iter-lifetime branch June 4, 2024 07:33
@AlbanWS AlbanWS restored the remove-iter-lifetime branch June 4, 2024 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants