Skip to content

Commit

Permalink
serialize to minimal form
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Apr 10, 2024
1 parent 2a4d2a9 commit f912d37
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 142 deletions.
26 changes: 0 additions & 26 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,6 @@ impl Header {
s[1..].copy_from_slice(&payload_size);
s
}

/// Serialize a header into its shortest possible form
pub fn write_minimal<W: std::io::Write>(
self,
mut w: W,
) -> std::io::Result<()> {
let payload_size = self.payload_size;
let b = self.element_type as u8;
if payload_size <= 11 {
w.write_all(&[((payload_size as u8) << 4) | b])
} else if payload_size <= 0xff {
w.write_all(&[0xc0 | b, payload_size as u8])
} else if payload_size <= 0xffff {
let mut header = [0xd0 | b; 3];
header[1..3].copy_from_slice(&(payload_size as u16).to_be_bytes());
w.write_all(&header)
} else if payload_size <= 0xffffff {
let mut header = [0xe0 | b; 5];
header[1..5].copy_from_slice(&(payload_size as u32).to_be_bytes());
w.write_all(&header)
} else {
let mut header = [0xf0 | b; 9];
header[1..9].copy_from_slice(&payload_size.to_be_bytes());
w.write_all(&header)
}
}
}

impl std::convert::From<u8> for ElementType {
Expand Down
Loading

0 comments on commit f912d37

Please sign in to comment.