From 0f5668e768b493d4270e827317d186b5c7bb3f09 Mon Sep 17 00:00:00 2001 From: Brian Porter Date: Mon, 10 Jul 2017 09:38:10 -0700 Subject: [PATCH] Fix binary op lifetime inference (#12) * fix lifetimes for borrowed-borrowed binary ops * added RELEASES.md and bumped patch version --- Cargo.toml | 4 ++-- RELEASES.md | 7 +++++++ src/binary.rs | 6 +++--- tests/binary.rs | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 RELEASES.md diff --git a/Cargo.toml b/Cargo.toml index 3ae3d6c..3ac41d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "impl_ops" -version = "0.1.0" +version = "0.1.1" authors = ["Brian Porter "] license="MIT" description = "Macros for easy operator overloading." keywords = ["macro", "operator", "overloading", "impl", "op"] -categories = ["Rust patterns"] +categories = ["rust-patterns"] repository = "https://github.com/brianwp3000/impl_ops" include = [ "**/*.rs", diff --git a/RELEASES.md b/RELEASES.md new file mode 100644 index 0000000..9715b5d --- /dev/null +++ b/RELEASES.md @@ -0,0 +1,7 @@ +Version 0.1 +=========== +* Initial release + +Version 0.1.1 +============= +* Fixed lifetime inference bug with binary operators \ No newline at end of file diff --git a/src/binary.rs b/src/binary.rs index d99c530..9cbe46e 100644 --- a/src/binary.rs +++ b/src/binary.rs @@ -53,7 +53,7 @@ macro_rules! _impl_binary_op_owned_borrowed { impl<'a> ops::$ops_trait<&'a $rhs> for $lhs { type Output = $out; - fn $ops_fn(self, $rhs_i: &'a $rhs) -> Self::Output { + fn $ops_fn(self, $rhs_i: &$rhs) -> Self::Output { let $lhs_i = self; $body } @@ -82,10 +82,10 @@ macro_rules! _impl_binary_op_borrowed_owned { #[macro_export] macro_rules! _impl_binary_op_borrowed_borrowed { ($ops_trait:ident, $ops_fn:ident, $lhs:ty, $rhs:ty, $out:ty, $lhs_i:ident, $rhs_i:ident, $body:block) => ( - impl<'a> ops::$ops_trait<&'a $rhs> for &'a $lhs { + impl<'a, 'b> ops::$ops_trait<&'a $rhs> for &'b $lhs { type Output = $out; - fn $ops_fn(self, $rhs_i: &'a $rhs) -> Self::Output { + fn $ops_fn(self, $rhs_i: &$rhs) -> Self::Output { let $lhs_i = self; $body } diff --git a/tests/binary.rs b/tests/binary.rs index 20ab4ae..e0be2de 100644 --- a/tests/binary.rs +++ b/tests/binary.rs @@ -340,4 +340,18 @@ mod multiline { assert_eq!(kong::Donkey::new(2 * 1), &kong::Barrel::new(1) * kong::Donkey::new(2)); assert_eq!(kong::Donkey::new(2 * 1), &kong::Barrel::new(1) * &kong::Donkey::new(2)); } +} + +fn do_bitor(a: &kong::Donkey, b: &kong::Dixie) -> kong::Diddy { + a | b +} + +fn do_bitor_2(a: &kong::Dixie, b: &kong::Donkey) -> kong::Diddy { + a | b +} + +#[test] +fn infer_lifetimes() { + assert_eq!(kong::Diddy::new(1 | 2), do_bitor(&kong::Donkey::new(1), &kong::Dixie::new(2))); + assert_eq!(kong::Diddy::new(2 | 1), do_bitor_2(&kong::Dixie::new(1), &kong::Donkey::new(2))); } \ No newline at end of file