[PATCH 10/17] compat/rust_types.h: define rust primitive types

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Ezekiel Newren <ezekielnewren@xxxxxxxxx>

Why Rust primitive types should be used in C:
  * Consistency across languages: Sharing the same type names makes it
    easier to translate and refactor code across boundaries, and search
    history.
  * Clarity and ergonomics: The types f32 and f64 are clearer than
    float and double. The types u64 or isize are easier to write than
    uint64_t, or ptrdiff_t.
  * Explicit intent: Inclusion of compat/rust_types.h signals other
    readers that the code is designed, or being cleaned up, for Rust
    interop.
  * Character types: Rust's char is defined as an unsigned 32-bit type.
    In contrast, C's char is an 8-bit type that is neither signed nor
    unsigned. The u8 type should be used instead of C's char when
    referring to bytes in memory.
  * Keep the FFI boundary precise: When Rust calls into C, the C
    interface should use Rust types exclusively in both functions and
    structs. If a broad refactor would cause too much churn, C stub
    functions may be used as an interim step.

Reasons to avoid c_* types (e.g. c_char, c_long) in Rust:
  * Rust remains precise: Bringing c_* into Rust reintroduces the very
    ambiguity Rust was designed to eliminate. Using only Rust
    primitives keeps our code portable and predictable.
  * One clear contract: Rust should define the interface with precise
    types. C adapts through compat/rust_types.h, ensuring the boundary
    is consistent and easy to audit.
  * Future-proof interop: Other runtimes (Python, Go, Java, Wasm, etc.)
    map cleanly onto Rust's primitives, but not onto c_*. Sticking with
    Rust types makes bindings straightforward and avoids locking Git's
    ABI to C's historical quirks.

Signed-off-by: Ezekiel Newren <ezekielnewren@xxxxxxxxx>
---
 compat/rust_types.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 compat/rust_types.h

diff --git a/compat/rust_types.h b/compat/rust_types.h
new file mode 100644
index 0000000000..af93d0a116
--- /dev/null
+++ b/compat/rust_types.h
@@ -0,0 +1,28 @@
+#ifndef COMPAT_RUST_TYPES_H
+#define COMPAT_RUST_TYPES_H
+
+#include <compat/posix.h>
+
+/*
+ * A typedef for bool is not needed because C bool and Rust bool are
+ * the same if #include <stdbool.h> is used.
+ */
+
+typedef uint8_t   u8;
+typedef uint16_t  u16;
+typedef uint32_t  u32;
+typedef uint64_t  u64;
+
+typedef int8_t    i8;
+typedef int16_t   i16;
+typedef int32_t   i32;
+typedef int64_t   i64;
+
+typedef float     f32;
+typedef double    f64;
+
+typedef size_t    usize;
+typedef ptrdiff_t isize;
+typedef uint32_t  rust_char;
+
+#endif /* COMPAT_RUST_TYPES_H */
-- 
gitgitgadget





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux