I am trying to implement a builder for a Record
type, which as you might guess, represents one of the tuples in a database relation:
#[derive(Eq, Hash, PartialEq, Default)]
pub struct Record<const N: usize>
{
pub utility: SVector<i64, N>,
pub draw: u64,
pub rem: u64,
pub mex: u64,
}
Now, I have seen online that canonically, a builder pattern is implemented by creating a separate RecordBuilder
type.
My question is, what is stopping me from doing something like the following:
impl<const N: usize> Record<N>
{
fn with_utility(mut self, utility: SVector<i64, N>) -> Self
{
/// Stuff, potentially
self.utility = utility;
self
}
...
}
...to avoid the conceptual/cleanliness overhead of having a RecordBuilder
with a build
method?
As a follow up: I am unexperienced with tools like CompilerExplorer -- does calling builder methods in the following way:
let result = BuildableType::default()
.with_field1("data")
...
.with_fieldN("data")
...mean that rustc can inline the sequential calls into something (somewhat) equivalent to the following:
let result = {
BuildableType {
field1: {
/// Stuff
"data"
},
...
fieldN: {
/// Stuff
"data"
},
}
}
...to avoid unnecessary memory jumps?
Thanks!