From 38378bca5b4a65af9bd9bfea4258269179a9252d Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 13 Nov 2024 14:43:53 +0100 Subject: [PATCH] nit: Add std and alloc features and organize imports --- .github/workflows/ci.yml | 1 + Cargo.toml | 5 +++++ genco-macros/src/fake.rs | 4 ++-- genco-macros/src/string_parser.rs | 4 ++-- src/fmt/cursor.rs | 6 +++--- src/fmt/fmt_writer.rs | 14 +++++++------- src/fmt/formatter.rs | 14 ++++++++------ src/fmt/io_writer.rs | 15 ++++++++------- src/fmt/mod.rs | 9 ++++++--- src/fmt/vec_writer.rs | 9 ++++++--- src/lang/c.rs | 6 ++++-- src/lang/csharp/mod.rs | 13 ++++++++----- src/lang/dart/mod.rs | 8 ++++---- src/lang/go.rs | 6 ++++-- src/lang/java/mod.rs | 13 +++++++------ src/lang/js.rs | 12 ++++++------ src/lang/mod.rs | 14 ++++---------- src/lang/nix.rs | 8 ++++++-- src/lang/python.rs | 7 +++++-- src/lang/rust.rs | 11 +++++++---- src/lang/swift.rs | 6 ++++-- src/lib.rs | 10 ++++++++++ src/macros.rs | 8 ++++---- src/tokens/display.rs | 5 ++++- src/tokens/format_into.rs | 9 ++++++--- src/tokens/from_fn.rs | 2 ++ src/tokens/internal.rs | 14 ++++++++++---- src/tokens/item.rs | 2 ++ src/tokens/item_str.rs | 12 ++++++++---- src/tokens/tokens.rs | 22 ++++++++++++++++------ 30 files changed, 169 insertions(+), 100 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c8e76a5..090a0b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,6 +25,7 @@ jobs: with: toolchain: ${{matrix.rust}} - run: cargo build --workspace + - run: cargo build --workspace --no-default-features --features alloc - run: cargo build --workspace --all-targets if: matrix.rust == 'stable' - run: cargo test --workspace --doc diff --git a/Cargo.toml b/Cargo.toml index 48f8c2d..d55d2be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,11 @@ license = "MIT OR Apache-2.0" keywords = ["code-generation", "template"] categories = ["template-engine"] +[features] +default = ["std", "alloc"] +std = [] +alloc = [] + [dependencies] genco-macros = { path = "./genco-macros", version = "=0.17.9" } diff --git a/genco-macros/src/fake.rs b/genco-macros/src/fake.rs index c92f9ca..0e66b1d 100644 --- a/genco-macros/src/fake.rs +++ b/genco-macros/src/fake.rs @@ -1,5 +1,5 @@ -use std::cell::{RefCell, RefMut}; -use std::fmt::Arguments; +use core::cell::{RefCell, RefMut}; +use core::fmt::Arguments; use proc_macro2::Span; diff --git a/genco-macros/src/string_parser.rs b/genco-macros/src/string_parser.rs index 543a4a8..5f7dc52 100644 --- a/genco-macros/src/string_parser.rs +++ b/genco-macros/src/string_parser.rs @@ -1,7 +1,7 @@ //! Helper to parse quoted strings. -use std::cell::{Cell, RefCell}; -use std::fmt::Write; +use core::cell::{Cell, RefCell}; +use core::fmt::Write; use proc_macro2::{Span, TokenStream, TokenTree}; use syn::parse::{ParseBuffer, ParseStream}; diff --git a/src/fmt/cursor.rs b/src/fmt/cursor.rs index 942e64e..5dfc8dc 100644 --- a/src/fmt/cursor.rs +++ b/src/fmt/cursor.rs @@ -34,7 +34,7 @@ where fn parse(item: &Item) -> fmt::Result<&Self::Output> { match item { Item::Literal(s) => Ok(s), - _ => Err(std::fmt::Error), + _ => Err(core::fmt::Error), } } } @@ -57,7 +57,7 @@ where fn parse(item: &Item) -> fmt::Result<&Self::Output> { match item { Item::CloseEval => Ok(&()), - _ => Err(std::fmt::Error), + _ => Err(core::fmt::Error), } } } @@ -114,7 +114,7 @@ where where P: Parse, { - let item = self.next().ok_or(std::fmt::Error)?; + let item = self.next().ok_or(core::fmt::Error)?; P::parse(item) } } diff --git a/src/fmt/fmt_writer.rs b/src/fmt/fmt_writer.rs index 0a4d53a..e76c258 100644 --- a/src/fmt/fmt_writer.rs +++ b/src/fmt/fmt_writer.rs @@ -34,14 +34,14 @@ use crate::fmt; /// ``` pub struct FmtWriter where - W: std::fmt::Write, + W: core::fmt::Write, { writer: W, } impl FmtWriter where - W: std::fmt::Write, + W: core::fmt::Write, { /// Construct a new line writer from the underlying writer. pub fn new(writer: W) -> Self { @@ -59,24 +59,24 @@ where } } -impl std::fmt::Write for FmtWriter +impl core::fmt::Write for FmtWriter where - W: std::fmt::Write, + W: core::fmt::Write, { #[inline(always)] - fn write_char(&mut self, c: char) -> std::fmt::Result { + fn write_char(&mut self, c: char) -> core::fmt::Result { self.writer.write_char(c) } #[inline(always)] - fn write_str(&mut self, s: &str) -> std::fmt::Result { + fn write_str(&mut self, s: &str) -> core::fmt::Result { self.writer.write_str(s) } } impl fmt::Write for FmtWriter where - W: std::fmt::Write, + W: core::fmt::Write, { #[inline(always)] fn write_line(&mut self, config: &fmt::Config) -> fmt::Result { diff --git a/src/fmt/formatter.rs b/src/fmt/formatter.rs index 2ad9552..922ba69 100644 --- a/src/fmt/formatter.rs +++ b/src/fmt/formatter.rs @@ -1,11 +1,13 @@ +use core::mem; + +use alloc::string::String; + use crate::fmt; use crate::fmt::config::{Config, Indentation}; use crate::fmt::cursor; use crate::lang::Lang; use crate::tokens::Item; -use std::mem; - /// Buffer used as indentation source. static SPACES: &str = " "; @@ -231,7 +233,7 @@ impl<'a> Formatter<'a> { } _ => { // Anything else is an illegal state for formatting. - return Err(std::fmt::Error); + return Err(core::fmt::Error); } } } @@ -305,7 +307,7 @@ impl<'a> Formatter<'a> { } } -impl std::fmt::Write for Formatter<'_> { +impl core::fmt::Write for Formatter<'_> { fn write_str(&mut self, s: &str) -> fmt::Result { if !s.is_empty() { Formatter::write_str(self, s)?; @@ -315,8 +317,8 @@ impl std::fmt::Write for Formatter<'_> { } } -impl std::fmt::Debug for Formatter<'_> { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Debug for Formatter<'_> { + fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fmt.debug_struct("Formatter") .field("line", &self.line) .field("spaces", &self.spaces) diff --git a/src/fmt/io_writer.rs b/src/fmt/io_writer.rs index dbd73b9..80e9bbd 100644 --- a/src/fmt/io_writer.rs +++ b/src/fmt/io_writer.rs @@ -1,6 +1,7 @@ -use crate::fmt; use std::io; +use crate::fmt; + /// Helper struct to format a token stream to an underlying writer implementing /// [io::Write][std::io::Write]. /// @@ -60,22 +61,22 @@ where } } -impl std::fmt::Write for IoWriter +impl core::fmt::Write for IoWriter where W: io::Write, { #[inline(always)] - fn write_char(&mut self, c: char) -> std::fmt::Result { + fn write_char(&mut self, c: char) -> core::fmt::Result { self.writer .write_all(c.encode_utf8(&mut [0; 4]).as_bytes()) - .map_err(|_| std::fmt::Error) + .map_err(|_| core::fmt::Error) } #[inline(always)] - fn write_str(&mut self, s: &str) -> std::fmt::Result { + fn write_str(&mut self, s: &str) -> core::fmt::Result { self.writer .write_all(s.as_bytes()) - .map_err(|_| std::fmt::Error) + .map_err(|_| core::fmt::Error) } } @@ -87,6 +88,6 @@ where fn write_line(&mut self, config: &fmt::Config) -> fmt::Result { self.writer .write_all(config.newline.as_bytes()) - .map_err(|_| std::fmt::Error) + .map_err(|_| core::fmt::Error) } } diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index e1f48f2..8b6cc63 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -51,26 +51,29 @@ mod config; mod cursor; mod fmt_writer; mod formatter; +#[cfg(feature = "std")] mod io_writer; mod vec_writer; pub use self::config::{Config, Indentation}; pub use self::fmt_writer::FmtWriter; pub use self::formatter::Formatter; +#[cfg(feature = "std")] pub use self::io_writer::IoWriter; pub use self::vec_writer::VecWriter; /// Result type for the `fmt` module. -pub type Result = std::result::Result; +pub type Result = core::result::Result; /// Error for the `fmt` module. -pub type Error = std::fmt::Error; +pub type Error = core::fmt::Error; /// Trait that defines a line writer. -pub(crate) trait Write: std::fmt::Write { +pub(crate) trait Write: core::fmt::Write { /// Implement for writing a line. fn write_line(&mut self, config: &Config) -> Result; /// Implement for writing the trailing line ending of the file. + #[inline] fn write_trailing_line(&mut self, config: &Config) -> Result { self.write_line(config) } diff --git a/src/fmt/vec_writer.rs b/src/fmt/vec_writer.rs index 876ffc8..952b878 100644 --- a/src/fmt/vec_writer.rs +++ b/src/fmt/vec_writer.rs @@ -1,3 +1,6 @@ +use alloc::string::String; +use alloc::vec::Vec; + use crate::fmt; /// Helper struct to format a token stream as a vector of strings. @@ -61,14 +64,14 @@ impl VecWriter { } } -impl std::fmt::Write for VecWriter { +impl core::fmt::Write for VecWriter { #[inline(always)] - fn write_char(&mut self, c: char) -> std::fmt::Result { + fn write_char(&mut self, c: char) -> core::fmt::Result { self.line_buffer.write_char(c) } #[inline(always)] - fn write_str(&mut self, s: &str) -> std::fmt::Result { + fn write_str(&mut self, s: &str) -> core::fmt::Result { self.line_buffer.write_str(s) } } diff --git a/src/lang/c.rs b/src/lang/c.rs index be9b763..15e0b7d 100644 --- a/src/lang/c.rs +++ b/src/lang/c.rs @@ -1,11 +1,13 @@ //! Specialization for C code generation. +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; + use crate as genco; use crate::fmt; use crate::quote_in; use crate::tokens::{quoted, ItemStr}; -use std::collections::BTreeSet; -use std::fmt::Write as _; /// Tokens container specialization for C. pub type Tokens = crate::Tokens; diff --git a/src/lang/csharp/mod.rs b/src/lang/csharp/mod.rs index 3d12095..5606b3a 100644 --- a/src/lang/csharp/mod.rs +++ b/src/lang/csharp/mod.rs @@ -20,12 +20,15 @@ mod block_comment; mod comment; +use core::fmt::Write as _; + +use alloc::collections::{BTreeMap, BTreeSet}; +use alloc::string::{String, ToString}; + use crate as genco; use crate::fmt; use crate::quote_in; use crate::tokens::ItemStr; -use std::collections::{BTreeSet, HashMap, HashSet}; -use std::fmt::Write as _; pub use self::block_comment::BlockComment; pub use self::comment::Comment; @@ -119,7 +122,7 @@ pub struct Format { /// their use has to be qualified or not. /// /// A missing name means that it has to be used in a qualified manner. - imported_names: HashMap, + imported_names: BTreeMap, } /// Config data for Csharp formatting. @@ -170,7 +173,7 @@ impl Csharp { out: &mut Tokens, tokens: &Tokens, config: &Config, - imported_names: &mut HashMap, + imported_names: &mut BTreeMap, ) { let mut modules = BTreeSet::new(); @@ -182,7 +185,7 @@ impl Csharp { return; } - let mut imported = HashSet::new(); + let mut imported = BTreeSet::new(); for (namespace, name) in modules { if Some(namespace) == config.namespace.as_deref() { diff --git a/src/lang/dart/mod.rs b/src/lang/dart/mod.rs index 1422969..077f932 100644 --- a/src/lang/dart/mod.rs +++ b/src/lang/dart/mod.rs @@ -34,14 +34,16 @@ //! ``` mod doc_comment; - pub use self::doc_comment::DocComment; +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; + use crate as genco; use crate::fmt; use crate::quote_in; use crate::tokens::{quoted, ItemStr}; -use std::fmt::Write as _; const SEP: &str = "."; /// dart:core package. @@ -188,8 +190,6 @@ impl Import { impl Dart { /// Resolve all imports. fn imports(out: &mut Tokens, input: &Tokens, _: &Config) { - use std::collections::BTreeSet; - let mut modules = BTreeSet::new(); for import in input.walk_imports() { diff --git a/src/lang/go.rs b/src/lang/go.rs index 0eb1a69..82c38db 100644 --- a/src/lang/go.rs +++ b/src/lang/go.rs @@ -42,12 +42,14 @@ //! # } //! ``` +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; + use crate as genco; use crate::fmt; use crate::quote_in; use crate::tokens::{quoted, ItemStr}; -use std::collections::BTreeSet; -use std::fmt::Write as _; const MODULE_SEP: &str = "/"; const SEP: &str = "."; diff --git a/src/lang/java/mod.rs b/src/lang/java/mod.rs index 445cbb1..265bee9 100644 --- a/src/lang/java/mod.rs +++ b/src/lang/java/mod.rs @@ -16,15 +16,17 @@ //! ``` mod block_comment; - pub use self::block_comment::BlockComment; +use core::fmt::Write as _; + +use alloc::collections::{BTreeMap, BTreeSet}; +use alloc::string::{String, ToString}; + use crate as genco; use crate::fmt; use crate::tokens::ItemStr; use crate::{quote, quote_in}; -use std::collections::{BTreeSet, HashMap}; -use std::fmt::Write as _; /// Tokens container specialized for Java. pub type Tokens = crate::Tokens; @@ -38,7 +40,6 @@ impl_lang! { fn write_quoted(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result { // From: https://docs.oracle.com/javase/tutorial/java/data/characters.html - use std::fmt::Write as _; for c in input.chars() { match c { @@ -107,7 +108,7 @@ const SEP: &str = "."; #[derive(Debug, Default)] pub struct Format { /// Types which has been imported into the local namespace. - imported: HashMap, + imported: BTreeMap, } /// Configuration for Java. @@ -173,7 +174,7 @@ impl Java { out: &mut Tokens, tokens: &Tokens, config: &Config, - imported: &mut HashMap, + imported: &mut BTreeMap, ) { let mut modules = BTreeSet::new(); diff --git a/src/lang/js.rs b/src/lang/js.rs index b72800c..6d129e4 100644 --- a/src/lang/js.rs +++ b/src/lang/js.rs @@ -49,11 +49,15 @@ //! # } //! ``` +use core::fmt::Write as _; + +use alloc::collections::{BTreeMap, BTreeSet}; +use alloc::string::String; + use crate::fmt; use crate::tokens::ItemStr; + use relative_path::{RelativePath, RelativePathBuf}; -use std::collections::{BTreeMap, BTreeSet}; -use std::fmt::Write as _; /// Tokens container specialization for Rust. pub type Tokens = crate::Tokens; @@ -74,8 +78,6 @@ impl_lang! { _format: &Self::Format, has_eval: bool, ) -> fmt::Result { - use std::fmt::Write as _; - if has_eval { out.write_char('`')?; } else { @@ -92,8 +94,6 @@ impl_lang! { _format: &Self::Format, has_eval: bool, ) -> fmt::Result { - use std::fmt::Write as _; - if has_eval { out.write_char('`')?; } else { diff --git a/src/lang/mod.rs b/src/lang/mod.rs index 3905d1e..85d2852 100644 --- a/src/lang/mod.rs +++ b/src/lang/mod.rs @@ -39,6 +39,8 @@ pub use self::python::Python; pub use self::rust::Rust; pub use self::swift::Swift; +use core::fmt::Write as _; + use crate::fmt; use crate::Tokens; @@ -48,7 +50,7 @@ use crate::Tokens; /// module. pub trait Lang where - Self: 'static + Sized + Copy + Eq + Ord + std::hash::Hash + std::fmt::Debug, + Self: 'static + Sized + Copy + Eq + Ord + core::hash::Hash + core::fmt::Debug, { /// Configuration associated with building a formatting element. type Config; @@ -69,7 +71,6 @@ where _format: &Self::Format, _has_eval: bool, ) -> fmt::Result { - use std::fmt::Write as _; out.write_char('"')?; Ok(()) } @@ -81,7 +82,6 @@ where _format: &Self::Format, _has_eval: bool, ) -> fmt::Result { - use std::fmt::Write as _; out.write_char('"')?; Ok(()) } @@ -93,8 +93,6 @@ where format: &Self::Format, literal: &str, ) -> fmt::Result { - use std::fmt::Write as _; - Self::start_string_eval(out, config, format)?; out.write_str(literal)?; Self::end_string_eval(out, config, format)?; @@ -121,8 +119,6 @@ where /// Performing string quoting according to language convention. fn write_quoted(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result { - use std::fmt::Write as _; - out.write_str(input) } @@ -166,7 +162,7 @@ where pub trait LangItem where L: Lang, - Self: 'static + Clone + Eq + Ord + std::hash::Hash + std::fmt::Debug, + Self: 'static + Clone + Eq + Ord + core::hash::Hash + core::fmt::Debug, { /// Format the language item appropriately. fn format( @@ -184,8 +180,6 @@ where /// This is one of the more common escape sequences and is provided here so you /// can use it if a language you've implemented requires it. pub fn c_family_write_quoted(out: &mut fmt::Formatter, input: &str) -> fmt::Result { - use std::fmt::Write as _; - for c in input.chars() { match c { // alert (bell) diff --git a/src/lang/nix.rs b/src/lang/nix.rs index 045073d..9e71ad6 100644 --- a/src/lang/nix.rs +++ b/src/lang/nix.rs @@ -1,10 +1,14 @@ //! Nix + +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; +use alloc::string::ToString; + use crate as genco; use crate::fmt; use crate::quote_in; use crate::tokens::ItemStr; -use std::collections::BTreeSet; -use std::fmt::Write as _; /// Tokens pub type Tokens = crate::Tokens; diff --git a/src/lang/python.rs b/src/lang/python.rs index 5508d23..f6272ee 100644 --- a/src/lang/python.rs +++ b/src/lang/python.rs @@ -17,12 +17,15 @@ //! # } //! ``` +use core::fmt::Write as _; + +use alloc::collections::{BTreeMap, BTreeSet}; +use alloc::vec::Vec; + use crate as genco; use crate::fmt; use crate::tokens::ItemStr; use crate::{quote, quote_in}; -use std::collections::{BTreeMap, BTreeSet}; -use std::fmt::Write as _; /// Tokens container specialization for Python. pub type Tokens = crate::Tokens; diff --git a/src/lang/rust.rs b/src/lang/rust.rs index b2e14bc..439e1ea 100644 --- a/src/lang/rust.rs +++ b/src/lang/rust.rs @@ -38,10 +38,12 @@ //! # Ok(()) //! # } +use core::fmt::Write as _; + +use alloc::collections::{BTreeMap, BTreeSet, VecDeque}; + use crate::fmt; use crate::tokens::ItemStr; -use std::collections::{BTreeMap, BTreeSet, VecDeque}; -use std::fmt::Write as _; const SEP: &str = "::"; @@ -408,9 +410,10 @@ impl Import { impl Rust { fn imports(out: &mut Tokens, config: &Config, tokens: &Tokens) { + use alloc::collections::btree_set; + use crate as genco; use crate::quote_in; - use std::collections::btree_set; let mut modules = BTreeMap::<&ItemStr, Import>::new(); @@ -539,7 +542,7 @@ impl Rust { type Item = RenderItem<'a>; fn next(&mut self) -> Option { - if std::mem::take(&mut self.self_import) { + if core::mem::take(&mut self.self_import) { // Only render self-import if it's not a top level module. if self.module.split(SEP).count() > 1 { return Some(RenderItem::SelfImport); diff --git a/src/lang/swift.rs b/src/lang/swift.rs index 132dd77..110c814 100644 --- a/src/lang/swift.rs +++ b/src/lang/swift.rs @@ -14,10 +14,12 @@ //! # Ok(()) //! # } +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; + use crate::fmt; use crate::tokens::ItemStr; -use std::collections::BTreeSet; -use std::fmt::Write as _; /// Tokens container specialization for Rust. pub type Tokens = crate::Tokens; diff --git a/src/lib.rs b/src/lib.rs index b6e0fd2..1f82934 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,6 +157,16 @@ #![deny(missing_docs)] #![deny(rustdoc::broken_intra_doc_links)] #![allow(clippy::needless_doctest_main)] +#![no_std] + +#[cfg(feature = "std")] +extern crate std; + +#[cfg(feature = "alloc")] +extern crate alloc; + +#[cfg(not(feature = "alloc"))] +compile_error!("genco: The `alloc` feature must be enabled"); /// Whitespace sensitive quasi-quoting. /// diff --git a/src/macros.rs b/src/macros.rs index f4684d6..47f4a72 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -161,25 +161,25 @@ macro_rules! impl_lang { $( impl $crate::tokens::FormatInto<$lang> for $ty { fn format_into(self, tokens: &mut $crate::Tokens<$lang>) { - tokens.append($crate::tokens::__lang_item::<$lang>(Box::new(self.into()))); + tokens.append($crate::tokens::__lang_item::<$lang>(self.into())); } } impl<'a> $crate::tokens::FormatInto<$lang> for &'a $ty { fn format_into(self, tokens: &mut $crate::Tokens<$lang>) { - tokens.append($crate::tokens::__lang_item::<$lang>(Box::new(self.clone().into()))); + tokens.append($crate::tokens::__lang_item::<$lang>(self.clone().into())); } } impl $crate::tokens::Register<$lang> for $ty { fn register(self, tokens: &mut $crate::Tokens<$lang>) { - tokens.append($crate::tokens::__lang_item_register::<$lang>(Box::new(self.into()))); + tokens.append($crate::tokens::__lang_item_register::<$lang>(self.into())); } } impl<'a> $crate::tokens::Register<$lang> for &'a $ty { fn register(self, tokens: &mut $crate::Tokens<$lang>) { - tokens.append($crate::tokens::__lang_item_register::<$lang>(Box::new(self.clone().into()))); + tokens.append($crate::tokens::__lang_item_register::<$lang>(self.clone().into())); } } diff --git a/src/tokens/display.rs b/src/tokens/display.rs index 56bcd71..7207d91 100644 --- a/src/tokens/display.rs +++ b/src/tokens/display.rs @@ -1,7 +1,10 @@ +use core::fmt; + +use alloc::string::ToString; + use crate::lang::Lang; use crate::tokens::{FormatInto, Item}; use crate::Tokens; -use std::fmt; /// Function to build a string literal. /// diff --git a/src/tokens/format_into.rs b/src/tokens/format_into.rs index 22f26f4..28dbf91 100644 --- a/src/tokens/format_into.rs +++ b/src/tokens/format_into.rs @@ -1,6 +1,9 @@ -use std::borrow::Cow; -use std::fmt::Arguments; -use std::rc::Rc; +use core::fmt::Arguments; + +use alloc::borrow::Cow; +use alloc::rc::Rc; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; use crate::lang::Lang; use crate::tokens::{Item, ItemStr, Tokens}; diff --git a/src/tokens/from_fn.rs b/src/tokens/from_fn.rs index 20c68ce..38357d6 100644 --- a/src/tokens/from_fn.rs +++ b/src/tokens/from_fn.rs @@ -23,6 +23,7 @@ use crate::Tokens; /// let _: Tokens = quote!($c $['\n'] $c); /// # Ok::<_, genco::fmt::Error>(()) /// ``` +#[inline] pub fn from_fn(f: F) -> FromFn where F: FnOnce(&mut Tokens), @@ -44,6 +45,7 @@ where L: Lang, F: FnOnce(&mut Tokens), { + #[inline] fn format_into(self, tokens: &mut Tokens) { (self.f)(tokens); } diff --git a/src/tokens/internal.rs b/src/tokens/internal.rs index 4fb382e..2eed37d 100644 --- a/src/tokens/internal.rs +++ b/src/tokens/internal.rs @@ -1,3 +1,5 @@ +use alloc::boxed::Box; + use crate::lang::Lang; use crate::tokens::{from_fn, FormatInto}; @@ -6,12 +8,14 @@ use crate::tokens::{from_fn, FormatInto}; /// This must only be used by the [impl_lang!] macro. /// /// [impl_lang!]: crate::impl_lang! -pub fn __lang_item(item: Box) -> impl FormatInto +#[doc(hidden)] +#[inline] +pub fn __lang_item(item: L::Item) -> impl FormatInto where L: Lang, { from_fn(|t| { - t.lang_item(item); + t.lang_item(Box::new(item)); }) } @@ -20,11 +24,13 @@ where /// This must only be used by the [impl_lang!] macro. /// /// [impl_lang!]: crate::impl_lang! -pub fn __lang_item_register(item: Box) -> impl FormatInto +#[doc(hidden)] +#[inline] +pub fn __lang_item_register(item: L::Item) -> impl FormatInto where L: Lang, { from_fn(|t| { - t.lang_item_register(item); + t.lang_item_register(Box::new(item)); }) } diff --git a/src/tokens/item.rs b/src/tokens/item.rs index 2ca85d7..af62e04 100644 --- a/src/tokens/item.rs +++ b/src/tokens/item.rs @@ -1,5 +1,7 @@ //! A single element +use alloc::boxed::Box; + use crate::lang::Lang; use crate::tokens::{FormatInto, ItemStr, Tokens}; diff --git a/src/tokens/item_str.rs b/src/tokens/item_str.rs index acc325a..928c84d 100644 --- a/src/tokens/item_str.rs +++ b/src/tokens/item_str.rs @@ -1,11 +1,15 @@ //! Helper trait to take ownership of strings. +use core::fmt; +use core::ops::Deref; + +use alloc::borrow::{Cow, ToOwned}; +use alloc::boxed::Box; +use alloc::rc::Rc; +use alloc::string::String; + use crate::lang::Lang; use crate::tokens::{FormatInto, Item, Tokens}; -use std::borrow::Cow; -use std::fmt; -use std::ops::Deref; -use std::rc::Rc; /// A managed string that permits immutable borrowing. #[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] diff --git a/src/tokens/tokens.rs b/src/tokens/tokens.rs index fbd5eca..3e5f81e 100644 --- a/src/tokens/tokens.rs +++ b/src/tokens/tokens.rs @@ -10,14 +10,18 @@ //! ``` #![allow(clippy::module_inception)] +use core::cmp; +use core::iter::FromIterator; +use core::mem; +use core::slice; + +use alloc::boxed::Box; +use alloc::string::String; +use alloc::vec::{self, Vec}; + use crate::fmt; use crate::lang::{Lang, LangSupportsEval}; use crate::tokens::{FormatInto, Item, Register}; -use std::cmp; -use std::iter::FromIterator; -use std::mem; -use std::slice; -use std::vec; /// A stream of tokens. /// @@ -864,6 +868,7 @@ impl cmp::PartialEq>> for Tokens where L: Lang, { + #[inline] fn eq(&self, other: &Vec>) -> bool { self.items == *other } @@ -1056,6 +1061,12 @@ where #[cfg(test)] mod tests { + use core::fmt::Write as _; + + use alloc::string::String; + use alloc::vec; + use alloc::vec::Vec; + use crate as genco; use crate::fmt; use crate::{quote, Tokens}; @@ -1073,7 +1084,6 @@ mod tests { Import { fn format(&self, out: &mut fmt::Formatter<'_>, _: &(), _: &()) -> fmt::Result { - use std::fmt::Write as _; write!(out, "{}", self.0) } }