#![no_std]
#![cfg_attr(feature = "unstable-doc-cfg", feature(doc_cfg))]
extern crate proc_macro2;
extern crate quote;
use core::{
fmt::{
self,
Formatter,
},
iter::FusedIterator,
};
use proc_macro2::{
Group,
Span,
TokenStream,
TokenTree,
};
use quote::{
IdentFragment,
ToTokens,
};
#[cfg(has_macros_in_doc)]
macro_rules! create_wrapper {
(
[ $($prefix:tt)* ]
[ $($desc:tt)* ]
$name:ident,
$iter_name:ident,
$f_name:ident,
$respan_single:path
) => {
$($prefix)*
#[doc = $($desc)*]
#[must_use = "only the returned TokenTree has its span changed."]
pub fn $f_name(mut token: TokenTree, span: Span, apply_to_groups: bool) -> TokenTree {
let mut new_span = token.span();
$respan_single(&mut new_span, span);
let mut token = if apply_to_groups {
if let TokenTree::Group(group) = &mut token {
TokenTree::Group(Group::new(
group.delimiter(),
$iter_name::new(group.stream().into_iter(), span).collect(),
))
} else {
token
}
} else {
token
};
token.set_span(new_span);
token
}
$($prefix)*
#[doc = $($desc)*]
#[doc = concat!("[`", stringify!($name), "::iter`]")]
#[doc = concat!("[`", stringify!($name), "`]")]
#[doc = concat!("[`", stringify!($name), "::new`]")]
#[derive(Debug, Clone, Copy)]
pub struct $name<T: ToTokens> {
pub inner: T,
pub span: Span,
pub apply_to_groups: bool,
}
$($prefix)*
impl<T: ToTokens> $name<T> {
#[inline]
#[must_use = "construction doesn't change the spans of the original tokens."]
pub fn new(tokens: T, span: Span) -> Self {
$name {
inner: tokens,
span,
apply_to_groups: true,
}
}
#[inline]
#[must_use = "construction doesn't change the spans of the original value."]
pub fn new_without_groups(tokens: T, span: Span) -> Self {
$name {
inner: tokens,
span,
apply_to_groups: false,
}
}
#[inline]
#[must_use = "unwrapping this type doesn't change any spans."]
pub fn undo(self) -> T {
self.inner
}
#[inline]
#[must_use = "iterators do nothing unless consumed."]
pub fn iter(&self) -> $iter_name<<TokenStream as IntoIterator>::IntoIter> {
$iter_name {
inner: self.to_token_stream().into_iter(),
span: self.span,
apply_to_groups: self.apply_to_groups,
}
}
}
$($prefix)*
impl<T: ToTokens> ToTokens for $name<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(self.iter());
}
#[inline]
fn to_token_stream(&self) -> TokenStream {
self.iter().collect()
}
#[inline]
fn into_token_stream(self) -> TokenStream
where
Self: Sized,
{
self.into_iter().collect()
}
}
$($prefix)*
impl<T: ToTokens> IntoIterator for $name<T> {
type IntoIter = $iter_name<<TokenStream as IntoIterator>::IntoIter>;
type Item = TokenTree;
#[inline]
fn into_iter(self) -> Self::IntoIter {
let span = self.span;
let apply_to_groups = self.apply_to_groups;
$iter_name {
inner: self.into_token_stream().into_iter(),
span,
apply_to_groups,
}
}
}
$($prefix)*
impl<T: ToTokens + IdentFragment> IdentFragment for $name<T> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.inner.fmt(f)
}
#[inline]
fn span(&self) -> Option<Span> {
self.inner.span().map(|mut span| {
$respan_single(&mut span, self.span);
span
})
}
}
$($prefix)*
#[doc = concat!("[`", stringify!($name), "`]")]
#[derive(Debug, Clone, Copy)]
pub struct $iter_name<T: Iterator<Item = TokenTree>> {
pub inner: T,
pub span: Span,
pub apply_to_groups: bool,
}
$($prefix)*
impl<T: Iterator<Item = TokenTree>> $iter_name<T> {
#[inline]
#[must_use = "unwrapping doesn't change any spans."]
pub fn unrespanned(self) -> T {
self.inner
}
}
$($prefix)*
impl<T: Iterator<Item = TokenTree>> $iter_name<T> {
#[inline]
#[must_use = "construction doesn't change the spans of the original value."]
pub fn new(inner: T, span: Span) -> Self {
$iter_name {
inner,
span,
apply_to_groups: true,
}
}
#[inline]
#[must_use = "construction doesn't change the spans of the original value."]
pub fn new_without_groups(inner: T, span: Span) -> Self {
$iter_name {
inner,
span,
apply_to_groups: false,
}
}
}
$($prefix)*
impl<T: Iterator<Item = TokenTree>> Iterator for $iter_name<T> {
type Item = TokenTree;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner
.next()
.map(|token| $f_name(token, self.span, self.apply_to_groups))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.inner.count()
}
#[inline]
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
let span = self.span;
let apply_to_groups = self.apply_to_groups;
self.inner
.last()
.map(|token| $f_name(token, span, apply_to_groups))
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
if n > 0 {
if self.inner.nth(n - 1).is_none() {
return None
}
}
self.next()
}
}
$($prefix)*
impl<T: DoubleEndedIterator<Item = TokenTree>> DoubleEndedIterator for $iter_name<T> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner
.next_back()
.map(|token| $f_name(token, self.span, self.apply_to_groups))
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
if n > 0 {
if self.inner.nth_back(n - 1).is_none() {
return None
}
}
self.next_back()
}
}
$($prefix)*
impl<T: ExactSizeIterator<Item = TokenTree>> ExactSizeIterator for $iter_name<T> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
$($prefix)*
impl<T: FusedIterator<Item = TokenTree>> FusedIterator for $iter_name<T> {}
$($prefix)*
impl<T: Iterator<Item = TokenTree> + Clone> ToTokens for $iter_name<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(self.clone());
}
#[inline]
fn to_token_stream(&self) -> TokenStream {
self.clone().collect()
}
#[inline]
fn into_token_stream(self) -> TokenStream
where
Self: Sized,
{
self.collect()
}
}
};
}
#[cfg(not(has_macros_in_doc))]
macro_rules! create_wrapper {
(
[ $($prefix:tt)* ]
[ $($desc:tt)* ]
$name:ident,
$iter_name:ident,
$f_name:ident,
$respan_single:path
) => {
$($prefix)*
#[doc = $($desc)*]
#[must_use = "only the returned TokenTree has its span changed."]
pub fn $f_name(mut token: TokenTree, span: Span, apply_to_groups: bool) -> TokenTree {
let mut new_span = token.span();
$respan_single(&mut new_span, span);
let mut token = if apply_to_groups {
if let TokenTree::Group(group) = &mut token {
TokenTree::Group(Group::new(
group.delimiter(),
$iter_name::new(group.stream().into_iter(), span).collect(),
))
} else {
token
}
} else {
token
};
token.set_span(new_span);
token
}
$($prefix)*
#[doc = $($desc)*]
#[doc = "`iter`"]
#[doc = "this type"]
#[doc = "`new`"]
#[derive(Debug, Clone, Copy)]
pub struct $name<T: ToTokens> {
pub inner: T,
pub span: Span,
pub apply_to_groups: bool,
}
$($prefix)*
impl<T: ToTokens> $name<T> {
#[inline]
#[must_use = "construction doesn't change the spans of the original tokens."]
pub fn new(tokens: T, span: Span) -> Self {
$name {
inner: tokens,
span,
apply_to_groups: true,
}
}
#[inline]
#[must_use = "construction doesn't change the spans of the original value."]
pub fn new_without_groups(tokens: T, span: Span) -> Self {
$name {
inner: tokens,
span,
apply_to_groups: false,
}
}
#[inline]
#[must_use = "unwrapping this type doesn't change any spans."]
pub fn undo(self) -> T {
self.inner
}
#[inline]
#[must_use = "iterators do nothing unless consumed."]
pub fn iter(&self) -> $iter_name<<TokenStream as IntoIterator>::IntoIter> {
$iter_name {
inner: self.to_token_stream().into_iter(),
span: self.span,
apply_to_groups: self.apply_to_groups,
}
}
}
$($prefix)*
impl<T: ToTokens> ToTokens for $name<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(self.iter());
}
#[inline]
fn to_token_stream(&self) -> TokenStream {
self.iter().collect()
}
#[inline]
fn into_token_stream(self) -> TokenStream
where
Self: Sized,
{
self.into_iter().collect()
}
}
$($prefix)*
impl<T: ToTokens> IntoIterator for $name<T> {
type IntoIter = $iter_name<<TokenStream as IntoIterator>::IntoIter>;
type Item = TokenTree;
#[inline]
fn into_iter(self) -> Self::IntoIter {
let span = self.span;
let apply_to_groups = self.apply_to_groups;
$iter_name {
inner: self.into_token_stream().into_iter(),
span,
apply_to_groups,
}
}
}
$($prefix)*
impl<T: ToTokens + IdentFragment> IdentFragment for $name<T> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.inner.fmt(f)
}
#[inline]
fn span(&self) -> Option<Span> {
self.inner.span().map(|mut span| {
$respan_single(&mut span, self.span);
span
})
}
}
$($prefix)*
#[doc = "the wrapper type"]
#[derive(Debug, Clone, Copy)]
pub struct $iter_name<T: Iterator<Item = TokenTree>> {
pub inner: T,
pub span: Span,
pub apply_to_groups: bool,
}
$($prefix)*
impl<T: Iterator<Item = TokenTree>> $iter_name<T> {
#[inline]
#[must_use = "unwrapping doesn't change any spans."]
pub fn unrespanned(self) -> T {
self.inner
}
}
$($prefix)*
impl<T: Iterator<Item = TokenTree>> $iter_name<T> {
#[inline]
#[must_use = "construction doesn't change the spans of the original value."]
pub fn new(inner: T, span: Span) -> Self {
$iter_name {
inner,
span,
apply_to_groups: true,
}
}
#[inline]
#[must_use = "construction doesn't change the spans of the original value."]
pub fn new_without_groups(inner: T, span: Span) -> Self {
$iter_name {
inner,
span,
apply_to_groups: false,
}
}
}
$($prefix)*
impl<T: Iterator<Item = TokenTree>> Iterator for $iter_name<T> {
type Item = TokenTree;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner
.next()
.map(|token| $f_name(token, self.span, self.apply_to_groups))
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.inner.count()
}
#[inline]
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
let span = self.span;
let apply_to_groups = self.apply_to_groups;
self.inner
.last()
.map(|token| $f_name(token, span, apply_to_groups))
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
if n > 0 {
if self.inner.nth(n - 1).is_none() {
return None
}
}
self.next()
}
}
$($prefix)*
impl<T: DoubleEndedIterator<Item = TokenTree>> DoubleEndedIterator for $iter_name<T> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.inner
.next_back()
.map(|token| $f_name(token, self.span, self.apply_to_groups))
}
#[cfg(has_nth_back)]
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
if n > 0 {
if self.inner.nth_back(n - 1).is_none() {
return None
}
}
self.next_back()
}
}
$($prefix)*
impl<T: ExactSizeIterator<Item = TokenTree>> ExactSizeIterator for $iter_name<T> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
$($prefix)*
impl<T: FusedIterator<Item = TokenTree>> FusedIterator for $iter_name<T> {}
$($prefix)*
impl<T: Iterator<Item = TokenTree> + Clone> ToTokens for $iter_name<T> {
#[inline]
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(self.clone());
}
#[inline]
fn to_token_stream(&self) -> TokenStream {
self.clone().collect()
}
#[inline]
fn into_token_stream(self) -> TokenStream
where
Self: Sized,
{
self.collect()
}
}
};
}
fn respan_single(original: &mut Span, with: Span) {
*original = with;
}
#[cfg(has_span_resolution)]
fn reresolve_single(original: &mut Span, with: Span) {
*original = original.resolved_at(with);
}
#[cfg(has_span_resolution)]
fn relocate_single(original: &mut Span, with: Span) {
*original = original.located_at(with);
}
create_wrapper! {
[]
["Replaces the span entirely."]
QuoteRespanned,
QuoteRespannedIter,
respan,
respan_single
}
create_wrapper! {
[ #[cfg(has_span_resolution)] ]
[ "Changes the resolution site for [`Span`]s." ]
QuoteReresolved,
QuoteReresolvedIter,
reresolve,
reresolve_single
}
create_wrapper! {
[ #[cfg(has_span_resolution)] ]
[ "Changes the resolution site for [`Span`]s." ]
QuoteRelocated,
QuoteRelocatedIter,
relocate,
relocate_single
}