struct Device;
struct CommandBuffer {
device: &Device,
// ...
}
// Does not work because Rust does not allow internal pointers
struct Something {
device: Device,
command_buffer: CommandBuffer,
}
一种解决方案是使用Arc
struct CommandBuffer {
device: Arc<Device>,
// ...
}
struct Something {
device: Arc<Device>,
command_buffer: CommandBuffer,
}
的缺点是Arc
是间接寻址,原子计数器以及逃避作用域并保持Device
alive.
现在我不认为你可以解决 Rust 中的一级间接,但是是否有可能在Box
and an Arc
?
struct CommandBuffer {
device: BoxRef<Device>,
// ...
}
struct Something {
device: Box<Device>,
command_buffer: CommandBuffer,
}
我在尝试实施时遇到的主要问题BoxRef
是我需要能够移动Box
即使目前有借用。由于间接级别,这在技术上应该是安全的,但我认为目前不能用 Rust 来表达。
let boxed_device = Box::new(device);
let device_ref = boxed_device.boxed_ref();
// Owner of the reference should be allowed to move
Something{device: boxed_device, CommandBuffer{device: device_ref}}
Could BoxRef
得到实施?我快速浏览了一下拥有_参考 http://kimundi.github.io/owning-ref-rs/owning_ref/index.html但它似乎并没有解决我遇到的问题。
我还有哪些其他选项可以在 Rust 中表达“内部指针”?