Skip to content

Commit 7e8a6b0

Browse files
authored
Merge pull request #543 from rust-osdev/enhancement/address-gap-panics
improve `VirtAddr` `Add` & `Sub` impls
2 parents f97b845 + 0de6929 commit 7e8a6b0

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

src/addr.rs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,41 @@ impl fmt::Pointer for VirtAddr {
364364

365365
impl Add<u64> for VirtAddr {
366366
type Output = Self;
367+
368+
#[cfg_attr(not(feature = "step_trait"), allow(rustdoc::broken_intra_doc_links))]
369+
/// Add an offset to a virtual address.
370+
///
371+
/// This function performs normal arithmetic addition and doesn't jump the
372+
/// address gap. If you're looking for a successor operation that jumps the
373+
/// address gap, use [`Step::forward`].
374+
///
375+
/// # Panics
376+
///
377+
/// This function will panic on overflow or if the result is not a
378+
/// canonical address.
367379
#[inline]
368380
fn add(self, rhs: u64) -> Self::Output {
369-
VirtAddr::new(self.0.checked_add(rhs).unwrap())
381+
VirtAddr::try_new(
382+
self.0
383+
.checked_add(rhs)
384+
.expect("attempt to add with overflow"),
385+
)
386+
.expect("attempt to add resulted in non-canonical virtual address")
370387
}
371388
}
372389

373390
impl AddAssign<u64> for VirtAddr {
391+
#[cfg_attr(not(feature = "step_trait"), allow(rustdoc::broken_intra_doc_links))]
392+
/// Add an offset to a virtual address.
393+
///
394+
/// This function performs normal arithmetic addition and doesn't jump the
395+
/// address gap. If you're looking for a successor operation that jumps the
396+
/// address gap, use [`Step::forward`].
397+
///
398+
/// # Panics
399+
///
400+
/// This function will panic on overflow or if the result is not a
401+
/// canonical address.
374402
#[inline]
375403
fn add_assign(&mut self, rhs: u64) {
376404
*self = *self + rhs;
@@ -379,13 +407,41 @@ impl AddAssign<u64> for VirtAddr {
379407

380408
impl Sub<u64> for VirtAddr {
381409
type Output = Self;
410+
411+
#[cfg_attr(not(feature = "step_trait"), allow(rustdoc::broken_intra_doc_links))]
412+
/// Subtract an offset from a virtual address.
413+
///
414+
/// This function performs normal arithmetic subtraction and doesn't jump
415+
/// the address gap. If you're looking for a predecessor operation that
416+
/// jumps the address gap, use [`Step::backward`].
417+
///
418+
/// # Panics
419+
///
420+
/// This function will panic on overflow or if the result is not a
421+
/// canonical address.
382422
#[inline]
383423
fn sub(self, rhs: u64) -> Self::Output {
384-
VirtAddr::new(self.0.checked_sub(rhs).unwrap())
424+
VirtAddr::try_new(
425+
self.0
426+
.checked_sub(rhs)
427+
.expect("attempt to subtract with overflow"),
428+
)
429+
.expect("attempt to subtract resulted in non-canonical virtual address")
385430
}
386431
}
387432

388433
impl SubAssign<u64> for VirtAddr {
434+
#[cfg_attr(not(feature = "step_trait"), allow(rustdoc::broken_intra_doc_links))]
435+
/// Subtract an offset from a virtual address.
436+
///
437+
/// This function performs normal arithmetic subtraction and doesn't jump
438+
/// the address gap. If you're looking for a predecessor operation that
439+
/// jumps the address gap, use [`Step::backward`].
440+
///
441+
/// # Panics
442+
///
443+
/// This function will panic on overflow or if the result is not a
444+
/// canonical address.
389445
#[inline]
390446
fn sub_assign(&mut self, rhs: u64) {
391447
*self = *self - rhs;
@@ -394,9 +450,17 @@ impl SubAssign<u64> for VirtAddr {
394450

395451
impl Sub<VirtAddr> for VirtAddr {
396452
type Output = u64;
453+
454+
/// Returns the difference between two addresses.
455+
///
456+
/// # Panics
457+
///
458+
/// This function will panic on overflow.
397459
#[inline]
398460
fn sub(self, rhs: VirtAddr) -> Self::Output {
399-
self.as_u64().checked_sub(rhs.as_u64()).unwrap()
461+
self.as_u64()
462+
.checked_sub(rhs.as_u64())
463+
.expect("attempt to subtract with overflow")
400464
}
401465
}
402466

0 commit comments

Comments
 (0)