Skip to content

Commit

Permalink
Manually implement Copy for DateTime if offset is Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 15, 2024
1 parent 760eb66 commit 46d44d6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
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 @@ fn test_datetime_format_with_local() {

#[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!()
}
fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {
unimplemented!()
}
fn offset_from_local_datetime(
&self,
_: &NaiveDateTime,
) -> crate::MappedLocalTime<Self::Offset> {
unimplemented!()
}
fn offset_from_utc_date(&self, _: &NaiveDate) -> Self::Offset {
unimplemented!()
}
fn offset_from_utc_datetime(&self, _: &NaiveDateTime) -> Self::Offset {
unimplemented!()
}
}

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

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

0 comments on commit 46d44d6

Please sign in to comment.