Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
QuoteOption, possibly updated for nightly
use quote::{Tokens, ToTokens};
use proc_macro2::{Term, Span, Group, Delimiter, TokenStream};
pub struct QuoteOption<T>(pub Option<T>);
impl<T> ToTokens for QuoteOption<T>
where T: ToTokens
{
fn to_tokens(&self, tokens: &mut Tokens) {
// tokens.append(Term::new("{", Span::call_site()));
// tokens.append(Term::new("::", Span::call_site()));
// tokens.append(Term::new("std", Span::call_site()));
// tokens.append(Term::new("::", Span::call_site()));
// tokens.append(Term::new("option", Span::call_site()));
// tokens.append(Term::new("::", Span::call_site()));
// tokens.append(Term::new("Option", Span::call_site()));
// tokens.append(Term::new("::", Span::call_site()));
match self.0 {
Some(ref t) => {
tokens.append(Term::new("Some", Span::call_site()));
let mut inner_tokens = Tokens::new();
t.to_tokens(&mut inner_tokens);
tokens.append(Group::new(Delimiter::Parenthesis, inner_tokens.into()));
}
None => {
tokens.append(Term::new("None", Span::call_site()));
}
}
// tokens.append(Term::new("}", Span::call_site()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment