pub trait FormatInto<L>where
L: Lang,{
// Required method
fn format_into(self, tokens: &mut Tokens<L>);
}
Expand description
Trait for types that can be formatted in-place into a token stream.
Things implementing FormatInto can be used as arguments for interpolation in the quote! macro.
from_fn() is a helper function which simplifies the task of creating a FormatInto implementation on the fly.
§Examples
use genco::quote_in;
use genco::tokens::{ItemStr, FormatInto, from_fn, static_literal};
use genco::lang::Lang;
fn comment<L>(s: impl Into<ItemStr>) -> impl FormatInto<L>
where
L: Lang
{
from_fn(move |tokens| {
let s = s.into();
quote_in!(*tokens => $(static_literal("//")) $s);
})
}
Required Methods§
Sourcefn format_into(self, tokens: &mut Tokens<L>)
fn format_into(self, tokens: &mut Tokens<L>)
Convert the type into tokens in-place.
A simple way to build ad-hoc format_into implementations is by using the from_fn() function.
Implementations on Foreign Types§
Source§impl<'a, L> FormatInto<L> for &Cow<'a, str>where
L: Lang,
Cow strings are formatted by either borrowing or cloning the string.
impl<'a, L> FormatInto<L> for &Cow<'a, str>where
L: Lang,
Cow strings are formatted by either borrowing or cloning the string.
§Examples
use std::borrow::Cow;
use genco::prelude::*;
let foo = &Cow::<str>::Borrowed("foo");
let bar = &Cow::<str>::Owned(String::from("bar"));
let result: Tokens = quote!($foo $bar baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<'a, L> FormatInto<L> for Cow<'a, str>where
L: Lang,
Cow strings are formatted by either borrowing or cloning the string.
impl<'a, L> FormatInto<L> for Cow<'a, str>where
L: Lang,
Cow strings are formatted by either borrowing or cloning the string.
§Examples
use std::borrow::Cow;
use genco::prelude::*;
let foo = Cow::<str>::Borrowed("foo");
let bar = Cow::<str>::Owned(String::from("bar"));
let result: Tokens = quote!($foo $bar baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<'a, L, T> FormatInto<L> for &Cow<'a, [T]>
Cow slices are formatted by either borrowing or cloning the slice.
impl<'a, L, T> FormatInto<L> for &Cow<'a, [T]>
Cow slices are formatted by either borrowing or cloning the slice.
§Examples
use std::borrow::Cow;
use genco::prelude::*;
let foo = &Cow::<[&str]>::Borrowed(&["foo", "bar"]);
let bar = &Cow::<[&str]>::Owned(vec!["baz"]);
let result: Tokens = quote!($foo $bar biz);
assert_eq!("foobar baz biz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<'a, L, T> FormatInto<L> for Cow<'a, [T]>
Cow slices are formatted by either borrowing or cloning the slice.
impl<'a, L, T> FormatInto<L> for Cow<'a, [T]>
Cow slices are formatted by either borrowing or cloning the slice.
§Examples
use std::borrow::Cow;
use genco::prelude::*;
let foo = Cow::<[&str]>::Borrowed(&["foo", "bar"]);
let bar = Cow::<[&str]>::Owned(vec!["baz"]);
let result: Tokens = quote!($foo $bar biz);
assert_eq!("foobar baz biz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for &strwhere
L: Lang,
Formatting borrowed string boxed them on the heap.
impl<L> FormatInto<L> for &strwhere
L: Lang,
Formatting borrowed string boxed them on the heap.
§Examples
use genco::prelude::*;
let foo = "foo";
let bar = "bar";
let result: Tokens = quote!($foo $bar baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for &Rc<String>where
L: Lang,
Refcounted strings are cloned and moved into the token stream without
copying.
impl<L> FormatInto<L> for &Rc<String>where
L: Lang,
Refcounted strings are cloned and moved into the token stream without copying.
§Examples
use std::rc::Rc;
use genco::prelude::*;
let foo = Rc::new(String::from("foo"));
let bar = Rc::new(String::from("bar"));
let result: Tokens = quote!($(&foo) $(&bar) baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for &Stringwhere
L: Lang,
Formatting borrowed string boxed them on the heap.
impl<L> FormatInto<L> for &Stringwhere
L: Lang,
Formatting borrowed string boxed them on the heap.
§Examples
use genco::prelude::*;
let foo = String::from("foo");
let bar = String::from("bar");
let result: Tokens = quote!($(&foo) $(&bar) baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for i8where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for i8where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for i16where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for i16where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for i32where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for i32where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for i64where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for i64where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for i128where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for i128where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for isizewhere
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for isizewhere
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for u8where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for u8where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for u16where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for u16where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for u32where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for u32where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for u64where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for u64where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for u128where
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for u128where
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for usizewhere
L: Lang,
Implementation for primitive type. Uses the corresponding
Display implementation for the
primitive type.
impl<L> FormatInto<L> for usizewhere
L: Lang,
Implementation for primitive type. Uses the corresponding Display implementation for the primitive type.
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for Rc<String>where
L: Lang,
Refcounted strings are moved into the token stream without copying.
impl<L> FormatInto<L> for Rc<String>where
L: Lang,
Refcounted strings are moved into the token stream without copying.
§Examples
use std::rc::Rc;
use genco::prelude::*;
let foo = Rc::new(String::from("foo"));
let bar = Rc::new(String::from("bar"));
let result: Tokens = quote!($foo $bar baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for Stringwhere
L: Lang,
Formatting owned strings takes ownership of the string directly from the
heap.
impl<L> FormatInto<L> for Stringwhere
L: Lang,
Formatting owned strings takes ownership of the string directly from the heap.
§Examples
use genco::prelude::*;
let foo = String::from("foo");
let bar = String::from("bar");
let result: Tokens = quote!($foo $bar baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L> FormatInto<L> for Arguments<'_>where
L: Lang,
Implementation for Arguments which allows for arbitrary and efficient
literal formatting.
impl<L> FormatInto<L> for Arguments<'_>where
L: Lang,
Implementation for Arguments which allows for arbitrary and efficient literal formatting.
§Examples
use genco::prelude::*;
let name = "John";
let result: Tokens = quote!($(format_args!("Hello {name}")));
assert_eq!("Hello John", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L, T> FormatInto<L> for &[T]
Formatting a slice of token streams is like formatting each, one after
another.
impl<L, T> FormatInto<L> for &[T]
Formatting a slice of token streams is like formatting each, one after another.
This will cause each token stream to be cloned into the destination stream.
§Examples
use genco::prelude::*;
let vec = vec!["foo", " ", "bar"];
let slice = &vec[..];
let result: Tokens = quote!($slice baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L, T> FormatInto<L> for &Vec<T>
Formatting a reference to a vector of token streams is like formatting each,
one after another.
impl<L, T> FormatInto<L> for &Vec<T>
Formatting a reference to a vector of token streams is like formatting each, one after another.
§Examples
use genco::prelude::*;
let mut vec = Vec::<Tokens>::new();
vec.push(quote!(foo));
vec.push(quote!($[' ']bar));
let result = quote!($(&vec) baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L, T> FormatInto<L> for Option<T>where
L: Lang,
T: FormatInto<L>,
Optional items are formatted if they are present.
impl<L, T> FormatInto<L> for Option<T>where
L: Lang,
T: FormatInto<L>,
Optional items are formatted if they are present.
§Examples
use std::rc::Rc;
use genco::prelude::*;
let foo = Some("foo");
let bar = Some("bar");
let biz = None::<&str>;
let result: Tokens = quote!($foo $bar baz $biz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Source§impl<L, T> FormatInto<L> for Vec<T>where
L: Lang,
T: FormatInto<L>,
Formatting a vector of token streams is like formatting each, one after
another.
impl<L, T> FormatInto<L> for Vec<T>where
L: Lang,
T: FormatInto<L>,
Formatting a vector of token streams is like formatting each, one after another.
§Examples
use genco::prelude::*;
let mut vec = Vec::<Tokens>::new();
vec.push(quote!(foo));
vec.push(quote!($[' ']bar));
let result = quote!($vec baz);
assert_eq!("foo bar baz", result.to_string()?);
fn format_into(self, tokens: &mut Tokens<L>)
Implementors§
impl FormatInto<C> for genco::lang::c::Import
impl FormatInto<Csharp> for genco::lang::csharp::Import
impl FormatInto<Dart> for genco::lang::dart::Import
impl FormatInto<Go> for genco::lang::go::Import
impl FormatInto<Java> for genco::lang::java::Import
impl FormatInto<JavaScript> for genco::lang::js::Import
impl FormatInto<Nix> for genco::lang::nix::Import
impl FormatInto<Python> for genco::lang::python::Import
impl FormatInto<Python> for ImportModule
impl FormatInto<Rust> for genco::lang::rust::Import
impl FormatInto<Swift> for genco::lang::swift::Import
impl<'a> FormatInto<C> for &'a genco::lang::c::Import
impl<'a> FormatInto<Csharp> for &'a genco::lang::csharp::Import
impl<'a> FormatInto<Dart> for &'a genco::lang::dart::Import
impl<'a> FormatInto<Go> for &'a genco::lang::go::Import
impl<'a> FormatInto<Java> for &'a genco::lang::java::Import
impl<'a> FormatInto<JavaScript> for &'a genco::lang::js::Import
impl<'a> FormatInto<Nix> for &'a genco::lang::nix::Import
impl<'a> FormatInto<Python> for &'a genco::lang::python::Import
impl<'a> FormatInto<Python> for &'a ImportModule
impl<'a> FormatInto<Rust> for &'a genco::lang::rust::Import
impl<'a> FormatInto<Swift> for &'a genco::lang::swift::Import
impl<L> FormatInto<L> for &ItemStrwhere
L: Lang,
impl<L> FormatInto<L> for &Tokens<L>where
L: Lang,
Formatting a reference to a token stream is exactly the same as extending the token stream with a copy of the stream being formatted.
§Examples
use genco::prelude::*;
let a: &Tokens = "e!(foo bar);
let result = quote!($a baz);
assert_eq!("foo bar baz", result.to_string()?);
impl<L> FormatInto<L> for Item<L>where
L: Lang,
Formatting an item is the same as simply adding that item to the token stream.
§Examples
use genco::prelude::*;
use genco::tokens::{Item, ItemStr};
let foo = Item::Literal(ItemStr::Static("foo"));
let bar = Item::Literal(ItemStr::Box("bar".into()));
let result: Tokens = quote!($foo $bar baz);
assert_eq!("foo bar baz", result.to_string()?);
assert_eq!{
vec![
Item::Literal(ItemStr::Static("foo")),
Item::Space,
Item::Literal(ItemStr::Box("bar".into())),
Item::Space,
Item::Literal(ItemStr::Static("baz")),
] as Vec<Item<()>>,
result,
};
impl<L> FormatInto<L> for ItemStrwhere
L: Lang,
Convert stringy things.