Skip to content

Commit 6f891df

Browse files
committed
impl Step for Page
1 parent 3680907 commit 6f891df

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/structures/paging/page.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use crate::structures::paging::page_table::PageTableLevel;
44
use crate::structures::paging::PageTableIndex;
55
use crate::VirtAddr;
66
use core::fmt;
7+
#[cfg(feature = "step_trait")]
8+
use core::iter::Step;
79
use core::marker::PhantomData;
810
use core::ops::{Add, AddAssign, Sub, SubAssign};
911

@@ -274,6 +276,32 @@ impl<S: PageSize> Sub<Self> for Page<S> {
274276
}
275277
}
276278

279+
#[cfg(feature = "step_trait")]
280+
impl<S: PageSize> Step for Page<S> {
281+
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
282+
Step::steps_between(&start.start_address, &end.start_address)
283+
.map(|steps| steps / S::SIZE as usize)
284+
}
285+
286+
fn forward_checked(start: Self, count: usize) -> Option<Self> {
287+
let count = count.checked_mul(S::SIZE as usize)?;
288+
let start_address = Step::forward_checked(start.start_address, count)?;
289+
Some(Self {
290+
start_address,
291+
size: PhantomData,
292+
})
293+
}
294+
295+
fn backward_checked(start: Self, count: usize) -> Option<Self> {
296+
let count = count.checked_mul(S::SIZE as usize)?;
297+
let start_address = Step::backward_checked(start.start_address, count)?;
298+
Some(Self {
299+
start_address,
300+
size: PhantomData,
301+
})
302+
}
303+
}
304+
277305
/// A range of pages with exclusive upper bound.
278306
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
279307
#[repr(C)]

0 commit comments

Comments
 (0)