From 43ff49d72789d78535717d2db04cf595cc511274 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Wed, 31 Jul 2024 12:58:24 -0700 Subject: [PATCH] fix(variadics): allow `PartialEqVariadic::eq_ref` to take `AsRefVar`s with different lifetimes (#1367) Bug found while working on GHTs --- variadics/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/variadics/src/lib.rs b/variadics/src/lib.rs index c5b88d541005..1bf827f9e17c 100644 --- a/variadics/src/lib.rs +++ b/variadics/src/lib.rs @@ -297,7 +297,7 @@ pub trait EitherRefVariadic: Variadic { /// let un_ref: ::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. /// @@ -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 PartialEqVariadic for (Item, Rest) @@ -502,9 +502,9 @@ where item_self == item_other && rest_self.eq(rest_other) } - fn eq_ref<'a>( - this: ::AsRefVar<'a>, - other: ::AsRefVar<'a>, + fn eq_ref( + this: ::AsRefVar<'_>, + other: ::AsRefVar<'_>, ) -> bool { let var_args!(item_self, ...rest_self) = this; let var_args!(item_other, ...rest_other) = other; @@ -517,9 +517,9 @@ impl PartialEqVariadic for () { true } - fn eq_ref<'a>( - _this: ::AsRefVar<'a>, - _other: ::AsRefVar<'a>, + fn eq_ref( + _this: ::AsRefVar<'_>, + _other: ::AsRefVar<'_>, ) -> bool { true } @@ -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 = vec![ + var_expr!(0, true, "hello"), + var_expr!(1, true, "world"), + var_expr!(2, false, "goodnight"), + var_expr!(3, false, "moon"), + ]; + let needle: ::AsRefVar<'_> = + var_expr!(2, false, "goodnight").as_ref_var(); + assert_eq!( + Some(2), + vec.iter() + .position(|item| ::eq_ref(needle, item.as_ref_var())) + ); + + let missing: ::AsRefVar<'_> = + var_expr!(3, false, "goodnight").as_ref_var(); + assert_eq!( + None, + vec.iter() + .position(|item| ::eq_ref(missing, item.as_ref_var())) + ); + } }