'How to `quote` an arbitrary structure at runtime into a `TokenStream` to create it?

Let's say I have an enum like the following:

pub enum Categories {
  A,
  B
}

I would like to get a function that converts a value x: Categories into a TokenStream that represents the same value. This function would have to work like so:

let converted: TokenStream = convert(Categories::A);
println!("{}", converted.to_string());
//=> Categories::A

Think of this as going from a runtime value of an object to its TokenStream representation. Perhaps there is some crate/macro decorator that I could use to decorate this object:

#[derives(TokenStreamConverter)]
pub enum Categories {
  A,
  B
}

That could then create a converter function for it automatically?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source