Skip to content

Commit

Permalink
fix signature of Step::steps_between implementations
Browse files Browse the repository at this point in the history
A recent nightly changed the signature of Step::steps_between to match
the signature of Iterator::size_hint. This patch changes our
implementations to match the new signature.
  • Loading branch information
Freax13 committed Nov 30, 2024
1 parent fb1e2db commit 0ee8fef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
35 changes: 23 additions & 12 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,20 @@ impl VirtAddr {
}

// FIXME: Move this into the `Step` impl, once `Step` is stabilized.
pub(crate) fn steps_between_impl(start: &Self, end: &Self) -> Option<usize> {
let mut steps = end.0.checked_sub(start.0)?;
pub(crate) fn steps_between_impl(start: &Self, end: &Self) -> (usize, Option<usize>) {
let mut steps = if let Some(steps) = end.0.checked_sub(start.0) {
steps
} else {
return (0, None);
};

// Check if we jumped the gap.
if end.0.get_bit(47) && !start.0.get_bit(47) {
steps = steps.checked_sub(0xffff_0000_0000_0000).unwrap();
}

usize::try_from(steps).ok()
let steps = usize::try_from(steps).ok();
(steps.unwrap_or(usize::MAX), steps)
}

// FIXME: Move this into the `Step` impl, once `Step` is stabilized.
Expand Down Expand Up @@ -379,7 +384,7 @@ impl Sub<VirtAddr> for VirtAddr {

#[cfg(feature = "step_trait")]
impl Step for VirtAddr {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
Self::steps_between_impl(start, end)
}

Expand Down Expand Up @@ -758,43 +763,49 @@ mod tests {
#[test]
#[cfg(feature = "step_trait")]
fn virtaddr_steps_between() {
assert_eq!(Step::steps_between(&VirtAddr(0), &VirtAddr(0)), Some(0));
assert_eq!(Step::steps_between(&VirtAddr(0), &VirtAddr(1)), Some(1));
assert_eq!(Step::steps_between(&VirtAddr(1), &VirtAddr(0)), None);
assert_eq!(
Step::steps_between(&VirtAddr(0), &VirtAddr(0)),
(0, Some(0))
);
assert_eq!(
Step::steps_between(&VirtAddr(0), &VirtAddr(1)),
(1, Some(1))
);
assert_eq!(Step::steps_between(&VirtAddr(1), &VirtAddr(0)), (0, None));
assert_eq!(
Step::steps_between(
&VirtAddr(0x7fff_ffff_ffff),
&VirtAddr(0xffff_8000_0000_0000)
),
Some(1)
(1, Some(1))
);
assert_eq!(
Step::steps_between(
&VirtAddr(0xffff_8000_0000_0000),
&VirtAddr(0x7fff_ffff_ffff)
),
None
(0, None)
);
assert_eq!(
Step::steps_between(
&VirtAddr(0xffff_8000_0000_0000),
&VirtAddr(0xffff_8000_0000_0000)
),
Some(0)
(0, Some(0))
);
assert_eq!(
Step::steps_between(
&VirtAddr(0xffff_8000_0000_0000),
&VirtAddr(0xffff_8000_0000_0001)
),
Some(1)
(1, Some(1))
);
assert_eq!(
Step::steps_between(
&VirtAddr(0xffff_8000_0000_0001),
&VirtAddr(0xffff_8000_0000_0000)
),
None
(0, None)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/instructions/tlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,14 @@ where
if let Some(mut pages) = self.page_range {
while !pages.is_empty() {
// Calculate out how many pages we still need to flush.
let count = Page::<S>::steps_between_impl(&pages.start, &pages.end).unwrap();
let count = Page::<S>::steps_between_impl(&pages.start, &pages.end).0;

// Make sure that we never jump the gap in the address space when flushing.
let second_half_start =
Page::<S>::containing_address(VirtAddr::new(0xffff_8000_0000_0000));
let count = if pages.start < second_half_start {
let count_to_second_half =
Page::steps_between_impl(&pages.start, &second_half_start).unwrap();
Page::steps_between_impl(&pages.start, &second_half_start).0;
cmp::min(count, count_to_second_half)
} else {
count
Expand Down
10 changes: 6 additions & 4 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ impl<S: PageSize> Page<S> {
}

// FIXME: Move this into the `Step` impl, once `Step` is stabilized.
pub(crate) fn steps_between_impl(start: &Self, end: &Self) -> Option<usize> {
VirtAddr::steps_between_impl(&start.start_address, &end.start_address)
.map(|steps| steps / S::SIZE as usize)
pub(crate) fn steps_between_impl(start: &Self, end: &Self) -> (usize, Option<usize>) {
let (lower, upper) = VirtAddr::steps_between_impl(&start.start_address, &end.start_address);
let lower = lower / S::SIZE as usize;
let upper = upper.map(|steps| steps / S::SIZE as usize);
(lower, upper)
}

// FIXME: Move this into the `Step` impl, once `Step` is stabilized.
Expand Down Expand Up @@ -279,7 +281,7 @@ impl<S: PageSize> Sub<Self> for Page<S> {

#[cfg(feature = "step_trait")]
impl<S: PageSize> Step for Page<S> {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
Self::steps_between_impl(start, end)
}

Expand Down

0 comments on commit 0ee8fef

Please sign in to comment.