Skip to content

Implement core::iter::Step trait for for PhysFrame #292

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ cc = { version = "1.0.37", optional = true }
default = [ "nightly", "instructions" ]
instructions = []
external_asm = [ "cc" ]
nightly = [ "inline_asm", "const_fn", "abi_x86_interrupt", "doc_cfg" ]
nightly = [ "inline_asm", "const_fn", "abi_x86_interrupt", "doc_cfg", "step_trait" ]
inline_asm = []
abi_x86_interrupt = []
const_fn = []
doc_cfg = []
step_trait = []

[package.metadata.release]
no-dev-version = true
Expand Down
19 changes: 19 additions & 0 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,25 @@ impl Sub<PhysAddr> for PhysAddr {
}
}

#[cfg(feature = "step_trait")]
impl core::iter::Step for PhysAddr {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
if *start <= *end {
Some((*end - *start) as usize)
} else {
None
}
}

fn forward_checked(start: Self, count: usize) -> Option<Self> {
start.as_u64().checked_add(count as u64).map(PhysAddr::new)
}

fn backward_checked(start: Self, count: usize) -> Option<Self> {
start.as_u64().checked_sub(count as u64).map(PhysAddr::new)
}
}

/// Align address downwards.
///
/// Returns the greatest `x` with alignment `align` so that `x <= addr`.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![cfg_attr(feature = "inline_asm", feature(asm))]
#![cfg_attr(feature = "abi_x86_interrupt", feature(abi_x86_interrupt))]
#![cfg_attr(feature = "doc_cfg", feature(doc_cfg))]
#![warn(missing_docs)]
#![cfg_attr(feature = "step_trait", feature(step_trait))]
#![deny(missing_debug_implementations)]

pub use crate::addr::{align_down, align_up, PhysAddr, VirtAddr};
Expand Down
17 changes: 17 additions & 0 deletions src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ impl<S: PageSize> Sub<PhysFrame<S>> for PhysFrame<S> {
}
}

#[cfg(feature = "step_trait")]
impl<S: PageSize> core::iter::Step for PhysFrame<S> {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
PhysAddr::steps_between(&start.start_address, &end.start_address)
}

fn forward_checked(start: Self, count: usize) -> Option<Self> {
PhysAddr::forward_checked(start.start_address, count)
.and_then(|start_addr| Self::from_start_address(start_addr).ok())
}

fn backward_checked(start: Self, count: usize) -> Option<Self> {
PhysAddr::backward_checked(start.start_address, count)
.and_then(|start_addr| Self::from_start_address(start_addr).ok())
}
}

Comment on lines +135 to +151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These implementations are incorrect. We need to be taking the page size into account (so that we are consistent with the PhysFrame<S>: Add<u64> implementation).

Specifically, steps_between should be:

end.checked_sub(start)?.try_into() 

and forward_checked should be:

start.checked_add(count.try_into()?)

(similar for backward_checked)

/// An range of physical memory frames, exclusive the upper bound.
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)]
Expand Down