Rust 有没有可能在不使用“弧”的情况下使用“内部指针”?

Rust 有没有可能在不使用“弧”的情况下使用“内部指针”?,rust,Rust,一种解决方案是使用Arc struct Device; struct CommandBuffer { device: &Device, // ... } // Does not work because Rust does not allow internal pointers struct Something { device: Device, command_buffer: CommandBuffer, } 我在尝试实现BoxRef时遇到的主要问

一种解决方案是使用
Arc

struct Device;

struct CommandBuffer {
    device: &Device,
    // ...
}

// Does not work because Rust does not allow internal pointers
struct Something {
    device: Device,
    command_buffer: CommandBuffer,
}
我在尝试实现
BoxRef
时遇到的主要问题是,我需要能够移动
Box
,即使当前有借来的内容。这在技术上应该是安全的,因为间接性的水平,但我不认为这可以用锈蚀来表示

struct CommandBuffer {
    device: BoxRef<Device>,
    // ...
}
struct Something {
    device: Box<Device>,
    command_buffer: CommandBuffer,
}
  • 能否实施
    BoxRef
    ?我快速地看了一眼 但这似乎并不能解决我的问题

  • 我还有什么其他选项可以用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}}
    
    struct设备;
    
    struct CommandBufferI可能是错的,但听起来你在找一个
    RefCell
    也许吧?@SimonWhitehead a
    RefCell
    在有活动借用时也无法移动,或者我误解了你的问题吗?抱歉,我想我误解了你的问题-已经很晚了。不使用
    不安全的
    。。据我所知,使用自定义包装的
    Arc
    和/或
    Rc
    将是唯一的方法。我不明白为什么你认为你应该能够移动有未偿还借款的
    框。不允许的原因是新所有者将控制何时删除
    ,也就是何时释放内存。你能详细说明一下为什么你的情景是合理的吗?
    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}}
    
    struct Device;
    
    struct CommandBuffer<'a> {
        device: &'a Device, // ...
    }
    
    struct Something<'a> {
        device: &'a Device,
        command_buffer: CommandBuffer<'a>,
    }
    
    fn main() {
    
        let dev = Device;
    
        let smth = Something {
            device: &dev,
            command_buffer: CommandBuffer { device: &dev },
        };
    }