45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
use smallvec::SmallVec;
|
|
|
|
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Hash,Debug)]
|
|
pub enum LabelOffset {
|
|
LabelStart(u8),
|
|
PacketStart(u16),
|
|
}
|
|
|
|
// the heap meta data is usually at least 2*usize big; assuming 64-bit
|
|
// platforms it should be ok to use 16 bytes in the smallvec.
|
|
#[derive(Clone,PartialEq,Eq,PartialOrd,Ord,Hash,Debug)]
|
|
pub enum LabelOffsets {
|
|
Uncompressed(SmallVec<[u8;16]>),
|
|
Compressed(usize, SmallVec<[LabelOffset;4]>),
|
|
}
|
|
|
|
impl LabelOffsets {
|
|
pub fn len(&self) -> u8 {
|
|
let l = match *self {
|
|
LabelOffsets::Uncompressed(ref offs) => offs.len(),
|
|
LabelOffsets::Compressed(_, ref offs) => offs.len(),
|
|
};
|
|
debug_assert!(l < 128);
|
|
l as u8
|
|
}
|
|
|
|
pub fn label_pos(&self, ndx: u8) -> usize {
|
|
debug_assert!(ndx < 127);
|
|
match *self {
|
|
LabelOffsets::Uncompressed(ref offs) => offs[ndx as usize] as usize,
|
|
LabelOffsets::Compressed(start, ref offs) => match offs[ndx as usize] {
|
|
LabelOffset::LabelStart(o) => start + (o as usize),
|
|
LabelOffset::PacketStart(o) => o as usize,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn is_compressed(&self) -> bool {
|
|
match *self {
|
|
LabelOffsets::Uncompressed(_) => false,
|
|
LabelOffsets::Compressed(_, _) => true,
|
|
}
|
|
}
|
|
}
|