diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-23 19:28:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-23 19:28:08 -0400 |
| commit | fcda36561d668f43bac91ea31cd55cbbd605d330 (patch) | |
| tree | eb74c0b17df584af0fd8154422ad924e04c96cc2 /src/stdlib/metamethods.c | |
| parent | 414b0c7472c87c5a013029aefef49e2dbc41e700 (diff) | |
Autoformat everything with clang-format
Diffstat (limited to 'src/stdlib/metamethods.c')
| -rw-r--r-- | src/stdlib/metamethods.c | 76 |
1 files changed, 32 insertions, 44 deletions
diff --git a/src/stdlib/metamethods.c b/src/stdlib/metamethods.c index 8c755c59..ea06d20b 100644 --- a/src/stdlib/metamethods.c +++ b/src/stdlib/metamethods.c @@ -9,52 +9,44 @@ #include "types.h" #include "util.h" -PUREFUNC public uint64_t generic_hash(const void *obj, const TypeInfo_t *type) -{ - if (type->metamethods.hash) - return type->metamethods.hash(obj, type); +PUREFUNC public uint64_t generic_hash(const void *obj, const TypeInfo_t *type) { + if (type->metamethods.hash) return type->metamethods.hash(obj, type); - return siphash24((void*)obj, (size_t)(type->size)); + return siphash24((void *)obj, (size_t)(type->size)); } -PUREFUNC public int32_t generic_compare(const void *x, const void *y, const TypeInfo_t *type) -{ +PUREFUNC public int32_t generic_compare(const void *x, const void *y, const TypeInfo_t *type) { if (x == y) return 0; - if (type->metamethods.compare) - return type->metamethods.compare(x, y, type); + if (type->metamethods.compare) return type->metamethods.compare(x, y, type); - return (int32_t)memcmp((void*)x, (void*)y, (size_t)(type->size)); + return (int32_t)memcmp((void *)x, (void *)y, (size_t)(type->size)); } -PUREFUNC public bool generic_equal(const void *x, const void *y, const TypeInfo_t *type) -{ +PUREFUNC public bool generic_equal(const void *x, const void *y, const TypeInfo_t *type) { if (x == y) return true; - if (type->metamethods.equal) - return type->metamethods.equal(x, y, type); + if (type->metamethods.equal) return type->metamethods.equal(x, y, type); return (generic_compare(x, y, type) == 0); } -public Text_t generic_as_text(const void *obj, bool colorize, const TypeInfo_t *type) -{ - if (!type->metamethods.as_text) - fail("No text metamethod provided for type!"); +public +Text_t generic_as_text(const void *obj, bool colorize, const TypeInfo_t *type) { + if (!type->metamethods.as_text) fail("No text metamethod provided for type!"); return type->metamethods.as_text(obj, colorize, type); } -public void _serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type) -{ - if (type->metamethods.serialize) - return type->metamethods.serialize(obj, out, pointers, type); +public +void _serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type) { + if (type->metamethods.serialize) return type->metamethods.serialize(obj, out, pointers, type); fwrite(obj, (size_t)type->size, 1, out); } -public List_t generic_serialize(const void *x, const TypeInfo_t *type) -{ +public +List_t generic_serialize(const void *x, const TypeInfo_t *type) { char *buf = NULL; size_t size = 0; FILE *stream = open_memstream(&buf, &size); @@ -62,31 +54,29 @@ public List_t generic_serialize(const void *x, const TypeInfo_t *type) _serialize(x, stream, &pointers, type); fclose(stream); List_t bytes = { - .data=GC_MALLOC_ATOMIC(size), - .length=(int64_t)size, - .stride=1, - .atomic=1, + .data = GC_MALLOC_ATOMIC(size), + .length = (int64_t)size, + .stride = 1, + .atomic = 1, }; memcpy(bytes.data, buf, size); free(buf); return bytes; } -public void _deserialize(FILE *input, void *outval, List_t *pointers, const TypeInfo_t *type) -{ +public +void _deserialize(FILE *input, void *outval, List_t *pointers, const TypeInfo_t *type) { if (type->metamethods.deserialize) { type->metamethods.deserialize(input, outval, pointers, type); return; } - if (fread(outval, (size_t)type->size, 1, input) != 1) - fail("Not enough data in stream to deserialize"); + if (fread(outval, (size_t)type->size, 1, input) != 1) fail("Not enough data in stream to deserialize"); } -public void generic_deserialize(List_t bytes, void *outval, const TypeInfo_t *type) -{ - if (bytes.stride != 1) - List$compact(&bytes, 1); +public +void generic_deserialize(List_t bytes, void *outval, const TypeInfo_t *type) { + if (bytes.stride != 1) List$compact(&bytes, 1); FILE *input = fmemopen(bytes.data, (size_t)bytes.length, "r"); List_t pointers = {}; @@ -94,23 +84,21 @@ public void generic_deserialize(List_t bytes, void *outval, const TypeInfo_t *ty fclose(input); } -public int generic_print(const void *obj, bool colorize, const TypeInfo_t *type) -{ +public +int generic_print(const void *obj, bool colorize, const TypeInfo_t *type) { Text_t text = generic_as_text(obj, colorize, type); return Text$print(stdout, text) + fputc('\n', stdout); } -__attribute__((noreturn)) -public void cannot_serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type) -{ +__attribute__((noreturn)) public +void cannot_serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type) { (void)obj, (void)out, (void)pointers; Text_t typestr = generic_as_text(NULL, false, type); fail("Values of type ", typestr, " cannot be serialized or deserialized!"); } -__attribute__((noreturn)) -public void cannot_deserialize(FILE *in, void *obj, List_t *pointers, const TypeInfo_t *type) -{ +__attribute__((noreturn)) public +void cannot_deserialize(FILE *in, void *obj, List_t *pointers, const TypeInfo_t *type) { (void)obj, (void)in, (void)pointers; Text_t typestr = generic_as_text(NULL, false, type); fail("Values of type ", typestr, " cannot be serialized or deserialized!"); |
