Skip to content

Commit

Permalink
fix(variadics): allow PartialEqVariadic::eq_ref to take AsRefVars…
Browse files Browse the repository at this point in the history
… with different lifetimes (#1367)

Bug found while working on GHTs
  • Loading branch information
MingweiSamuel committed Jul 31, 2024
1 parent 8856c85 commit 43ff49d
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions variadics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ pub trait EitherRefVariadic: Variadic {
/// let un_ref: <var_type!(&u32, &String, &bool) as EitherRefVariadic>::UnRefVar =
/// var_expr!(1_u32, "Hello".to_owned(), false);
/// ```
type UnRefVar: Variadic;
type UnRefVar: VariadicExt;

/// This type with all exclusive `&mut` references replaced with shared `&` references.
///
Expand Down Expand Up @@ -488,7 +488,7 @@ pub trait PartialEqVariadic: VariadicExt {
fn eq(&self, other: &Self) -> bool;

/// `PartialEq` for the `AsRefVar` version op `Self`.
fn eq_ref<'a>(this: Self::AsRefVar<'a>, other: Self::AsRefVar<'a>) -> bool;
fn eq_ref(this: Self::AsRefVar<'_>, other: Self::AsRefVar<'_>) -> bool;
}
#[sealed]
impl<Item, Rest> PartialEqVariadic for (Item, Rest)
Expand All @@ -502,9 +502,9 @@ where
item_self == item_other && rest_self.eq(rest_other)
}

fn eq_ref<'a>(
this: <Self as VariadicExt>::AsRefVar<'a>,
other: <Self as VariadicExt>::AsRefVar<'a>,
fn eq_ref(
this: <Self as VariadicExt>::AsRefVar<'_>,
other: <Self as VariadicExt>::AsRefVar<'_>,
) -> bool {
let var_args!(item_self, ...rest_self) = this;
let var_args!(item_other, ...rest_other) = other;
Expand All @@ -517,9 +517,9 @@ impl PartialEqVariadic for () {
true
}

fn eq_ref<'a>(
_this: <Self as VariadicExt>::AsRefVar<'a>,
_other: <Self as VariadicExt>::AsRefVar<'a>,
fn eq_ref(
_this: <Self as VariadicExt>::AsRefVar<'_>,
_other: <Self as VariadicExt>::AsRefVar<'_>,
) -> bool {
true
}
Expand Down Expand Up @@ -713,4 +713,30 @@ mod test {
assert_eq!(Some(i), var.get_mut(i).copied());
}
}

#[test]
fn test_eq_ref_vec() {
type MyVar = var_type!(i32, bool, &'static str);
let vec: Vec<MyVar> = vec![
var_expr!(0, true, "hello"),
var_expr!(1, true, "world"),
var_expr!(2, false, "goodnight"),
var_expr!(3, false, "moon"),
];
let needle: <MyVar as VariadicExt>::AsRefVar<'_> =
var_expr!(2, false, "goodnight").as_ref_var();
assert_eq!(
Some(2),
vec.iter()
.position(|item| <MyVar as PartialEqVariadic>::eq_ref(needle, item.as_ref_var()))
);

let missing: <MyVar as VariadicExt>::AsRefVar<'_> =
var_expr!(3, false, "goodnight").as_ref_var();
assert_eq!(
None,
vec.iter()
.position(|item| <MyVar as PartialEqVariadic>::eq_ref(missing, item.as_ref_var()))
);
}
}

0 comments on commit 43ff49d

Please sign in to comment.