pub struct SwitchTargets<'a> { /* private fields */ }Expand description
An allocation-free view over a validated switch displacement table. All targets use the address immediately after the entire table as base.
Implementations§
Source§impl<'a> SwitchTargets<'a>
impl<'a> SwitchTargets<'a>
Sourcepub const fn len(self) -> usize
pub const fn len(self) -> usize
Examples found in repository?
examples/support/stress.rs (line 151)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}pub const fn is_empty(self) -> bool
pub const fn base_ip(self) -> u64
pub fn displacement(self, index: usize) -> Option<i32>
Sourcepub fn get(self, index: usize) -> Option<u64>
pub fn get(self, index: usize) -> Option<u64>
Examples found in repository?
examples/support/stress.rs (line 153)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Sourcepub fn iter(self) -> SwitchTargetIter<'a> ⓘ
pub fn iter(self) -> SwitchTargetIter<'a> ⓘ
Examples found in repository?
examples/support/stress.rs (line 151)
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}Trait Implementations§
Source§impl<'a> Clone for SwitchTargets<'a>
impl<'a> Clone for SwitchTargets<'a>
impl<'a> Copy for SwitchTargets<'a>
Source§impl<'a> Debug for SwitchTargets<'a>
impl<'a> Debug for SwitchTargets<'a>
impl<'a> Eq for SwitchTargets<'a>
Source§impl<'a> Hash for SwitchTargets<'a>
impl<'a> Hash for SwitchTargets<'a>
Source§impl<'a> IntoIterator for SwitchTargets<'a>
impl<'a> IntoIterator for SwitchTargets<'a>
Source§impl<'a> PartialEq for SwitchTargets<'a>
impl<'a> PartialEq for SwitchTargets<'a>
impl<'a> StructuralPartialEq for SwitchTargets<'a>
Auto Trait Implementations§
impl<'a> Freeze for SwitchTargets<'a>
impl<'a> RefUnwindSafe for SwitchTargets<'a>
impl<'a> Send for SwitchTargets<'a>
impl<'a> Sync for SwitchTargets<'a>
impl<'a> Unpin for SwitchTargets<'a>
impl<'a> UnsafeUnpin for SwitchTargets<'a>
impl<'a> UnwindSafe for SwitchTargets<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more