Skip to content

Commit a0f6b62

Browse files
committed
implement Step for PageTableIndex
1 parent 0cb87fe commit a0f6b62

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/structures/paging/page_table.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Abstractions for page tables and page table entries.
22
33
use core::fmt;
4+
#[cfg(feature = "step_trait")]
5+
use core::iter::Step;
46
use core::ops::{Index, IndexMut};
57

68
use super::{PageSize, PhysFrame, Size4KiB};
@@ -341,6 +343,26 @@ impl From<PageTableIndex> for usize {
341343
}
342344
}
343345

346+
#[cfg(feature = "step_trait")]
347+
impl Step for PageTableIndex {
348+
#[inline]
349+
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
350+
end.0.checked_sub(start.0).map(usize::from)
351+
}
352+
353+
#[inline]
354+
fn forward_checked(start: Self, count: usize) -> Option<Self> {
355+
let idx = usize::from(start).checked_add(count)?;
356+
(idx < ENTRY_COUNT).then(|| Self::new(idx as u16))
357+
}
358+
359+
#[inline]
360+
fn backward_checked(start: Self, count: usize) -> Option<Self> {
361+
let idx = usize::from(start).checked_sub(count)?;
362+
Some(Self::new(idx as u16))
363+
}
364+
}
365+
344366
/// A 12-bit offset into a 4KiB Page.
345367
///
346368
/// This type is returned by the `VirtAddr::page_offset` method.

0 commit comments

Comments
 (0)