Skip to main content

msft_typelib/
guidentry.rs

1//! [`GuidEntry`] -- zero-copy view of an `MSFT_GuidEntry` (24 bytes).
2//!
3//! The GUID table (segment 5) is an array of 24-byte entries.  Each entry
4//! stores a 16-byte GUID, an `hreftype` linking it to the TypeInfo that
5//! owns the GUID (or `-2` for the library's own GUID), and a hash-chain
6//! pointer for the GUID hash table (segment 4).
7
8use crate::{Guid, util::read_i32_le};
9
10/// Zero-copy view of an `MSFT_GuidEntry` (24 bytes) in the GUID table.
11///
12/// Each entry stores a 16-byte GUID, an `hreftype` linking it to a
13/// TypeInfo (or `-2` for the typelib's own GUID), and a hash chain pointer.
14#[derive(Clone, Copy, Debug)]
15pub struct GuidEntry<'a> {
16    bytes: &'a [u8],
17}
18
19impl<'a> GuidEntry<'a> {
20    /// Wraps a 24-byte slice as a `GuidEntry`.
21    pub(crate) fn new(bytes: &'a [u8]) -> Self {
22        Self { bytes }
23    }
24
25    /// Size of one `MSFT_GuidEntry` in bytes.
26    pub const SIZE: usize = 24;
27
28    /// The 16-byte GUID value.
29    #[inline]
30    pub fn guid(&self) -> Guid<'a> {
31        Guid::new(self.bytes.get(..16).unwrap_or(&[0; 16]))
32    }
33
34    /// `hreftype` of the associated TypeInfo.
35    ///
36    /// `-2` indicates this is the typelib's own GUID.
37    #[inline]
38    pub fn hreftype(&self) -> i32 {
39        read_i32_le(self.bytes, 16).unwrap_or(-1)
40    }
41
42    /// Offset of the next entry in the hash chain, or `-1` if last.
43    #[inline]
44    pub fn next_hash(&self) -> i32 {
45        read_i32_le(self.bytes, 20).unwrap_or(-1)
46    }
47}