Skip to content

Commit bf5b29e

Browse files
Serban Iorgaalxiord
authored andcommitted
[address.rs] documentation improvements and fixes
1 parent f623049 commit bf5b29e

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/address.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010

1111
//! Traits to represent an address within an address space.
1212
//!
13-
//! Two traits are defined to present an address within an address space:
14-
//! - [AddressValue](trait.AddressValue.html): stores the raw value of an address. Typically u32,
15-
//! u64 or usize is used to store the raw value. But pointers, such as *u8, can't be used because
16-
//! it doesn't implement the Add and Sub traits.
17-
//! - [Address](trait.Address.html): encapsulates an AddressValue object and defines methods to
18-
//! access and manipulate it.
13+
//! Two traits are defined to represent an address within an address space:
14+
//! - [AddressValue](trait.AddressValue.html): stores the raw value of an address. Typically `u32`,
15+
//! `u64` or `usize` is used to store the raw value. But pointers, such as `*u8`, can't be used
16+
//! because they don't implement the [`Add`](https://doc.rust-lang.org/std/ops/trait.Add.html) and
17+
//! [`Sub`](https://doc.rust-lang.org/std/ops/trait.Sub.html) traits.
18+
//! - [Address](trait.Address.html): encapsulates an [`AddressValue`](trait.AddressValue.html)
19+
//! object and defines methods to access and manipulate it.
1920
2021
use std::cmp::{Eq, Ord, PartialEq, PartialOrd};
2122
use std::ops::{Add, BitAnd, BitOr, Sub};
2223

2324
/// Simple helper trait used to store a raw address value.
2425
pub trait AddressValue {
25-
/// Type of the address raw value.
26+
/// Type of the raw address value.
2627
type V: Copy
2728
+ PartialEq
2829
+ Eq
@@ -41,7 +42,7 @@ pub trait AddressValue {
4142
/// could be used to manage address, size and offset. On the other hand, type aliases may be
4243
/// defined to improve code readability.
4344
///
44-
/// One design rule is applied to the Address trait that operators (+, -, &, | etc) are not
45+
/// One design rule is applied to the Address trait, namely that operators (+, -, &, | etc) are not
4546
/// supported and it forces clients to explicitly invoke corresponding methods. But there are
4647
/// always exceptions:
4748
/// Address (BitAnd|BitOr) AddressValue are supported.
@@ -57,23 +58,25 @@ pub trait Address:
5758
+ BitAnd<<Self as AddressValue>::V, Output = Self>
5859
+ BitOr<<Self as AddressValue>::V, Output = Self>
5960
{
60-
/// Create an address from a raw address value.
61+
/// Creates an address from a raw address value.
6162
fn new(addr: Self::V) -> Self;
6263

63-
/// Get the raw value of the address.
64+
/// Returns the raw value of the address.
6465
fn raw_value(&self) -> Self::V;
6566

6667
/// Returns the bitwise and of the address with the given mask.
6768
fn mask(&self, mask: Self::V) -> Self::V {
6869
self.raw_value() & mask
6970
}
7071

71-
/// Returns the offset from this address to the given base address and None if there is
72-
/// underflow.
72+
/// Computes the offset from this address to the given base address.
73+
///
74+
/// Returns `None` if there is underflow.
7375
fn checked_offset_from(&self, base: Self) -> Option<Self::V>;
7476

75-
/// Returns the offset from this address to the given base address.
76-
/// Only use this when `base` is guaranteed not to overflow.
77+
/// Computes the offset from this address to the given base address.
78+
///
79+
/// Results in undefined behavior when an underflow occurs.
7780
/// # Examples
7881
///
7982
/// ```
@@ -86,24 +89,34 @@ pub trait Address:
8689
self.raw_value() - base.raw_value()
8790
}
8891

89-
/// Returns the result of the add or None if there is overflow.
92+
/// Computes `self + other`, returning `None` if overflow occurred.
9093
fn checked_add(&self, other: Self::V) -> Option<Self>;
9194

92-
/// Returns the result of the add and a flag identifying whether there was overflow
95+
/// Computes `self + other`.
96+
///
97+
/// Returns a tuple of the addition result along with a boolean indicating whether an arithmetic
98+
/// overflow would occur. If an overflow would have occurred then the wrapped address
99+
/// is returned.
93100
fn overflowing_add(&self, other: Self::V) -> (Self, bool);
94101

95-
/// Returns the result of the base address + the size.
96-
/// Only use this when `offset` is guaranteed not to overflow.
102+
/// Computes `self + offset`.
103+
///
104+
/// Results in undefined behavior when an overflow occurs.
97105
fn unchecked_add(&self, offset: Self::V) -> Self;
98106

99-
/// Returns the result of the subtraction or None if there is underflow.
107+
/// Subtracts two addresses, checking for underflow. If underflow happens, `None` is returned.
100108
fn checked_sub(&self, other: Self::V) -> Option<Self>;
101109

102-
/// Returns the result of the subtraction and a flag identifying whether there was overflow
110+
/// Computes `self - other`.
111+
///
112+
/// Returns a tuple of the subtraction result along with a boolean indicating whether an
113+
/// arithmetic overflow would occur. If an overflow would have occurred then the wrapped
114+
/// address is returned.
103115
fn overflowing_sub(&self, other: Self::V) -> (Self, bool);
104116

105-
/// Returns the result of the subtraction.
106-
/// Only use this when `other` is guaranteed not to underflow.
117+
/// Computes `self - other`.
118+
///
119+
/// Results in undefined behavior when an underflow occurs.
107120
fn unchecked_sub(&self, other: Self::V) -> Self;
108121
}
109122

0 commit comments

Comments
 (0)