Cannot borrow `network2` as mutable more than once at a time

⚓ rust    📅 2025-05-12    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 50 days ago. The information described in this article may have changed.

When the project is small I usually manage to deal with rust, bu I started a big project and there is something I don't understand how to deal with.

My project is a rust implementation of
GitHub - abriotde/openhems-sample: Sample and very basic Home Energy Management System based on Home-Assistant.

My project is GitHub - abriotde/openhems-rust at rust-pb and I get the error:

error[E0499]: cannot borrow `network2` as mutable more than once at a time
  --> src/main.rs:54:6
   |
52 |             match Server::new(& mut network2, &configurator) {
   |                               -------------- first mutable borrow occurs here
53 |                 Ok(mut hems_server) => {
54 |                     network2.set_server(Rc::new(&hems_server));
   |                     ^^^^^^^^                    ------------ first borrow later used here
   |                     |
   |                     second mutable borrow occurs here

The problem occurs at top level because under, we did atomic thinks or we can report it at upper level.

It's a Home-Assistant add-on to Optimize electric consumption. Basically:
The "Server" own a "Network" with own devices "Switch" and "PublicPowerGrid". Each devices own there "Feeder" witch are way to get informations from Home-Assistant (or not if const). Network own too a HomeAssistantAPI witch update the Network.
Server should loop and update network often.
Beside an OffpeakStrategy should choice the best to do on network : when to switch on/off devices.

I am pretty sure I should change some lifetime and maybe re-think somethink in architecture. But what?

3 posts - 2 participants

Read full topic

🏷️ rust_feed