Skip to content

Instantly share code, notes, and snippets.

@lachie
Created January 28, 2022 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lachie/673f3ca6fb63cf2b085dcdeb935ae864 to your computer and use it in GitHub Desktop.
Save lachie/673f3ca6fb63cf2b085dcdeb935ae864 to your computer and use it in GitHub Desktop.
const std = @import("std");
// Proving to myself the struct should be 14 bytes
// #include <stdio.h>
// #include <stdint.h>
// // /** Never change this, only on major IPC breakage (don’t do that) */
// #define I3_IPC_MAGIC "i3-ipc"
// typedef struct i3_ipc_header {
// /* 6 = strlen(I3_IPC_MAGIC) */
// char magic[6];
// uint32_t size;
// uint32_t type;
// } __attribute__((packed)) i3_ipc_header_t;
// int main() {
// printf("believe me it's %lu == 14 => %s\n", sizeof(i3_ipc_header_t), (sizeof(i3_ipc_header_t) == 14) ? "true" : "false");
// }
pub const Magic1 = "i3-ipc";
pub const I3Header1 = packed struct {
_magic: [6]u8 = Magic1,
size: u32 = 0,
type: u32,
};
pub const Magic2 = "i3-ipc"[0..6].*;
pub const I3Header2 = packed struct {
_magic: [6]u8 = Magic2,
size: u32 = 0,
type: u32,
};
// how do I just remove a sentinel? :P
pub const Magic3 = std.mem.sliceAsBytes("i3-ipc")[0..6].*;
comptime {
@compileLog(Magic3); // => [6]u8{105,51,45,105,112,99}
}
pub const I3Header3 = packed struct {
_magic: [6]u8 = Magic3,
size: u32 = 0,
type: u32,
};
// what actually worked (:
pub const I3Header4 = packed struct {
_i: u8 = 'i',
_3: u8 = '3',
@"_-": u8 = '-',
_i2: u8 = 'i',
_p: u8 = 'p',
_c: u8 = 'c',
size: u32 = 0,
type: u32,
};
test "1: size == 14" {
try std.testing.expect(@sizeOf(I3Header1) == 14);
}
test "2: size == 14" {
try std.testing.expect(@sizeOf(I3Header2) == 14);
}
test "3: size == 14" {
try std.testing.expect(@sizeOf(I3Header3) == 14);
}
test "4: size == 14" {
try std.testing.expect(@sizeOf(I3Header4) == 14);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment