Manually implement Copy for DateTime if offset is Copy

This commit is contained in:
Paul Dicker 2024-04-15 10:58:14 +02:00 committed by Paul Dicker
parent 760eb660d3
commit 46d44d6074
2 changed files with 49 additions and 5 deletions

View File

@ -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),
@ -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

View File

@ -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)]
@ -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]