Skip to content

Commit

Permalink
Deprecated warnings in cargo output for rustc-serialize feature
Browse files Browse the repository at this point in the history
Unfortunately due to rust-lang/rust#39935 placing the annotation on the `impl`s
of `Encodable`/`Decodable` for the various items have no effect whatsoever, so
we need to place it on some type that chrono actually uses internally. The only
*type* that I can find that only exists for rustc-serialize only is the
 `TsSeconds` struct.

So, marking TsSeconds deprecated causes Chrono's internal uses of `TsSeconds`
to emit deprecation warnings, both in our builds and for packages that specify
Chrono as a dependency with the `rustc-serialize` feature active. This means
that the current commit will cause a `warning: use of deprecated item:
RustcSerialize will be removed before chrono 1.0, use Serde instead` to appear
in `cargo build` output.

Unfortunately I don't think that it's possible for downstream crates to disable
the warning the warning in any way other than actually switching to Serde or
using an older chrono. That's the reason for all the `#[allow(deprecated)]`
through the code, it means that the warning appears almost exactly once,
instead of dozens of times.
  • Loading branch information
quodlibetor committed Jul 9, 2017
1 parent 91d5dd4 commit 6c28949
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ pub mod rustc_serialize {
}
}

#[allow(deprecated)]
impl Decodable for TsSeconds<FixedOffset> {
#[allow(deprecated)]
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<FixedOffset>, D::Error> {
from(FixedOffset::east(0).timestamp_opt(d.read_i64()?, 0), d)
.map(TsSeconds)
Expand All @@ -573,13 +575,16 @@ pub mod rustc_serialize {
#[derive(Debug)]
pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);

#[allow(deprecated)]
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
/// Pull the inner DateTime<Tz> out
#[allow(deprecated)]
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
obj.0
}
}

#[allow(deprecated)]
impl<Tz: TimeZone> Deref for TsSeconds<Tz> {
type Target = DateTime<Tz>;

Expand All @@ -588,6 +593,7 @@ pub mod rustc_serialize {
}
}

#[allow(deprecated)]
impl Decodable for TsSeconds<Utc> {
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> {
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
Expand All @@ -604,7 +610,9 @@ pub mod rustc_serialize {
}
}

#[allow(deprecated)]
impl Decodable for TsSeconds<Local> {
#[allow(deprecated)]
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Local>, D::Error> {
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
.map(|dt| TsSeconds(dt.with_timezone(&Local)))
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ pub use oldtime::Duration;
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
pub use date::{Date, MIN_DATE, MAX_DATE};
pub use datetime::DateTime;
#[cfg(feature = "rustc-serialize")] pub use datetime::rustc_serialize::TsSeconds;
#[cfg(feature = "rustc-serialize")]
pub use datetime::rustc_serialize::TsSeconds;
pub use format::{ParseError, ParseResult};

/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
Expand Down Expand Up @@ -413,6 +414,7 @@ pub mod naive {
pub use self::time::NaiveTime;
pub use self::datetime::NaiveDateTime;
#[cfg(feature = "rustc-serialize")]
#[allow(deprecated)]
pub use self::datetime::rustc_serialize::TsSeconds;


Expand Down
8 changes: 8 additions & 0 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,24 +1476,32 @@ pub mod rustc_serialize {

/// A `DateTime` that can be deserialized from a seconds-based timestamp
#[derive(Debug)]
#[deprecated(since = "1.4.1",
note = "RustcSerialize will be removed before chrono 1.0, use Serde instead")]
pub struct TsSeconds(NaiveDateTime);

#[allow(deprecated)]
impl From<TsSeconds> for NaiveDateTime {
/// Pull the internal NaiveDateTime out
#[allow(deprecated)]
fn from(obj: TsSeconds) -> NaiveDateTime {
obj.0
}
}

#[allow(deprecated)]
impl Deref for TsSeconds {
type Target = NaiveDateTime;

#[allow(deprecated)]
fn deref(&self) -> &Self::Target {
&self.0
}
}

#[allow(deprecated)]
impl Decodable for TsSeconds {
#[allow(deprecated)]
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds, D::Error> {
Ok(TsSeconds(
NaiveDateTime::from_timestamp_opt(d.read_i64()?, 0)
Expand Down

0 comments on commit 6c28949

Please sign in to comment.