Skip to content

Commit

Permalink
add usize methods for Size getters
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 25, 2020
1 parent b7db732 commit 7400955
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/librustc/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<Tag> Allocation<Tag> {

pub fn undef(size: Size, align: Align) -> Self {
Allocation {
bytes: vec![0; usize::try_from(size.bytes()).unwrap()],
bytes: vec![0; size.bytes_usize()],
relocations: Relocations::new(),
undef_mask: UndefMask::new(size, false),
size,
Expand Down Expand Up @@ -153,7 +153,7 @@ impl Allocation<(), ()> {
/// Raw accessors. Provide access to otherwise private bytes.
impl<Tag, Extra> Allocation<Tag, Extra> {
pub fn len(&self) -> usize {
usize::try_from(self.size.bytes()).unwrap()
self.size.bytes_usize()
}

/// Looks at a slice which may describe undefined bytes or describe a relocation. This differs
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
size.bytes(),
self.len()
);
usize::try_from(offset.bytes()).unwrap()..end
offset.bytes_usize()..end
}

/// The last argument controls whether we error out when there are undefined
Expand Down Expand Up @@ -290,7 +290,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
cx: &impl HasDataLayout,
ptr: Pointer<Tag>,
) -> InterpResult<'tcx, &[u8]> {
let offset = usize::try_from(ptr.offset.bytes()).unwrap();
let offset = ptr.offset.bytes_usize();
Ok(match self.bytes[offset..].iter().position(|&c| c == 0) {
Some(size) => {
let size_with_null = Size::from_bytes(size) + Size::from_bytes(1);
Expand Down
13 changes: 6 additions & 7 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
}
if alloc.undef_mask().is_range_defined(i, i + Size::from_bytes(1)).is_ok() {
// this `as usize` is fine, since `i` came from a `usize`
let i = usize::try_from(i.bytes()).unwrap();
let i = i.bytes_usize();

// Checked definedness (and thus range) and relocations. This access also doesn't
// influence interpreter execution but is only for debugging.
Expand All @@ -693,8 +693,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
let mut pos = Size::ZERO;
let relocation_width = (self.pointer_size().bytes() - 1) * 3;
for (i, target_id) in relocations {
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes() as usize).unwrap();
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes_usize()).unwrap();
let target = format!("({})", target_id);
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
Expand Down Expand Up @@ -924,16 +923,16 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
for i in 0..length {
ptr::copy(
src_bytes,
dest_bytes.offset(isize::try_from((size * i).bytes()).unwrap()), // `Size` multiplication
usize::try_from(size.bytes()).unwrap(),
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
size.bytes_usize(),
);
}
} else {
for i in 0..length {
ptr::copy_nonoverlapping(
src_bytes,
dest_bytes.offset(isize::try_from((size * i).bytes()).unwrap()), // `Size` multiplication
usize::try_from(size.bytes()).unwrap(),
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
size.bytes_usize(),
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_target/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,23 @@ impl Size {
self.raw
}

#[inline]
pub fn bytes_usize(self) -> usize {
self.bytes().try_into().unwrap()
}

#[inline]
pub fn bits(self) -> u64 {
self.bytes().checked_mul(8).unwrap_or_else(|| {
panic!("Size::bits: {} bytes in bits doesn't fit in u64", self.bytes())
})
}

#[inline]
pub fn bits_usize(self) -> usize {
self.bits().try_into().unwrap()
}

#[inline]
pub fn align_to(self, align: Align) -> Size {
let mask = align.bytes() - 1;
Expand Down

0 comments on commit 7400955

Please sign in to comment.