10
10
11
11
//! Traits to represent an address within an address space.
12
12
//!
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.
19
20
20
21
use std:: cmp:: { Eq , Ord , PartialEq , PartialOrd } ;
21
22
use std:: ops:: { Add , BitAnd , BitOr , Sub } ;
22
23
23
24
/// Simple helper trait used to store a raw address value.
24
25
pub trait AddressValue {
25
- /// Type of the address raw value.
26
+ /// Type of the raw address value.
26
27
type V : Copy
27
28
+ PartialEq
28
29
+ Eq
@@ -41,7 +42,7 @@ pub trait AddressValue {
41
42
/// could be used to manage address, size and offset. On the other hand, type aliases may be
42
43
/// defined to improve code readability.
43
44
///
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
45
46
/// supported and it forces clients to explicitly invoke corresponding methods. But there are
46
47
/// always exceptions:
47
48
/// Address (BitAnd|BitOr) AddressValue are supported.
@@ -57,23 +58,25 @@ pub trait Address:
57
58
+ BitAnd < <Self as AddressValue >:: V , Output = Self >
58
59
+ BitOr < <Self as AddressValue >:: V , Output = Self >
59
60
{
60
- /// Create an address from a raw address value.
61
+ /// Creates an address from a raw address value.
61
62
fn new ( addr : Self :: V ) -> Self ;
62
63
63
- /// Get the raw value of the address.
64
+ /// Returns the raw value of the address.
64
65
fn raw_value ( & self ) -> Self :: V ;
65
66
66
67
/// Returns the bitwise and of the address with the given mask.
67
68
fn mask ( & self , mask : Self :: V ) -> Self :: V {
68
69
self . raw_value ( ) & mask
69
70
}
70
71
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.
73
75
fn checked_offset_from ( & self , base : Self ) -> Option < Self :: V > ;
74
76
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.
77
80
/// # Examples
78
81
///
79
82
/// ```
@@ -86,24 +89,34 @@ pub trait Address:
86
89
self . raw_value ( ) - base. raw_value ( )
87
90
}
88
91
89
- /// Returns the result of the add or None if there is overflow.
92
+ /// Computes `self + other`, returning ` None` if overflow occurred .
90
93
fn checked_add ( & self , other : Self :: V ) -> Option < Self > ;
91
94
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.
93
100
fn overflowing_add ( & self , other : Self :: V ) -> ( Self , bool ) ;
94
101
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.
97
105
fn unchecked_add ( & self , offset : Self :: V ) -> Self ;
98
106
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 .
100
108
fn checked_sub ( & self , other : Self :: V ) -> Option < Self > ;
101
109
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.
103
115
fn overflowing_sub ( & self , other : Self :: V ) -> ( Self , bool ) ;
104
116
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.
107
120
fn unchecked_sub ( & self , other : Self :: V ) -> Self ;
108
121
}
109
122
0 commit comments