- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 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],
});
}
另见:
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/
有没有办法用连词创建原子 if ?也就是说,我可以以某种方式在 C 中自动测试 if(A && B) 吗?如果它在第一个连接处短路,那么没问题,但如果没有短路,则在检查 B 时,A 可能已更改。有什么
我有很多 fork 的过程。子进程做很多事情和另一个系统调用。 当任何子进程从系统调用中获取错误时,它会将错误描述打印到 stderr 并将 SIGUSR1 发送到组长(主要父进程)。 SIGUSR1
阅读 boost::atomic 上的文档和 std::atomic 让我感到困惑的是 atomic 是否接口(interface)应该支持非平凡类型? 也就是说,给定一个只能通过将读/写包含在一个完
我有一个命令,可以将叠加图像放在视频上。 之后,我调整输出大小以适合某些尺寸。 通常一切正常,但有时且仅在某台台式计算机上,当第二次精化开始时,命令返回错误:moov atom not found 让
我最近发现当 LANG 设置为 C.utf8 时,X11 原子 WM_NAME 未在 Swing JFrame 中设置。但为 LANG 的其他值设置。这发生在带有 OpenJDK 11.0.9 的 L
我目前正在使用blackmagic的prorecorder录制视频。我使用 ffmpeg 将视频即时转码为 mp4 视频容器。持续时间未知,因为我正在对 prorecorder 输出到命名管道的 .t
这里真的有人使用 atom 来处理 git 提交消息吗?我想但我遇到了这个问题并且一直坚持使用 git commit -m '....' 。当我尝试使用 atom 时,它会打开 atom,我几乎立即从
考虑: void foo() { std::vector> foo(10); ... } foo 的内容现在有效吗?或者我是否需要显式循环并初始化它们?我检查过 Godbolt,看起来不错,但
在official FAQ我阅读的 Memcached: “发送到 memcached 的所有单独命令都是绝对原子的。” 然而,当涉及到 get_multi 和 set_multi 时,我仍然不清楚。
在测试程序的可扩展性时,我遇到了必须将 memcpy 操作设置为原子操作的情况。我必须将 64 字节的数据从一个位置复制到另一个位置。 我遇到了一种解决方案,即使用旋转变量: struct recor
我对 C++ 原子变量感到困惑。如果我有一个原子 x,我想在一个线程中递增并在另一个线程中读取,我可以执行++x 还是必须执行 x.atomic_fetch_add(1)。在读者线程中,我可以做类似
跟进自 Multiple assignment in one line ,我很想知道这对原子数据类型是如何工作的,特别是 bool 类型的例子。 给定: class foo { std::at
我想创建一个版本控制系统,并且对版本号为 1 的新条目的查询如下所示: ID 和修订号组合起来就是主键。 insert into contentfile (id, name, revision, ac
我在 iOS 项目中有下一个独立的测试片段: /// ... std::atomic_bool ab; ab.store(true); bool expected = false; while (!a
我了解如何使用条件变量(此构造的名称很糟糕,IMO,因为 cv 对象既不是变量也不表示条件)。所以我有一对线程,canonically使用 Boost.Thread 设置为: bool awake =
因此,对于最终项目,我尝试制作一款包含三种不同 meteor 的游戏;铜牌、银牌和金牌。虽然青铜阵列在Setup()中工作正常,但银色和金色 meteor 由于某种未知原因而高速移动。 functio
第一个问题,为什么不在 atomic_compare_exchange_weak 操作的参数中应用后缀求值 (++)?运算前后a的值相同。然而,当在 printf() 中使用时,正如预期的那样,该值会
我正在尝试使用 OpenMP 对已经矢量化的代码进行内部函数并行化,但问题是我使用一个 XMM 寄存器作为外部“变量”,我会在每个循环中递增。现在我正在使用 shared 子句 __m128d xmm
clojure“atom”的文档指出 - "Changes to atoms are always free of race conditions." 但是,竞争条件不仅根据更改定义,而且在不同线程中
我一直在研究原子引用计数的实现。 库之间的大多数操作都非常一致,但我在“减少引用计数”操作中发现了惊人的多样性。 (请注意,通常情况下,shared 和 weak decref 之间的唯一区别是调用了
我是一名优秀的程序员,十分优秀!