Skip to content

Commit

Permalink
foundation: remove optional chrono dependency (#666)
Browse files Browse the repository at this point in the history
* foundation: remove optional chrono dependency

* Bump version numbers for semver-incompatible changes
  • Loading branch information
djc committed Apr 8, 2024
1 parent d7879eb commit 9721646
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 77 deletions.
7 changes: 3 additions & 4 deletions cocoa-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "cocoa-foundation"
description = "Bindings to Cocoa Foundation for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.1.2"
version = "0.2.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand All @@ -16,12 +16,11 @@ default-target = "x86_64-apple-darwin"
block = "0.1"
bitflags = "1.0"
libc = "0.2"
core-foundation = { default-features = false, path = "../core-foundation", version = "0.9" }
core-graphics-types = { default-features = false, path = "../core-graphics-types", version = "0.1" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.10" }
core-graphics-types = { default-features = false, path = "../core-graphics-types", version = "0.2" }
objc = "0.2.3"

[features]
default = ["link"]
# Disable to manually link. Enabled by default.
link = ["core-foundation/link", "core-graphics-types/link"]

8 changes: 4 additions & 4 deletions cocoa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "cocoa"
description = "Bindings to Cocoa for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.25.0"
version = "0.26.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand All @@ -16,9 +16,9 @@ default-target = "x86_64-apple-darwin"
block = "0.1"
bitflags = "1.0"
libc = "0.2"
cocoa-foundation = { default-features = false, path = "../cocoa-foundation", version = "0.1" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.9" }
core-graphics = { default-features = false, path = "../core-graphics", version = "0.23" }
cocoa-foundation = { default-features = false, path = "../cocoa-foundation", version = "0.2" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.10" }
core-graphics = { default-features = false, path = "../core-graphics", version = "0.24" }
foreign-types = "0.5"
objc = "0.2.3"

Expand Down
4 changes: 1 addition & 3 deletions core-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "core-foundation"
description = "Bindings to Core Foundation for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.9.4"
version = "0.10.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
categories = ["os::macos-apis"]
Expand All @@ -17,15 +17,13 @@ version = "0.8.6"

[dependencies]
libc = "0.2"
chrono = { version = "0.4", optional = true }
uuid = { version = "0.5", optional = true }

[features]
default = ["link"]

mac_os_10_7_support = ["core-foundation-sys/mac_os_10_7_support"] # backwards compatibility
mac_os_10_8_features = ["core-foundation-sys/mac_os_10_8_features"] # enables new features
with-chrono = ["chrono"]
with-uuid = ["uuid"]
# Disable to manually link. Enabled by default.
link = ["core-foundation-sys/link"]
Expand Down
56 changes: 0 additions & 56 deletions core-foundation/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ pub use core_foundation_sys::date::*;

use crate::base::TCFType;

#[cfg(feature = "with-chrono")]
use chrono::NaiveDateTime;

declare_TCFType! {
/// A date.
CFDate, CFDateRef
Expand All @@ -43,45 +40,13 @@ impl CFDate {
pub fn abs_time(&self) -> CFAbsoluteTime {
unsafe { CFDateGetAbsoluteTime(self.0) }
}

#[cfg(feature = "with-chrono")]
pub fn naive_utc(&self) -> NaiveDateTime {
let ts = unsafe { self.abs_time() + kCFAbsoluteTimeIntervalSince1970 };
let (secs, nanos) = if ts.is_sign_positive() {
(ts.trunc() as i64, ts.fract())
} else {
// nanoseconds can't be negative in NaiveDateTime
(ts.trunc() as i64 - 1, 1.0 - ts.fract().abs())
};
NaiveDateTime::from_timestamp(secs, (nanos * 1e9).floor() as u32)
}

#[cfg(feature = "with-chrono")]
pub fn from_naive_utc(time: NaiveDateTime) -> CFDate {
let secs = time.timestamp();
let nanos = time.timestamp_subsec_nanos();
let ts = unsafe { secs as f64 + (nanos as f64 / 1e9) - kCFAbsoluteTimeIntervalSince1970 };
CFDate::new(ts)
}
}

#[cfg(test)]
mod test {
use super::CFDate;
use std::cmp::Ordering;

#[cfg(feature = "with-chrono")]
use chrono::NaiveDateTime;

#[cfg(feature = "with-chrono")]
fn approx_eq(a: f64, b: f64) -> bool {
use std::f64;

let same_sign = a.is_sign_positive() == b.is_sign_positive();
let equal = ((a - b).abs() / f64::min(a.abs() + b.abs(), f64::MAX)) < f64::EPSILON;
same_sign && equal
}

#[test]
fn date_comparison() {
let now = CFDate::now();
Expand All @@ -97,25 +62,4 @@ mod test {
let same_time = CFDate::new(now.abs_time());
assert_eq!(now, same_time);
}

#[test]
#[cfg(feature = "with-chrono")]
fn date_chrono_conversion_positive() {
let date = CFDate::now();
let datetime = date.naive_utc();
let converted = CFDate::from_naive_utc(datetime);
assert!(approx_eq(date.abs_time(), converted.abs_time()));
}

#[test]
#[cfg(feature = "with-chrono")]
fn date_chrono_conversion_negative() {
use super::kCFAbsoluteTimeIntervalSince1970;

let ts = unsafe { kCFAbsoluteTimeIntervalSince1970 - 420.0 };
let date = CFDate::new(ts);
let datetime: NaiveDateTime = date.naive_utc();
let converted = CFDate::from_naive_utc(datetime);
assert!(approx_eq(date.abs_time(), converted.abs_time()));
}
}
4 changes: 2 additions & 2 deletions core-graphics-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ name = "core-graphics-types"
description = "Bindings for some fundamental Core Graphics types"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.1.3"
version = "0.2.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
bitflags = "1.0"
core-foundation = { default-features = false, path = "../core-foundation", version = "0.9.4" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.10" }
libc = "0.2"

[features]
Expand Down
6 changes: 3 additions & 3 deletions core-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "core-graphics"
description = "Bindings to Core Graphics for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.23.2"
version = "0.24.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand All @@ -17,8 +17,8 @@ link = ["core-foundation/link", "core-graphics-types/link"]

[dependencies]
bitflags = "1.0"
core-foundation = { default-features = false, path = "../core-foundation", version = "0.9" }
core-graphics-types = { default-features = false, path = "../core-graphics-types", version = "0.1" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.10" }
core-graphics-types = { default-features = false, path = "../core-graphics-types", version = "0.2" }
foreign-types = "0.5.0"
libc = "0.2"

Expand Down
6 changes: 3 additions & 3 deletions core-text/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-text"
version = "20.1.0"
version = "21.0.0"
authors = ["The Servo Project Developers"]
description = "Bindings to the Core Text framework."
license = "MIT OR Apache-2.0"
Expand All @@ -22,5 +22,5 @@ link = ["core-foundation/link", "core-graphics/link"]
[dependencies]
foreign-types = "0.5"
libc = "0.2"
core-foundation = { default-features = false, path = "../core-foundation", version = "0.9" }
core-graphics = { default-features = false, path = "../core-graphics", version = "0.23.0" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.10" }
core-graphics = { default-features = false, path = "../core-graphics", version = "0.24" }
4 changes: 2 additions & 2 deletions io-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "io-surface"
description = "Bindings to IO Surface for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.15.1"
version = "0.16.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
edition = "2018"
Expand All @@ -13,7 +13,7 @@ default-target = "x86_64-apple-darwin"

[dependencies]
libc = "0.2"
core-foundation = { default-features = false, path = "../core-foundation", version = "0.9" }
core-foundation = { default-features = false, path = "../core-foundation", version = "0.10" }
core-foundation-sys = { default-features = false, path = "../core-foundation-sys", version = "0.8" }
cgl = "0.3"
leaky-cow = "0.1.1"
Expand Down

0 comments on commit 9721646

Please sign in to comment.