Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird bug without #[repr(C)] #4

Closed
cxw620 opened this issue May 4, 2024 · 0 comments
Closed

Weird bug without #[repr(C)] #4

cxw620 opened this issue May 4, 2024 · 0 comments

Comments

@cxw620
Copy link

cxw620 commented May 4, 2024

According to offcial example, we should add rust2go::R2G attribute to structs that will be passed to Go side.

#[derive(Debug, rust2go::R2G)]
pub struct Http2SettingFfi {
    pub setting_id: u16,
    pub setting_val: u32,
}

However, when it comes to the example above, there will come a weird bug that Go side will exchange, well I wonder if I explain clearly, values of these two fields.

After thinking for a while, I come to the solution. It's simple: don't forget #[repr(C)], which assures that the order, size, and alignment of fields is exactly what you would expect from C that acting as FFI bridge between Rust and Go.

#[derive(Debug, rust2go::R2G)]
#[repr(C)]
pub struct Http2SettingFfi {
    pub setting_id: u16,
    pub setting_val: u32,
}

BTW, I also want to share my experience here: if you pass a string from Rust to Go, you'd better make a deep copy especially when it acts like config, etc, otherwise if original one is dropped within Rust side, you may encounter with unfarished behaviors.

@cxw620 cxw620 closed this as completed May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant