Repository: plotters-rs/plotters (language: rust)

Issue:
[BUG] minifb example broken
I'm trying to run the minifb example (https://github.com/plotters-rs/plotters/blob/master/examples/minifb-demo/src/main.rs) , but I get this error:
```
error[E0499]: cannot borrow `buf` as mutable more than once at a time
   --> src/main.rs:121:17
    |
73  |         BitMapBackend::<BGRXPixel>::with_buffer_and_format(buf.borrow_mut(), (W as u32, H as u32))?
    |                                                            ---------------- first mutable borrow occurs here
...
121 |                 buf.borrow_mut(),
    |                 ^^^^^^^^^^^^^^^^ second mutable borrow occurs here
...
187 | }
    | - first borrow might be used here, when `root` is dropped and runs the destructor for type `DrawingArea<BitMapBackend<'_, BGRXPixel>, Shift>`

error[E0502]: cannot borrow `buf` as immutable because it is also borrowed as mutable
   --> src/main.rs:175:39
    |
73  |         BitMapBackend::<BGRXPixel>::with_buffer_and_format(buf.borrow_mut(), (W as u32, H as u32))?
    |                                                            ---------------- mutable borrow occurs here
...
175 |             window.update_with_buffer(buf.borrow())?;
    |                                       ^^^^^^^^^^^^ immutable borrow occurs here
...
187 | }
    | - mutable borrow might be used here, when `root` is dropped and runs the destructor for type `DrawingArea<BitMapBackend<'_, BGRXPixel>, Shift>`

Some errors have detailed explanations: E0499, E0502.
For more information about an error, try `rustc --explain E0499`.
```

Now I'm new to rust, but I don't get how this is supposed to work, since it the first mutable of borrow of `buf` wont go out of scope any time in main, so no other function will be able to borrow it, or what am I missing?


Discussion / hints:
Hi @jonsjoberg, did you try running the example from the stable version of plotters?
These are my dependencies:

```
plotters = {version = "0.3", default_features = false, features = ["ttf"]}
plotters-bitmap = {version = "^0.3.*", default-features = false}
minifb = "0.13"

```
(pretty much copied from the Cargo.toml file for that example)

And this is the rust version I'm using
```
active toolchain
----------------

stable-x86_64-unknown-linux-gnu (default)
rustc 1.61.0 (fe5b13d68 2022-05-18)
```

Fix patch:
diff --git a/examples/minifb-demo/Cargo.toml b/examples/minifb-demo/Cargo.toml
index d0c0fcd3..4cac8e70 100644
--- a/examples/minifb-demo/Cargo.toml
+++ b/examples/minifb-demo/Cargo.toml
@@ -7,6 +7,6 @@ edition = "2018"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-minifb = "0.13.0"
+minifb = "0.23.0"
 plotters = { path = "../.." , default_features = false, features = ["ttf"]}
 plotters-bitmap = {version = "^0.3.*", default_features = false}
diff --git a/examples/minifb-demo/src/main.rs b/examples/minifb-demo/src/main.rs
index 795aadd4..ce48c82e 100644
--- a/examples/minifb-demo/src/main.rs
+++ b/examples/minifb-demo/src/main.rs
@@ -6,11 +6,11 @@ use std::collections::VecDeque;
 use std::error::Error;
 use std::time::SystemTime;
 use std::borrow::{Borrow, BorrowMut};
-const W: usize = 480;
-const H: usize = 320;
+const W: usize = 800;
+const H: usize = 600;
 
 const SAMPLE_RATE: f64 = 10_000.0;
-const FREAME_RATE: f64 = 30.0;
+const FRAME_RATE: f64 = 30.0;
 
 struct BufferWrapper(Vec<u32>);
 impl Borrow<[u8]> for BufferWrapper {
@@ -69,24 +69,27 @@ fn main() -> Result<(), Box<dyn Error>> {
         H,
         WindowOptions::default(),
     )?;
-    let root =
-        BitMapBackend::<BGRXPixel>::with_buffer_and_format(buf.borrow_mut(), (W as u32, H as u32))?
-            .into_drawing_area();
-    root.fill(&BLACK)?;
-
-    let mut chart = ChartBuilder::on(&root)
-        .margin(10)
-        .set_all_label_area_size(30)
-        .build_cartesian_2d(-1.2..1.2, -1.2..1.2)?;
-
-    chart
-        .configure_mesh()
-        .label_style(("sans-serif", 15).into_font().color(&GREEN))
-        .axis_style(&GREEN)
-        .draw()?;
-
-    let cs = chart.into_chart_state();
-    root.present()?;
+    let cs = {
+        let root =
+            BitMapBackend::<BGRXPixel>::with_buffer_and_format(buf.borrow_mut(), (W as u32, H as u32))?
+                .into_drawing_area();
+        root.fill(&BLACK)?;
+
+        let mut chart = ChartBuilder::on(&root)
+            .margin(10)
+            .set_all_label_area_size(30)
+            .build_cartesian_2d(-1.2..1.2, -1.2..1.2)?;
+
+        chart
+            .configure_mesh()
+            .label_style(("sans-serif", 15).into_font().color(&GREEN))
+            .axis_style(&GREEN)
+            .draw()?;
+
+        let cs = chart.into_chart_state();
+        root.present()?;
+        cs
+    };
 
     let mut data = VecDeque::new();
     let start_ts = SystemTime::now();
@@ -116,34 +119,35 @@ fn main() -> Result<(), Box<dyn Error>> {
         let phase_y = 2.0 * epoch * std::f64::consts::PI * fy + yphase;
         data.push_back((epoch, phase_x.sin(), phase_y.sin()));
 
-        if epoch - last_flushed > 1.0 / FREAME_RATE {
-            let root = BitMapBackend::<BGRXPixel>::with_buffer_and_format(
-                buf.borrow_mut(),
-                (W as u32, H as u32),
-            )?
-            .into_drawing_area();
-            let mut chart = cs.clone().restore(&root);
-            chart.plotting_area().fill(&BLACK)?;
-
-            chart
-                .configure_mesh()
-                .bold_line_style(&GREEN.mix(0.2))
-                .light_line_style(&TRANSPARENT)
-                .draw()?;
-
-            chart.draw_series(data.iter().zip(data.iter().skip(1)).map(
-                |(&(e, x0, y0), &(_, x1, y1))| {
-                    PathElement::new(
-                        vec![(x0, y0), (x1, y1)],
-                        &GREEN.mix(((e - epoch) * 20.0).exp()),
-                    )
-                },
-            ))?;
-
-            root.present()?;
-            drop(chart);
-
-            if let Some(keys) = window.get_keys_pressed(KeyRepeat::Yes) {
+        if epoch - last_flushed > 1.0 / FRAME_RATE {
+            {
+                let root = BitMapBackend::<BGRXPixel>::with_buffer_and_format(
+                    buf.borrow_mut(),
+                    (W as u32, H as u32),
+                )?
+                .into_drawing_area();
+                {
+                    let mut chart = cs.clone().restore(&root);
+                    chart.plotting_area().fill(&BLACK)?;
+
+                    chart
+                        .configure_mesh()
+                        .bold_line_style(&GREEN.mix(0.2))
+                        .light_line_style(&TRANSPARENT)
+                        .draw()?;
+
+                    chart.draw_series(data.iter().zip(data.iter().skip(1)).map(
+                        |(&(e, x0, y0), &(_, x1, y1))| {
+                            PathElement::new(
+                                vec![(x0, y0), (x1, y1)],
+                                &GREEN.mix(((e - epoch) * 20.0).exp()),
+                            )
+                        },
+                    ))?;
+                }
+                root.present()?;
+
+                let keys = window.get_keys_pressed(KeyRepeat::Yes);
                 for key in keys {
                     let old_fx = fx;
                     let old_fy = fy;
@@ -166,13 +170,11 @@ fn main() -> Result<(), Box<dyn Error>> {
                     }
                     xphase += 2.0 * epoch * std::f64::consts::PI * (old_fx - fx);
                     yphase += 2.0 * epoch * std::f64::consts::PI * (old_fy - fy);
-
                     window.set_title(&get_window_title(fx, fy, yphase - xphase));
-                    break;
                 }
             }
 
-            window.update_with_buffer(buf.borrow())?;
+            window.update_with_buffer(buf.borrow(), W, H)?;
             last_flushed = epoch;
         }
 


Test patch: