### as_array Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Gets a reference to the underlying array if the size matches. ```APIDOC ## pub fn as_array(&self) -> Option<&[T; N]> ### Description Gets a reference to the underlying array. If N is not exactly equal to the length of self, then this method returns None. ``` -------------------------------- ### Trim ASCII whitespace Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Examples of trimming leading and trailing ASCII whitespace from byte slices. ```rust assert_eq!(b" \t hello world\n".trim_ascii_start(), b"hello world\n"); assert_eq!(b" ".trim_ascii_start(), b""); assert_eq!(b"".trim_ascii_start(), b""); ``` ```rust assert_eq!(b"\r hello world\n ".trim_ascii_end(), b"\r hello world"); assert_eq!(b" ".trim_ascii_end(), b""); assert_eq!(b"".trim_ascii_end(), b""); ``` ```rust assert_eq!(b"\r hello world\n ".trim_ascii(), b"hello world"); assert_eq!(b" ".trim_ascii(), b""); assert_eq!(b"".trim_ascii(), b""); ``` -------------------------------- ### Check slice prefix Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Determines if a slice starts with a specific needle. ```rust let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(v.starts_with(&v)); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50])); ``` ```rust let v = &[10, 40, 30]; assert!(v.starts_with(&[])); let v: &[u8] = &[]; assert!(v.starts_with(&[])); ``` -------------------------------- ### Get value from HashMap Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Retrieve a reference to a value by its key. ```rust use std::collections::HashMap; let mut map = HashMap::new(); map.insert(1, "a"); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.get(&2), None); ``` -------------------------------- ### Strip Prefix from Slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Removes a specified prefix from the start of a slice, returning the remainder if the prefix matches. ```rust let v = &[10, 40, 30]; assert_eq!(v.strip_prefix(&[10]), Some(&[40, 30][..])); assert_eq!(v.strip_prefix(&[10, 40]), Some(&[30][..])); assert_eq!(v.strip_prefix(&[10, 40, 30]), Some(&[][..])); assert_eq!(v.strip_prefix(&[50]), None); assert_eq!(v.strip_prefix(&[10, 50]), None); let prefix : &str = "he"; assert_eq!(b"hello".strip_prefix(prefix.as_bytes()), Some(b"llo".as_ref())); ``` -------------------------------- ### Get slice element or subslice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns a reference to an element or subslice based on the provided index or range. ```rust let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(Some(&[10, 40][..]), v.get(0..2)); assert_eq!(None, v.get(3)); assert_eq!(None, v.get(0..4)); ``` -------------------------------- ### Get first chunk of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an array reference to the first N items in the slice, or None if the slice is too short. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[10, 40]), u.first_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.first_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.first_chunk::<0>()); ``` -------------------------------- ### Reverse split slice into N parts Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice into at most n subslices starting from the end. ```rust let v = [10, 40, 30, 20, 60, 50]; for group in v.rsplitn(2, |num| *num % 3 == 0) { println!("{group:?}"); } ``` -------------------------------- ### Iterate over slice elements Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html The iter method returns an iterator yielding all items in the slice from start to end. ```rust let x = &[1, 2, 4]; let mut iterator = x.iter(); assert_eq!(iterator.next(), Some(&1)); assert_eq!(iterator.next(), Some(&2)); assert_eq!(iterator.next(), Some(&4)); assert_eq!(iterator.next(), None); ``` -------------------------------- ### Split slice into chunks from the end Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Use as_rchunks to split a slice into fixed-size arrays starting from the end, returning a remainder slice for the beginning. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let (remainder, chunks) = slice.as_rchunks(); assert_eq!(remainder, &['l']); assert_eq!(chunks, &[['o', 'r'], ['e', 'm']]); ``` -------------------------------- ### Iterate over chunks from the end with rchunks Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an iterator over chunks of a slice starting from the end. Panics if chunk_size is zero. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert_eq!(iter.next().unwrap(), &['l']); assert!(iter.next().is_none()); ``` -------------------------------- ### Get last element of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the last element of the slice, or None if empty. ```rust let v = [10, 40, 30]; assert_eq!(Some(&30), v.last()); let w: &[i32] = &[]; assert_eq!(None, w.last()); ``` -------------------------------- ### Calculate element offset with element_offset Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Determines the index of an element reference using pointer arithmetic. Panics if the type is zero-sized and returns None if the reference does not align with an element start. ```rust let nums: &[u32] = &[1, 7, 1, 1]; let num = &nums[2]; assert_eq!(num, &1); assert_eq!(nums.element_offset(num), Some(2)); ``` ```rust let arr: &[[u32; 2]] = &[[0, 1], [2, 3]]; let flat_arr: &[u32] = arr.as_flattened(); let ok_elm: &[u32; 2] = flat_arr[0..2].try_into().unwrap(); let weird_elm: &[u32; 2] = flat_arr[1..3].try_into().unwrap(); assert_eq!(ok_elm, &[0, 1]); assert_eq!(weird_elm, &[1, 2]); assert_eq!(arr.element_offset(ok_elm), Some(0)); // Points to element 0 assert_eq!(arr.element_offset(weird_elm), None); // Points between element 0 and 1 ``` -------------------------------- ### Iterate over exact chunks from the end with rchunks_exact Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an iterator over chunks of exactly chunk_size starting from the end, with a remainder method for leftover elements. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.rchunks_exact(2); assert_eq!(iter.next().unwrap(), &['e', 'm']); assert_eq!(iter.next().unwrap(), &['o', 'r']); assert!(iter.next().is_none()); assert_eq!(iter.remainder(), &['l']); ``` -------------------------------- ### Get raw pointer to slice buffer Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns a raw pointer to the slice's buffer. The caller must ensure the slice outlives the pointer and that the memory is not mutated. ```rust let x = &[1, 2, 4]; let x_ptr = x.as_ptr(); unsafe { for i in 0..x.len() { assert_eq!(x.get_unchecked(i), &*x_ptr.add(i)); } } ``` -------------------------------- ### Get last chunk of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an array reference to the last N items in the slice, or None if the slice is too short. ```rust let u = [10, 40, 30]; assert_eq!(Some(&[40, 30]), u.last_chunk::<2>()); let v: &[i32] = &[10]; assert_eq!(None, v.last_chunk::<2>()); let w: &[i32] = &[]; assert_eq!(Some(&[]), w.last_chunk::<0>()); ``` -------------------------------- ### Get slice element without bounds checking Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns a reference to an element or subslice without bounds checking. Use with caution as it is undefined behavior if out of bounds. ```rust let x = &[1, 2, 4]; unsafe { assert_eq!(x.get_unchecked(1), &2); } ``` -------------------------------- ### Initialize File from path or bytes Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.File.html Methods to create a new File instance from a filesystem path or raw byte data. ```rust pub fn read>(path: P) -> Option ``` ```rust pub fn new(bytes: Vec) -> Option ``` -------------------------------- ### File::new Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.File.html Creates a new File instance from a byte buffer. ```APIDOC ### pub fn new(bytes: Vec) -> Option Creates a new File instance from the provided byte vector. Returns an Option containing the File instance if successful. ``` -------------------------------- ### Add windows-metadata dependency Source: https://docs.rs/windows-metadata/latest/windows_metadata/index.html Include the crate in your Cargo.toml file to begin using the library. ```toml [dependencies.windows-metadata] version = "0.59" ``` -------------------------------- ### Identify ranges of matching elements using partition_point Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Shows how to find the range of indices for duplicate elements and handle cases where an element is not present. ```rust let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let low = s.partition_point(|x| x < &1); assert_eq!(low, 1); let high = s.partition_point(|x| x <= &1); assert_eq!(high, 5); let r = s.binary_search(&1); assert!((low..high).contains(&r.unwrap())); assert!(s[..low].iter().all(|&x| x < 1)); assert!(s[low..high].iter().all(|&x| x == 1)); assert!(s[high..].iter().all(|&x| x > 1)); // For something not found, the "range" of equal items is empty assert_eq!(s.partition_point(|x| x < &11), 9); assert_eq!(s.partition_point(|x| x <= &11), 9); assert_eq!(s.binary_search(&11), Err(9)); ``` -------------------------------- ### windows Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an iterator over all contiguous windows of length size. ```APIDOC ## pub fn windows(&self, size: usize) -> Windows<'_, T> ### Description Returns an iterator over all contiguous windows of length size. The windows overlap. If the slice is shorter than size, the iterator returns no values. ### Panics Panics if size is zero. ``` -------------------------------- ### Query type information with the reader Source: https://docs.rs/windows-metadata/latest/windows_metadata/index.html Use the reader to load a .winmd file and inspect type definitions, fields, and inheritance. ```rust use windows_metadata::*; let index = reader::TypeIndex::read("Windows.winmd").unwrap(); let def = index.expect("Windows.Foundation", "Point"); assert_eq!(def.namespace(), "Windows.Foundation"); assert_eq!(def.name(), "Point"); let extends = def.extends().unwrap(); assert_eq!(extends.namespace(), "System"); assert_eq!(extends.name(), "ValueType"); let fields: Vec<_> = def.fields().collect(); assert_eq!(fields.len(), 2); assert_eq!(fields[0].name(), "X"); assert_eq!(fields[1].name(), "Y"); assert_eq!(fields[0].ty(), Type::F32); assert_eq!(fields[1].ty(), Type::F32); ``` -------------------------------- ### File::read Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.File.html Reads metadata from a specified file path. ```APIDOC ### pub fn read>(path: P) -> Option Reads metadata from the file located at the provided path. Returns an Option containing the File instance if successful. ``` -------------------------------- ### Perform binary search on a sorted slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Demonstrates searching for elements in a sorted array and handling both found and not-found results. ```rust let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; assert_eq!(s.binary_search(&13), Ok(9)); assert_eq!(s.binary_search(&4), Err(7)); assert_eq!(s.binary_search(&100), Err(13)); let r = s.binary_search(&1); assert!(match r { Ok(1..=4) => true, _ => false, }); ``` -------------------------------- ### Perform binary search with a comparator function Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Demonstrates using binary_search_by with a custom closure to search for elements. ```rust let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let seek = 13; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9)); let seek = 4; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7)); let seek = 100; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13)); let seek = 1; let r = s.binary_search_by(|probe| probe.cmp(&seek)); assert!(match r { Ok(1..=4) => true, _ => false, }); ``` -------------------------------- ### Iterate over HashMap keys Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Retrieve and sort keys from a HashMap. ```rust use std::collections::HashMap; let map: HashMap<&str, i32> = HashMap::from([ ("a", 1), ("b", 2), ("c", 3), ]); let mut values: Vec<_> = map.keys().copied().collect(); values.sort(); assert_eq!(values, vec!["a", "b", "c"]); ``` -------------------------------- ### Perform SIMD operations with as_simd Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice into a prefix, a middle slice of aligned SIMD types, and a suffix. This is an experimental API requiring the portable_simd feature. ```rust #![feature(portable_simd)] use core::simd::prelude::*; let short = &[1, 2, 3]; let (prefix, middle, suffix) = short.as_simd::<4>(); assert_eq!(middle, []); // Not enough elements for anything in the middle // They might be split in any possible way between prefix and suffix let it = prefix.iter().chain(suffix).copied(); assert_eq!(it.collect::>(), vec![1, 2, 3]); fn basic_simd_sum(x: &[f32]) -> f32 { use std::ops::Add; let (prefix, middle, suffix) = x.as_simd(); let sums = f32x4::from_array([ prefix.iter().copied().sum(), 0.0, 0.0, suffix.iter().copied().sum(), ]); let sums = middle.iter().copied().fold(sums, f32x4::add); sums.reduce_sum() } let numbers: Vec = (1..101).map(|x| x as _).collect(); assert_eq!(basic_simd_sum(&numbers[1..99]), 4949.0); ``` -------------------------------- ### Define Param struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/writer/struct.Param.html The basic definition of the Param struct with private fields. ```rust pub struct Param(/* private fields */); ``` -------------------------------- ### Check HashMap capacity Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Verify the allocated capacity of a HashMap. ```rust use std::collections::HashMap; let map: HashMap = HashMap::with_capacity(100); assert!(map.capacity() >= 100); ``` -------------------------------- ### Binary search by key in a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Performs a binary search on a sorted slice using a key extraction function. The slice must be sorted by the key for the result to be meaningful. ```rust let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13), (1, 21), (2, 34), (4, 55)]; assert_eq!(s.binary_search_by_key(&13, |&(a, b)| b), Ok(9)); assert_eq!(s.binary_search_by_key(&4, |&(a, b)| b), Err(7)); assert_eq!(s.binary_search_by_key(&100, |&(a, b)| b), Err(13)); let r = s.binary_search_by_key(&1, |&(a, b)| b); assert!(match r { Ok(1..=4) => true, _ => false, }); ``` -------------------------------- ### Insert an element into a sorted vector Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Uses partition_point to find the correct insertion index to maintain sorted order. ```rust let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let num = 42; let idx = s.partition_point(|&x| x <= num); // If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to // `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` will allow `insert` // to shift less elements. s.insert(idx, num); assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); ``` -------------------------------- ### Retrieve key-value pair with get_key_value Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Demonstrates retrieving a value using a borrowed key where equality is based on a subset of the struct fields. ```rust use std::collections::HashMap; use std::hash::{Hash, Hasher}; #[derive(Clone, Copy, Debug)] struct S { id: u32, name: &'static str, // ignored by equality and hashing operations } impl PartialEq for S { fn eq(&self, other: &S) -> bool { self.id == other.id } } impl Eq for S {} impl Hash for S { fn hash(&self, state: &mut H) { self.id.hash(state); } } let j_a = S { id: 1, name: "Jessica" }; let j_b = S { id: 1, name: "Jess" }; let p = S { id: 2, name: "Paul" }; assert_eq!(j_a, j_b); let mut map = HashMap::new(); map.insert(j_a, "Paris"); assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris"))); assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case assert_eq!(map.get_key_value(&p), None); ``` -------------------------------- ### Create overlapping windows Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html The windows method creates an iterator over contiguous, overlapping sub-slices of a specified size. Panics if size is zero. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.windows(3); assert_eq!(iter.next().unwrap(), &['l', 'o', 'r']); assert_eq!(iter.next().unwrap(), &['o', 'r', 'e']); assert_eq!(iter.next().unwrap(), &['r', 'e', 'm']); assert!(iter.next().is_none()); ``` ```rust let slice = ['f', 'o', 'o']; let mut iter = slice.windows(4); assert!(iter.next().is_none()); ``` ```rust use std::cell::Cell; let mut array = ['R', 'u', 's', 't', ' ', '2', '0', '1', '5']; let slice = &mut array[..]; let slice_of_cells: &[Cell] = Cell::from_mut(slice).as_slice_of_cells(); for w in slice_of_cells.windows(3) { Cell::swap(&w[0], &w[2]); } assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']); ``` -------------------------------- ### Iterate over overlapping windows with array_windows Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Creates an iterator over overlapping windows of N elements. Panics if N is zero. ```rust let slice = [0, 1, 2, 3]; let mut iter = slice.array_windows(); assert_eq!(iter.next().unwrap(), &[0, 1]); assert_eq!(iter.next().unwrap(), &[1, 2]); assert_eq!(iter.next().unwrap(), &[2, 3]); assert!(iter.next().is_none()); ``` -------------------------------- ### Split first element of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the first element and the remaining slice, or None if empty. ```rust let x = &[0, 1, 2]; if let Some((first, elements)) = x.split_first() { assert_eq!(first, &0); assert_eq!(elements, &[1, 2]); } ``` -------------------------------- ### Connect slice elements (Deprecated) Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Deprecated alias for the join method. ```rust assert_eq!(["hello", "world"].connect(" "), "hello world"); assert_eq!([[1, 2], [3, 4]].connect(&0), [1, 2, 0, 3, 4]); ``` -------------------------------- ### Check slice length and emptiness Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Methods to determine the size of the slice or check if it is empty. ```rust let a = [1, 2, 3]; assert_eq!(a.len(), 3); ``` ```rust let a = [1, 2, 3]; assert!(!a.is_empty()); let b: &[i32] = &[]; assert!(b.is_empty()); ``` -------------------------------- ### partition_point Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the index of the partition point according to the given predicate. ```APIDOC ## pub fn partition_point

(&self, pred: P) -> usize ### Description Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). The slice is assumed to be partitioned according to the given predicate. ``` -------------------------------- ### Module Struct Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Module.html The definition of the Module struct. ```rust pub struct Module<'a>(/* private fields */); ``` -------------------------------- ### Define File struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.File.html The definition of the File struct within the reader module. ```rust pub struct File { /* private fields */ } ``` -------------------------------- ### Split first chunk of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an array reference to the first N items and the remaining slice, or None if the slice is too short. ```rust let x = &[0, 1, 2]; if let Some((first, elements)) = x.split_first_chunk::<2>() { assert_eq!(first, &[0, 1]); assert_eq!(elements, &[2]); } assert_eq!(None, x.split_first_chunk::<4>()); ``` -------------------------------- ### Check HashMap length Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Verify the number of elements in a HashMap. ```rust use std::collections::HashMap; let mut a = HashMap::new(); assert_eq!(a.len(), 0); a.insert(1, "a"); assert_eq!(a.len(), 1); ``` -------------------------------- ### Iterate over HashMap values Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Retrieve and sort values from a HashMap. ```rust use std::collections::HashMap; let map: HashMap<&str, i32> = HashMap::from([ ("a", 1), ("b", 2), ("c", 3), ]); let mut values: Vec<_> = map.values().copied().collect(); values.sort(); assert_eq!(values, vec![1, 2, 3]); ``` -------------------------------- ### Access first element Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Retrieves the first element of the slice as an Option. ```rust let v = [10, 40, 30]; assert_eq!(Some(&10), v.first()); let w: &[i32] = &[]; assert_eq!(None, w.first()); ``` -------------------------------- ### Find partition point with partition_point Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Locates the index where a slice transitions from satisfying a predicate to failing it. The slice must be partitioned according to the predicate for correct results. ```rust let v = [1, 2, 3, 3, 5, 6, 7]; let i = v.partition_point(|&x| x < 5); assert_eq!(i, 4); assert!(v[..i].iter().all(|&x| x < 5)); assert!(v[i..].iter().all(|&x| !(x < 5))); ``` ```rust let a = [2, 4, 8]; assert_eq!(a.partition_point(|x| x < &100), a.len()); let a: [i32; 0] = []; assert_eq!(a.partition_point(|x| x < &100), 0); ``` ```rust let mut s = vec![0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let num = 42; let idx = s.partition_point(|&x| x <= num); s.insert(idx, num); assert_eq!(s, [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]); ``` -------------------------------- ### Split slice into chunks and remainder Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Use as_chunks to split a slice into fixed-size arrays and a remainder slice. It can be combined with pattern matching to enforce exact length requirements. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let (chunks, remainder) = slice.as_chunks(); assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]); assert_eq!(remainder, &['m']); ``` ```rust let slice = ['R', 'u', 's', 't']; let (chunks, []) = slice.as_chunks::<2>() else { panic!("slice didn't have even length") }; assert_eq!(chunks, &[['R', 'u'], ['s', 't']]); ``` -------------------------------- ### Iterate over HashMap key-value pairs Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Count the number of entries in a HashMap using an iterator. ```rust use std::collections::HashMap; let map = HashMap::from([ ("a", 1), ("b", 2), ("c", 3), ]); let mut count = 0; for (_key, _val) in map.iter() { count += 1; } assert_eq!(count, 3); ``` -------------------------------- ### iter Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an iterator over the slice. ```APIDOC ## pub fn iter(&self) -> Iter<'_, T> ### Description Returns an iterator over the slice. The iterator yields all items from start to end. ``` -------------------------------- ### Check slice order with is_sorted_by_key Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Verifies if a slice is sorted based on keys extracted from elements. ```rust assert!([ ``` ```rust c", "bb", "aaa"].is_sorted_by_key(|s| s.len())); assert!(![-2i32, -1, 0, 3].is_sorted_by_key(|n| n.abs())); ``` -------------------------------- ### Divide a slice with split_at Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice into two at a given index. Panics if the index is out of bounds. ```rust let v = ['a', 'b', 'c']; { let (left, right) = v.split_at(0); assert_eq!(left, []); assert_eq!(right, ['a', 'b', 'c']); } { let (left, right) = v.split_at(2); assert_eq!(left, ['a', 'b']); assert_eq!(right, ['c']); } { let (left, right) = v.split_at(3); assert_eq!(left, ['a', 'b', 'c']); assert_eq!(right, []); } ``` -------------------------------- ### Check if HashMap is empty Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Verify if a HashMap contains no elements. ```rust use std::collections::HashMap; let mut a = HashMap::new(); assert!(a.is_empty()); a.insert(1, "a"); assert!(!a.is_empty()); ``` -------------------------------- ### Signature Struct Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/struct.Signature.html The structure definition for Signature, including its fields for flags, return type, and parameter types. ```rust pub struct Signature { pub flags: MethodCallAttributes, pub return_type: Type, pub types: Vec, } ``` -------------------------------- ### as_ptr_range Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the two raw pointers spanning the slice as a half-open range. ```APIDOC ## pub fn as_ptr_range(&self) -> Range<*const T> ### Description Returns the two raw pointers spanning the slice. The returned range is half-open, which means that the end pointer points one past the last element of the slice. ``` -------------------------------- ### Field Struct Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Field.html The definition of the Field struct as it appears in the windows_metadata::reader module. ```rust pub struct Field<'a>(/* private fields */); ``` -------------------------------- ### Split slice at index with bounds checking Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Divides a slice into two at a given index, returning None if the index is out of bounds. ```rust let v = [1, -2, 3, -4, 5, -6]; { let (left, right) = v.split_at_checked(0).unwrap(); assert_eq!(left, []); assert_eq!(right, [1, -2, 3, -4, 5, -6]); } { let (left, right) = v.split_at_checked(2).unwrap(); assert_eq!(left, [1, -2]); assert_eq!(right, [3, -4, 5, -6]); } { let (left, right) = v.split_at_checked(6).unwrap(); assert_eq!(left, [1, -2, 3, -4, 5, -6]); assert_eq!(right, []); } assert_eq!(None, v.split_at_checked(7)); ``` -------------------------------- ### Blob struct definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html The primary structure used for reading metadata blobs. ```rust pub struct Blob<'a> { /* private fields */ } ``` -------------------------------- ### Iterate over UTF-8 chunks Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Shows how to use utf8_chunks to process valid UTF-8 ranges and invalid byte fragments, here used to format bytes into a C-string literal. ```rust use std::fmt::Write as _; pub fn cstr_literal(bytes: &[u8]) -> String { let mut repr = String::new(); repr.push_str("c\""); for chunk in bytes.utf8_chunks() { for ch in chunk.valid().chars() { // Escapes \0, \t, \r, \n, \\, \', \", and uses \u{...} for non-printable characters. write!(repr, "{}", ch.escape_debug()).unwrap(); } for byte in chunk.invalid() { write!(repr, "\\x{:02X}", byte).unwrap(); } } repr.push('"'); repr } fn main() { let lit = cstr_literal(b"\xferris the \xf0\x9f\xa6\x80\x07"); let expected = stringify!(c"\xFErris the 🦀\u{7}"); assert_eq!(lit, expected); } ``` -------------------------------- ### Split slice once Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice on the first element matching a predicate. Requires the nightly-only slice_split_once feature. ```rust #![feature(slice_split_once)] let s = [1, 2, 3, 2, 4]; assert_eq!(s.split_once(|&x| x == 2), Some(( &[1][..], &[3, 2, 4][..] ))); assert_eq!(s.split_once(|&x| x == 0), None); ``` -------------------------------- ### HasAttributes Implementation Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/trait.HasAttributes.html The implementation block for types that satisfy the AsRow and Into bounds. ```rust impl<'a, R: AsRow<'a> + Into>> HasAttributes<'a> for R ``` -------------------------------- ### Access HashMap hasher Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Retrieve the BuildHasher used by the HashMap. ```rust use std::collections::HashMap; use std::hash::RandomState; let hasher = RandomState::new(); let map: HashMap = HashMap::with_hasher(hasher); let hasher: &RandomState = map.hasher(); ``` -------------------------------- ### element_offset Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the index that an element reference points to within the slice. ```APIDOC ## pub fn element_offset(&self, element: &T) -> Option ### Description Returns the index that an element reference points to. Returns `None` if `element` does not point to the start of an element within the slice. Note that this uses pointer arithmetic and does not compare elements. ``` -------------------------------- ### get_key_value Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ItemIndex.html Returns the key-value pair corresponding to the supplied key, allowing for borrowed key lookups. ```APIDOC ## pub fn get_key_value(&self, k: &Q) -> Option<(&K, &V)> ### Description Returns the key-value pair corresponding to the supplied key. This is useful for key types where non-identical keys are considered equal, or for retrieving the stored key reference. ### Parameters - **k** (&Q) - The key to search for in the map. Must be a borrowed form of the map's key type. ``` -------------------------------- ### Split slice into chunks Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html The chunks method returns an iterator over non-overlapping sub-slices of a fixed size. Panics if chunk_size is zero. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.chunks(2); assert_eq!(iter.next().unwrap(), &['l', 'o']); assert_eq!(iter.next().unwrap(), &['r', 'e']); assert_eq!(iter.next().unwrap(), &['m']); assert!(iter.next().is_none()); ``` -------------------------------- ### Define ClassLayout struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ClassLayout.html The definition of the ClassLayout struct. ```rust pub struct ClassLayout<'a>(/* private fields */); ``` -------------------------------- ### Repeat slice elements Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Creates a new vector by repeating the slice contents a specified number of times. Panics if the resulting capacity overflows. ```rust assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]); ``` ```rust // this will panic at runtime b"0123456789abcdef".repeat(usize::MAX); ``` -------------------------------- ### Convert slice to vector with allocator Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Copies a slice into a new vector using a specific allocator, requiring the allocator_api feature. ```rust #![feature(allocator_api)] use std::alloc::System; let s = [10, 40, 30]; let x = s.to_vec_in(System); // Here, `s` and `x` can be modified independently. ``` -------------------------------- ### AssemblyRef Struct Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.AssemblyRef.html The definition of the AssemblyRef struct. ```rust pub struct AssemblyRef<'a>(/* private fields */); ``` -------------------------------- ### Transmute slice alignment with align_to Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice into a prefix, a middle slice of a new type, and a suffix, ensuring alignment requirements are met. This is an unsafe operation equivalent to transmute. ```rust unsafe { let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7]; let (prefix, shorts, suffix) = bytes.align_to::(); // less_efficient_algorithm_for_bytes(prefix); // more_efficient_algorithm_for_aligned_shorts(shorts); // less_efficient_algorithm_for_bytes(suffix); } ``` -------------------------------- ### Split last element of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the last element and the remaining slice, or None if empty. ```rust let x = &[0, 1, 2]; if let Some((last, elements)) = x.split_last() { assert_eq!(last, &2); assert_eq!(elements, &[0, 1]); } ``` -------------------------------- ### Define TypeName struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/struct.TypeName.html The structure definition for TypeName, containing the namespace, name, and generic type parameters. ```rust pub struct TypeName { pub namespace: String, pub name: String, pub generics: Vec, } ``` -------------------------------- ### Define ImplMap struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.ImplMap.html The primary definition of the ImplMap struct. ```rust pub struct ImplMap<'a>(/* private fields */); ``` -------------------------------- ### Trim Prefix from Slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Removes an optional prefix from a slice. Requires the nightly-only `trim_prefix_suffix` feature. ```rust #![feature(trim_prefix_suffix)] let v = &[10, 40, 30]; // Prefix present - removes it assert_eq!(v.trim_prefix(&[10]), &[40, 30][..]); assert_eq!(v.trim_prefix(&[10, 40]), &[30][..]); assert_eq!(v.trim_prefix(&[10, 40, 30]), &[][..]); // Prefix absent - returns original slice assert_eq!(v.trim_prefix(&[50]), &[10, 40, 30][..]); assert_eq!(v.trim_prefix(&[10, 50]), &[10, 40, 30][..]); let prefix : &str = "he"; assert_eq!(b"hello".trim_prefix(prefix.as_bytes()), b"llo".as_ref()); ``` -------------------------------- ### AsRow Trait Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/trait.AsRow.html The core trait definition for row-based metadata access, requiring implementation of to_row and from_row. ```rust pub trait AsRow<'a>: Copy { const TABLE: usize; Show 13 methods // Required methods fn to_row(&self) -> Row<'a>; fn from_row(row: Row<'a>) -> Self; // Provided methods fn index(&self) -> &'a TypeIndex { ... } fn file(&self) -> &'a File { ... } fn pos(&self) -> usize { ... } fn usize(&self, column: usize) -> usize { ... } fn str(&self, column: usize) -> &'a str { ... } fn row>(&self, column: usize) -> R { ... } fn decode>(&self, column: usize) -> T { ... } fn blob(&self, column: usize) -> Blob<'a> { ... } fn list>(&self, column: usize) -> RowIterator<'a, R> ⓘ { ... } fn equal_range>( &self, column: usize, value: usize, ) -> RowIterator<'a, L> ⓘ { ... } fn parent_row>(&self, column: usize) -> P { ... } } ``` -------------------------------- ### Check pointer range containment Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Use as_ptr_range to verify if a pointer falls within the memory range of a slice. ```rust let a = [1, 2, 3]; let x = &a[1] as *const _; let y = &5 as *const _; assert!(a.as_ptr_range().contains(&x)); assert!(!a.as_ptr_range().contains(&y)); ``` -------------------------------- ### HasAttribute Encode Method Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/enum.HasAttribute.html Encodes the HasAttribute instance into a usize value. ```rust pub fn encode(&self) -> usize ``` -------------------------------- ### Constant struct definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/writer/struct.Constant.html The definition of the Constant struct, which contains private fields. ```rust pub struct Constant(/* private fields */); ``` -------------------------------- ### Join slice elements Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Flattens a slice of items into a single value, inserting a separator between each element. ```rust assert_eq!(["hello", "world"].join(" "), "hello world"); assert_eq!([[1, 2], [3, 4]].join(&0), [1, 2, 0, 3, 4]); assert_eq!([[1, 2], [3, 4]].join(&[0, 0][..]), [1, 2, 0, 0, 3, 4]); ``` -------------------------------- ### chunks Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an iterator over chunk_size elements of the slice at a time. ```APIDOC ## pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> ### Description Returns an iterator over chunk_size elements of the slice at a time, starting at the beginning of the slice. The chunks are slices and do not overlap. ### Panics Panics if chunk_size is zero. ``` -------------------------------- ### AssemblyFlags Struct Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/struct.AssemblyFlags.html The definition of the AssemblyFlags struct. ```rust pub struct AssemblyFlags(/* private fields */); ``` -------------------------------- ### Reverse split slice once Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice on the last element matching a predicate. Requires the nightly-only slice_split_once feature. ```rust #![feature(slice_split_once)] let s = [1, 2, 3, 2, 4]; assert_eq!(s.rsplit_once(|&x| x == 2), Some(( &[1, 2, 3][..], &[4][..] ))); assert_eq!(s.rsplit_once(|&x| x == 0), None); ``` -------------------------------- ### Define the Row struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Row.html The primary structure definition for Row, which holds a reference to a TypeIndex and location metadata. ```rust pub struct Row<'a> { pub index: &'a TypeIndex, pub file: usize, pub pos: usize, } ``` -------------------------------- ### TypeRef Struct Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.TypeRef.html The definition of the TypeRef struct. ```rust pub struct TypeRef<'a>(/* private fields */); ``` -------------------------------- ### Define TypeIndex struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.TypeIndex.html The definition of the TypeIndex struct. ```rust pub struct TypeIndex { /* private fields */ } ``` -------------------------------- ### Define TypeAttributes struct Source: https://docs.rs/windows-metadata/latest/windows_metadata/struct.TypeAttributes.html The basic definition of the TypeAttributes struct. ```rust pub struct TypeAttributes(/* private fields */); ``` -------------------------------- ### Trim Suffix from Slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Removes an optional suffix from a slice. Requires the nightly-only `trim_prefix_suffix` feature. ```rust #![feature(trim_prefix_suffix)] let v = &[10, 40, 30]; // Suffix present - removes it assert_eq!(v.trim_suffix(&[30]), &[10, 40][..]); assert_eq!(v.trim_suffix(&[40, 30]), &[10][..]); assert_eq!(v.trim_suffix(&[10, 40, 30]), &[][..]); // Suffix absent - returns original slice assert_eq!(v.trim_suffix(&[50]), &[10, 40, 30][..]); assert_eq!(v.trim_suffix(&[50, 30]), &[10, 40, 30][..]); ``` -------------------------------- ### Iterate over exact chunks Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Use chunks_exact to iterate over non-overlapping slices of a fixed size, with a remainder available via the iterator. ```rust let slice = ['l', 'o', 'r', 'e', 'm']; let mut iter = slice.chunks_exact(2); assert_eq!(iter.next().unwrap(), &['l', 'o']); assert_eq!(iter.next().unwrap(), &['r', 'e']); assert!(iter.next().is_none()); assert_eq!(iter.remainder(), &['m']); ``` -------------------------------- ### Decode Trait Definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/trait.Decode.html The core trait definition for decoding types from metadata indices. ```rust pub trait Decode<'a> { // Required method fn decode(index: &'a TypeIndex, file: usize, code: usize) -> Self; } ``` -------------------------------- ### Split last chunk of a slice Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns the remaining slice and an array reference to the last N items, or None if the slice is too short. ```rust let x = &[0, 1, 2]; if let Some((elements, last)) = x.split_last_chunk::<2>() { assert_eq!(elements, &[0]); assert_eq!(last, &[1, 2]); } assert_eq!(None, x.split_last_chunk::<4>()); ``` -------------------------------- ### TypeDef struct definition Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.TypeDef.html The primary structure used to represent a type definition in the metadata reader. ```rust pub struct TypeDef<'a>(/* private fields */); ``` -------------------------------- ### is_sorted_by_key Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Checks if the elements of the slice are sorted using a key extraction function. ```APIDOC ## pub fn is_sorted_by_key<'a, F, K>(&'a self, f: F) -> bool ### Description Checks if the elements of this slice are sorted using the given key extraction function. Instead of comparing the slice’s elements directly, this function compares the keys of the elements, as determined by `f`. ``` -------------------------------- ### Escape ASCII bytes Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Returns an iterator that produces an escaped version of the slice, treating it as an ASCII string. ```rust let s = b"0\t\r\n'\"\\\x9d"; let escaped = s.escape_ascii().to_string(); assert_eq!(escaped, "0\\t\\r\\n\\'\\\"\\\\\\x9d"); ``` -------------------------------- ### Split slice into N parts Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Splits a slice into at most n subslices based on a predicate. ```rust let v = [10, 40, 30, 20, 60, 50]; for group in v.splitn(2, |num| *num % 3 == 0) { println!("{group:?}"); } ``` -------------------------------- ### Check slice order with is_sorted_by Source: https://docs.rs/windows-metadata/latest/windows_metadata/reader/struct.Blob.html Verifies if a slice is sorted using a custom comparator function. ```rust assert!([1, 2, 2, 9].is_sorted_by(|a, b| a <= b)); assert!(![1, 2, 2, 9].is_sorted_by(|a, b| a < b)); assert!([0].is_sorted_by(|a, b| true)); assert!([0].is_sorted_by(|a, b| false)); let empty: [i32; 0] = []; assert!(empty.is_sorted_by(|a, b| false)); assert!(empty.is_sorted_by(|a, b| true)); ``` -------------------------------- ### contains Source: https://docs.rs/windows-metadata/latest/windows_metadata/struct.PInvokeAttributes.html Checks if the current PInvokeAttributes instance contains the specified attributes. ```APIDOC ## pub fn contains(&self, contains: Self) -> bool ### Description Checks if the current PInvokeAttributes instance contains the specified attributes. ### Parameters - **contains** (Self) - The attributes to check for. ```