From f699ca24a99c6435e296592f170034d3ba0e199a Mon Sep 17 00:00:00 2001 From: Soispha Date: Sun, 18 Feb 2024 13:30:03 +0100 Subject: [PATCH] fix(trixy-types): Rework c header files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old implementation tried to provide rust types in c—like the result or option types. This implementation now removes this source of complexity by ensuring, that these types are unwrapped on the rust->c boundary. This first commit changes the header files to conform to the new api. --- trixy-types/src/c_headers/errno.h | 31 ++++++++++++---------- trixy-types/src/c_headers/option.h | 12 --------- trixy-types/src/c_headers/result.h | 24 ----------------- trixy-types/src/c_headers/result_dec.h | 14 ---------- trixy-types/src/c_headers/string.h | 16 ++++++----- trixy-types/src/c_headers/type_id.h | 35 ------------------------- trixy-types/src/c_headers/type_id_dec.h | 17 ------------ trixy-types/src/c_headers/vec.h | 33 +++++++++-------------- trixy-types/src/c_headers/vec_dec.h | 18 ------------- 9 files changed, 39 insertions(+), 161 deletions(-) delete mode 100644 trixy-types/src/c_headers/option.h delete mode 100644 trixy-types/src/c_headers/result.h delete mode 100644 trixy-types/src/c_headers/result_dec.h delete mode 100644 trixy-types/src/c_headers/type_id.h delete mode 100644 trixy-types/src/c_headers/type_id_dec.h delete mode 100644 trixy-types/src/c_headers/vec_dec.h diff --git a/trixy-types/src/c_headers/errno.h b/trixy-types/src/c_headers/errno.h index 80cd198..64f8d0d 100644 --- a/trixy-types/src/c_headers/errno.h +++ b/trixy-types/src/c_headers/errno.h @@ -1,21 +1,24 @@ #ifndef TRIXY_ERRNO_H #define TRIXY_ERRNO_H +#include -/// Calculate the number of bytes in the last error's error message **not** -/// including any trailing `null` characters. +/** Calculate the number of bytes in the last error's error message **not** + * including any trailing `null` characters. + */ extern int last_error_length(); -/// Write the most recent error message into a caller-provided buffer as a UTF-8 -/// string, returning the number of bytes written. -/// -/// # Note -/// -/// This writes a **UTF-8** string into the buffer. Windows users may need to -/// convert it to a UTF-16 “Unicode” afterwards. -/// -/// If there are no recent errors then this returns `0` (because we wrote 0 -/// bytes). `-1` is returned if there are any errors, for example when passed a -/// null pointer or a buffer of insufficient size. -extern int last_error_message(char *buffer, int length); +/** Write the most recent error message into a caller-provided buffer as a UTF-8 + * string, returning the number of bytes written. + * + * # Note + * + * This writes a **UTF-8** string into the buffer. Windows users may need to + * convert it to a UTF-16 “Unicode” afterwards. + * + * If there are no recent errors then this returns `0` (because we wrote 0 + * bytes). `-1` is returned if there are any errors, for example when passed a + * null pointer or a buffer of insufficient size. + */ +extern int last_error_message(char *buffer, uint64_t length); #endif // TRIXY_ERRNO_H diff --git a/trixy-types/src/c_headers/option.h b/trixy-types/src/c_headers/option.h deleted file mode 100644 index 50560fa..0000000 --- a/trixy-types/src/c_headers/option.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef TRIXY_OPTION_H -#define TRIXY_OPTION_H -#include "type_id_dec.h" -#include - -typedef struct { - type_id_t type_id; - void *value; - bool some; -} option_t; - -#endif // TRIXY_OPTION_H diff --git a/trixy-types/src/c_headers/result.h b/trixy-types/src/c_headers/result.h deleted file mode 100644 index 918093d..0000000 --- a/trixy-types/src/c_headers/result.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef TRIXY_RESULT_H -#define TRIXY_RESULT_H -#include "result_dec.h" -#include "type_id_dec.h" - -// Function to create an Ok Result variant -result_t ok_result(void *value, type_id_t type_id) { - result_t result; - result.tag = ok; - result.value = value; - result.type_id = type_id; - return result; -} - -// Function to create an Err Result variant -result_t err_result(void *value, type_id_t type_id) { - result_t result; - result.tag = err; - result.value = value; - result.type_id = type_id; - return result; -} - -#endif // TRIXY_RESULT_H diff --git a/trixy-types/src/c_headers/result_dec.h b/trixy-types/src/c_headers/result_dec.h deleted file mode 100644 index b08fdcb..0000000 --- a/trixy-types/src/c_headers/result_dec.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef TRIXY_RESULT_DEC_H -#define TRIXY_RESULT_DEC_H -#include "type_id_dec.h" - -typedef enum { ok, err } result_tag_t; - -typedef struct { - type_id_t type_id; - result_tag_t tag; - /// The type here should be remembered - void *value; -} result_t; - -#endif // TRIXY_RESULT_DEC_H diff --git a/trixy-types/src/c_headers/string.h b/trixy-types/src/c_headers/string.h index 65664f0..e3c5707 100644 --- a/trixy-types/src/c_headers/string.h +++ b/trixy-types/src/c_headers/string.h @@ -1,12 +1,16 @@ #ifndef TRIXY_STRING_H #define TRIXY_STRING_H -/// This is an “owned” string, that means that you have not only a reference to the string -/// but are also required to free it yourself. -typedef char *string_t; +/** + * @brief This string type is allocated by rust, which means that you can't just + * free it from c, but need to return it to rust to be freed. + * @see the `string_free` method + */ +typedef const char *string_t; -/// This type is, in comparison, *not* owned but still owned its source. -/// Thus, it would be undefined behaviour if you free this string slice or mutate it. -typedef const char *str_t; +/** + * @brief The free function for rust stings + */ +extern int string_free(string_t string); #endif // TRIXY_STRING_H diff --git a/trixy-types/src/c_headers/type_id.h b/trixy-types/src/c_headers/type_id.h deleted file mode 100644 index 8d2d207..0000000 --- a/trixy-types/src/c_headers/type_id.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef TRIXY_TYPE_ID_H -#define TRIXY_TYPE_ID_H -#include "option.h" -#include "result_dec.h" -#include "string.h" -#include "type_id_dec.h" -#include "vec_dec.h" -#include -#include - -size_t type_id_size(type_id_t type_id) { - switch (type_id) { - case type_unknown: - fputs("Tried to get size of type with type_id 'unknown'!\n", stderr); - exit(1); - case type_void: - return sizeof(size_t); - case type_str_t: - return sizeof(str_t); - case type_string_t: - return sizeof(string_t); - case type_result_t: - return sizeof(result_t); - case type_vec_t: - return sizeof(vec_t); - case type_option_t: - return sizeof(option_t); - } - fputs("This is unreachable, as all variants of the type_id enum are listed " - "above", - stderr); - exit(1); -}; - -#endif // TRIXY_TYPE_ID_H diff --git a/trixy-types/src/c_headers/type_id_dec.h b/trixy-types/src/c_headers/type_id_dec.h deleted file mode 100644 index 0dbc83c..0000000 --- a/trixy-types/src/c_headers/type_id_dec.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TRIXY_TYPE_ID_DEC_H -#define TRIXY_TYPE_ID_DEC_H - -/// The type of something (option, result, vec, etc.). -typedef enum { - /// We simply don't know which type this is - type_unknown, - - type_void, - type_str_t, - type_string_t, - type_result_t, - type_vec_t, - type_option_t, -} type_id_t; - -#endif // TRIXY_TYPE_ID_DEC_H diff --git a/trixy-types/src/c_headers/vec.h b/trixy-types/src/c_headers/vec.h index b9e3b14..808c856 100644 --- a/trixy-types/src/c_headers/vec.h +++ b/trixy-types/src/c_headers/vec.h @@ -1,28 +1,19 @@ #ifndef TRIXY_VEC_H #define TRIXY_VEC_H -#include "type_id_dec.h" -#include "type_id.h" -#include "vec_dec.h" #include -#include -/// This will abort execution when called with an un-typed vector (that is one -/// of type_id = unknown). -void push_back(vec_t *vec, const void *value) { - if (vec->size >= vec->capacity) { - // If the current size exceeds the capacity, reallocate memory - vec->capacity = - (vec->capacity == 0) ? 1 : vec->capacity * 2; // Double the capacity - vec->data = realloc(vec->data, vec->capacity * type_id_size(vec->type_id)); - } - void *ptr = (size_t *)vec->data + (vec->size / type_id_size(vec->type_id)); - memcpy(ptr, value, type_id_size(vec->type_id)); -} +/** + * @brief A read-only vector from rust. + * + * @detail + * You are must not free it by calling c's `free`. Use `vec_free` + * instead. + */ +struct vec { + void *data; + size_t length; +}; -void free_vector(vec_t *vec) { - free(vec->data); - vec->data = NULL; - vec->size = vec->capacity = 0; -} +extern int vec_free(struct vec vector); #endif // TRIXY_VEC_H diff --git a/trixy-types/src/c_headers/vec_dec.h b/trixy-types/src/c_headers/vec_dec.h deleted file mode 100644 index 6359ea2..0000000 --- a/trixy-types/src/c_headers/vec_dec.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef TRIXY_VEC_DEC_H -#define TRIXY_VEC_DEC_H -#include "type_id_dec.h" -#include - -typedef struct vec { - type_id_t type_id; - void *data; - size_t size; - size_t capacity; -} vec_t; - -void init_vector(vec_t *vec) { - vec->data = NULL; - vec->size = 0; - vec->capacity = 0; -} -#endif // TRIXY_VEC_DEC_H