Skip to content

Commit

Permalink
privaTE
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 11, 2024
1 parent 3973d32 commit 984bdd9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
32 changes: 16 additions & 16 deletions crates/libs/metadata/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Blob {
D::decode(self.file, self.read_usize())
}

pub fn try_read(&mut self, expected: usize) -> bool {
pub(crate) fn try_read(&mut self, expected: usize) -> bool {
let (value, offset) = self.peek();
if value == expected {
self.offset(offset);
Expand All @@ -48,7 +48,7 @@ impl Blob {
}
}

pub fn read_modifiers(&mut self) -> Vec<TypeDefOrRef> {
pub(crate) fn read_modifiers(&mut self) -> Vec<TypeDefOrRef> {
let mut mods = vec![];
loop {
let (value, offset) = self.peek();
Expand All @@ -68,14 +68,14 @@ impl Blob {
value
}

pub fn read_str(&mut self) -> &'static str {
pub(crate) fn read_str(&mut self) -> &'static str {
let len = self.read_usize();
let value = unsafe { std::str::from_utf8_unchecked(&self.slice[..len]) };
self.offset(len);
value
}

pub fn read_utf16(self) -> String {
pub(crate) fn read_utf16(self) -> String {
let slice = self.slice;
if slice.as_ptr().align_offset(std::mem::align_of::<u16>()) > 0 {
let slice = slice
Expand All @@ -92,7 +92,7 @@ impl Blob {
}
}

pub fn read_bool(&mut self) -> bool {
pub(crate) fn read_bool(&mut self) -> bool {
// A bool is specified as "a single byte with value 0 (false) or 1 (true)".
match self.read_u8() {
0 => false,
Expand All @@ -101,67 +101,67 @@ impl Blob {
}
}

pub fn read_i8(&mut self) -> i8 {
pub(crate) fn read_i8(&mut self) -> i8 {
let value = i8::from_le_bytes(self[..1].try_into().unwrap());
self.offset(1);
value
}

pub fn read_u8(&mut self) -> u8 {
pub(crate) fn read_u8(&mut self) -> u8 {
let value = u8::from_le_bytes(self[..1].try_into().unwrap());
self.offset(1);
value
}

pub fn read_i16(&mut self) -> i16 {
pub(crate) fn read_i16(&mut self) -> i16 {
let value = i16::from_le_bytes(self[..2].try_into().unwrap());
self.offset(2);
value
}

pub fn read_u16(&mut self) -> u16 {
pub(crate) fn read_u16(&mut self) -> u16 {
let value = u16::from_le_bytes(self[..2].try_into().unwrap());
self.offset(2);
value
}

pub fn read_i32(&mut self) -> i32 {
pub(crate) fn read_i32(&mut self) -> i32 {
let value = i32::from_le_bytes(self[..4].try_into().unwrap());
self.offset(4);
value
}

pub fn read_u32(&mut self) -> u32 {
pub(crate) fn read_u32(&mut self) -> u32 {
let value = u32::from_le_bytes(self[..4].try_into().unwrap());
self.offset(4);
value
}

pub fn read_i64(&mut self) -> i64 {
pub(crate) fn read_i64(&mut self) -> i64 {
let value = i64::from_le_bytes(self[..8].try_into().unwrap());
self.offset(8);
value
}

pub fn read_u64(&mut self) -> u64 {
pub(crate) fn read_u64(&mut self) -> u64 {
let value = u64::from_le_bytes(self[..8].try_into().unwrap());
self.offset(8);
value
}

pub fn read_f32(&mut self) -> f32 {
pub(crate) fn read_f32(&mut self) -> f32 {
let value = f32::from_le_bytes(self[..4].try_into().unwrap());
self.offset(4);
value
}

pub fn read_f64(&mut self) -> f64 {
pub(crate) fn read_f64(&mut self) -> f64 {
let value = f64::from_le_bytes(self[..8].try_into().unwrap());
self.offset(8);
value
}

pub fn read_integer(&mut self, ty: Type) -> Value {
pub(crate) fn read_integer(&mut self, ty: Type) -> Value {
match ty {
Type::I8 => Value::I8(self.read_i8()),
Type::U8 => Value::U8(self.read_u8()),
Expand Down
49 changes: 24 additions & 25 deletions crates/libs/metadata/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,25 +566,29 @@ impl File {
Blob::new(self, &self.bytes[offset..offset + blob_size])
}

pub fn list<R: AsRow>(
pub(crate) fn list<R: AsRow>(
&'static self,
row: usize,
table: usize,
column: usize,
) -> RowIterator<R> {
let first = self.usize(row, table, column) - 1;
let next = row + 1;
let last = if next < self.table_len(table) {
let last = if next < self.tables[table].len {
self.usize(next, table, column) - 1
} else {
self.table_len(table)
self.tables[table].len
};
RowIterator::new(self, first..last)
}

pub fn equal_range<L: AsRow>(&'static self, column: usize, value: usize) -> RowIterator<L> {
pub(crate) fn equal_range<L: AsRow>(
&'static self,
column: usize,
value: usize,
) -> RowIterator<L> {
let mut first = 0;
let mut last = self.table_len(L::TABLE);
let mut last = self.tables[L::TABLE].len;
let mut count = last;

loop {
Expand Down Expand Up @@ -616,7 +620,7 @@ impl File {
RowIterator::new(self, first..last)
}

pub fn lower_bound_of(
fn lower_bound_of(
&self,
table: usize,
mut first: usize,
Expand All @@ -638,7 +642,7 @@ impl File {
first
}

pub fn upper_bound_of(
fn upper_bound_of(
&self,
table: usize,
mut first: usize,
Expand All @@ -660,11 +664,7 @@ impl File {
first
}

pub fn table_len(&self, table: usize) -> usize {
self.tables[table].len
}

pub fn table<R: AsRow>(&'static self) -> RowIterator<R> {
pub(crate) fn table<R: AsRow>(&'static self) -> RowIterator<R> {
RowIterator::new(self, 0..self.tables[R::TABLE].len)
}
}
Expand Down Expand Up @@ -800,26 +800,25 @@ impl Column {
}
}


#[repr(C)]
#[derive(Default)]
struct METADATA_HEADER {
signature: u32,
major_version: u16,
minor_version: u16,
reserved: u32,
length: u32,
version: [u8; 20],
flags: u16,
streams: u16,
struct METADATA_HEADER {
signature: u32,
major_version: u16,
minor_version: u16,
reserved: u32,
length: u32,
version: [u8; 20],
flags: u16,
streams: u16,
}

const METADATA_SIGNATURE: u32 = 0x424A_5342;
const METADATA_SIGNATURE: u32 = 0x424A_5342;

// A coded index (see codes.rs) is a table index that may refer to different tables. The size of the column in memory
// must therefore be large enough to hold an index for a row in the largest possible table. This function determines
// this size for the given winmd file.
fn coded_index_size(tables: &[usize]) -> usize {
fn coded_index_size(tables: &[usize]) -> usize {
fn small(row_count: usize, bits: u8) -> bool {
(row_count as u64) < (1u64 << (16 - bits))
}
Expand All @@ -843,4 +842,4 @@ impl Column {
} else {
4
}
}
}
3 changes: 2 additions & 1 deletion crates/libs/metadata/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub trait HasAttributes {

impl<R: AsRow + Into<HasAttribute>> HasAttributes for R {
fn attributes(&self) -> RowIterator<Attribute> {
self.file().equal_range(0, Into::<HasAttribute>::into(*self).encode())
self.file()
.equal_range(0, Into::<HasAttribute>::into(*self).encode())
}

fn find_attribute(&self, name: &str) -> Option<Attribute> {
Expand Down
12 changes: 8 additions & 4 deletions crates/libs/metadata/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ impl Field {
}

pub fn constant(&self) -> Option<Constant> {
self.file().equal_range(1, HasConstant::Field(*self).encode())
self.file()
.equal_range(1, HasConstant::Field(*self).encode())
.next()
}

Expand Down Expand Up @@ -265,7 +266,8 @@ impl MethodDef {
}

pub fn impl_map(&self) -> Option<ImplMap> {
self.file().equal_range(1, MemberForwarded::MethodDef(*self).encode())
self.file()
.equal_range(1, MemberForwarded::MethodDef(*self).encode())
.next()
}

Expand Down Expand Up @@ -356,15 +358,17 @@ impl TypeDef {
}

pub fn generics(&self) -> RowIterator<GenericParam> {
self.file().equal_range(2, TypeOrMethodDef::TypeDef(*self).encode())
self.file()
.equal_range(2, TypeOrMethodDef::TypeDef(*self).encode())
}

pub fn interface_impls(&self) -> RowIterator<InterfaceImpl> {
self.file().equal_range(0, self.index() + 1)
}

pub fn enclosing_type(&self) -> Option<TypeDef> {
self.file().equal_range::<NestedClass>(0, self.index() + 1)
self.file()
.equal_range::<NestedClass>(0, self.index() + 1)
.next()
.map(|row| TypeDef(row.row(1)))
}
Expand Down

0 comments on commit 984bdd9

Please sign in to comment.