Skip to content

Commit

Permalink
#116: now remove also associated types predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
la10736 committed May 8, 2021
1 parent 07d2c3a commit 75a133a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
58 changes: 29 additions & 29 deletions src/render/fixture.rs
Expand Up @@ -347,38 +347,38 @@ mod should {
}
}

#[test]
fn clean_generics_in_partial_methods() {
let (_, out) = parse_fixture(
r#"
pub fn test<S: AsRef<str>, U: AsRef<u32>, F: ToString>(mut s: S, v: U) -> F
{ }
"#,
);
#[rstest]
#[case::base("fn test<S: AsRef<str>, U: AsRef<u32>, F: ToString>(mut s: S, v: U) -> F {}",
vec![
"fn default<F: ToString>() -> F {}",
"fn partial_1<S: AsRef<str>, F: ToString>(mut s: S) -> F {}",
"fn partial_2<S: AsRef<str>, U: AsRef<u32>, F: ToString>(mut s: S, v: U) -> F {}",
]
)]
#[case::associated_type("fn test<T: IntoIterator>(mut i: T) where T::Item: Copy {}",
vec![
"fn default() {}",
"fn partial_1<T: IntoIterator>(mut i: T) where T::Item: Copy {}",
]
)]
fn clean_generics(#[case] code: &str, #[case] expected: Vec<&str>) {
let (item_fn, out) = parse_fixture(code);
let n_args = item_fn.sig.inputs.iter().count();

let partials = (1..=2)
.map(|n| {
select_method(out.core_impl.clone(), format!("partial_{}", n))
.unwrap()
.sig
})
let mut signatures = vec![select_method(out.core_impl.clone(), "default").unwrap().sig];
signatures.extend((1..=n_args).map(|n| {
select_method(out.core_impl.clone(), format!("partial_{}", n))
.unwrap()
.sig
}));

let expected = expected
.into_iter()
.map(parse_str::<ItemFn>)
.map(|f| f.unwrap().sig)
.collect::<Vec<_>>();

let expected = vec![
parse_str::<ItemFn>(
r#"
pub fn partial_1<S: AsRef<str>, F: ToString>(mut s: S) -> F
{ }
"#
).unwrap().sig,
parse_str::<ItemFn>(
r#"
pub fn partial_2<S: AsRef<str>, U: AsRef<u32>, F: ToString>(mut s: S, v: U) -> F
{ }
"#
).unwrap().sig];

assert_eq!(expected, partials);
assert_eq!(expected, signatures);
}

#[test]
Expand Down
21 changes: 20 additions & 1 deletion src/render/mod.rs
Expand Up @@ -309,7 +309,7 @@ fn generics_clean_up<'a>(
.into_iter()
.filter(|wp| {
where_predicate_bounded_type(wp)
.and_then(MaybeIdent::maybe_ident)
.and_then(|t| first_type_path_segment_ident(t))
.map(|t| outs.0.contains(t))
.unwrap_or(true)
})
Expand All @@ -318,6 +318,25 @@ fn generics_clean_up<'a>(
result
}

// If type is not self and doesn't starts with :: return the first ident
// of its path segment: only if is a simple path.
// If type is a simple ident just return the this ident. That is useful to
// find the base type for associate type indication
fn first_type_path_segment_ident(t: &Type) -> Option<&Ident> {
match t {
Type::Path(tp) if tp.qself.is_none() && tp.path.leading_colon.is_none() => tp
.path
.segments
.iter()
.nth(0)
.and_then(|ps| match ps.arguments {
syn::PathArguments::None => Some(&ps.ident),
_ => None,
}),
_ => None,
}
}

struct TestCaseRender<'a> {
name: Ident,
attrs: &'a [syn::Attribute],
Expand Down

0 comments on commit 75a133a

Please sign in to comment.