gpt4 book ai didi

pointers - 如何在 Rust 中安全地构建指向 LV2 原子 DST 的胖指针/指针?

转载 作者:行者123 更新时间:2023-11-29 08:20:15 31 4
gpt4 key购买 nike

我正在为 Rust 集成 LV2 原子,它们是基于切片的动态大小类型 (DST)。一般来说,原子是由主机或其他插件创建的,我的代码只收到一个指向它们的瘦指针。因此,我需要从一个瘦指针创建一个指向基于切片的 DST 的胖指针。这是我的代码大致的样子:

#[repr(C)]
struct Atom {
/// The len of the `data` field.
len: u32,
/// The data.
data: [u8],
}

/// This is the host. It creates the atom in a generally unknown way and calls
/// the plugin's run function. This isn't really part of my code, but it is
/// needed to understand it.
fn host() {
// The raw representation of the atom
let raw_representation: [u8; 8] = [
// len: Raw representation of a `u32`. We have four data bytes.
4, 0, 0, 0,
// The actual data:
1, 2, 3, 4
];

let ptr: *const u8 = raw_representation.as_ptr();

plugin_run(ptr);
}

/// This function represents the plugin's run function:
/// It only knows the pointer to the atom, nothing more.
fn plugin_run(ptr: *const u8) {
// The length of the data.
let len: u32 = *unsafe { (ptr as *const u32).as_ref() }.unwrap();

// The "true" representation of the fat pointer.
let fat_pointer: (*const u8, usize) = (ptr, len as usize);

// transmuting the tuple into the actuall raw pointer.
let atom: *const Atom = unsafe { std::mem::transmute(fat_pointer) };

println!("{:?}", &atom.data);
}

有趣的是 plugin_run 中的倒数第二行:在这里,通过转换包含瘦指针和长度的元组来创建胖指针。这种方法有效,但它使用了高度不安全的 transmute方法。根据文档,这种方法应该是绝对的最后手段,但据我了解 Rust 的设计,我没有做任何不安全的事情。我只是在创建一个指针!

我不想坚持使用这个解决方案。除了使用 transmute 之外,是否有安全的方法来创建指向基于数组的结构的指针?

唯一朝这个方向的是std::slice::from_raw_parts ,但它也是不安全的,直接创建一个引用,而不是指针,并且专为切片工作。我在标准库中什么也没发现,我在 crates.io 上什么也没发现,我什至在 git 仓库中也没有发现任何问题。我在搜索错误的关键字吗? Rust 真的欢迎这样的事情吗?

如果不存在这样的情况,我会在 Rust 的 Github 存储库中为此创建一个问题。

编辑:我不应该谈论 LV2,它使讨论朝着完全不同的方向发展。一切正常,我对数据模型进行了深入思考并进行了测试;我唯一想要的是使用 transmute 创建胖指针的安全替代方法。 :(

最佳答案

假设您想要动态大小的类型

将您的类型更改为允许默认为 [u8] 的未调整参数的通用类型。然后你可以取一个具体的值并得到未调整大小的版本:

#[repr(C)]
struct Atom<D: ?Sized = [u8]> {
/// The len of the `data` field.
len: u32,
/// The data.
data: D,
}

fn main() {
let a1: &Atom = &Atom {
len: 12,
data: [1, 2, 3],
};

let a2: Box<Atom> = Box::new(Atom {
len: 34,
data: [4, 5, 6],
});
}

另见:

假设你想要 LV2 原子

I am working on an integration of LV2 atoms for Rust

您有没有使用 existing crate that deals with this 的原因? ?

LV2 atoms for Rust, which are slice-based dynamically sized types

根据the source code I could find,它们不是.相反,它仅使用结构组成:

typedef struct {
uint32_t size; /**< Size in bytes, not including type and size. */
uint32_t type; /**< Type of this atom (mapped URI). */
} LV2_Atom;

/** An atom:Int or atom:Bool. May be cast to LV2_Atom. */
typedef struct {
LV2_Atom atom; /**< Atom header. */
int32_t body; /**< Integer value. */
} LV2_Atom_Int;

official documentation似乎同意。这意味着按值获取 LV2_Atom 可能是无效的,因为您只会复制标题,而不是主体。这通常称为结构撕裂


在 Rust 中,这可以实现为:

use libc::{int32_t, uint32_t};

#[repr(C)]
struct Atom {
size: uint32_t,
r#type: uint32_t,
}

#[repr(C)]
struct Int {
atom: Atom,
body: int32_t,
}

const INT_TYPE: uint32_t = 42; // Not real

extern "C" {
fn get_some_atom() -> *const Atom;
}

fn get_int() -> Option<*const Int> {
unsafe {
let a = get_some_atom();
if (*a).r#type == INT_TYPE {
Some(a as *const Int)
} else {
None
}
}
}

fn use_int() {
if let Some(i) = get_int() {
let val = unsafe { (*i).body };
println!("{}", val);
}
}

如果您查看 lv2_raw crate 的源代码,你会看到很多相同的东西。还有一个 lv2 crate试图带来更高级别的界面。

关于pointers - 如何在 Rust 中安全地构建指向 LV2 原子 DST 的胖指针/指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55496176/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com