Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TryFrom<Builder> for Target for all builders exposing a public build method #76

Open
TedDriggs opened this issue Apr 12, 2017 · 6 comments

Comments

@TedDriggs
Copy link
Collaborator

TedDriggs commented Apr 12, 2017

With #72 (fallible setters) merged and rust-lang/rust#33417 (TryFrom) approaching stabilization, having a TryFrom<Builder> for Target implementation for all builders with a generated build method will enable the following code:

let request = RequestBuilder::default()
    .from(-18000000)
    .sources(vec![Source::device_group(Oid::new(2))])
    .metric(Metric::builder()
        .stat_name("extrahop.device.ssl_server_detail")
        .field_name("cert_expiration"))?
    .total(true)
    .build()
    .expect("All inputs hardcoded");

Once TryFrom is stabilized I'm not planning to expose a struct-level attribute to control this. This seems too standard for a crate user not to want. #70 gives authors control over exposing the build method; this would look to that attribute and skip implementation if the build method was skipped.

I'm planning to take a stab at a PR for this.

@colin-kiegel
Copy link
Owner

Makes sense 👍

I generally agree that we should always emit that implementation. There is only one reason to make it opt-into and that is backwards compatibility with Rust 1.15-x. I don't want to drop backwards compatibility on every new cool feature. For some intermediate time I would like to make it opt-in (either feature flag or attribute). And after some time we can raise the minimum Rust version and get rid of opting-in. I guess a feature flag would be a little easier to implement? With this we can already implement it for nightly. :-)

Minor corrections in your code example

  • You meant RequestEnvelope::try_from
  • the build().expext("..") calls should not be there
  • Metric::builder() would currently still be MetricBuilder::default()

@TedDriggs
Copy link
Collaborator Author

I went with feature flag.

@colin-kiegel
Copy link
Owner

Ah .. I see .. I thought the outer RequestEnvelope::from(RequestBuilder should've also been a demonstration of the usefulness of the automatic TryFrom implementation.

Ok, that was fast. I'll review later. Have to got now. :-)

@TedDriggs
Copy link
Collaborator Author

TryFrom is now supported below this crate's MSRV, so this should be ready to implement.

@TedDriggs TedDriggs changed the title Implement TryFrom<Builder> for Target for all builders exposing a build method Implement TryFrom<Builder> for Target for all builders exposing a public build method Feb 28, 2022
@MrBeeMovie
Copy link

Is this still being looked at? Would love if the From and TryFrom traits were implemented.

@TedDriggs
Copy link
Collaborator Author

It is not actively being looked at.

  • We wouldn't support From given that building is typically fallible.
  • Supporting TryFrom is possible, but may be fiddly due to all the settings that now flow through the build_fn. Given that it's very short to add this directly to source code, I don't think it's a priority, but would be open to reviewing a PR for it, as long as that PR had good test coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants