我有这个Emulator课程。
pub struct Emulator {
cart: Rc<RefCell<Cartridge>>,
bus: Rc<RefCell<Bus>>, /* requirs access to cartridge, dma, ppu, and joypad */
cpu: Rc<RefCell<M6502>>, /* requires access to bus */
ppu: Rc<RefCell<PPU>>, /* requires access to cartridge */
dma: Rc<RefCell<DMA>>, /* requires access to cpu, ppu, and bus */
joypad: Rc<RefCell<Joypad>>
}您可以看到,它的组件相互依赖。我决定使用Rc和Weak来维护这种循环关系。
我很难弄清楚如何创建这个结构的新实例。
impl Emulator {
pub fn new(fname: &str) -> Self {
let cart_ref = Rc::new(RefCell::new(Cartridge::new(fname)));
let weak_cart = Rc::downgrade(&cart_ref);
let ppu_ref = Rc::new(RefCell::new(PPU::new(weak_cart.clone())));
let weak_ppu = Rc::downgrade(&ppu_ref);
let joypad_ref = Rc::new(RefCell::new(Joypad::new()));
let weak_joypad = Rc::downgrade(&joypad_ref);
let mut bus_ref: Rc<RefCell<Bus>>;
let mut cpu_ref: Rc<RefCell<M6502>>;
let dma_ref = Rc::new_cyclic(|weak_dma| {
bus_ref = Rc::new(RefCell::new(Bus::new(
weak_cart.clone(),
weak_ppu.clone(),
weak_joypad.clone(),
weak_dma.clone()
)));
let weak_bus = Rc::downgrade(&bus_ref);
cpu_ref = Rc::new(RefCell::new(M6502::new(weak_bus.clone())));
let weak_cpu = Rc::downgrade(&cpu_ref);
RefCell::new(DMA::new(weak_cpu.clone(), weak_ppu.clone(), weak_bus.clone()))
});
Emulator {
cart: cart_ref,
bus: bus_ref,
cpu: cpu_ref,
ppu: ppu_ref,
dma: dma_ref,
joypad: joypad_ref
}
}
...
}我犯了这个错误
error[E0381]: borrow of possibly-uninitialized variable: `bus_ref`
--> src\emulator.rs:38:38
|
38 | let dma_ref = Rc::new_cyclic(|weak_dma| {
| ^^^^^^^^^^ use of possibly-uninitialized `bus_ref`
39 | bus_ref = Rc::new(RefCell::new(Bus::new(
| ------- borrow occurs due to use in closure
error[E0381]: borrow of possibly-uninitialized variable: `cpu_ref`
--> src\emulator.rs:38:38
|
38 | let dma_ref = Rc::new_cyclic(|weak_dma| {
| ^^^^^^^^^^ use of possibly-uninitialized `cpu_ref`
...
47 | cpu_ref = Rc::new(RefCell::new(M6502::new(weak_bus.clone())));
| ------- borrow occurs due to use in closure我不知道怎么修好它。我想不出其他的组合。我想知道我是否应该重组我的代码。
发布于 2022-07-19 18:23:35
您不能在闭包中借用未初始化的变量,因为当它被删除时,编译器必须知道它是否已初始化,但它无法跟踪闭包内的变量(不是完全不能,但无论如何,这是Rust不支持的)。见第41124期。
我处理这个问题的方法是在这里和那里洒一些Options和.unwrap()s:
let mut bus_ref: Option<Rc<RefCell<Bus>>> = None;
let mut cpu_ref: Option<Rc<RefCell<M6502>>> = None;
let dma_ref = Rc::new_cyclic(|weak_dma| {
let bus_ref_ = Rc::new(RefCell::new(Bus::new(
weak_cart.clone(),
weak_ppu.clone(),
weak_joypad.clone(),
weak_dma.clone(),
)));
let weak_bus = Rc::downgrade(&bus_ref_);
let cpu_ref_ = Rc::new(RefCell::new(M6502::new(weak_bus.clone())));
let weak_cpu = Rc::downgrade(&cpu_ref_);
bus_ref = Some(bus_ref_);
cpu_ref = Some(cpu_ref_);
RefCell::new(DMA::new(
weak_cpu.clone(),
weak_ppu.clone(),
weak_bus.clone(),
))
});
Emulator {
cart: cart_ref,
bus: bus_ref.unwrap(),
cpu: cpu_ref.unwrap(),
ppu: ppu_ref,
dma: dma_ref,
joypad: joypad_ref,
}不完美,但有效。
https://stackoverflow.com/questions/73041372
复制相似问题