Hi,
I'm new to Rust and I'm trying to write a miniflow data struct used in open vswitch. I'm stuck at this error due to multiple mutable borrows. Any suggestion or things to read are welcome. Thanks!
use std::usize;
struct flowmap {
bits: [u64; FLOWMAP_UNITS],
}
pub const FLOWMAP_UNITS:usize = 2;
pub const MAP_T_BITS:usize = 64;
pub const FLOW_U64S:usize = 84;
impl flowmap {
pub fn new() -> flowmap {
flowmap {
bits: [0; FLOWMAP_UNITS],
}
}
}
struct miniflow {
pub map: flowmap, // a bitmap
pub values: [u64; FLOW_U64S],
}
impl miniflow {
pub fn new() -> miniflow {
miniflow {
map: flowmap::new(),
values: [0; FLOW_U64S],
}
}
}
/* Context for pushing data to a miniflow. */
struct mf_ctx<'a> {
map: flowmap,
data: &'a mut [u64],
//end: &'a mut [u64], // cause multiple mutable borrow: XD
}
impl<'a> mf_ctx<'a> {
pub fn from_mf(map_: flowmap, data_: &'a mut [u64]) -> mf_ctx<'a> {
mf_ctx {
map: map_,
data: data_,
}
}
pub fn miniflow_push_uint64_(&'a mut self, ofs: usize, value: u64) {
self.data[0] = value;
self.data = &mut self.data[1..]; // move self.data to next u64
}
}
fn main() {
println!("Hello, world!");
let mut mf: miniflow = miniflow::new();
let mut mfx = &mut mf_ctx::from_mf(mf.map, &mut mf.values);
let ofs = 0;
mfx.miniflow_push_uint64_(ofs, 1234);
mfx.miniflow_push_uint64_(ofs + 1, 1234);
}
I got error saying below, but I though I'm not creating another mutable borrower? I'm reusing the same one 'mfx', so I don't understand why this is an error...
error[E0499]: cannot borrow `*mfx` as mutable more than once at a time
--> src/main.rs:104:5
|
103 | mfx.miniflow_push_uint64_(ofs, 1234);
| --- first mutable borrow occurs here
104 | mfx.miniflow_push_uint64_(ofs + 1, 1234);
| ^^^
| |
| second mutable borrow occurs here
| first borrow later used here