Skip to content

Commit

Permalink
feat: add set of chrono types guarded behind the chrono feature
Browse files Browse the repository at this point in the history
  • Loading branch information
xkikeg authored and fujiapple852 committed Jun 23, 2024
1 parent 4529911 commit e979521
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ jobs:
command: check
args: --workspace --no-default-features --features smartstring

- name: check --no-default-features --features chrono
uses: actions-rs/cargo@v1
with:
command: check
args: --workspace --no-default-features --features chrono

- name: check --no-default-features --features chrono-clock
uses: actions-rs/cargo@v1
with:
command: check
args: --workspace --no-default-features --features chrono-clock

- name: check --all-features
uses: actions-rs/cargo@v1
with:
Expand Down
6 changes: 5 additions & 1 deletion bounded-static/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ alloc = []
collections = [ "alloc" ]

# Enable impls of [To|Into]BoundedStatic for other types in std.
std = [ "alloc", "ahash?/std" ]
std = [ "alloc", "ahash?/std", "chrono?/std" ]

# Enable the ToStatic custom derive macro.
derive = [ "bounded-static-derive" ]

# Enable the clock feature for chrono.
chrono-clock = [ "chrono", "chrono/clock" ]

[dependencies]
bounded-static-derive = { version = "0.7.0", path = "../bounded-static-derive", optional = true }
smol_str = { version = "0.2.2", optional = true, default-features = false }
smallvec = { version = "1.13.2", optional = true, default-features = false }
smartstring = { version = "1.0.1", optional = true, default-features = false }
ahash = { version = "0.8.11", optional = true, default-features = false }
chrono = { version = "0.4.38", optional = true, default-features = false }

[package.metadata.docs.rs]
all-features = true
171 changes: 170 additions & 1 deletion bounded-static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@
//! - [`RandomState`](https://docs.rs/ahash/0.8.6/ahash/random_state/struct.RandomState.html)
//! - [`AHashMap`](https://docs.rs/ahash/0.8.6/ahash/struct.AHashMap.html)
//! - [`AHashSet`](https://docs.rs/ahash/0.8.6/ahash/struct.AHashSet.html)
//! - `chrono` for:
//! - [`DateTime`](https://docs.rs/chrono/0.4.38/chrono/struct.DateTime.html)
//! - [`FixedOffset`](https://docs.rs/chrono/0.4.38/chrono/struct.FixedOffset.html)
//! - [`Months`](https://docs.rs/chrono/0.4.38/chrono/struct.Months.html)
//! - [`TimeDelta`](https://docs.rs/chrono/0.4.38/chrono/struct.TimeDelta.html)
//! - [`Utc`](https://docs.rs/chrono/0.4.38/chrono/struct.Utc.html)
//! - [`Month`](https://docs.rs/chrono/0.4.38/chrono/enum.Month.html)
//! - [`Weekday`](https://docs.rs/chrono/0.4.38/chrono/enum.Weekday.html)
//! - [`Days`](https://docs.rs/chrono/0.4.38/chrono/naive/struct.Days.html)
//! - [`IsoWeek`](https://docs.rs/chrono/0.4.38/chrono/naive/struct.IsoWeek.html)
//! - [`NaiveDate`](https://docs.rs/chrono/0.4.38/chrono/naive/struct.NaiveDate.html)
//! - [`NaiveDateTime`](https://docs.rs/chrono/0.4.38/chrono/naive/struct.NaiveDateTime.html)
//! - [`NaiveTime`](https://docs.rs/chrono/0.4.38/chrono/naive/struct.NaiveTime.html)
//! - `chrono-clock` for:
//! - [`Local`](https://docs.rs/chrono/0.4.38/chrono/struct.Local.html)
//!
//! # Examples
//!
Expand Down Expand Up @@ -237,7 +252,7 @@ impl IntoBoundedStatic for &'static str {

/// No-op [`ToBoundedStatic`] and [`IntoBoundedStatic`] impls for `Copy` types.
macro_rules! make_copy_impl {
($id:ident) => {
($id:ty) => {
/// No-op [`ToBoundedStatic`] impl for this `Copy` type.
impl ToBoundedStatic for $id {
type Static = Self;
Expand Down Expand Up @@ -952,6 +967,50 @@ where
}
}

#[cfg(feature = "chrono")]
impl<Tz: 'static + chrono::TimeZone> ToBoundedStatic for chrono::DateTime<Tz> {
type Static = Self;

fn to_static(&self) -> Self::Static {
self.clone()
}
}

#[cfg(feature = "chrono")]
impl<Tz: 'static + chrono::TimeZone> IntoBoundedStatic for chrono::DateTime<Tz> {
type Static = Self;

fn into_static(self) -> Self::Static {
self
}
}

#[cfg(feature = "chrono")]
make_copy_impl!(chrono::FixedOffset);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::Months);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::TimeDelta);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::Utc);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::Month);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::Weekday);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::naive::Days);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::naive::IsoWeek);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::naive::NaiveDate);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::naive::NaiveDateTime);
#[cfg(feature = "chrono")]
make_copy_impl!(chrono::naive::NaiveTime);
#[cfg(feature = "chrono-clock")]
make_copy_impl!(chrono::Local);
// No implementation for chrono::NaiveWeek as it's not Copy nor Clone.

#[cfg(test)]
mod core_tests {
use super::*;
Expand Down Expand Up @@ -1690,3 +1749,113 @@ mod ahash_tests {
ensure_static(to_static);
}
}

#[cfg(feature = "chrono")]
#[cfg(test)]
mod chrono_tests {
use super::*;

fn ensure_static<T: 'static>(t: T) {
drop(t);
}

#[test]
fn test_chrono_fixed_offset() {
let value = chrono::FixedOffset::east_opt(1).unwrap();
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_months() {
let value = chrono::Months::new(1);
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_time_delta() {
let value = chrono::TimeDelta::days(10);
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_utc() {
let value = chrono::Utc;
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_month() {
let value = chrono::Month::January;
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_weekday() {
let value = chrono::Weekday::Mon;
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_naive_days() {
let value = chrono::naive::Days::new(1);
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_naive_iso_week() {
use chrono::Datelike;
let value = chrono::naive::NaiveDate::from_ymd_opt(2024, 6, 1)
.unwrap()
.iso_week();
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_naive_date() {
let value = chrono::naive::NaiveDate::from_ymd_opt(2024, 6, 1).unwrap();
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_naive_date_time() {
let value = chrono::naive::NaiveDateTime::new(
chrono::NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
chrono::NaiveTime::from_hms_opt(22, 33, 44).unwrap(),
);
let to_static = value.to_static();
ensure_static(to_static);
}

#[test]
fn test_chrono_naive_time() {
let value = chrono::naive::NaiveTime::from_hms_opt(22, 33, 44).unwrap();
let to_static = value.to_static();
ensure_static(to_static);
}
}

#[cfg(feature = "chrono-clock")]
#[cfg(test)]
mod chrono_clock_tests {
use super::*;

fn ensure_static<T: 'static>(t: T) {
drop(t);
}

#[test]
fn test_chrono_local() {
let value = chrono::Local::now();
let to_static = value.to_static();
ensure_static(to_static);
}
}

0 comments on commit e979521

Please sign in to comment.