Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generic consts #28

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ strum = { version = "0.24.1", features = ["derive"], default-features = false }

[dependencies]
quote = "1.0.23"
syn = { version = "1.0.107", features = ["full", "extra-traits"] }
syn = "1.0.107"
proc-macro2 = "1.0.51"
heck = "0.4.1"

Expand Down
6 changes: 3 additions & 3 deletions src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Enum {
.type_params()
.map(|param| {
(
Param::Ident(param.ident.clone()),
Param::Type(param.ident.clone()),
param.bounds.iter().cloned().collect(),
)
})
Expand All @@ -64,9 +64,9 @@ impl Enum {
// We need to treat this as a bound on both `T` and on `U`.
let bounds: Vec<TypeParamBound> = ty.bounds.iter().cloned().collect();
ty.bounded_ty
.extract_idents()
.extract_types()
.into_iter()
.map(move |ident| (Param::Ident(ident), bounds.clone()))
.map(move |ident| (Param::Type(ident), bounds.clone()))
.boxed()
}
syn::WherePredicate::Lifetime(lt) => [(
Expand Down
58 changes: 48 additions & 10 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use crate::{iter::BoxedIter, param::Param};

pub trait Extractor {
fn extract_lifetimes(&self) -> Vec<Lifetime>;
fn extract_idents(&self) -> Vec<Ident>;
fn extract_types(&self) -> Vec<Ident>;
fn extract_consts(&self) -> Vec<Ident>;
fn extract_params(&self) -> Box<dyn Iterator<Item = Param>> {
self.extract_lifetimes()
.into_iter()
.map(Param::Lifetime)
.chain(self.extract_idents().into_iter().map(Param::Ident))
.chain(self.extract_types().into_iter().map(Param::Type))
.chain(self.extract_consts().into_iter().map(Param::Const))
.boxed()
}
}
Expand Down Expand Up @@ -63,11 +65,11 @@ impl Extractor for Type {
}
}

fn extract_idents(&self) -> Vec<Ident> {
fn extract_types(&self) -> Vec<Ident> {
match self {
Type::Array(a) => a.elem.extract_idents(),
Type::Array(a) => a.elem.extract_types(),
Type::BareFn(_) => Vec::new(),
Type::Group(g) => g.elem.extract_idents(),
Type::Group(g) => g.elem.extract_types(),
Type::ImplTrait(it) => it
.bounds
.iter()
Expand All @@ -80,18 +82,54 @@ impl Extractor for Type {
Type::Infer(_) => Vec::new(),
Type::Macro(_) => Vec::new(),
Type::Never(_) => Vec::new(),
Type::Paren(p) => p.elem.extract_idents(),
Type::Paren(p) => p.elem.extract_types(),
Type::Path(p) => p
.path
.get_ident()
.map(ToOwned::to_owned)
.into_iter()
.collect(),
Type::Ptr(p) => p.elem.extract_idents(),
Type::Reference(r) => r.elem.extract_idents(),
Type::Slice(s) => s.elem.extract_idents(),
Type::Ptr(p) => p.elem.extract_types(),
Type::Reference(r) => r.elem.extract_types(),
Type::Slice(s) => s.elem.extract_types(),
Type::TraitObject(_) => Vec::new(),
Type::Tuple(t) => t.elems.iter().flat_map(Self::extract_idents).collect(),
Type::Tuple(t) => t.elems.iter().flat_map(Self::extract_types).collect(),
Type::Verbatim(_) => Vec::new(),
#[allow(unknown_lints)]
#[cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
_ => Vec::new(),
}
}

fn extract_consts(&self) -> Vec<Ident> {
match self {
Type::Array(a) => a.elem.extract_consts(),
Type::BareFn(_) => Vec::new(),
Type::Group(g) => g.elem.extract_consts(),
Type::ImplTrait(it) => it
.bounds
.iter()
.cloned()
.filter_map(|b| match b {
TypeParamBound::Trait(t) => t.path.get_ident().map(ToOwned::to_owned),
TypeParamBound::Lifetime(_) => None,
})
.collect(),
Type::Infer(_) => Vec::new(),
Type::Macro(_) => Vec::new(),
Type::Never(_) => Vec::new(),
Type::Paren(p) => p.elem.extract_consts(),
Type::Path(p) => p
.path
.get_ident()
.map(ToOwned::to_owned)
.into_iter()
.collect(),
Type::Ptr(p) => p.elem.extract_consts(),
Type::Reference(r) => r.elem.extract_consts(),
Type::Slice(s) => s.elem.extract_consts(),
Type::TraitObject(_) => Vec::new(),
Type::Tuple(t) => t.elems.iter().flat_map(Self::extract_consts).collect(),
Type::Verbatim(_) => Vec::new(),
#[allow(unknown_lints)]
#[cfg_attr(test, deny(non_exhaustive_omitted_patterns))]
Expand Down
37 changes: 20 additions & 17 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@ use crate::extractor::Extractor;
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub enum Param {
Lifetime(Lifetime),
Ident(Ident),
}

impl From<Ident> for Param {
fn from(value: Ident) -> Self {
Param::Ident(value)
}
}

impl From<Lifetime> for Param {
fn from(value: Lifetime) -> Self {
Param::Lifetime(value)
}
Type(Ident),
Const(Ident),
}

impl Param {
Expand All @@ -34,7 +23,8 @@ impl Param {
) -> (Option<&'b GenericParam>, Option<&'b WherePredicate>) {
match self {
Param::Lifetime(lt) => find_lt(lt, generics),
Param::Ident(ty) => find_ident(ty, generics),
Param::Type(ty) => find_ident(ty, generics),
Param::Const(c) => find_const(c, generics),
}
}

Expand All @@ -54,11 +44,11 @@ impl Param {
.get_ident()
.into_iter()
.cloned()
.flat_map(|ident| Param::from(ident).find_relevant(bound_map))
.flat_map(|ident| Param::Type(ident).find_relevant(bound_map))
.collect()
}
TypeParamBound::Lifetime(lifetime) => {
Param::from(lifetime.clone()).find_relevant(bound_map)
Param::Lifetime(lifetime.clone()).find_relevant(bound_map)
}
})
.chain([self.clone()])
Expand All @@ -67,6 +57,7 @@ impl Param {
}
}
}

fn find_lt<'a>(
lt: &Lifetime,
generics: &'a Generics,
Expand Down Expand Up @@ -104,11 +95,23 @@ fn find_ident<'a>(
.find(|pred| match pred {
WherePredicate::Type(pred_ty) => pred_ty
.bounded_ty
.extract_idents()
.extract_types()
.iter()
.any(|id| id == ident),
_ => false,
});

(generic_param, predicate)
}

fn find_const<'a>(
con: &Ident,
generics: &'a Generics,
) -> (Option<&'a GenericParam>, Option<&'a WherePredicate>) {
let generic_param = generics.params.iter().find(|p| match p {
GenericParam::Const(const_param) => &const_param.ident == con,
_ => false,
});

(generic_param, None)
}
20 changes: 17 additions & 3 deletions tests/it.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use subenum::subenum;

// Test tuple variants, struct variants, and repeated types.
#[subenum(Bar)]
#[derive(Clone, Debug, PartialEq, Eq)]
enum Foo {
Expand All @@ -14,6 +15,7 @@ enum Foo {
},
}

// Test lifetimes, generics, as well as unused lifetimes/generics.
#[subenum(Both, Str, Tee, Neither)]
#[derive(Clone, Debug)]
enum Pippy<'a, T> {
Expand All @@ -31,6 +33,7 @@ enum Pippy<'a, T> {
F { a: u32 },
}

// Test where clause
#[subenum(Flip, Flop)]
#[derive(Clone, Debug, PartialEq, Eq)]
enum Flippy<T, U>
Expand All @@ -43,6 +46,7 @@ where
B(U),
}

// Test conversion.
#[subenum(Floop)]
#[derive(Clone, Debug, PartialEq)]
enum Snoo<T> {
Expand All @@ -65,10 +69,20 @@ enum Boo<'a> {
A(&'a str),
}

// Test lifetime relationships.
#[subenum(Dop, Mop)]
enum Dip<'a: 'b, 'b, 'c, T, U> {
#[subenum(Dop, Mop)]
A(&'a T),
#[subenum(Dop)]
B(&'b [&'c [U; 7]]),
}

// Test generic consts.
#[subenum(Phew)]
enum Whew<'a: 'b, 'b, 'c, T, U> {
enum Whew<const N: usize> {
#[subenum(Phew)]
A(&'a T),
A,
#[subenum(Phew)]
B(&'b [&'c [U; 7]]),
B([u8; N]),
}
Loading