/// VTable encapsulates read-only usage of a vtable. It is only to be used /// by generated code. #[derive(Debug)] pubstruct VTable<'a> {
buf: &'a [u8],
loc: usize,
}
impl<'a> VTable<'a> { /// SAFETY /// `buf` must contain a valid vtable at `loc` /// /// This consists of a number of `VOffsetT` /// - size of vtable in bytes including size element /// - size of object in bytes including the vtable offset /// - n fields where n is the number of fields in the table's schema when the code was compiled pubunsafefn init(buf: &'a [u8], loc: usize) -> Self {
VTable { buf, loc }
}
pubfn num_bytes(&self) -> usize { // Safety: // Valid VTable at time of construction unsafe { read_scalar_at::<VOffsetT>(self.buf, self.loc) as usize }
}
pubfn object_inline_num_bytes(&self) -> usize { // Safety: // Valid VTable at time of construction let n = unsafe { read_scalar_at::<VOffsetT>(self.buf, self.loc + SIZE_VOFFSET) };
n as usize
}
pubfn get_field(&self, idx: usize) -> VOffsetT { // TODO(rw): distinguish between None and 0? if idx > self.num_fields() { return0;
}
// Safety: // Valid VTable at time of construction unsafe {
read_scalar_at::<VOffsetT>( self.buf, self.loc + SIZE_VOFFSET + SIZE_VOFFSET + SIZE_VOFFSET * idx,
)
}
}
pubfn get(&self, byte_loc: VOffsetT) -> VOffsetT { // TODO(rw): distinguish between None and 0? if byte_loc as usize + 2 > self.num_bytes() { return0;
} // Safety: // byte_loc is within bounds of vtable, which was valid at time of construction unsafe { read_scalar_at::<VOffsetT>(self.buf, self.loc + byte_loc as usize) }
}
pubfn as_bytes(&self) -> &[u8] { let len = self.num_bytes();
&self.buf[self.loc..self.loc + len]
}
}
#[allow(dead_code)] pubfn field_index_to_field_offset(field_id: VOffsetT) -> VOffsetT { // Should correspond to what end_table() below builds up. let fixed_fields = 2; // Vtable size and Object Size.
((field_id + fixed_fields) * (SIZE_VOFFSET as VOffsetT)) as VOffsetT
}
#[allow(dead_code)] pubfn field_offset_to_field_index(field_o: VOffsetT) -> VOffsetT {
debug_assert!(field_o >= 2); let fixed_fields = 2; // VTable size and Object Size.
(field_o / (SIZE_VOFFSET as VOffsetT)) - fixed_fields
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.