Skip to content

Commit

Permalink
Remove dependency on smallvec
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed May 29, 2019
1 parent 906b11e commit 8469829
Show file tree
Hide file tree
Showing 49 changed files with 170 additions and 186 deletions.
1 change: 0 additions & 1 deletion core/Cargo.toml
Expand Up @@ -21,7 +21,6 @@ proc-macro = true
[dependencies]
proc-macro2 = "0.4.13"
quote = "0.6.8"
smallvec = "0.6.9"
syn = { version = "0.15.29", features = ["full", "visit-mut"] }

[features]
Expand Down
17 changes: 7 additions & 10 deletions core/src/auto_enum/args.rs
@@ -1,10 +1,7 @@
use proc_macro2::{token_stream::IntoIter, Delimiter, Group, Ident, TokenStream, TokenTree};
use quote::ToTokens;
use smallvec::smallvec;
use syn::{Path, Result};

use crate::utils::Stack;

// =============================================================================
// Arg

Expand Down Expand Up @@ -80,17 +77,17 @@ macro_rules! arg_err {
};
}

pub(super) fn parse_group(group: TokenStream) -> Result<(Stack<Arg>, Option<String>, bool)> {
pub(super) fn parse_group(group: TokenStream) -> Result<(Vec<Arg>, Option<String>, bool)> {
syn::parse2::<Group>(group)
.map_err(|e| arg_err!(e))
.and_then(|group| parse_args(group.stream()))
}

pub(super) fn parse_args(args: TokenStream) -> Result<(Stack<Arg>, Option<String>, bool)> {
pub(super) fn parse_args(args: TokenStream) -> Result<(Vec<Arg>, Option<String>, bool)> {
const ERR: &str = "expected one of `,`, `::`, or identifier, found ";

let mut iter = args.into_iter();
let mut args = Stack::new();
let mut args = Vec::new();
let mut marker = None;
let mut never = false;
while let Some(tt) = iter.next() {
Expand All @@ -116,7 +113,7 @@ pub(super) fn parse_args(args: TokenStream) -> Result<(Stack<Arg>, Option<String
},
TokenTree::Punct(p) => match p.as_char() {
',' => {}
':' => args.push(parse_path(smallvec![p.into()], &mut iter)?),
':' => args.push(parse_path(vec![p.into()], &mut iter)?),
_ => arg_err!(p, "{}`{}`", ERR, p)?,
},
_ => arg_err!(tt, "{}`{}`", ERR, tt)?,
Expand All @@ -126,7 +123,7 @@ pub(super) fn parse_args(args: TokenStream) -> Result<(Stack<Arg>, Option<String
Ok((args, marker, never))
}

fn parse_path(mut path: Stack<TokenTree>, iter: &mut IntoIter) -> Result<Arg> {
fn parse_path(mut path: Vec<TokenTree>, iter: &mut IntoIter) -> Result<Arg> {
for tt in iter {
match tt {
TokenTree::Punct(ref p) if p.as_char() == ',' => break,
Expand All @@ -144,7 +141,7 @@ fn path_or_ident(ident: Ident, tt: Option<TokenTree>, iter: &mut IntoIter) -> Re
None => Ok(Arg::Ident(ident)),
Some(TokenTree::Punct(p)) => match p.as_char() {
',' => Ok(Arg::Ident(ident)),
':' => parse_path(smallvec![ident.into(), p.into()], iter),
':' => parse_path(vec![ident.into(), p.into()], iter),
_ => arg_err!(p, "{}`{}`", ERR, p),
},
Some(tt) => arg_err!(tt, "{}`{}`", ERR, tt),
Expand All @@ -154,7 +151,7 @@ fn path_or_ident(ident: Ident, tt: Option<TokenTree>, iter: &mut IntoIter) -> Re
fn marker_opt(
ident: Ident,
iter: &mut IntoIter,
args: &mut Stack<Arg>,
args: &mut Vec<Arg>,
marker: &mut Option<String>,
) -> Result<()> {
match iter.next() {
Expand Down
19 changes: 9 additions & 10 deletions core/src/auto_enum/context.rs
Expand Up @@ -2,10 +2,9 @@ use std::cell::RefCell;

use proc_macro2::{Ident, TokenStream};
use quote::ToTokens;
use smallvec::{smallvec, SmallVec};
use syn::{token, Attribute, Expr, ExprCall, ExprPath, ItemEnum, Macro, Result};

use crate::utils::{ident, path, Stack};
use crate::utils::{ident, path};

use super::{
visitor::{Dummy, FindTry, Visitor},
Expand Down Expand Up @@ -42,7 +41,7 @@ pub(super) enum VisitLastMode {
pub(super) struct Context {
/// Span passed to `syn::Error::new_spanned`.
span: Option<TokenStream>,
pub(super) args: Stack<Arg>,
pub(super) args: Vec<Arg>,
builder: Builder,
pub(super) marker: Marker,
// pub(super) depth: usize,
Expand All @@ -57,7 +56,7 @@ pub(super) struct Context {
impl Context {
fn new<T: ToTokens>(
span: T,
args: Stack<Arg>,
args: Vec<Arg>,
marker: Option<String>,
never: bool,
root: bool,
Expand All @@ -78,14 +77,14 @@ impl Context {

pub(super) fn root<T: ToTokens>(
span: T,
(args, marker, never): (Stack<Arg>, Option<String>, bool),
(args, marker, never): (Vec<Arg>, Option<String>, bool),
) -> Self {
Self::new(span, args, marker, never, true)
}

pub(super) fn child<T: ToTokens>(
span: T,
(args, marker, never): (Stack<Arg>, Option<String>, bool),
(args, marker, never): (Vec<Arg>, Option<String>, bool),
) -> Self {
Self::new(span, args, marker, never, false)
}
Expand Down Expand Up @@ -232,14 +231,14 @@ impl Marker {

struct Builder {
ident: String,
variants: Stack<String>,
variants: Vec<String>,
}

impl Builder {
fn new() -> Self {
Self {
ident: format!("___Enum{}", RNG.with(|rng| rng.borrow_mut().next())),
variants: Stack::new(),
variants: Vec::new(),
}
}

Expand All @@ -250,8 +249,8 @@ impl Builder {
fn next_expr(&mut self, attrs: Vec<Attribute>, expr: Expr) -> Expr {
let variant = format!("___Variant{}", self.variants.len());

let segments: SmallVec<[_; 2]> =
smallvec![ident(&self.ident).into(), ident(&variant).into()];
let segments =
Some(ident(&self.ident).into()).into_iter().chain(Some(ident(&variant).into()));

self.variants.push(variant);

Expand Down
12 changes: 6 additions & 6 deletions core/src/auto_enum/traits.rs
Expand Up @@ -7,14 +7,14 @@ use crate::utils::*;

use super::Arg;

pub(super) fn collect_impl_traits(args: &mut Stack<Arg>, ty: &mut Type) {
pub(super) fn collect_impl_traits(args: &mut Vec<Arg>, ty: &mut Type) {
if let Some(traits) = collect(ty) {
parse(args, traits);
}
}

fn collect(ty: &mut Type) -> Option<Stack<Path>> {
let mut traits = Stack::new();
fn collect(ty: &mut Type) -> Option<Vec<Path>> {
let mut traits = Vec::new();
ImplTraits::new(&mut traits).visit_type_mut(ty);

if traits.is_empty() {
Expand All @@ -24,7 +24,7 @@ fn collect(ty: &mut Type) -> Option<Stack<Path>> {
}
}

fn parse(args: &mut Stack<Arg>, traits: Stack<Path>) {
fn parse(args: &mut Vec<Arg>, traits: Vec<Path>) {
traits.into_iter().map(Arg::from).for_each(|t| {
if !args.contains(&t) && TRAITS.contains(&&*t.to_trimed_string()) {
args.push(t);
Expand All @@ -33,11 +33,11 @@ fn parse(args: &mut Stack<Arg>, traits: Stack<Path>) {
}

struct ImplTraits<'a> {
traits: &'a mut Stack<Path>,
traits: &'a mut Vec<Path>,
}

impl<'a> ImplTraits<'a> {
fn new(traits: &'a mut Stack<Path>) -> Self {
fn new(traits: &'a mut Vec<Path>) -> Self {
Self { traits }
}
}
Expand Down
3 changes: 0 additions & 3 deletions core/src/utils.rs
@@ -1,14 +1,11 @@
use std::mem;

use proc_macro2::{Ident, Span, TokenStream};
use smallvec::SmallVec;
use syn::{
punctuated::Punctuated, token, Block, Expr, ExprBlock, ExprTuple, ExprVerbatim, Path,
PathSegment, Stmt,
};

pub(crate) type Stack<T> = SmallVec<[T; 4]>;

// =============================================================================
// Extension traits

Expand Down
1 change: 0 additions & 1 deletion derive/Cargo.toml
Expand Up @@ -23,7 +23,6 @@ derive_utils = { version = "0.7.1" }
lazy_static = "1.2"
proc-macro2 = "0.4.13"
quote = "0.6.8"
smallvec = "0.6.9"
syn = { version = "0.15.29", features = ["full"] }

[features]
Expand Down
4 changes: 2 additions & 2 deletions derive/src/derive/core/convert/as_mut.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["AsMut"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
parse_quote!(::core::convert::AsMut)?,
Expand All @@ -13,5 +13,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/convert/as_ref.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["AsRef"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
parse_quote!(::core::convert::AsRef)?,
Expand All @@ -13,5 +13,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/fmt/mod.rs
Expand Up @@ -5,7 +5,7 @@ macro_rules! fmt_impl {

pub(crate) const NAME: &[&str] = &[$($name),*];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
parse_quote!(::core::fmt::$Trait)?,
Expand All @@ -16,7 +16,7 @@ macro_rules! fmt_impl {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions derive/src/derive/core/fmt/write.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["fmt::Write"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
parse_quote!(::core::fmt::Write)?,
Expand All @@ -17,5 +17,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/future.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["Future"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
parse_quote!(::core::future::Future)?,
Expand All @@ -17,5 +17,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/iter/double_ended_iterator.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["DoubleEndedIterator"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
#[cfg(feature = "try_trait")]
let try_trait = quote! {
#[inline]
Expand Down Expand Up @@ -36,5 +36,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/iter/exact_size_iterator.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["ExactSizeIterator"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
#[cfg(not(feature = "exact_size_is_empty"))]
let is_empty = quote!();
#[cfg(feature = "exact_size_is_empty")]
Expand All @@ -23,5 +23,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/iter/extend.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["Extend"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
parse_quote!(::core::iter::Extend)?,
Expand All @@ -13,5 +13,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/iter/fused_iterator.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["FusedIterator"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
Some(ident("Item")),
Expand All @@ -11,5 +11,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
trait FusedIterator: ::core::iter::Iterator {}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/iter/iterator.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["Iterator"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
#[cfg(feature = "try_trait")]
let try_trait = quote! {
#[inline]
Expand Down Expand Up @@ -68,5 +68,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}
4 changes: 2 additions & 2 deletions derive/src/derive/core/iter/trusted_len.rs
Expand Up @@ -2,7 +2,7 @@ use crate::utils::*;

pub(crate) const NAME: &[&str] = &["TrustedLen"];

pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
pub(crate) fn derive(data: &Data, items: &mut Vec<ItemImpl>) -> Result<()> {
derive_trait!(
data,
Some(ident("Item")),
Expand All @@ -11,5 +11,5 @@ pub(crate) fn derive(data: &Data, stack: &mut Stack<ItemImpl>) -> Result<()> {
unsafe trait TrustedLen: ::core::iter::Iterator {}
}?,
)
.map(|item| stack.push(item))
.map(|item| items.push(item))
}

0 comments on commit 8469829

Please sign in to comment.