@@ -106,35 +106,31 @@ impl From<i32> for BranchOffset {
106106 }
107107}
108108
109+ impl From < BranchOffset > for i32 {
110+ fn from ( offset : BranchOffset ) -> Self {
111+ offset. 0
112+ }
113+ }
114+
109115impl BranchOffset {
110- /// Creates an uninitialized [`BranchOffset`] .
111- pub fn uninit ( ) -> Self {
112- Self ( 0 )
116+ /// Returns `true` if [`Self`] is an offset for a backward branch .
117+ pub fn is_backwards ( & self ) -> bool {
118+ self . 0 . is_negative ( )
113119 }
114120
115- /// Creates an initialized [`BranchOffset`] from `src` to `dst`.
116- ///
117- /// # Errors
118- ///
119- /// If the resulting [`BranchOffset`] is out of bounds.
120- pub fn from_src_to_dst ( src : u32 , dst : u32 ) -> Result < Self , Error > {
121- let src = i64:: from ( src) ;
122- let dst = i64:: from ( dst) ;
123- let Some ( offset) = dst. checked_sub ( src) else {
124- // Note: This never needs to be called on backwards branches since they are immediated resolved.
125- unreachable ! (
126- "offset for forward branches must have `src` be smaller than or equal to `dst`"
127- ) ;
128- } ;
129- let Ok ( offset) = i32:: try_from ( offset) else {
130- return Err ( Error :: BranchOffsetOutOfBounds ) ;
131- } ;
132- Ok ( Self ( offset) )
121+ /// Returns `true` if [`Self`] is an offset for a forward branch.
122+ pub fn is_forwards ( & self ) -> bool {
123+ self . 0 . is_positive ( )
133124 }
134125
135126 /// Returns `true` if the [`BranchOffset`] has been initialized.
136127 pub fn is_init ( self ) -> bool {
137- self . to_i32 ( ) != 0
128+ self . 0 != 0
129+ }
130+
131+ /// Creates an uninitialized [`BranchOffset`].
132+ pub fn uninit ( ) -> Self {
133+ Self ( 0 )
138134 }
139135
140136 /// Initializes the [`BranchOffset`] with a proper value.
@@ -148,11 +144,6 @@ impl BranchOffset {
148144 assert ! ( !self . is_init( ) ) ;
149145 * self = valid_offset;
150146 }
151-
152- /// Returns the `i32` representation of the [`BranchOffset`].
153- pub fn to_i32 ( self ) -> i32 {
154- self . 0
155- }
156147}
157148
158149/// The accumulated fuel to execute a block via [`Op::ConsumeFuel`].
0 commit comments