Skip to content

Commit 7400955

Browse files
committed
add usize methods for Size getters
1 parent b7db732 commit 7400955

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/librustc/mir/interpret/allocation.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<Tag> Allocation<Tag> {
110110

111111
pub fn undef(size: Size, align: Align) -> Self {
112112
Allocation {
113-
bytes: vec![0; usize::try_from(size.bytes()).unwrap()],
113+
bytes: vec![0; size.bytes_usize()],
114114
relocations: Relocations::new(),
115115
undef_mask: UndefMask::new(size, false),
116116
size,
@@ -153,7 +153,7 @@ impl Allocation<(), ()> {
153153
/// Raw accessors. Provide access to otherwise private bytes.
154154
impl<Tag, Extra> Allocation<Tag, Extra> {
155155
pub fn len(&self) -> usize {
156-
usize::try_from(self.size.bytes()).unwrap()
156+
self.size.bytes_usize()
157157
}
158158

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

198198
/// The last argument controls whether we error out when there are undefined
@@ -290,7 +290,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
290290
cx: &impl HasDataLayout,
291291
ptr: Pointer<Tag>,
292292
) -> InterpResult<'tcx, &[u8]> {
293-
let offset = usize::try_from(ptr.offset.bytes()).unwrap();
293+
let offset = ptr.offset.bytes_usize();
294294
Ok(match self.bytes[offset..].iter().position(|&c| c == 0) {
295295
Some(size) => {
296296
let size_with_null = Size::from_bytes(size) + Size::from_bytes(1);

src/librustc_mir/interpret/memory.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
668668
}
669669
if alloc.undef_mask().is_range_defined(i, i + Size::from_bytes(1)).is_ok() {
670670
// this `as usize` is fine, since `i` came from a `usize`
671-
let i = usize::try_from(i.bytes()).unwrap();
671+
let i = i.bytes_usize();
672672

673673
// Checked definedness (and thus range) and relocations. This access also doesn't
674674
// influence interpreter execution but is only for debugging.
@@ -693,8 +693,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
693693
let mut pos = Size::ZERO;
694694
let relocation_width = (self.pointer_size().bytes() - 1) * 3;
695695
for (i, target_id) in relocations {
696-
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
697-
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes() as usize).unwrap();
696+
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes_usize()).unwrap();
698697
let target = format!("({})", target_id);
699698
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
700699
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
@@ -924,16 +923,16 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
924923
for i in 0..length {
925924
ptr::copy(
926925
src_bytes,
927-
dest_bytes.offset(isize::try_from((size * i).bytes()).unwrap()), // `Size` multiplication
928-
usize::try_from(size.bytes()).unwrap(),
926+
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
927+
size.bytes_usize(),
929928
);
930929
}
931930
} else {
932931
for i in 0..length {
933932
ptr::copy_nonoverlapping(
934933
src_bytes,
935-
dest_bytes.offset(isize::try_from((size * i).bytes()).unwrap()), // `Size` multiplication
936-
usize::try_from(size.bytes()).unwrap(),
934+
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
935+
size.bytes_usize(),
937936
);
938937
}
939938
}

src/librustc_target/abi/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,23 @@ impl Size {
260260
self.raw
261261
}
262262

263+
#[inline]
264+
pub fn bytes_usize(self) -> usize {
265+
self.bytes().try_into().unwrap()
266+
}
267+
263268
#[inline]
264269
pub fn bits(self) -> u64 {
265270
self.bytes().checked_mul(8).unwrap_or_else(|| {
266271
panic!("Size::bits: {} bytes in bits doesn't fit in u64", self.bytes())
267272
})
268273
}
269274

275+
#[inline]
276+
pub fn bits_usize(self) -> usize {
277+
self.bits().try_into().unwrap()
278+
}
279+
270280
#[inline]
271281
pub fn align_to(self, align: Align) -> Size {
272282
let mask = align.bytes() - 1;

0 commit comments

Comments
 (0)