Skip to content

Commit 46d44d6

Browse files
committed
Manually implement Copy for DateTime if offset is Copy
1 parent 760eb66 commit 46d44d6

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/datetime/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod tests;
4646
/// There are some constructors implemented here (the `from_*` methods), but
4747
/// the general-purpose constructors are all via the methods on the
4848
/// [`TimeZone`](./offset/trait.TimeZone.html) implementations.
49-
#[derive(Copy, Clone)]
49+
#[derive(Clone)]
5050
#[cfg_attr(
5151
any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"),
5252
derive(Archive, Deserialize, Serialize),
@@ -1408,6 +1408,15 @@ impl<Tz: TimeZone> Timelike for DateTime<Tz> {
14081408
}
14091409
}
14101410

1411+
// We don't store a field with the `Tz` type, so it doesn't need to influence whether `DateTime` can
1412+
// be `Copy`. Implement it manually if the two types we do have are `Copy`.
1413+
impl<Tz: TimeZone> Copy for DateTime<Tz>
1414+
where
1415+
<Tz as TimeZone>::Offset: Copy,
1416+
NaiveDateTime: Copy,
1417+
{
1418+
}
1419+
14111420
impl<Tz: TimeZone, Tz2: TimeZone> PartialEq<DateTime<Tz2>> for DateTime<Tz> {
14121421
fn eq(&self, other: &DateTime<Tz2>) -> bool {
14131422
self.datetime == other.datetime

src/datetime/tests.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::DateTime;
22
use crate::naive::{NaiveDate, NaiveTime};
3-
use crate::offset::{FixedOffset, TimeZone, Utc};
43
#[cfg(feature = "clock")]
5-
use crate::offset::{Local, Offset};
4+
use crate::offset::Local;
5+
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
66
use crate::{Datelike, Days, MappedLocalTime, Months, NaiveDateTime, TimeDelta, Timelike, Weekday};
77

88
#[derive(Clone)]
@@ -1318,9 +1318,44 @@ fn test_datetime_format_with_local() {
13181318

13191319
#[test]
13201320
fn test_datetime_is_send_and_copy() {
1321+
#[derive(Clone)]
1322+
struct Tz {
1323+
_not_send: *const i32,
1324+
}
1325+
impl TimeZone for Tz {
1326+
type Offset = Off;
1327+
1328+
fn from_offset(_: &Self::Offset) -> Self {
1329+
unimplemented!()
1330+
}
1331+
fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {
1332+
unimplemented!()
1333+
}
1334+
fn offset_from_local_datetime(
1335+
&self,
1336+
_: &NaiveDateTime,
1337+
) -> crate::MappedLocalTime<Self::Offset> {
1338+
unimplemented!()
1339+
}
1340+
fn offset_from_utc_date(&self, _: &NaiveDate) -> Self::Offset {
1341+
unimplemented!()
1342+
}
1343+
fn offset_from_utc_datetime(&self, _: &NaiveDateTime) -> Self::Offset {
1344+
unimplemented!()
1345+
}
1346+
}
1347+
1348+
#[derive(Copy, Clone, Debug)]
1349+
struct Off(());
1350+
impl Offset for Off {
1351+
fn fix(&self) -> FixedOffset {
1352+
unimplemented!()
1353+
}
1354+
}
1355+
13211356
fn _assert_send_copy<T: Send + Copy>() {}
1322-
// UTC is known to be `Send + Copy`.
1323-
_assert_send_copy::<DateTime<Utc>>();
1357+
// `DateTime` is `Send + Copy` if the offset is.
1358+
_assert_send_copy::<DateTime<Tz>>();
13241359
}
13251360

13261361
#[test]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy