From 034275c6e23831b3a60adb0d89b842834e3f2e16 Mon Sep 17 00:00:00 2001 From: naure Date: Wed, 27 Feb 2019 08:48:54 +0100 Subject: [PATCH] Rust: Fix lifetime in union _as_ accessors (#5140) * Fix lifetime in union _as_ accessors In the accessors for union field, the return value is implicitly taking the lifetime of &self. This is irrelevant and prevents usages of the returned value, because it is needlessly bound to the parent field lifetime. This patch makes the return value inherit the lifetime of the data, like other methods do. --- src/idl_gen_rust.cpp | 4 ++-- tests/monster_test_generated.rs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 861a2e9c0..fd6a06693 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -1331,8 +1331,8 @@ class RustGenerator : public BaseGenerator { code_ += " #[inline]"; code_ += " #[allow(non_snake_case)]"; - code_ += " pub fn {{FIELD_NAME}}_as_{{U_ELEMENT_NAME}}(&'a self) -> " - "Option<{{U_ELEMENT_TABLE_TYPE}}> {"; + code_ += " pub fn {{FIELD_NAME}}_as_{{U_ELEMENT_NAME}}(&self) -> " + "Option<{{U_ELEMENT_TABLE_TYPE}}<'a>> {"; code_ += " if self.{{FIELD_NAME}}_type() == {{U_ELEMENT_ENUM_TYPE}} {"; code_ += " self.{{FIELD_NAME}}().map(|u| " "{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))"; diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index a796ff9a5..a538eaea8 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -1259,7 +1259,7 @@ impl<'a> Monster<'a> { } #[inline] #[allow(non_snake_case)] - pub fn test_as_monster(&'a self) -> Option { + pub fn test_as_monster(&self) -> Option> { if self.test_type() == Any::Monster { self.test().map(|u| Monster::init_from_table(u)) } else { @@ -1269,7 +1269,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn test_as_test_simple_table_with_enum(&'a self) -> Option { + pub fn test_as_test_simple_table_with_enum(&self) -> Option> { if self.test_type() == Any::TestSimpleTableWithEnum { self.test().map(|u| TestSimpleTableWithEnum::init_from_table(u)) } else { @@ -1279,7 +1279,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn test_as_my_game_example_2_monster(&'a self) -> Option { + pub fn test_as_my_game_example_2_monster(&self) -> Option> { if self.test_type() == Any::MyGame_Example2_Monster { self.test().map(|u| super::example_2::Monster::init_from_table(u)) } else { @@ -1289,7 +1289,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn any_unique_as_m(&'a self) -> Option { + pub fn any_unique_as_m(&self) -> Option> { if self.any_unique_type() == AnyUniqueAliases::M { self.any_unique().map(|u| Monster::init_from_table(u)) } else { @@ -1299,7 +1299,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn any_unique_as_t(&'a self) -> Option { + pub fn any_unique_as_t(&self) -> Option> { if self.any_unique_type() == AnyUniqueAliases::T { self.any_unique().map(|u| TestSimpleTableWithEnum::init_from_table(u)) } else { @@ -1309,7 +1309,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn any_unique_as_m2(&'a self) -> Option { + pub fn any_unique_as_m2(&self) -> Option> { if self.any_unique_type() == AnyUniqueAliases::M2 { self.any_unique().map(|u| super::example_2::Monster::init_from_table(u)) } else { @@ -1319,7 +1319,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn any_ambiguous_as_m1(&'a self) -> Option { + pub fn any_ambiguous_as_m1(&self) -> Option> { if self.any_ambiguous_type() == AnyAmbiguousAliases::M1 { self.any_ambiguous().map(|u| Monster::init_from_table(u)) } else { @@ -1329,7 +1329,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn any_ambiguous_as_m2(&'a self) -> Option { + pub fn any_ambiguous_as_m2(&self) -> Option> { if self.any_ambiguous_type() == AnyAmbiguousAliases::M2 { self.any_ambiguous().map(|u| Monster::init_from_table(u)) } else { @@ -1339,7 +1339,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn any_ambiguous_as_m3(&'a self) -> Option { + pub fn any_ambiguous_as_m3(&self) -> Option> { if self.any_ambiguous_type() == AnyAmbiguousAliases::M3 { self.any_ambiguous().map(|u| Monster::init_from_table(u)) } else {