Skip to content

Commit

Permalink
Add Rust VecDeque formatter (#610)
Browse files Browse the repository at this point in the history
Add Rust VecDeque formatter
  • Loading branch information
russelltg authored Feb 10, 2022
1 parent bed5484 commit 0ece78e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 5 additions & 1 deletion debuggee/rust/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod tests;

use std::borrow::Cow;
use std::cell;
use std::collections::{HashMap, HashSet};
use std::collections::{HashMap, HashSet, VecDeque};
use std::path;
use std::rc;
use std::sync;
Expand Down Expand Up @@ -150,6 +150,10 @@ fn arrays() {
let vec_str = vec!["111", "2222", "3333", "4444", "5555"];
let vec_tuple = vec![(1, 2), (2, 3), (3, 4)];
let large_vec: Vec<i32> = (0..20000).collect();
let vecdeque_int = VecDeque::from(vec_int.clone());

let mut vecdeque_popped = vecdeque_int.clone();
vecdeque_popped.pop_front();

println!("---"); // #BP_arrays
println!("---");
Expand Down
39 changes: 39 additions & 0 deletions formatters/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def initialize_category(debugger):
attach_synthetic_to_type(StdVectorSynthProvider, r'^collections::vec::Vec<.+>$', True) # Before 1.20
attach_synthetic_to_type(StdVectorSynthProvider, r'^alloc::vec::Vec<.+>$', True) # Since 1.20

attach_synthetic_to_type(StdVecDequeSynthProvider, r'^collections::vec_deque::VecDeque<.+>$', True) # Before 1.20
attach_synthetic_to_type(StdVecDequeSynthProvider, r'^alloc::collections::vec_deque::VecDeque<.+>$', True) # Since 1.20

attach_synthetic_to_type(MsvcEnumSynthProvider, r'^enum\$<.+>$', True)

attach_synthetic_to_type(SliceSynthProvider, r'^&(mut *)?\[.*\]$', True)
Expand Down Expand Up @@ -275,6 +278,42 @@ def ptr_and_len(self, vec):
def get_summary(self):
return '(%d) vec![%s]' % (self.len, sequence_summary((self.get_child_at_index(i) for i in range(self.len))))

class StdVecDequeSynthProvider(RustSynthProvider):
def initialize(self):
self.ptr = read_unique_ptr(gcm(self.valobj, 'buf', 'ptr'))
self.cap = gcm(self.valobj, 'buf', 'cap').GetValueAsUnsigned()
self.tail = gcm(self.valobj, 'tail').GetValueAsUnsigned()
self.head = gcm(self.valobj, 'head').GetValueAsUnsigned()
self.item_type = self.ptr.GetType().GetPointeeType()
self.item_size = self.item_type.GetByteSize()

def num_children(self):
return (self.head - self.tail) % self.cap

def has_children(self):
return True

def get_child_at_index(self, index):
try:
if not 0 <= index < self.num_children():
return None
offset = ((self.tail + index) % self.cap) * self.item_size
return self.ptr.CreateChildAtOffset('[%s]' % index, offset, self.item_type)
except Exception as e:
log.error('%s', e)
raise

def get_child_index(self, name):
try:
return int(name.lstrip('[').rstrip(']'))
except Exception as e:
log.error('%s', e)
raise


def get_summary(self):
return '(%d) VecDeque[%s]' % (self.num_children(), sequence_summary((self.get_child_at_index(i) for i in range(self.num_children()))))

##################################################################################################################


Expand Down
8 changes: 8 additions & 0 deletions tests/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,14 @@ function generateSuite(triple: string) {
$: '(10) vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]',
'[0]': 1, '[1]': 2, '[9]': 10
},
vecdeque_int: {
$: '(10) VecDeque[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]',
'[0]': 1, '[1]': 2, '[9]': 10
},
vecdeque_popped: {
$: '(9) VecDeque[2, 3, 4, 5, 6, 7, 8, 9, 10]',
'[0]': 2, '[1]': 3, '[8]': 10
},
large_vec: '(20000) vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]',
vec_str: {
$: '(5) vec!["111", "2222", "3333", "4444", "5555", ...]',
Expand Down

0 comments on commit 0ece78e

Please sign in to comment.