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

能否讲解一下这个工程是怎么创建的 #2

Open
Ultraman95 opened this issue Jul 30, 2024 · 16 comments
Open

能否讲解一下这个工程是怎么创建的 #2

Ultraman95 opened this issue Jul 30, 2024 · 16 comments

Comments

@Ultraman95
Copy link

No description provided.

@Ultraman95
Copy link
Author

比如bindings.rs是怎么创建的

@kcrazy
Copy link

kcrazy commented Jul 30, 2024

比如bindings.rs是怎么创建的

是通过 libctp-build 这个程序创建的,其实就是将 wrapper.cpp 转换为了 rust代码,保存到了 bindings.rs里

@Ultraman95
Copy link
Author

auto_bind.rs main.rs需要自己去写,对吗?

@Ultraman95
Copy link
Author

还有就是这个工程和使用cxx,autocxx有什么区别

@kcrazy
Copy link

kcrazy commented Jul 31, 2024

auto_bind.rs main.rs需要自己去写,对吗?

嗯,仓库里都有代码的

@kcrazy
Copy link

kcrazy commented Jul 31, 2024

还有就是这个工程和使用cxx,autocxx有什么区别

cxx 只是把原始头文件转换一下,方便 bindgen 转换为 rust 代码

@Ultraman95
Copy link
Author

Ultraman95 commented Aug 1, 2024

问一个问题,bindings.rs中生成的结构体中很多字段都是c风格的字符数组,这些字段如何在输出时被自动检测输出成字符串

@Ultraman95
Copy link
Author

Ultraman95 commented Aug 1, 2024

bindgen生成的代码太底层了,不利于rust写程序,可以结合autocxx,生成更友好的rust接口吗

@kcrazy
Copy link

kcrazy commented Aug 1, 2024

bindgen生成的代码太底层了,不利于rust写程序,可以结合autocxx,生成更友好的rust接口吗

你的意思是用autocxx替代 bindgen?

@kcrazy
Copy link

kcrazy commented Aug 1, 2024

问一个问题,bindings.rs中生成的结构体中很多字段都是c风格的字符数组,这些字段如何在输出时被自动检测输出成字符串

我是这么转换的

fn buf_to_string(input: &[c_char]) -> String {
    let bytes = unsafe {
        std::slice::from_raw_parts(
            input.as_ptr() as *const u8,
            input.iter().take_while(|&c| *c != 0).count(),
        )
    };

    std::str::from_utf8(bytes)
        .map(|str_slice| str_slice.to_owned())
        .unwrap()
}

@Ultraman95
Copy link
Author

问一个问题,bindings.rs中生成的结构体中很多字段都是c风格的字符数组,这些字段如何在输出时被自动检测输出成字符串

我是这么转换的

fn buf_to_string(input: &[c_char]) -> String {
    let bytes = unsafe {
        std::slice::from_raw_parts(
            input.as_ptr() as *const u8,
            input.iter().take_while(|&c| *c != 0).count(),
        )
    };

    std::str::from_utf8(bytes)
        .map(|str_slice| str_slice.to_owned())
        .unwrap()
}

这样的话,我要输出整个结构体,必须对很多字段都要执行这个方法

@Ultraman95
Copy link
Author

bindgen生成的代码太底层了,不利于rust写程序,可以结合autocxx,生成更友好的rust接口吗

你的意思是用autocxx替代 bindgen?

不知道可不可以直接替代,还是先用bindgen生成底层代码,然后对bindings.rs再用autocxx生成更rust友好的代码,具体我也没有研究过

@kcrazy
Copy link

kcrazy commented Aug 2, 2024

bindgen生成的代码太底层了,不利于rust写程序,可以结合autocxx,生成更友好的rust接口吗

你的意思是用autocxx替代 bindgen?

不知道可不可以直接替代,还是先用bindgen生成底层代码,然后对bindings.rs再用autocxx生成更rust友好的代码,具体我也没有研究过

昨天试了一下 autocxx,感觉挺麻烦的,头文件路径还没搞定

@kcrazy
Copy link

kcrazy commented Aug 2, 2024

问一个问题,bindings.rs中生成的结构体中很多字段都是c风格的字符数组,这些字段如何在输出时被自动检测输出成字符串

我是这么转换的

fn buf_to_string(input: &[c_char]) -> String {
    let bytes = unsafe {
        std::slice::from_raw_parts(
            input.as_ptr() as *const u8,
            input.iter().take_while(|&c| *c != 0).count(),
        )
    };

    std::str::from_utf8(bytes)
        .map(|str_slice| str_slice.to_owned())
        .unwrap()
}

这样的话,我要输出整个结构体,必须对很多字段都要执行这个方法

嗯,有的字符串还有中文编码问题,不知道有什么减少内存拷贝的简单方式

@Ultraman95
Copy link
Author

不好意思,autocxx你还在继续吗?

@kcrazy
Copy link

kcrazy commented Aug 23, 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

2 participants