小编E Y*_*E Y的帖子

将 C++ 程序移植到 Rust:关于 reinterpret_cast、Structs 和蓝牙

我有一个静态链接到libbluetooth/的 C++ 程序,BlueZ我想将它移植到 Rust 作为练习。

C++ 代码中的一个特别难看的地方是通过 读取 UNIX 文件描述符中的数据,read()然后通过将结果缓冲区转换为结构体reinterpret_cast。不幸的是,我不知道如何在 Rust 中实现类似的功能。这个想法是捕获le_advertising_infofrom 的实例libbluetooth

C++11 代码:

std::uint8_t buf [HCI_MAX_EVENT_SIZE];
evt_le_meta_event* evt;
le_advertising_info* info;

if (read(_deviceFD, buf, sizeof (buf)) >= HCI_EVENT_HDR_SIZE) {
    evt = reinterpret_cast<evt_le_meta_event*>(buf + HCI_EVENT_HDR_SIZE + 1);
    if (evt != nullptr && evt->subevent == EVT_LE_ADVERTISING_REPORT) {
        void* offset = evt->data + 1;
        for (auto i = 0; i < evt->data [0]; i++) {
            info = reinterpret_cast<le_advertising_info*>(offset);
            if (info …
Run Code Online (Sandbox Code Playgroud)

struct rust c++11 bluez

4
推荐指数
1
解决办法
1860
查看次数

标签 统计

bluez ×1

c++11 ×1

rust ×1

struct ×1