Re: [PATCH 4/7] xdiff: make fields of xrecord_t Rust friendly

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

 



On Mon, Jul 28, 2025 at 1:52 PM Phillip Wood <phillip.wood123@xxxxxxxxx> wrote:
>
> On 28/07/2025 20:34, Ezekiel Newren wrote:
> > On Fri, Jul 18, 2025 at 7:35 AM Phillip Wood <phillip.wood123@xxxxxxxxx> wrote:
> >> On 17/07/2025 21:32, Ezekiel Newren via GitGitGadget wrote:
> >>> From: Ezekiel Newren <ezekielnewren@xxxxxxxxx>
> >>>
> >>> A few commits ago, we added definitions for Rust primitive types,
> >>> to facilitate interoperability between C and Rust. Switch a
> >>> few variables to use these types. Which, for now, will
> >>> require adding some casts.
> >>
> >> How necessary is it to change char' to 'u8' so long as the rust and C
> >> sides both use a type that is the same size? Also what's the advantage
> >> of using these typedefs rather than the normal C types like unit8_t ?
> >
> > Rust defines char as 32 bits. C treats char as signed 8 bits. What git
> > really means by char* is treat everything like a byte string, and u8
> > is how raw bytes are handled in Rust.
>
> Right - we need to use u8 on the rust side but I'm trying to understand
> why we need to change the type on the C side and why do we need typedefs
> like usize and u32 on the C side when we already have size_t and uint32_t?

Ah, I misunderstood the scope of your question. I could not fit an
example of why this design pattern made sense into this patch series,
so I'll explain with an example here:

If C defines a struct like below then it's obvious how to translate
that into rust for ffi purposes. It also makes it clear that this C
struct is expressly for the purpose of C <-> Rust interoperability.
struct some_struct {
    u8* ptr;
    usize length;
    u64 counter;
};

This is how that C struct needs to be defined in Rust so that it can
interoperate with C, and making C use the Rust types reduces the
chance of copy paste, and primitive type definition mismatch errors.
#[repr(C)]
pub struct some_struct {
    ptr: *mut u8,
    length: usize,
    counter: u64,
};

The Rust function would look like:
#[no_mangle]
unsafe extern "C" fn do_something(data: *mut some_struct) {...}

And C would have a forward declaration like:
extern void do_something(struct some_struct *data);

void some_c_function() {
    struct some_struct x;
    do_something(&x);
}

> Thanks
>
> Phillip





[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