Skip to content

Commit be66c2e

Browse files
impl Step for Size; copy-paste fixme
1 parent 98c5649 commit be66c2e

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -761,14 +761,11 @@ impl InitMask {
761761
}
762762

763763
// FIXME(oli-obk): optimize this for allocations larger than a block.
764-
let idx = (start.bytes()..end.bytes()).map(Size::from_bytes).find(|&i| !self.get(i));
764+
let idx = (start..end).find(|&i| !self.get(i));
765765

766766
match idx {
767767
Some(idx) => {
768-
let uninit_end = (idx.bytes()..end.bytes())
769-
.map(Size::from_bytes)
770-
.find(|&i| self.get(i))
771-
.unwrap_or(end);
768+
let uninit_end = (idx..end).find(|&i| self.get(i)).unwrap_or(end);
772769
Err(idx..uninit_end)
773770
}
774771
None => Ok(()),
@@ -906,10 +903,9 @@ impl<'a> Iterator for InitChunkIter<'a> {
906903
}
907904

908905
let is_init = self.init_mask.get(self.start);
909-
let end_of_chunk = (self.start.bytes()..self.end.bytes())
910-
.map(Size::from_bytes)
911-
.find(|&i| self.init_mask.get(i) != is_init)
912-
.unwrap_or(self.end);
906+
// FIXME(oli-obk): optimize this for allocations larger than a block.
907+
let end_of_chunk =
908+
(self.start..self.end).find(|&i| self.init_mask.get(i) != is_init).unwrap_or(self.end);
913909
let range = self.start..end_of_chunk;
914910

915911
self.start = end_of_chunk;

compiler/rustc_target/src/abi/mod.rs

+38
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::spec::Target;
55

66
use std::convert::{TryFrom, TryInto};
77
use std::fmt;
8+
use std::iter::Step;
89
use std::num::NonZeroUsize;
910
use std::ops::{Add, AddAssign, Deref, Mul, Range, RangeInclusive, Sub};
1011
use std::str::FromStr;
@@ -433,6 +434,43 @@ impl AddAssign for Size {
433434
}
434435
}
435436

437+
unsafe impl Step for Size {
438+
#[inline]
439+
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
440+
u64::steps_between(&start.bytes(), &end.bytes())
441+
}
442+
443+
#[inline]
444+
fn forward_checked(start: Self, count: usize) -> Option<Self> {
445+
u64::forward_checked(start.bytes(), count).map(Self::from_bytes)
446+
}
447+
448+
#[inline]
449+
fn forward(start: Self, count: usize) -> Self {
450+
Self::from_bytes(u64::forward(start.bytes(), count))
451+
}
452+
453+
#[inline]
454+
unsafe fn forward_unchecked(start: Self, count: usize) -> Self {
455+
Self::from_bytes(u64::forward_unchecked(start.bytes(), count))
456+
}
457+
458+
#[inline]
459+
fn backward_checked(start: Self, count: usize) -> Option<Self> {
460+
u64::backward_checked(start.bytes(), count).map(Self::from_bytes)
461+
}
462+
463+
#[inline]
464+
fn backward(start: Self, count: usize) -> Self {
465+
Self::from_bytes(u64::backward(start.bytes(), count))
466+
}
467+
468+
#[inline]
469+
unsafe fn backward_unchecked(start: Self, count: usize) -> Self {
470+
Self::from_bytes(u64::backward_unchecked(start.bytes(), count))
471+
}
472+
}
473+
436474
/// Alignment of a type in bytes (always a power of two).
437475
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
438476
#[derive(HashStable_Generic)]

compiler/rustc_target/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#![feature(never_type)]
1616
#![feature(associated_type_bounds)]
1717
#![feature(exhaustive_patterns)]
18+
#![feature(step_trait)]
19+
#![feature(step_trait_ext)]
20+
#![feature(unchecked_math)]
1821

1922
#[macro_use]
2023
extern crate rustc_macros;

0 commit comments

Comments
 (0)