Skip to content

Commit f0ce360

Browse files
authored
Add write::Expression::as_raw (#863)
1 parent 0a1c1db commit f0ce360

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/write/op.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ impl Expression {
3131
}
3232
}
3333

34+
/// If the expression consists only of raw bytecode, then return that bytecode.
35+
pub fn as_raw(&self) -> Option<&[u8]> {
36+
match &self.operations[..] {
37+
[Operation::Raw(bytecode)] => Some(bytecode),
38+
_ => None,
39+
}
40+
}
41+
3442
/// Add an operation to the expression.
3543
///
3644
/// This should only be used for operations that have no explicit operands.
@@ -1776,4 +1784,13 @@ mod tests {
17761784
let mut sections = Sections::new(EndianVec::new(LittleEndian));
17771785
assert_eq!(dwarf.write(&mut sections), Err(Error::InvalidReference));
17781786
}
1787+
1788+
#[test]
1789+
fn test_expression_as_raw() {
1790+
assert_eq!(Expression::new().as_raw(), None);
1791+
assert_eq!(Expression::raw(b"1234".into()).as_raw(), Some(&b"1234"[..]));
1792+
let mut expression = Expression::raw(b"1234".into());
1793+
expression.op_constu(1);
1794+
assert_eq!(expression.as_raw(), None);
1795+
}
17791796
}

0 commit comments

Comments
 (0)