ms_pdb/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Misc utilities

pub mod align;
pub mod io;
pub mod iter;
pub mod path;
pub mod swizzle;
pub mod vec;

use std::ops::Range;
use zerocopy::{FromBytes, Immutable, IntoBytes};

/// Copies a value that implements `FromBytes`, by simply copying its byte representation.
pub fn copy_from_bytes<T>(t: &T) -> T
where
    T: IntoBytes + FromBytes + Immutable,
{
    FromBytes::read_from_bytes(t.as_bytes()).unwrap()
}

/// Helps decode records that are indexed using "starts" arrays.
pub struct StartsOf<'a, T> {
    /// The "starts" array
    pub starts: &'a [u32],
    /// The items that are being indexed.
    pub items: &'a [T],
}

impl<'a, T> StartsOf<'a, T> {
    /// Initializes a new starts-based array accessor.
    pub fn new(starts: &'a [u32], items: &'a [T]) -> Self {
        debug_assert!(!starts.is_empty());
        debug_assert_eq!(starts[0], 0);
        debug_assert_eq!(*starts.last().unwrap() as usize, items.len());
        debug_assert!(starts.windows(2).all(|w| w[0] <= w[1]));

        Self { starts, items }
    }
}

impl<'a, T> std::ops::Index<usize> for StartsOf<'a, T> {
    type Output = [T];

    fn index(&self, i: usize) -> &[T] {
        let start = self.starts[i] as usize;
        let end = self.starts[i + 1] as usize;
        &self.items[start..end]
    }
}

/// True if `n` is a multiple of 4.
pub fn is_aligned_4(n: usize) -> bool {
    (n & 3) == 0
}

/// Align n up to the next multiple of 4, if it is not already a multiple of 4.
pub fn align_4(n: usize) -> usize {
    (n + 3) & !3
}

/// Iterates ranges of items within a slice that share a common property.
pub fn iter_similar_ranges<'a, T, F>(items: &'a [T], is_eq: F) -> IterSimilarRanges<'a, T, F> {
    IterSimilarRanges {
        items,
        is_eq,
        start: 0,
    }
}

/// Iterates ranges of items within a slice that share a common property.
pub struct IterSimilarRanges<'a, T, F> {
    items: &'a [T],
    is_eq: F,
    start: usize,
}

impl<'a, T, F> Iterator for IterSimilarRanges<'a, T, F>
where
    F: FnMut(&T, &T) -> bool,
{
    type Item = Range<usize>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.items.is_empty() {
            return None;
        }

        let first = &self.items[0];

        let mut i = 1;
        while i < self.items.len() && (self.is_eq)(first, &self.items[i]) {
            i += 1;
        }

        let start = self.start;
        self.start += i;
        self.items = &self.items[i..];

        Some(start..start + i)
    }
}

/// Iterates ranges of items within a slice that share a common property.
pub fn iter_similar_slices<'a, T, F>(items: &'a [T], is_eq: F) -> IterSimilarSlices<'a, T, F> {
    IterSimilarSlices { items, is_eq }
}

/// Iterates slices of items within a slice that share a common property.
pub struct IterSimilarSlices<'a, T, F> {
    items: &'a [T],
    is_eq: F,
}

impl<'a, T, F> Iterator for IterSimilarSlices<'a, T, F>
where
    F: FnMut(&T, &T) -> bool,
{
    type Item = &'a [T];

    fn next(&mut self) -> Option<Self::Item> {
        if self.items.is_empty() {
            return None;
        }

        let first = &self.items[0];

        let mut i = 1;
        while i < self.items.len() && (self.is_eq)(first, &self.items[i]) {
            i += 1;
        }

        let (lo, hi) = self.items.split_at(i);
        self.items = hi;
        Some(lo)
    }
}

/// Iterates ranges of items within a slice that share a common property.
pub fn iter_similar_slices_mut<'a, T, F>(
    items: &'a mut [T],
    is_eq: F,
) -> IterSimilarSlicesMut<'a, T, F> {
    IterSimilarSlicesMut { items, is_eq }
}

/// Iterates slices of items within a slice that share a common property.
pub struct IterSimilarSlicesMut<'a, T, F> {
    items: &'a mut [T],
    is_eq: F,
}

impl<'a, T, F> Iterator for IterSimilarSlicesMut<'a, T, F>
where
    F: FnMut(&T, &T) -> bool,
{
    type Item = &'a mut [T];

    fn next(&mut self) -> Option<Self::Item> {
        if self.items.is_empty() {
            return None;
        }

        let items = std::mem::take(&mut self.items);
        let first = &items[0];

        let mut i = 1;
        while i < items.len() && (self.is_eq)(first, &items[i]) {
            i += 1;
        }

        let (lo, hi) = items.split_at_mut(i);
        self.items = hi;
        Some(lo)
    }
}