From 4d1b5874b33e8a584b47b1f50cbcf46c6f746200 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 --- Cargo.toml | 5 +++++ genco-macros/src/fake.rs | 4 ++-- genco-macros/src/string_parser.rs | 4 ++-- src/fmt/formatter.rs | 6 ++++-- src/fmt/io_writer.rs | 3 ++- src/fmt/vec_writer.rs | 3 +++ src/lang/csharp/mod.rs | 9 +++++++-- src/lang/java/mod.rs | 10 +++++++--- src/lang/js.rs | 12 ++++++------ src/lang/mod.rs | 8 ++------ src/lang/nix.rs | 8 ++++++-- src/lang/python.rs | 7 +++++-- 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 ++++++++++++++++------ 21 files changed, 113 insertions(+), 50 deletions(-) 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/formatter.rs b/src/fmt/formatter.rs index 2ad9552..51b117f 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 = " "; diff --git a/src/fmt/io_writer.rs b/src/fmt/io_writer.rs index dbd73b9..55dd5c8 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]. /// diff --git a/src/fmt/vec_writer.rs b/src/fmt/vec_writer.rs index 876ffc8..6addf2d 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. diff --git a/src/lang/csharp/mod.rs b/src/lang/csharp/mod.rs index 3d12095..bc8e012 100644 --- a/src/lang/csharp/mod.rs +++ b/src/lang/csharp/mod.rs @@ -20,12 +20,17 @@ mod block_comment; mod comment; +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; +use alloc::string::{String, ToString}; + +use std::collections::{HashMap, HashSet}; + 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; diff --git a/src/lang/java/mod.rs b/src/lang/java/mod.rs index 445cbb1..0455905 100644 --- a/src/lang/java/mod.rs +++ b/src/lang/java/mod.rs @@ -16,15 +16,19 @@ //! ``` mod block_comment; - pub use self::block_comment::BlockComment; +use core::fmt::Write as _; + +use alloc::collections::BTreeSet; +use alloc::string::{String, ToString}; + +use std::collections::HashMap; + 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; 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..30633b8 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; @@ -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) } 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/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) } }