-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking Issue for proc_macro::ToTokens
#130977
Comments
I haven't had a chance to work on this yet and probably won't for a little while, so somebody else is welcome to pick this up. Should be pretty easy so I'll label it as such. |
I think a lot of those impls need // Indirect types
impl<T: ToTokens + ?Sized> ToTokens for &T { /* ... */ }
impl<T: ToTokens + ?Sized> ToTokens for &mut { /* ... */ }
impl<T: ToTokens + ?Sized> ToTokens for Box<T> { /* ... */ }
impl<T: ToTokens + ?Sized> ToTokens for Rc<T> { /* ... */ }
impl<T: ToTokens> ToTokens for Option<T> { /* ... */ }
impl<T: ToTokens + ?Sized> ToTokens for Cow<T> { /* ... */ } |
Thanks, I updated the top post. I hadn't thought about that :) |
@rustbot claim BTW, the link in the section "Public API" should be |
I have opened a PR #131441 for the initial implementation.
I have the same concerns about the unnecessary clone, and commented in #131441 (comment). In the current If this change will be applied, a more reasonable definition of the trait might be trait IntoTokenStream {
fn into_token_stream(self) -> TokenStream;
fn extend_token_stream(self, tokens: &mut TokenStream) {
tokens.extend_one(self.into_token_stream());
}
} Just a quick thought. |
I think taking by value probably makes sense too, and then just cloning for the |
unless the macro auto-clones as necessary, let ty: TokenStream = ...;
let fields: Vec<Ident> = ...;
quote::quote! {
struct S {
#(#fields: #ty,)* // calls ty.to_tokens() once for every field
}
} |
…it, r=dtolnay Add a new trait `proc_macro::ToTokens` Tracking issue rust-lang#130977 This PR adds a new trait `ToTokens`, implemented for types that can be interpolated inside a `quote!` invocation. ```rust impl ToTokens for TokenTree impl ToTokens for TokenStream impl ToTokens for Literal impl ToTokens for Ident impl ToTokens for Punct impl ToTokens for Group impl<T: ToTokens + ?Sized> ToTokens for &T impl<T: ToTokens + ?Sized> ToTokens for &mut T impl<T: ToTokens + ?Sized> ToTokens for Box<T> impl<T: ToTokens + ?Sized> ToTokens for Rc<T> impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T> impl<T: ToTokens> ToTokens for Option<T> impl ToTokens for u{8,16,32,64,128} impl ToTokens for i{8,16,32,64,128} impl ToTokens for f{32,64} impl ToTokens for {u,i}size impl ToTokens for bool impl ToTokens for char impl ToTokens for str impl ToTokens for String impl ToTokens for CStr impl ToTokens for CString ``` ~This PR also implements the migration mentioned in the tracking issue, replacing `Extend<Token{Tree,Stream}>` with `Extend<T: ToTokens>`, and replacing `FromIterator<Token{Tree,Stream}>` with `FromIterator<T: ToTokens>`.~ **UPDATE**: Reverted. ```diff -impl FromIterator<TokenTree> for TokenStream -impl FromIterator<TokenStream> for TokenStream +impl<T: ToTokens> FromIterator<T> for TokenStream -impl Extend<TokenTree> for TokenStream -impl Extend<TokenStream> for TokenStream +impl<T: ToTokens> Extend<T> for TokenStream ``` I'm going to leave some comments in the review where I'm unsure and concerned. r? `@dtolnay` CC `@tgross35`
…it, r=dtolnay Add a new trait `proc_macro::ToTokens` Tracking issue rust-lang#130977 This PR adds a new trait `ToTokens`, implemented for types that can be interpolated inside a `quote!` invocation. ```rust impl ToTokens for TokenTree impl ToTokens for TokenStream impl ToTokens for Literal impl ToTokens for Ident impl ToTokens for Punct impl ToTokens for Group impl<T: ToTokens + ?Sized> ToTokens for &T impl<T: ToTokens + ?Sized> ToTokens for &mut T impl<T: ToTokens + ?Sized> ToTokens for Box<T> impl<T: ToTokens + ?Sized> ToTokens for Rc<T> impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T> impl<T: ToTokens> ToTokens for Option<T> impl ToTokens for u{8,16,32,64,128} impl ToTokens for i{8,16,32,64,128} impl ToTokens for f{32,64} impl ToTokens for {u,i}size impl ToTokens for bool impl ToTokens for char impl ToTokens for str impl ToTokens for String impl ToTokens for CStr impl ToTokens for CString ``` ~This PR also implements the migration mentioned in the tracking issue, replacing `Extend<Token{Tree,Stream}>` with `Extend<T: ToTokens>`, and replacing `FromIterator<Token{Tree,Stream}>` with `FromIterator<T: ToTokens>`.~ **UPDATE**: Reverted. ```diff -impl FromIterator<TokenTree> for TokenStream -impl FromIterator<TokenStream> for TokenStream +impl<T: ToTokens> FromIterator<T> for TokenStream -impl Extend<TokenTree> for TokenStream -impl Extend<TokenStream> for TokenStream +impl<T: ToTokens> Extend<T> for TokenStream ``` I'm going to leave some comments in the review where I'm unsure and concerned. r? ``@dtolnay`` CC ``@tgross35``
…it, r=dtolnay Add a new trait `proc_macro::ToTokens` Tracking issue rust-lang#130977 This PR adds a new trait `ToTokens`, implemented for types that can be interpolated inside a `quote!` invocation. ```rust impl ToTokens for TokenTree impl ToTokens for TokenStream impl ToTokens for Literal impl ToTokens for Ident impl ToTokens for Punct impl ToTokens for Group impl<T: ToTokens + ?Sized> ToTokens for &T impl<T: ToTokens + ?Sized> ToTokens for &mut T impl<T: ToTokens + ?Sized> ToTokens for Box<T> impl<T: ToTokens + ?Sized> ToTokens for Rc<T> impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T> impl<T: ToTokens> ToTokens for Option<T> impl ToTokens for u{8,16,32,64,128} impl ToTokens for i{8,16,32,64,128} impl ToTokens for f{32,64} impl ToTokens for {u,i}size impl ToTokens for bool impl ToTokens for char impl ToTokens for str impl ToTokens for String impl ToTokens for CStr impl ToTokens for CString ``` ~This PR also implements the migration mentioned in the tracking issue, replacing `Extend<Token{Tree,Stream}>` with `Extend<T: ToTokens>`, and replacing `FromIterator<Token{Tree,Stream}>` with `FromIterator<T: ToTokens>`.~ **UPDATE**: Reverted. ```diff -impl FromIterator<TokenTree> for TokenStream -impl FromIterator<TokenStream> for TokenStream +impl<T: ToTokens> FromIterator<T> for TokenStream -impl Extend<TokenTree> for TokenStream -impl Extend<TokenStream> for TokenStream +impl<T: ToTokens> Extend<T> for TokenStream ``` I'm going to leave some comments in the review where I'm unsure and concerned. r? ```@dtolnay``` CC ```@tgross35```
…it, r=dtolnay Add a new trait `proc_macro::ToTokens` Tracking issue rust-lang#130977 This PR adds a new trait `ToTokens`, implemented for types that can be interpolated inside a `quote!` invocation. ```rust impl ToTokens for TokenTree impl ToTokens for TokenStream impl ToTokens for Literal impl ToTokens for Ident impl ToTokens for Punct impl ToTokens for Group impl<T: ToTokens + ?Sized> ToTokens for &T impl<T: ToTokens + ?Sized> ToTokens for &mut T impl<T: ToTokens + ?Sized> ToTokens for Box<T> impl<T: ToTokens + ?Sized> ToTokens for Rc<T> impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T> impl<T: ToTokens> ToTokens for Option<T> impl ToTokens for u{8,16,32,64,128} impl ToTokens for i{8,16,32,64,128} impl ToTokens for f{32,64} impl ToTokens for {u,i}size impl ToTokens for bool impl ToTokens for char impl ToTokens for str impl ToTokens for String impl ToTokens for CStr impl ToTokens for CString ``` ~This PR also implements the migration mentioned in the tracking issue, replacing `Extend<Token{Tree,Stream}>` with `Extend<T: ToTokens>`, and replacing `FromIterator<Token{Tree,Stream}>` with `FromIterator<T: ToTokens>`.~ **UPDATE**: Reverted. ```diff -impl FromIterator<TokenTree> for TokenStream -impl FromIterator<TokenStream> for TokenStream +impl<T: ToTokens> FromIterator<T> for TokenStream -impl Extend<TokenTree> for TokenStream -impl Extend<TokenStream> for TokenStream +impl<T: ToTokens> Extend<T> for TokenStream ``` I'm going to leave some comments in the review where I'm unsure and concerned. r? ````@dtolnay```` CC ````@tgross35````
Rollup merge of rust-lang#131441 - SpriteOvO:proc-macro-to-tokens-trait, r=dtolnay Add a new trait `proc_macro::ToTokens` Tracking issue rust-lang#130977 This PR adds a new trait `ToTokens`, implemented for types that can be interpolated inside a `quote!` invocation. ```rust impl ToTokens for TokenTree impl ToTokens for TokenStream impl ToTokens for Literal impl ToTokens for Ident impl ToTokens for Punct impl ToTokens for Group impl<T: ToTokens + ?Sized> ToTokens for &T impl<T: ToTokens + ?Sized> ToTokens for &mut T impl<T: ToTokens + ?Sized> ToTokens for Box<T> impl<T: ToTokens + ?Sized> ToTokens for Rc<T> impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T> impl<T: ToTokens> ToTokens for Option<T> impl ToTokens for u{8,16,32,64,128} impl ToTokens for i{8,16,32,64,128} impl ToTokens for f{32,64} impl ToTokens for {u,i}size impl ToTokens for bool impl ToTokens for char impl ToTokens for str impl ToTokens for String impl ToTokens for CStr impl ToTokens for CString ``` ~This PR also implements the migration mentioned in the tracking issue, replacing `Extend<Token{Tree,Stream}>` with `Extend<T: ToTokens>`, and replacing `FromIterator<Token{Tree,Stream}>` with `FromIterator<T: ToTokens>`.~ **UPDATE**: Reverted. ```diff -impl FromIterator<TokenTree> for TokenStream -impl FromIterator<TokenStream> for TokenStream +impl<T: ToTokens> FromIterator<T> for TokenStream -impl Extend<TokenTree> for TokenStream -impl Extend<TokenStream> for TokenStream +impl<T: ToTokens> Extend<T> for TokenStream ``` I'm going to leave some comments in the review where I'm unsure and concerned. r? ``@dtolnay`` CC ``@tgross35``
@SpriteOvO are you still interested in working on this? The expansion of |
@tgross35 Oh, I missed that step.. I'm still willing to work on it, just a bit busy recently, probably this week or next. |
Feature gate:
#![feature(proc_macro_totokens)]
This is a tracking issue for adding a
ToTokens
trait inproc_macro
, which can then be used inproc_macro::quote!
. See the ACP for motivation.Public API
This will be similar to
quote::ToTokens
. That can be used as a reference for implementation details since it already provides all of these.Steps / History
proc_macro::ToTokens
to supportproc_macro::quote
libs-team#431proc_macro
: Add a new traitproc_macro::ToTokens
#131441proc_macro::quote!
to use these traits: #...Unresolved Questions
ToTokens
doesn't seem quite accurate, but I don't know what would be better (ToTokenStream
?ExtendTokenStream
? Those seem a bit clunky).impl<T: ToTokens> ToTokens
for T is provided, should to_tokens take self by value rather than by reference so cloning isn't always necessary? (fn to_tokens(self, tokens: &mut TokenStream))Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: