ruxla_pjrt/
compile_options.rs1#[derive(Clone, Debug, Default, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct CompileOptions {}
17
18impl CompileOptions {
19 pub fn new() -> Self {
21 Self::default()
22 }
23
24 pub(crate) fn encode(&self) -> Vec<u8> {
26 let mut build = Vec::new();
28 write_varint_field(&mut build, 1, -1i64 as u64); write_varint_field(&mut build, 4, 1); write_varint_field(&mut build, 5, 1); let mut out = Vec::new();
33 write_bytes_field(&mut out, 3, &build); out
35 }
36}
37
38fn write_varint(out: &mut Vec<u8>, mut value: u64) {
39 loop {
40 let byte = (value & 0x7f) as u8;
41 value >>= 7;
42 if value == 0 {
43 out.push(byte);
44 return;
45 }
46 out.push(byte | 0x80);
47 }
48}
49
50fn write_varint_field(out: &mut Vec<u8>, field: u32, value: u64) {
51 write_varint(out, u64::from(field) << 3);
52 write_varint(out, value);
53}
54
55fn write_bytes_field(out: &mut Vec<u8>, field: u32, bytes: &[u8]) {
56 write_varint(out, (u64::from(field) << 3) | 2);
57 write_varint(out, bytes.len() as u64);
58 out.extend_from_slice(bytes);
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn default_options_match_the_hand_encoded_proto() {
67 let expected = [
69 0x1a, 0x0f, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x20,
70 0x01, 0x28, 0x01,
71 ];
72 assert_eq!(CompileOptions::new().encode(), expected);
73 }
74}