zslayton / cron

A cron expression parser in Rust
Apache License 2.0
376 stars 69 forks source link

Added `upcoming_owned(...)` and `after_owned(...)` #88

Closed DaanA32 closed 2 years ago

DaanA32 commented 3 years ago

Hi all,

I needed the following which was not possible:

use chrono::prelude::*;
fn main() {
    let mut boxed = box_helper_upcoming();
    assert!(boxed.next().is_some());
    let mut boxed = box_helper_after();
    assert!(boxed.next().is_some());
}

fn box_helper_upcoming() -> Box<dyn Iterator<Item = DateTime<Local>>> {
    let schedule = Schedule::from_str("0 0 0 * * 1 *").unwrap();
    let upcoming = schedule.upcoming_owned(Local);
    Box::new(upcoming)
}

fn box_helper_after() -> Box<dyn Iterator<Item = DateTime<Local>>> {
    let schedule = Schedule::from_str("0 0 0 * * 1 *").unwrap();
    let upcoming = schedule.after_owned(Local);
    Box::new(upcoming)
}

I've made minimal changes to ScheduleIterator and Schedule so that it does compile.

zslayton commented 3 years ago

Hi, thanks for the PR! I don't think i understand what new functionality this is trying to add. Are you trying to get owned DateTime<Tz> instances from the iterator? If so, why not use schedule.upcoming().cloned()?

DaanA32 commented 3 years ago

Hi zslaton, The ScheduleIterator is currently not exposed as a public struct, so it is not possible to use it in a struct. This would allow having an owned iterator instead of having be dependant on the lifetime of the schedule.

It allows for the following in dependant projects:

struct Example<Z> {
    iterator: Box<dyn Iterator<Item = DateTime<Z>>
}

As the following is not possible

struct Example<Z> {
    iterator: ScheduleIterator<Z>
}
zslayton commented 2 years ago

Hi @DaanA32, sincere apologies for dropping the ball on this. I think it's been addressed by #104, so I'm going to close this out.