Skip to content

Instantly share code, notes, and snippets.

@ayourtch
Created February 12, 2023 11:25
Show Gist options
  • Save ayourtch/1d6d8d61639dc2c3a73d215bddf3a258 to your computer and use it in GitHub Desktop.
Save ayourtch/1d6d8d61639dc2c3a73d215bddf3a258 to your computer and use it in GitHub Desktop.
use int_enum::IntEnum;
use std::convert::TryFrom;
use serde::{Deserialize, Serialize};
use serde_json::Result;
use bincode::Options;
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, IntEnum)]
enum DT {
Zero = 0x11111111,
Five = 0x22222222,
Blah = 0x33333333,
Boop = 0x44444444,
}
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
enum Data {
Zero = DT::Zero as u32,
Five = DT::Five as u32,
Blah(u32) = DT::Blah as u32,
Boop(u32) = DT::Boop as u32,
}
fn get_encoder() -> impl bincode::config::Options {
bincode::DefaultOptions::new()
.with_little_endian()
.with_fixint_encoding()
}
fn main() {
let v = vec![ Data::Five, Data::Zero, Data::Five, Data::Blah(0xeeeeeeee) ];
//let v = vec![ DT::Five, DT::Zero, DT::Five, DT::Blah ];
let enc = get_encoder();
let b1 = enc.serialize(&v).unwrap();
println!("b1 {:x?}", b1);
}
/*****
*
(gdb) b main
Breakpoint 1 at 0xada0
(gdb) r
Starting program: /home/ayourtch/rust/aytest/int_enum/target/debug/int_enum_test
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, 0x000055555555eda0 in main ()
(gdb) l
42 }
(gdb) disass
Dump of assembler code for function main:
=> 0x000055555555eda0 <+0>: push %rax
0x000055555555eda1 <+1>: mov %rsi,%rdx
0x000055555555eda4 <+4>: lea 0x4277b(%rip),%rax # 0x5555555a1526 <__rustc_debug_gdb_scripts_section__>
0x000055555555edab <+11>: mov (%rax),%al
0x000055555555edad <+13>: movslq %edi,%rsi
0x000055555555edb0 <+16>: lea -0x1f7(%rip),%rdi # 0x55555555ebc0 <int_enum_test::main>
0x000055555555edb7 <+23>: xor %ecx,%ecx
0x000055555555edb9 <+25>: callq 0x55555555f770 <std::rt::lang_start>
0x000055555555edbe <+30>: pop %rcx
0x000055555555edbf <+31>: retq
End of assembler dump.
(gdb) b int_enum_test::main
Breakpoint 2 at 0x55555555ebd6: file src/main.rs, line 36.
(gdb) c
Continuing.
Breakpoint 2, int_enum_test::main () at src/main.rs:36
36 let v = vec![ Data::Five, Data::Zero, Data::Five, Data::Blah(0xeeeeeeee) ];
(gdb) n
38 let enc = get_encoder();
(gdb) p v
$1 = alloc::vec::Vec<int_enum_test::Data, alloc::alloc::Global> {buf: alloc::raw_vec::RawVec<int_enum_test::Data, alloc::alloc::Global> {ptr: core::ptr::unique::Unique<int_enum_test::Data> {pointer: core::ptr::non_null::NonNull<int_enum_test::Data> {pointer: 0x5555555b1a10}, _marker: core::marker::PhantomData<int_enum_test::Data>}, cap: 4, alloc: alloc::alloc::Global}, len: 4}
(gdb) x/32xh v.buf.ptr.pointer.pointer
0x5555555b1a10: 0x2222 0x2222 0x5555 0x0000 0x1111 0x1111 0x7fff 0x0000
0x5555555b1a20: 0x2222 0x2222 0x5555 0x0000 0x3333 0x3333 0xeeee 0xeeee
0x5555555b1a30: 0x0000 0x0000 0x0000 0x0000 0x0021 0x0000 0x0000 0x0000
0x5555555b1a40: 0x616d 0x6e69 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
(gdb) c
Continuing.
b1 [4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, ee, ee, ee, ee]
[Inferior 1 (process 924313) exited normally]
(gdb)
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment