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

source

pub fn new() -> Self

Generate a new StreamBuilder

source

pub fn extend(&mut self, item: impl IntoIterator<Item = TokenTree>) -> &mut Self

Add multiple TokenTree items to the stream.

source

pub fn append(&mut self, builder: StreamBuilder) -> &mut Self

Append another StreamBuilder to the current StreamBuilder.

source

pub fn push(&mut self, item: impl Into<TokenTree>) -> &mut Self

Push a single token to the stream.

source

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.

source

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.

source

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.

source

pub fn group<FN>(&mut self, delim: Delimiter, inner: FN) -> Result<&mut Self>
where FN: FnOnce(&mut StreamBuilder) -> Result<()>,

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn lit_str(&mut self, str: impl AsRef<str>) -> &mut Self

Add a literal string (&'static str) to the stream.

source

pub fn lit_usize(&mut self, val: usize) -> &mut Self

Add an usize value to the stream.

source

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.

Trait Implementations§

source§

impl Default for StreamBuilder

source§

fn default() -> StreamBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.