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

Manually implement Copy for DateTime if offset is Copy #1573

Merged
merged 2 commits into from
Apr 15, 2024
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
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cff-version: 1.2.0
message: Please cite this crate using these information.

# Version information.
date-released: 2024-03-27
version: 0.4.37
date-released: 2024-04-15
version: 0.4.38

# Project information.
abstract: Date and time library for Rust
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chrono"
version = "0.4.37"
version = "0.4.38"
description = "Date and time library for Rust"
homepage = "https://github.com/chronotope/chrono"
documentation = "https://docs.rs/chrono/"
Expand Down
11 changes: 10 additions & 1 deletion src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod tests;
/// There are some constructors implemented here (the `from_*` methods), but
/// the general-purpose constructors are all via the methods on the
/// [`TimeZone`](./offset/trait.TimeZone.html) implementations.
#[derive(Copy, Clone)]
#[derive(Clone)]
#[cfg_attr(
any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"),
derive(Archive, Deserialize, Serialize),
Expand Down Expand Up @@ -1408,6 +1408,15 @@ impl<Tz: TimeZone> Timelike for DateTime<Tz> {
}
}

// We don't store a field with the `Tz` type, so it doesn't need to influence whether `DateTime` can
// be `Copy`. Implement it manually if the two types we do have are `Copy`.
impl<Tz: TimeZone> Copy for DateTime<Tz>
where
<Tz as TimeZone>::Offset: Copy,
NaiveDateTime: Copy,
{
}

impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateTime<Tz2>> for DateTime<Tz> {
fn eq(&self, other: &DateTime<Tz2>) -> bool {
self.datetime == other.datetime
Expand Down
43 changes: 39 additions & 4 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::DateTime;
use crate::naive::{NaiveDate, NaiveTime};
use crate::offset::{FixedOffset, TimeZone, Utc};
#[cfg(feature = "clock")]
use crate::offset::{Local, Offset};
use crate::offset::Local;
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
use crate::{Datelike, Days, MappedLocalTime, Months, NaiveDateTime, TimeDelta, Timelike, Weekday};

#[derive(Clone)]
Expand Down Expand Up @@ -1318,9 +1318,44 @@

#[test]
fn test_datetime_is_send_and_copy() {
#[derive(Clone)]
struct Tz {
_not_send: *const i32,
}
impl TimeZone for Tz {
type Offset = Off;

fn from_offset(_: &Self::Offset) -> Self {
unimplemented!()

Check warning on line 1329 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L1329

Added line #L1329 was not covered by tests
}
fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {
unimplemented!()

Check warning on line 1332 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L1332

Added line #L1332 was not covered by tests
}
fn offset_from_local_datetime(
&self,
_: &NaiveDateTime,
) -> crate::MappedLocalTime<Self::Offset> {
unimplemented!()

Check warning on line 1338 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L1335-L1338

Added lines #L1335 - L1338 were not covered by tests
}
fn offset_from_utc_date(&self, _: &NaiveDate) -> Self::Offset {
unimplemented!()

Check warning on line 1341 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L1341

Added line #L1341 was not covered by tests
}
fn offset_from_utc_datetime(&self, _: &NaiveDateTime) -> Self::Offset {
unimplemented!()

Check warning on line 1344 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L1344

Added line #L1344 was not covered by tests
}
}

#[derive(Copy, Clone, Debug)]
struct Off(());
impl Offset for Off {
fn fix(&self) -> FixedOffset {
unimplemented!()

Check warning on line 1352 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L1352

Added line #L1352 was not covered by tests
}
}

fn _assert_send_copy<T: Send + Copy>() {}
// UTC is known to be `Send + Copy`.
_assert_send_copy::<DateTime<Utc>>();
// `DateTime` is `Send + Copy` if the offset is.
_assert_send_copy::<DateTime<Tz>>();
}

#[test]
Expand Down