feat: #[constructor(into)] syntax for automatic Into generics#551
Draft
gavincrawford wants to merge 2 commits into
Draft
feat: #[constructor(into)] syntax for automatic Into generics#551gavincrawford wants to merge 2 commits into
#[constructor(into)] syntax for automatic Into generics#551gavincrawford wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #541
Synopsis
The following pattern is common in many Rust projects/libraries as a way to expand what types constructors accept:
It would be convenient to be able to have a derived constructor that is capable of replicating this behavior for types that implement
Into<T>.Solution
This solution uses a new attribute,
#[constructor(into)](very subject to change) that allows users to designate specific fields to accept generic inputs. For example:will have a corresponding
newimplementation that acceptsid: impl Into<String>.One caveat of this feature is that
Into::into()is non-constant, which means the new, derived constructor function can't be either. In this implementation, I chose to handle this conditionally-- ifInto<T>fields are present,newis not constant; otherwise, it still is.I haven't added docs/tests yet, because I think it'd be worth talking about exactly what we want this syntax to look like, and if this is a feature that is within the scope of what this crate is meant to provide.
Checklist