Struct virtue::generate::StreamBuilder
source · pub struct StreamBuilder { /* private fields */ }
Expand description
A helper struct build around a TokenStream to make it easier to build code.
Implementations§
source§impl StreamBuilder
impl StreamBuilder
sourcepub fn extend(&mut self, item: impl IntoIterator<Item = TokenTree>) -> &mut Self
pub fn extend(&mut self, item: impl IntoIterator<Item = TokenTree>) -> &mut Self
Add multiple TokenTree
items to the stream.
sourcepub fn append(&mut self, builder: StreamBuilder) -> &mut Self
pub fn append(&mut self, builder: StreamBuilder) -> &mut Self
Append another StreamBuilder to the current StreamBuilder.
sourcepub fn push(&mut self, item: impl Into<TokenTree>) -> &mut Self
pub fn push(&mut self, item: impl Into<TokenTree>) -> &mut Self
Push a single token to the stream.
sourcepub fn push_parsed(&mut self, item: impl AsRef<str>) -> Result<&mut Self>
pub fn push_parsed(&mut self, item: impl AsRef<str>) -> Result<&mut Self>
Attempt to parse the given string as valid Rust code, and append the parsed result to the internal stream.
Currently panics if the string could not be parsed as valid Rust code.
sourcepub fn ident(&mut self, ident: Ident) -> &mut Self
pub fn ident(&mut self, ident: Ident) -> &mut Self
Push a single ident to the stream. An ident is any word that a code file may contain, e.g. fn
, struct
, where
, names of functions and structs, etc.
sourcepub fn ident_str(&mut self, ident: impl AsRef<str>) -> &mut Self
pub fn ident_str(&mut self, ident: impl AsRef<str>) -> &mut Self
Push a single ident to the stream. An ident is any word that a code file may contain, e.g. fn
, struct
, where
, names of functions and structs, etc.
sourcepub fn group<FN>(&mut self, delim: Delimiter, inner: FN) -> Result<&mut Self>
pub fn group<FN>(&mut self, delim: Delimiter, inner: FN) -> Result<&mut Self>
Add a group. A group is any block surrounded by { .. }
, [ .. ]
or ( .. )
.
delim
indicates which group it is. The inner
callback is used to fill the contents of the group.
sourcepub fn punct(&mut self, p: char) -> &mut Self
pub fn punct(&mut self, p: char) -> &mut Self
Add a single punctuation to the stream. Puncts are single-character tokens like .
, <
, #
, etc
Note that this should not be used for multi-punct constructions like ::
or ->
. For that use puncts
instead.
sourcepub fn puncts(&mut self, puncts: &str) -> &mut Self
pub fn puncts(&mut self, puncts: &str) -> &mut Self
Add multiple punctuations to the stream. Multi punct tokens are e.g. ::
, ->
and =>
.
Note that this is the only way to add multi punct tokens.
If you were to use Punct
to insert ->
it would be inserted as -
and then >
, and not form a single token. Rust would interpret this as a “minus sign and then a greater than sign”, not as a single arrow.
sourcepub fn lifetime(&mut self, lt: Ident) -> &mut Self
pub fn lifetime(&mut self, lt: Ident) -> &mut Self
Add a lifetime to the stream.
Note that this is the only way to add lifetimes, if you were to do:
builder.punct('\'');
builder.ident_str("static");
It would not add 'static
, but instead it would add ' static
as seperate tokens, and the lifetime would not work.
sourcepub fn lifetime_str(&mut self, lt: &str) -> &mut Self
pub fn lifetime_str(&mut self, lt: &str) -> &mut Self
Add a lifetime to the stream.
Note that this is the only way to add lifetimes, if you were to do:
builder.punct('\'');
builder.ident_str("static");
It would not add 'static
, but instead it would add ' static
as seperate tokens, and the lifetime would not work.
sourcepub fn lit_str(&mut self, str: impl AsRef<str>) -> &mut Self
pub fn lit_str(&mut self, str: impl AsRef<str>) -> &mut Self
Add a literal string (&'static str
) to the stream.
sourcepub fn set_span_on_all_tokens(&mut self, span: Span)
pub fn set_span_on_all_tokens(&mut self, span: Span)
Set the given span on all tokens in the stream. This span is used by rust for e.g. compiler errors, to indicate the position of the error.