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

Small fixes to NaiveDateDaysIterator and NaiveDateWeeksIterator #1134

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 11 additions & 20 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#[cfg(any(feature = "alloc", feature = "std", test))]
use core::borrow::Borrow;
use core::iter::FusedIterator;
use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
use core::{fmt, str};

Expand Down Expand Up @@ -2016,13 +2017,10 @@ impl Iterator for NaiveDateDaysIterator {
type Item = NaiveDate;

fn next(&mut self) -> Option<Self::Item> {
if self.value == NaiveDate::MAX {
return None;
}
// current < NaiveDate::MAX from here on:
// We return the current value, and have no way to return `NaiveDate::MAX`.
let current = self.value;
// This can't panic because current is < NaiveDate::MAX:
self.value = current.succ_opt().unwrap();
self.value = current.succ_opt()?;
Some(current)
}

Expand All @@ -2036,15 +2034,16 @@ impl ExactSizeIterator for NaiveDateDaysIterator {}

impl DoubleEndedIterator for NaiveDateDaysIterator {
fn next_back(&mut self) -> Option<Self::Item> {
if self.value == NaiveDate::MIN {
return None;
}
// We return the current value, and have no way to return `NaiveDate::MIN`.
let current = self.value;
self.value = current.pred_opt().unwrap();
self.value = current.pred_opt()?;
Some(current)
}
}

impl FusedIterator for NaiveDateDaysIterator {}

/// Iterator over `NaiveDate` with a step size of one week.
#[derive(Debug, Copy, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct NaiveDateWeeksIterator {
value: NaiveDate,
Expand All @@ -2054,11 +2053,8 @@ impl Iterator for NaiveDateWeeksIterator {
type Item = NaiveDate;

fn next(&mut self) -> Option<Self::Item> {
if NaiveDate::MAX - self.value < OldDuration::weeks(1) {
return None;
}
let current = self.value;
self.value = current + OldDuration::weeks(1);
self.value = current.checked_add_signed(OldDuration::weeks(1))?;
Some(current)
}

Expand All @@ -2072,18 +2068,13 @@ impl ExactSizeIterator for NaiveDateWeeksIterator {}

impl DoubleEndedIterator for NaiveDateWeeksIterator {
fn next_back(&mut self) -> Option<Self::Item> {
if self.value - NaiveDate::MIN < OldDuration::weeks(1) {
return None;
}
let current = self.value;
self.value = current - OldDuration::weeks(1);
self.value = current.checked_sub_signed(OldDuration::weeks(1))?;
Some(current)
}
}

// TODO: NaiveDateDaysIterator and NaiveDateWeeksIterator should implement FusedIterator,
// TrustedLen, and Step once they becomes stable.
// See: https://github.com/chronotope/chrono/issues/208
impl FusedIterator for NaiveDateWeeksIterator {}

/// The `Debug` output of the naive date `d` is the same as
/// [`d.format("%Y-%m-%d")`](../format/strftime/index.html).
Expand Down
3 changes: 2 additions & 1 deletion src/naive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ mod internals;
mod isoweek;
mod time;

pub use self::date::{Days, NaiveDate, NaiveDateDaysIterator, NaiveDateWeeksIterator, NaiveWeek};
#[allow(deprecated)]
pub use self::date::{Days, NaiveDate, NaiveWeek, MAX_DATE, MIN_DATE};
pub use self::date::{MAX_DATE, MIN_DATE};
#[cfg(feature = "rustc-serialize")]
#[allow(deprecated)]
pub use self::datetime::rustc_serialize::TsSeconds;
Expand Down