Skip to content
Open
18 changes: 14 additions & 4 deletions src/conversions/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ fn datetime_to_pydatetime<'py>(
fold: bool,
timezone: Option<&TimeZone>,
) -> PyResult<Bound<'py, PyDateTime>> {
let micros = datetime.subsec_nanosecond() / 1000;
// SAFETY: `subsec_nanosecond()` [0, 999_999_999], after / 1000 always non-negative
unsafe { core::hint::assert_unchecked(micros >= 0) };
PyDateTime::new_with_fold(
py,
datetime.year().into(),
Expand All @@ -76,7 +79,7 @@ fn datetime_to_pydatetime<'py>(
datetime.hour().try_into()?,
datetime.minute().try_into()?,
datetime.second().try_into()?,
(datetime.subsec_nanosecond() / 1000).try_into()?,
micros as u32,
timezone
.map(|tz| tz.into_pyobject(py))
.transpose()?
Expand All @@ -87,10 +90,17 @@ fn datetime_to_pydatetime<'py>(

#[cfg(not(Py_LIMITED_API))]
fn pytime_to_time(time: &impl PyTimeAccess) -> PyResult<Time> {
// SAFETY: Python guarantees hour belongs to [0,23],
// minute / second belong to [0,59], all < 128
unsafe {
core::hint::assert_unchecked(time.get_hour() < 128);
core::hint::assert_unchecked(time.get_minute() < 128);
core::hint::assert_unchecked(time.get_second() < 128);
}
Ok(Time::new(
time.get_hour().try_into()?,
time.get_minute().try_into()?,
time.get_second().try_into()?,
time.get_hour() as i8,
time.get_minute() as i8,
time.get_second() as i8,
(time.get_microsecond() * 1000).try_into()?,
)?)
}
Expand Down
6 changes: 5 additions & 1 deletion src/conversions/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ fn int_to_u32_vec<const SIGNED: bool>(long: &Bound<'_, PyInt>) -> PyResult<Vec<u
let n_digits = n_bytes_unsigned.div_ceil(4);
buffer.reserve_exact(n_digits);
unsafe {
// SAFETY: `n_bytes_unsigned <= isize::MAX` (try_into above) and
// `n_digits * 4 <= n_bytes_unsigned + 3`
#[expect(clippy::checked_conversions)]
core::hint::assert_unchecked(n_digits * 4 <= isize::MAX as usize);
ffi::PyLong_AsNativeBytes(
long.as_ptr().cast(),
buffer.as_mut_ptr().cast(),
(n_digits * 4).try_into().unwrap(),
(n_digits * 4) as isize,
flags,
);
buffer.set_len(n_digits);
Expand Down