Replies: 1 comment 1 reply
-
|
Hi! My main concern with the direction of the solution design for this problem is the level of its complexity. I would really like to avoid complicating use bon::Builder;
trait Auth {
fn get_auth(&self) -> Option<String>;
}
struct NoAuth;
impl Auth for NoAuth {
fn get_auth(&self) -> Option<String> {
None
}
}
struct StaticCreds(String);
impl Auth for StaticCreds {
fn get_auth(&self) -> Option<String> {
Some(self.0.clone())
}
}
#[derive(Builder)]
#[builder(start_fn(name = builder_internal, vis = ""))]
struct Blah<A: Auth> {
#[builder(start_fn)]
auth: A,
}
impl BlahBuilder<NoAuth> {
fn auth<A: Auth>(self, value: A) -> BlahBuilder<A> {
Blah::builder_internal(value)
}
}
impl Blah<NoAuth> {
fn builder() -> BlahBuilder<NoAuth> {
Blah::builder_internal(NoAuth)
}
}
fn main() {
let default = Blah::builder().build();
let overwritten = Blah::builder()
.auth(StaticCreds("secret".to_owned()))
.build();
}Another way to avoid this problem would be to type-erase the generic and use smth like |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've opened an issue on this before (though my idea wasn't a good one): that if the user hasn't set a value for a generic type, the default of the concrete type at build time could be used to fill in the value (avoiding an Option that makes no sense).
e.g. for a scenario like this:
Flipping the idea on its head: I have an idea that the initial (custom) builder could set a default value, and we then use the
experimental_overwritableto replace it later. The fundamental ergonomics (a default concrete value for the generic) are what I want.It seems possible (but is it wise)? that we add the ability to "unset" a value (
Unset<X>is a value where X has been unset). Then when bothexperimental_genericsandexperimental_overwritableare set, callingwith_x()would wrap all fields with generic type parameterXwithUnset. This does break backwards compatibility slightly in the case of this combination, as well as requiring the extra wrapping layer.Afterwards, the builder could return
and then you can use that:
Thoughts on the idea?
Beta Was this translation helpful? Give feedback.
All reactions