I'm new to rust and having trouble implementing traits. Let me know if I'm going about this the wrong way. I'm trying to setup a trait with two functions for accessing a value. The get_value seems to function properly but when trying to setup the set_value with the &mut self reference, I'm getting the following error
warning: function cannot return without recursing
--> src\main.rs:7:5
|
7 | fn set_value(&mut self, new_value: bool) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
8 | (*self).set_value(new_value);
| ---------------------------- recursive call site
|
= note: `#[warn(unconditional_recursion)]` on by default
= help: a `loop` may express intention better if this is on purpose
warning: 1 warning emitted
Example code:
trait Trait1 {
fn set_value(&mut self, new_value: bool);
fn get_value(&self) -> bool;
}
impl<'a, T> Trait1 for &'a T where T: Trait1 {
fn set_value(&mut self, new_value: bool) {
(*self).set_value(new_value);
}
fn get_value(&self) -> bool {
(*self).get_value()
}
}
impl<'a, T> Trait1 for &'a mut T where T: Trait1 {
fn set_value(&mut self, new_value: bool) {
(**self).set_value(new_value)
}
fn get_value(&self) -> bool {
(**self).get_value()
}
}
struct Foo {
value: bool
}
impl Trait1 for Foo {
fn set_value(&mut self, new_value: bool) {
self.value = new_value;
}
fn get_value(&self) -> bool {
self.value
}
}
fn main() {
}
Trait1for& Fooif not necessary. The& Foowould automatically callDereftrait to deref and getFoo. Finally, it get the implementation onFoo. - Forsworn