enum Operation {
//Open((usize, &'static str)),
Close,
Write([u8; 3]),
//Read,
//FileExists(&'static str),
}
pub fn main() {
let mut operation = Operation::Close; // size of Vec<u8,1024>(largest type) + overhead gets allocated
// Filehandler will do something with the operation
let vec: [u8; 3] = [1, 2, 3];
//vec.push(5); // Now sizeof(operation) and sizeof(vec) is allocated
operation = Operation::Write(vec);
// Filehandler will do something with the operation
let s: &'static str = "Teststring";
//string.push_str(s); // Now sizeof(operation) and sizeof(string) is allocated.
//operation = Operation::FileExists(s);
// Filehandler will do something with the operation
}
Assembly:
example::main::h02a42a1dcab42298:
mov byte ptr [rsp - 11], 0 // 4 Bytes (3Bytes Array + 1 Byte the enum value?)
mov byte ptr [rsp - 7], 1 // vec[0]
mov byte ptr [rsp - 6], 2 // vec[1]
mov byte ptr [rsp - 5], 3 // vec[2]
mov ax, word ptr [rsp - 7]
mov word ptr [rsp - 3], ax
mov al, byte ptr [rsp - 5]
mov byte ptr [rsp - 1], al
mov byte ptr [rsp - 4], 1
mov eax, dword ptr [rsp - 4]
mov dword ptr [rsp - 11], eax
ret
When I enable the FileExists value in the enum and in the main I get:
example::main::h02a42a1dcab42298:
mov byte ptr [rsp - 80], 0 // 29 Bytes? Why
mov byte ptr [rsp - 51], 1
Why the size of the enum is now that large? I am expecting that the size increases to maybe 4 or 8 Bytes to store a reference + 1Byte the enum value.
Using your proposal:
enum Operation {
//Open((usize, &'static str)),
Close,
Write([u8; 3]),
//Read,
//FileExists(&'static str),
}
pub fn main() {
let mut operation = Operation::Close; // size of Vec<u8,1024>(largest type) + overhead gets allocated
// Filehandler will do something with the operation
//let vec: [u8; 3] = [1, 2, 3];
//vec.push(5); // Now sizeof(operation) and sizeof(vec) is allocated
operation = Operation::Write([0, 0, 0]);
let Operation::Write(vec) = &mut operation else {unreachable!()};
vec[0] = 1;
vec[1] = 2;
vec[2] = 3;
// Filehandler will do something with the operation
let s: &'static str = "Teststring";
//string.push_str(s); // Now sizeof(operation) and sizeof(string) is allocated.
//operation = Operation::FileExists(s);
// Filehandler will do something with the operation
}
I get
example::main::h02a42a1dcab42298:
sub rsp, 24
mov byte ptr [rsp + 13], 0
mov byte ptr [rsp + 21], 0
mov byte ptr [rsp + 22], 0
mov byte ptr [rsp + 23], 0
mov ax, word ptr [rsp + 21]
mov word ptr [rsp + 18], ax
mov al, byte ptr [rsp + 23]
mov byte ptr [rsp + 20], al
mov byte ptr [rsp + 17], 1
mov eax, dword ptr [rsp + 17]
mov dword ptr [rsp + 13], eax
mov al, byte ptr [rsp + 13]
and al, 1
movzx eax, al
cmp rax, 1
jne .LBB0_2
mov byte ptr [rsp + 14], 1
mov byte ptr [rsp + 15], 2
mov byte ptr [rsp + 16], 3
add rsp, 24
ret
.LBB0_2:
lea rdi, [rip + .L__unnamed_1]
lea rdx, [rip + .L__unnamed_2]
mov rax, qword ptr [rip + core::panicking::panic::h59297120e85ea178@GOTPCREL]
mov esi, 40
call rax
.L__unnamed_1:
.ascii "internal error: entered unreachable code"
.L__unnamed_3:
.ascii "/app/example.rs"
.L__unnamed_2:
.quad .L__unnamed_3
.asciz "\017\000\000\000\000\000\000\000\021\000\000\0006\000\000"