| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/; |
| 2 | |
| 3 | use craterust-analyzer cargo udevrs 0.4.0 crate/::{hwdbrust-analyzer cargo udevrs 0.4.0 hwdb/, Errorrust-analyzer cargo udevrs 0.4.0 error/Error#, Resultrust-analyzer cargo udevrs 0.4.0 error/Result#}; |
| 4 | |
| 5 | use superrust-analyzer cargo udevrs 0.4.0 hwdb/trie/::{TrieChildEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/TrieChildEntry#, TrieNoderust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/TrieNode#, TrieValueEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/value_entry/TrieValueEntry#}; |
| 6 | |
| 7 | /// Represents the full Trie entry in the HWDB. |
| 8 | #[repr(C)] |
| 9 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 10 | pub struct TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry# { |
| 11 | noderust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#node.: TrieNoderust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/TrieNode#, |
| 12 | childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TrieChildEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/TrieChildEntry#>, |
| 13 | valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#values.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TrieValueEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/value_entry/TrieValueEntry#>, |
| 14 | } |
| 15 | |
| 16 | impl TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry# { |
| 17 | /// Creates a new [TrieEntry]. |
| 18 | pub const fn newrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]new().() -> Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry] { |
| 19 | Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry] { |
| 20 | noderust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#node.: TrieNoderust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/TrieNode#::newrust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/impl#[TrieNode]new().(), |
| 21 | childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 22 | valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#values.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Gets a reference to the [TrieNode]. |
| 27 | pub const fn noderust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]node().(&self) -> &TrieNoderust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/TrieNode# { |
| 28 | &self.noderust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#node. |
| 29 | } |
| 30 | |
| 31 | /// Gets a reference to the list of [TrieChildEntry]. |
| 32 | pub fn childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]children().(&self) -> &[TrieChildEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/TrieChildEntry#] { |
| 33 | self.childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children..as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsRef<[T]>`]as_ref().() |
| 34 | } |
| 35 | |
| 36 | /// Gets a reference to the list of [TrieValueEntry]. |
| 37 | pub fn valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]values().(&self) -> &[TrieValueEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/value_entry/TrieValueEntry#] { |
| 38 | self.valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#values..as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsRef<[T]>`]as_ref().() |
| 39 | } |
| 40 | |
| 41 | /// Gets the total length of the [TrieEntry]. |
| 42 | pub fn lenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]len().(&self) -> usize { |
| 43 | let children_len = self.childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().().saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(hwdbrust-analyzer cargo udevrs 0.4.0 hwdb/::child_entry_sizerust-analyzer cargo udevrs 0.4.0 hwdb/child_entry_size().()); |
| 44 | let values_len = self.valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#values..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().().saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(hwdbrust-analyzer cargo udevrs 0.4.0 hwdb/::value_entry_sizerust-analyzer cargo udevrs 0.4.0 hwdb/value_entry_size().()); |
| 45 | |
| 46 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<TrieNoderust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/TrieNode#>() |
| 47 | .saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(children_len) |
| 48 | .saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(values_len) |
| 49 | } |
| 50 | |
| 51 | /// Gets whether the [TrieValueEntry] has no children and values. |
| 52 | pub fn is_emptyrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]is_empty().(&self) -> bool { |
| 53 | self.childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() && self.valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#values..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() |
| 54 | } |
| 55 | |
| 56 | /// Looks up a child node in the HWDB buffer. |
| 57 | /// |
| 58 | /// Parameters: |
| 59 | /// |
| 60 | /// - `hwdb_buf`: in-memory buffer of the entire HWDB. |
| 61 | /// - `c`: Child index to search the list of [TrieChildEntry]. |
| 62 | /// |
| 63 | /// Returns [Some(TrieNode)](TrieNode) on success, [`None`] otherwise. |
| 64 | pub fn lookup_childrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]lookup_child().(&self, hwdb_buf: &[u8], c: u8) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]> { |
| 65 | let search = TrieChildEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/TrieChildEntry#::newrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/impl#[TrieChildEntry]new().().with_crust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/impl#[TrieChildEntry]with_c().(c); |
| 66 | let buf_len = hwdb_buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 67 | |
| 68 | // assuming children are sorted (done in initialisation), perform a binary search instead like C hwdb |
| 69 | let child = self |
| 70 | .childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children. |
| 71 | .binary_search_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search_by().(|child| child.cmprust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/impl#[TrieChildEntry][Ord]cmp().(&search)) |
| 72 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 73 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|idx| self.childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(idx))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 74 | |
| 75 | let child_off = child.child_offrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/impl#[TrieChildEntry]child_off().() as usize; |
| 76 | |
| 77 | // if the child offset is in range, attempt to construct a `TrieNode` at that offset |
| 78 | if (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#buf_len).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`]contains().(&child_off) { |
| 79 | Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]::try_fromrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][`TryFrom<&[u8]>`]try_from().(&hwdb_buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().child_off..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 80 | } else { |
| 81 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry# { |
| 87 | fn defaultrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][Default]default().() -> Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][Default] { |
| 88 | Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][Default]::newrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry]new().() |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<&[u8]> for TrieEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry# { |
| 93 | type Errorrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][`TryFrom<&[u8]>`]Error# = Errorrust-analyzer cargo udevrs 0.4.0 error/Error#; |
| 94 | |
| 95 | fn try_fromrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][`TryFrom<&[u8]>`]try_from().(val: &[u8]) -> Resultrust-analyzer cargo udevrs 0.4.0 error/Result#<Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][`TryFrom<&[u8]>`]> { |
| 96 | let node = TrieNoderust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/TrieNode#::try_fromrust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/impl#[TrieNode][`TryFrom<&[u8]>`]try_from().(val)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 97 | |
| 98 | let mut idx = hwdbrust-analyzer cargo udevrs 0.4.0 hwdb/::node_sizerust-analyzer cargo udevrs 0.4.0 hwdb/node_size().(); |
| 99 | |
| 100 | let val_end = val.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 101 | let child_len = hwdbrust-analyzer cargo udevrs 0.4.0 hwdb/::child_entry_sizerust-analyzer cargo udevrs 0.4.0 hwdb/child_entry_size().(); |
| 102 | let child_count = node.children_countrust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/impl#[TrieNode]children_count().() as usize; |
| 103 | let child_end = idx.saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(child_count.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(child_len)); |
| 104 | |
| 105 | let mut children: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TrieChildEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/child_entry/TrieChildEntry#> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(child_count); |
| 106 | |
| 107 | if (idx..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#val_end).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`]contains().(&child_end) && child_count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 108 | for c in val[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(child_len).takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(child_count) { |
| 109 | children.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(c.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 110 | idx = idx.saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(child_len); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | children.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(); |
| 115 | |
| 116 | let value_len = hwdbrust-analyzer cargo udevrs 0.4.0 hwdb/::value_entry_sizerust-analyzer cargo udevrs 0.4.0 hwdb/value_entry_size().(); |
| 117 | let value_count = node.values_countrust-analyzer cargo udevrs 0.4.0 hwdb/trie/node/impl#[TrieNode]values_count().() as usize; |
| 118 | let value_end = idx.saturating_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_add().(value_count.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(value_len)); |
| 119 | |
| 120 | let mut values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TrieValueEntryrust-analyzer cargo udevrs 0.4.0 hwdb/trie/value_entry/TrieValueEntry#> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(value_count); |
| 121 | |
| 122 | if (idx..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#val_end).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`Range<Idx>`]contains().(&value_end) && value_count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 123 | for c in val[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(value_len).takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(value_count) { |
| 124 | values.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(c.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/impl#[TrieEntry][`TryFrom<&[u8]>`] { |
| 129 | noderust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#node., |
| 130 | childrenrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#children., |
| 131 | valuesrust-analyzer cargo udevrs 0.4.0 hwdb/trie/entry/TrieEntry#values., |
| 132 | }) |
| 133 | } |
| 134 | } |