我需要消除本法典中的重复:
pub struct Memory {
layout: MemoryLayout,
rom: Vec<u8>,
ram: Vec<u8>,
}
impl Memory {
pub fn get_mem_vec_ref(&self, address: u32) -> Result<&Vec<u8>, RiscvError> {
// ...
let mem_vec_ref = match address {
addr if (rom_start..rom_end).contains(&addr) => Ok(&self.rom),
addr if (ram_start..ram_end).contains(&addr) => Ok(&self.ram),
addr => Err(RiscvError::MemoryAlignmentError(addr)),
}?;
return Ok(mem_vec_ref);
}
pub fn get_mem_vec_mut_ref(&mut self, address: u32) -> Result<&mut Vec<u8>, RiscvError> {
// ...
let mem_vec_ref = match address {
addr if (rom_start..rom_end).contains(&addr) => Ok(&mut self.rom),
addr if (ram_start..ram_end).contains(&addr) => Ok(&mut self.ram),
addr => Err(RiscvError::MemoryAlignmentError(addr)),
}?;
return Ok(mem_vec_ref);
}
}
How can I abstract using mutable vs immutable reference to self? Can Box
or RefCell
be helpful in this case?