I figure borrows got out of scope after the block their declared in, so I don't understand why I can't borrow as mutable in a block, and then after that block borrow again as immutable?
This is sort of convoluted but I think it will show you what I'm trying to do, build a DSP graph.
type Sample = f32;
type Index = usize;
pub enum SignalInput<'a> {
Single { val: Sample, len: Index },
Vector(&'a [Sample]),
}
pub enum SignalOutput<'a> {
Dummy { len: Index },
Vector(&'a mut [Sample]),
}
struct BlahSignalObject;
impl BlahSignalObject {
fn perform(
&mut self,
_nframes: Index,
_inputs: &[&SignalInput; 2],
_outputs: &mut [&mut SignalOutput; 3],
) {
}
}
struct Foo {
blah0: BlahSignalObject,
blah1: BlahSignalObject,
}
impl Foo {
fn perform(&mut self, nframes: Index, _inputs: &[&[Sample]], _outputs: &mut [&mut [Sample]]) {
let mut internal0: [Sample; 64] = [0.0; 64];
let _in0: SignalInput = SignalInput::Single {
val: 0.0,
len: nframes,
};
let _in1: SignalInput = SignalInput::Single {
val: 0.0,
len: nframes,
};
let in2: SignalInput = SignalInput::Single {
val: 0.0,
len: nframes,
};
let in3: SignalInput = SignalInput::Single {
val: 0.0,
len: nframes,
};
let zero: SignalInput = SignalInput::Single {
val: 0.0,
len: nframes,
};
let mut _out0: SignalOutput = SignalOutput::Dummy { len: nframes };
let mut out1: SignalOutput = SignalOutput::Dummy { len: nframes };
let mut out2: SignalOutput = SignalOutput::Dummy { len: nframes };
{
let mut i0 = SignalOutput::Vector(&mut internal0);
let ins = [&in2, &zero];
let mut outs = [&mut i0, &mut out2, &mut out1];
self.blah0.perform(nframes, &ins, &mut outs);
}
{
let i0 = SignalInput::Vector(&internal0);
let ins = [&i0, &i0];
let mut outs = [
&mut out2,
&mut SignalOutput::Dummy { len: nframes },
&mut out1,
];
self.blah1.perform(nframes, &ins, &mut outs);
}
}
}
Errors:
Compiling playground v0.0.1 (/playground)
warning: unused variable: `in3`
--> src/lib.rs:44:13
|
44 | let in3: SignalInput = SignalInput::Single {
| ^^^ help: if this is intentional, prefix it with an underscore: `_in3`
|
= note: `#[warn(unused_variables)]` on by default
error[E0502]: cannot borrow `internal0` as immutable because it is also borrowed as mutable
--> src/lib.rs:64:42
|
58 | let mut i0 = SignalOutput::Vector(&mut internal0);
| -------------- mutable borrow occurs here
...
64 | let i0 = SignalInput::Vector(&internal0);
| ^^^^^^^^^^ immutable borrow occurs here
...
67 | &mut out2,
| --------- mutable borrow later used here
For more information about this error, try `rustc --explain E0502`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` due to previous error; 1 warning emitted