For example, I'm designing a builder:
#[derive(Default)]
struct BarBuilder {
}
impl BarBuilder {
pub fn add_foo(&mut self) -> &mut Self {
// add foo
self
}
pub fn add_foo(mut self) -> Self { // even though passed in a mut self, trait it as a &mut self
self.add_foo(&mut self)
}
pub fn build(self) -> Bar { // build will consume self
...
}
}
fn main() {
let bar = BarBuilder::default().add_foo().build();
let bar_builder = BarBuilder::default();
bar_builder.add_foo();
let bar = bar_builder.build();
}
If I can check whether the input of add_foo
is a mut self
or a &mut self
and return the correspond type, it is really convenient to write both codes:
let bar = BarBuilder::default().add_foo().build();
and
let bar_builder = BarBuilder::default();
bar_builder.add_foo();
let bar = bar_builder.build();
But Rust does not support function overloading. I tried to use AsRef<Self>
, but it seems that only self
, &self
, &mut self
, self: Box<Self>
, self: Rc<Self>
, self: Arc<Self>
, or self: Pin<P>
are allowed.
So is there any way I can do this?