Closed fawkesLi-lhh closed 1 year ago
@TedDriggs Why Clone is required for mut-based builder impl ? Regarding pseudo code in documentation, I see no relationship between chaining and ownership.
Here is my blueprint impl:
#[derive(Default)]
struct Data {
value: usize,
}
#[derive(Default)]
struct DataBuilder {
value: Option<usize>,
}
impl Data {
pub fn builder() -> DataBuilder {
DataBuilder::default()
}
}
impl DataBuilder {
pub fn build(&mut self) -> Data {
let mut built = Data::default(); // Custom or generated constructor may be used to init struct
if let Some(value) = self.value.take() {
built.value = value;
}
built
}
}
Probably because that seems like it should be an owned-pattern build method at that point; it’s weird in my opinion to have the mut build method reset the builder but leave it intact.
Clone
, no ?self
without breaking chaining. Therefore, owned setters correspond with the owned signature for build
.
Most of the time, we only use builder once, but each build() will clone() members, which is inefficient and unnecessary, if we can provide a function that will consume ownership and avoid additional clones