`impl Step` for block `Address` to allow range iterating

pull/3/head
Szymon Walter 2018-03-20 10:42:22 +01:00
parent 63f0203601
commit 88b1e3b668
2 changed files with 35 additions and 0 deletions

View File

@ -1,6 +1,8 @@
use core::mem;
use core::marker::PhantomData;
use core::ops::{Add, Sub};
use core::fmt::{self, Debug, Display, LowerHex};
use core::iter::Step;
pub trait Size {
// log_block_size = log_2(block_size) - 10
@ -81,6 +83,38 @@ impl<S: Size> Address<S> {
}
}
impl<S: Size + Clone + PartialOrd> Step for Address<S> {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
if end.block >= start.block {
Some(end.block - start.block)
} else {
None
}
}
fn replace_one(&mut self) -> Self {
mem::replace(self, Address::new(1, 0))
}
fn replace_zero(&mut self) -> Self {
mem::replace(self, Address::new(0, 0))
}
fn add_one(&self) -> Self {
Address::new(self.block + 1, 0)
}
fn sub_one(&self) -> Self {
Address::new(self.block - 1, 0)
}
fn add_usize(&self, n: usize) -> Option<Self> {
self.block
.checked_add(n)
.map(|block| Address::new(block, 0))
}
}
impl<S: Size> Debug for Address<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = format!("Address<{}>", S::SIZE);

View File

@ -3,6 +3,7 @@
#![feature(swap_with_slice)]
#![feature(macro_lifetime_matcher)]
#![feature(const_fn)]
#![feature(step_trait)]
#![cfg_attr(all(not(test), feature = "no_std"), no_std)]
#[macro_use]