pub struct TokenStream(/* private fields */);
展开描述
此 crate 提供的主要类型,表示抽象的 token 流,或者更具体地说,表示一个 token tree 序列。该类型提供了迭代这些 token tree 的接口,反之亦然,可以将多个 token tree 收集到一个流中。
这是 #[proc_macro]
、#[proc_macro_attribute]
和 #[proc_macro_derive]
定义的输入和输出。
实现§
源代码§impl TokenStream
impl TokenStream
1.29.0 · 源代码pub fn new() -> TokenStream
pub fn new() -> TokenStream
返回一个空的 TokenStream
,不包含任何 token tree。
源代码pub fn expand_expr(&self) -> Result<TokenStream, ExpandError>
🔬这是一个仅限每夜构建的实验性 API。(proc_macro_expand
#90765)
pub fn expand_expr(&self) -> Result<TokenStream, ExpandError>
proc_macro_expand
#90765)将此 TokenStream
解析为表达式并尝试展开其中的任何宏。返回展开后的 TokenStream
。
目前只有展开为字面量的表达式会成功,尽管将来可能会放宽此限制。
注意:在错误条件下,expand_expr
可能会留下未展开的宏,报告错误,导致编译失败,和/或返回 Err(..)
。任何错误条件下的具体行为以及哪些条件被视为错误是未指定的,并且将来可能会发生变化。
Trait 实现§
1.15.0 · 源代码§impl Clone for TokenStream
impl Clone for TokenStream
源代码§fn clone(&self) -> TokenStream
fn clone(&self) -> TokenStream
1.0.0 · 源代码§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
执行拷贝赋值。了解更多1.15.0 · 源代码§impl Debug for TokenStream
以方便调试的形式打印 token。
impl Debug for TokenStream
以方便调试的形式打印 token。
1.15.0 · 源代码§impl Display for TokenStream
将 token stream 打印为字符串,该字符串应能无损地转换回相同的 token stream(不考虑 span),但可能不包括带有 Delimiter::None
分隔符的 TokenTree::Group
和负数字面量。
impl Display for TokenStream
将 token stream 打印为字符串,该字符串应能无损地转换回相同的 token stream(不考虑 span),但可能不包括带有 Delimiter::None
分隔符的 TokenTree::Group
和负数字面量。
注意:输出的确切形式可能会改变,例如,token 之间使用的空白符可能会改变。因此,在实现过程宏时,您不应依赖对输出字符串(由 to_string
生成)进行简单的子字符串匹配,因为如果发生此类更改,该匹配可能会失效。相反,您应在 TokenTree
级别进行操作,例如匹配 TokenTree::Ident
、TokenTree::Punct
或 TokenTree::Literal
。
1.30.0 · 源代码§impl Extend<TokenStream> for TokenStream
impl Extend<TokenStream> for TokenStream
源代码§fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I)
fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I)
1.30.0 · 源代码§impl Extend<TokenTree> for TokenStream
impl Extend<TokenTree> for TokenStream
1.29.0 · 源代码§impl From<TokenTree> for TokenStream
创建一个包含单个 token tree 的 token stream。
impl From<TokenTree> for TokenStream
创建一个包含单个 token tree 的 token stream。
源代码§fn from(tree: TokenTree) -> TokenStream
fn from(tree: TokenTree) -> TokenStream
1.15.0 · 源代码§impl FromIterator<TokenStream> for TokenStream
token streams 上的“展平”操作,将来自多个 token streams 的 token tree 收集到单个 stream 中。
impl FromIterator<TokenStream> for TokenStream
token streams 上的“展平”操作,将来自多个 token streams 的 token tree 收集到单个 stream 中。
源代码§fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self
1.29.0 · 源代码§impl FromIterator<TokenTree> for TokenStream
将多个 token tree 收集到单个 stream 中。
impl FromIterator<TokenTree> for TokenStream
将多个 token tree 收集到单个 stream 中。
1.15.0 · 源代码§impl FromStr for TokenStream
尝试将字符串分解为 token 并将这些 token 解析为 token stream。可能会因多种原因失败,例如字符串包含不平衡的分隔符或语言中不存在的字符。解析后的 stream 中的所有 token 都将获得 Span::call_site()
span。
impl FromStr for TokenStream
尝试将字符串分解为 token 并将这些 token 解析为 token stream。可能会因多种原因失败,例如字符串包含不平衡的分隔符或语言中不存在的字符。解析后的 stream 中的所有 token 都将获得 Span::call_site()
span。
注意:某些错误可能会导致 panic 而非返回 LexError
。我们保留将来将这些错误更改为 LexError
的权利。
1.29.0 · 源代码§impl IntoIterator for TokenStream
impl IntoIterator for TokenStream
源代码§impl ToTokens for TokenStream
impl ToTokens for TokenStream
源代码§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
proc_macro_totokens
#130977)源代码§fn into_token_stream(self) -> TokenStream
fn into_token_stream(self) -> TokenStream
proc_macro_totokens
#130977)源代码§fn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
proc_macro_totokens
#130977)