I have a problem I don't understand. I tried to read but really, I cannot make it.
I have a nested struct like
struct A { B : struct { C : struct { amp: a float ! }}}}
and so on...
I'm trying to set the float inside C.
I receive a string like "A B C 43" which tells me to set the float inside C at 43. (of course for other objects, other paths can be chosen)
OK. To me, the idea is
I know that in A, B can be get, so if i have B, i get it
I know that in B, C can be get, so if i have C, i get it
I know that in C, amp can be set
every node are Iterator, every leaf are Scalar.
I was thinking doing something like
pub trait Parameter {
type Param = Iterator<Item = f32>;
fn parse(&mut self, msg: &mut Message) -> Option<Self::Param>;
}
But of course, you cannot return a impl Trait
into a trait
So, I was thinking returning a &mut ref, which made sense, and doing something like
impl<'a, T: Iterator<Item = f32> + 'a> Parameter<'a> for Sin<T, Scalar> {
type Param = &'a mut T;
fn parse(&'a mut self, mut msg: Message) -> Option<Self::Param> {
match msg.param {
"phasor" => Some(&mut self.phasor),
"amp" => {
set_param!(msg, self.amp); return None;
}
_ => None,
}
}
}
Of course, I'm listening to messages, so my main struct is moved into a thread.
Here self is bounded with Parameter trait
std::thread::spawn(move || loop {
while let Ok(msg) = rx_message.try_recv() {
if let Ok(msg) = Message::parse(&msg) {
while let Some(thing) = self.parse(msg) {}
}
}
});
and bim
I have
error[E0499]: cannot borrow `self` as mutable more than once at a time
--> src\back\synth.rs:29:45
|
17 | 'a: 'static,
| -- lifetime `'a` defined here
...
29 | while let Some(thing) = self.parse(&mut msg) {}
| ^^^^----------------
| |
| mutable borrow starts here in previous iteration of loop
| argument requires that `self` is borrowed for `'a`
error[E0597]: `self` does not live long enough
--> src\back\synth.rs:29:45
|
17 | 'a: 'static,
| -- lifetime `'a` defined here
...
29 | while let Some(thing) = self.parse(&mut msg) {}
| ^^^^----------------
| |
| borrowed value does not live long enough
| argument requires that `self` is borrowed for `'a`
...
37 | });
| - `self` dropped here while still borrowed
After that I changed 'a for 'static, since self, being borrowed in a thread is 'static, but this change nothing
I'm really sorry for the length of the post, but reaaaaaally I completely lost......
I understand that the lifetime should express the simple lifetime inside the {} loop in my while let ....
Maybe I could give my function under the trait [called parse] a lifetime ???