Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove num-integer dependency #1037

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ __doctest = []

[dependencies]
time = { version = "0.1.43", optional = true }
num-integer = { version = "0.1.36", default-features = false }
num-traits = { version = "0.2", default-features = false }
rustc-serialize = { version = "0.3.20", optional = true }
serde = { version = "1.0.99", default-features = false, optional = true }
Expand Down
10 changes: 4 additions & 6 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,6 @@ fn format_inner(
) -> fmt::Result {
let locale = Locales::new(locale);

use num_integer::{div_floor, mod_floor};

match *item {
Item::Literal(s) | Item::Space(s) => result.push_str(s),
#[cfg(any(feature = "alloc", feature = "std", test))]
Expand All @@ -527,11 +525,11 @@ fn format_inner(

let (width, v) = match *spec {
Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
YearDiv100 => (2, date.map(|d| i64::from(d.year()).div_euclid(100))),
YearMod100 => (2, date.map(|d| i64::from(d.year()).rem_euclid(100))),
IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))),
IsoYearDiv100 => (2, date.map(|d| div_floor(i64::from(d.iso_week().year()), 100))),
IsoYearMod100 => (2, date.map(|d| mod_floor(i64::from(d.iso_week().year()), 100))),
IsoYearDiv100 => (2, date.map(|d| i64::from(d.iso_week().year()).div_euclid(100))),
IsoYearMod100 => (2, date.map(|d| i64::from(d.iso_week().year()).rem_euclid(100))),
Month => (2, date.map(|d| i64::from(d.month()))),
Day => (2, date.map(|d| i64::from(d.day()))),
WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),
Expand Down
10 changes: 4 additions & 6 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! A collection of parsed date and time items.
//! They can be constructed incrementally while being checked for consistency.

use num_integer::div_rem;
use num_traits::ToPrimitive;

use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
Expand Down Expand Up @@ -311,7 +310,8 @@ impl Parsed {
if y < 0 {
return Err(OUT_OF_RANGE);
}
let (q_, r_) = div_rem(y, 100);
let q_ = y / 100;
let r_ = y % 100;
if q.unwrap_or(q_) == q_ && r.unwrap_or(r_) == r_ {
Ok(Some(y))
} else {
Expand Down Expand Up @@ -346,8 +346,7 @@ impl Parsed {
let verify_ymd = |date: NaiveDate| {
let year = date.year();
let (year_div_100, year_mod_100) = if year >= 0 {
let (q, r) = div_rem(year, 100);
(Some(q), Some(r))
(Some(year / 100), Some(year % 100))
} else {
(None, None) // they should be empty to be consistent
};
Expand All @@ -367,8 +366,7 @@ impl Parsed {
let isoweek = week.week();
let weekday = date.weekday();
let (isoyear_div_100, isoyear_mod_100) = if isoyear >= 0 {
let (q, r) = div_rem(isoyear, 100);
(Some(q), Some(r))
(Some(isoyear / 100), Some(isoyear % 100))
} else {
(None, None) // they should be empty to be consistent
};
Expand Down
5 changes: 4 additions & 1 deletion src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use core::convert::TryFrom;
use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
use core::{fmt, str};

use num_integer::div_mod_floor;
use num_traits::ToPrimitive;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};
Expand Down Expand Up @@ -2037,6 +2036,10 @@ impl Default for NaiveDate {
}
}

fn div_mod_floor(val: i32, div: i32) -> (i32, i32) {
(val.div_euclid(div), val.rem_euclid(div))
}

#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
fn test_encodable_json<F, E>(to_string: F)
where
Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, str};

use num_integer::div_mod_floor;
use num_traits::ToPrimitive;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};
Expand Down Expand Up @@ -239,7 +238,8 @@ impl NaiveDateTime {
#[inline]
#[must_use]
pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option<NaiveDateTime> {
let (days, secs) = div_mod_floor(secs, 86_400);
let days = secs.div_euclid(86_400);
let secs = secs.rem_euclid(86_400);
let date = days
.to_i32()
.and_then(|days| days.checked_add(719_163))
Expand Down
6 changes: 3 additions & 3 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use crate::Weekday;
use core::{fmt, i32};
use num_integer::{div_rem, mod_floor};
use num_traits::FromPrimitive;

/// The internal date representation. This also includes the packed `Mdf` value.
Expand Down Expand Up @@ -95,7 +94,8 @@ static YEAR_DELTAS: [u8; 401] = [
];

pub(super) fn cycle_to_yo(cycle: u32) -> (u32, u32) {
let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365);
let mut year_mod_400 = cycle / 365;
let mut ordinal0 = cycle % 365;
let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]);
if ordinal0 < delta {
year_mod_400 -= 1;
Expand All @@ -116,7 +116,7 @@ impl YearFlags {
#[inline]
#[must_use]
pub fn from_year(year: i32) -> YearFlags {
let year = mod_floor(year, 400);
let year = year.rem_euclid(400);
YearFlags::from_year_mod_400(year)
}

Expand Down
7 changes: 4 additions & 3 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use core::borrow::Borrow;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, str};

use num_integer::div_mod_floor;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -762,8 +761,10 @@ impl NaiveTime {

/// Returns a triple of the hour, minute and second numbers.
fn hms(&self) -> (u32, u32, u32) {
let (mins, sec) = div_mod_floor(self.secs, 60);
let (hour, min) = div_mod_floor(mins, 60);
let sec = self.secs % 60;
let mins = self.secs / 60;
djc marked this conversation as resolved.
Show resolved Hide resolved
let min = mins % 60;
let hour = mins / 60;
(hour, min, sec)
}

Expand Down
7 changes: 4 additions & 3 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use core::fmt;
use core::ops::{Add, Sub};

use num_integer::div_mod_floor;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -142,8 +141,10 @@ impl fmt::Debug for FixedOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let offset = self.local_minus_utc;
let (sign, offset) = if offset < 0 { ('-', -offset) } else { ('+', offset) };
let (mins, sec) = div_mod_floor(offset, 60);
let (hour, min) = div_mod_floor(mins, 60);
let sec = offset.rem_euclid(60);
let mins = offset.div_euclid(60);
let min = mins.rem_euclid(60);
let hour = mins.div_euclid(60);
if sec == 0 {
write!(f, "{}{:02}:{:02}", sign, hour, min)
} else {
Expand Down
24 changes: 1 addition & 23 deletions src/oldtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,31 +462,9 @@ impl Error for OutOfRangeError {
}
}

// Copied from libnum
#[inline]
const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
(div_floor_64(this, other), mod_floor_64(this, other))
}

#[inline]
const fn div_floor_64(this: i64, other: i64) -> i64 {
match div_rem_64(this, other) {
(d, r) if (r > 0 && other < 0) || (r < 0 && other > 0) => d - 1,
(d, _) => d,
}
}

#[inline]
const fn mod_floor_64(this: i64, other: i64) -> i64 {
match this % other {
r if (r > 0 && other < 0) || (r < 0 && other > 0) => r + other,
r => r,
}
}

#[inline]
const fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
(this / other, this % other)
(this.div_euclid(other), this.rem_euclid(other))
}

#[cfg(feature = "arbitrary")]
Expand Down