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

Various small improvements #1191

Merged
merged 7 commits into from
Jul 18, 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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ jobs:
# line-continuation marks
shell: bash
- run: cargo test --no-default-features
- run: cargo test --no-default-features --features=alloc
- run: cargo test --no-default-features --features=unstable-locales
- run: cargo test --no-default-features --features=alloc,unstable-locales

no_std:
strategy:
Expand Down
3 changes: 0 additions & 3 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

//! ISO 8601 date and time with time zone.

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::string::{String, ToString};
use core::borrow::Borrow;
Expand Down
27 changes: 5 additions & 22 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,7 @@ fn test_parse_datetime_utc() {
};
assert!(
d == d_,
"`{}` is parsed into `{:?}`, but reparsed result \
`{:?}` does not match",
"`{}` is parsed into `{:?}`, but reparsed result `{:?}` does not match",
s,
d,
d_
Expand Down Expand Up @@ -1149,26 +1148,10 @@ fn test_datetime_format_with_local() {
}

#[test]
#[cfg(feature = "clock")]
fn test_datetime_is_copy() {
// UTC is known to be `Copy`.
let a = Utc::now();
let b = a;
assert_eq!(a, b);
}

#[test]
#[cfg(feature = "clock")]
fn test_datetime_is_send() {
use std::thread;

// UTC is known to be `Send`.
let a = Utc::now();
thread::spawn(move || {
let _ = a;
})
.join()
.unwrap();
fn test_datetime_is_send_and_copy() {
fn _assert_send_copy<T: Send + Copy>() {}
// UTC is known to be `Send + Copy`.
_assert_send_copy::<DateTime<Utc>>();
}

#[test]
Expand Down
3 changes: 0 additions & 3 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

//! Date and time formatting routines.

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
#[cfg(any(feature = "alloc", feature = "std"))]
Expand Down
3 changes: 0 additions & 3 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
//! # Ok::<(), chrono::ParseError>(())
//! ```

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::fmt;
Expand Down
4 changes: 2 additions & 2 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,8 +1644,8 @@ mod tests {

// Test against test data above
for &(date, checkdate) in testdates.iter() {
eprintln!("Test input: {:?}", date);
eprintln!(" Expect: {:?}", checkdate);
#[cfg(feature = "std")]
eprintln!("Test input: {:?}\n Expect: {:?}", date, checkdate);
let dt = rfc2822_to_datetime(date); // parse a date
if dt != checkdate {
// check for expected result
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,18 @@
#![cfg_attr(feature = "rustc-serialize", allow(deprecated))]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "oldtime")]
#[cfg_attr(docsrs, doc(cfg(feature = "oldtime")))]
extern crate time as oldtime;
#[cfg(not(feature = "oldtime"))]
mod oldtime;
// this reexport is to aid the transition and should not be in the prelude!
pub use oldtime::{Duration, OutOfRangeError};
pub use oldtime::Duration;
#[cfg(feature = "std")]
pub use oldtime::OutOfRangeError;

use core::fmt;

Expand Down
3 changes: 1 addition & 2 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,6 @@ mod tests {
use super::{Days, Months, NaiveDate, MAX_YEAR, MIN_YEAR};
use crate::oldtime::Duration;
use crate::{Datelike, Weekday};
use std::{i32, u32};

// as it is hard to verify year flags in `NaiveDate::MIN` and `NaiveDate::MAX`,
// we use a separate run-time test.
Expand Down Expand Up @@ -3123,7 +3122,7 @@ mod tests {
#[test]
fn test_naiveweek() {
let date = NaiveDate::from_ymd_opt(2022, 5, 18).unwrap();
let asserts = vec![
let asserts = [
(Weekday::Mon, "Mon 2022-05-16", "Sun 2022-05-22"),
(Weekday::Tue, "Tue 2022-05-17", "Mon 2022-05-23"),
(Weekday::Wed, "Wed 2022-05-18", "Tue 2022-05-24"),
Expand Down
1 change: 0 additions & 1 deletion src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::NaiveDateTime;
use crate::oldtime::Duration;
use crate::NaiveDate;
use crate::{Datelike, FixedOffset, Utc};
use std::i64;

#[test]
fn test_datetime_from_timestamp_millis() {
Expand Down
4 changes: 1 addition & 3 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#![cfg_attr(feature = "__internal_bench", allow(missing_docs))]

use crate::Weekday;
use core::{fmt, i32};
use core::fmt;

/// The internal date representation: `year << 13 | Of`
pub(super) type DateImpl = i32;
Expand Down Expand Up @@ -504,8 +504,6 @@ const fn weekday_from_u32_mod7(n: u32) -> Weekday {

#[cfg(test)]
mod tests {
use std::u32;

use super::weekday_from_u32_mod7;
use super::{Mdf, Of};
use super::{YearFlags, A, AG, B, BA, C, CB, D, DC, E, ED, F, FE, G, GF};
Expand Down
1 change: 0 additions & 1 deletion src/naive/time/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::NaiveTime;
use crate::oldtime::Duration;
use crate::Timelike;
use std::u32;

#[test]
fn test_time_from_hms_milli() {
Expand Down
35 changes: 18 additions & 17 deletions src/oldtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
//! Temporal quantification

use core::ops::{Add, Div, Mul, Neg, Sub};
#[cfg(feature = "std")]
use core::time::Duration as StdDuration;
use core::{fmt, i64};
#[cfg(any(feature = "std", test))]
#[cfg(feature = "std")]
use std::error::Error;

#[cfg(feature = "rkyv")]
Expand Down Expand Up @@ -291,6 +292,7 @@ impl Duration {
///
/// This function errors when original duration is larger than the maximum
/// value supported for this type.
#[cfg(feature = "std")]
pub fn from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError> {
// We need to check secs as u64 before coercing to i64
if duration.as_secs() > MAX.secs as u64 {
Expand All @@ -307,6 +309,7 @@ impl Duration {
///
/// This function errors when duration is less than zero. As standard
/// library implementation is limited to non-negative values.
#[cfg(feature = "std")]
pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
if self.secs < 0 {
return Err(OutOfRangeError(()));
Expand Down Expand Up @@ -388,17 +391,13 @@ impl Div<i32> for Duration {
}
}

#[cfg(any(feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<'a> std::iter::Sum<&'a Duration> for Duration {
impl<'a> core::iter::Sum<&'a Duration> for Duration {
fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
iter.fold(Duration::zero(), |acc, x| acc + *x)
}
}

#[cfg(any(feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::iter::Sum<Duration> for Duration {
impl core::iter::Sum<Duration> for Duration {
fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
iter.fold(Duration::zero(), |acc, x| acc + x)
}
Expand Down Expand Up @@ -444,16 +443,18 @@ impl fmt::Display for Duration {
/// The `std::time::Duration` supports a range from zero to `u64::MAX`
/// *seconds*, while this module supports signed range of up to
/// `i64::MAX` of *milliseconds*.
#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutOfRangeError(());

#[cfg(feature = "std")]
impl fmt::Display for OutOfRangeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Source duration value is out of range for the target type")
}
}

#[cfg(any(feature = "std", test))]
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Error for OutOfRangeError {
#[allow(deprecated)]
Expand Down Expand Up @@ -487,9 +488,11 @@ impl arbitrary::Arbitrary<'_> for Duration {

#[cfg(test)]
mod tests {
use super::{Duration, OutOfRangeError, MAX, MIN};
#[cfg(feature = "std")]
use super::OutOfRangeError;
use super::{Duration, MAX, MIN};
#[cfg(feature = "std")]
use std::time::Duration as StdDuration;
use std::{i32, i64};

#[test]
fn test_duration() {
Expand Down Expand Up @@ -682,13 +685,9 @@ mod tests {
let sum_2: Duration = duration_list_2.iter().sum();
assert_eq!(sum_2, Duration::seconds(17));

let duration_vec = vec![
Duration::zero(),
Duration::seconds(1),
Duration::seconds(6),
Duration::seconds(10),
];
let sum_3: Duration = duration_vec.into_iter().sum();
let duration_arr =
[Duration::zero(), Duration::seconds(1), Duration::seconds(6), Duration::seconds(10)];
let sum_3: Duration = duration_arr.into_iter().sum();
assert_eq!(sum_3, Duration::seconds(17));
}

Expand All @@ -713,6 +712,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_to_std() {
assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0)));
Expand All @@ -725,6 +725,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_from_std() {
assert_eq!(Ok(Duration::seconds(1)), Duration::from_std(StdDuration::new(1, 0)));
assert_eq!(Ok(Duration::seconds(86401)), Duration::from_std(StdDuration::new(86401, 0)));
Expand Down