This is an automated email from the ASF dual-hosted git repository. jking pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/thrift.git
The following commit(s) were added to refs/heads/master by this push: new 6436899 THRIFT-4662: Rust const string calls function at compile time 6436899 is described below commit 6436899e4723d4a22eac107041019aa981226237 Author: jake-ruyi <j...@playruyi.com> AuthorDate: Wed Nov 7 17:18:03 2018 +0800 THRIFT-4662: Rust const string calls function at compile time Client: rs --- compiler/cpp/src/thrift/generate/t_rs_generator.cc | 37 ++++++++++++++++++---- lib/rs/test/thrifts/Base_One.thrift | 3 ++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/compiler/cpp/src/thrift/generate/t_rs_generator.cc b/compiler/cpp/src/thrift/generate/t_rs_generator.cc index 5cd67f6..a01c626 100644 --- a/compiler/cpp/src/thrift/generate/t_rs_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_rs_generator.cc @@ -127,7 +127,7 @@ private: void render_const_value_holder(const string& name, t_type* ttype, t_const_value* tvalue); // Write the actual const value - the right side of a const definition. - void render_const_value(t_type* ttype, t_const_value* tvalue); + void render_const_value(t_type* ttype, t_const_value* tvalue, bool is_owned = true); // Write a const struct (returned from `const_value` method). void render_const_struct(t_type* ttype, t_const_value* tvalue); @@ -411,6 +411,9 @@ private: // Return a string representing the rust type given a `t_type`. string to_rust_type(t_type* ttype, bool ordered_float = true); + // Return a string representing the `const` rust type given a `t_type` + string to_rust_const_type(t_type* ttype, bool ordered_float = true); + // Return a string representing the rift `protocol::TType` given a `t_type`. string to_rust_field_type_enum(t_type* ttype); @@ -645,8 +648,8 @@ void t_rs_generator::render_const_value(const string& name, t_type* ttype, t_con throw "cannot generate simple rust constant for " + ttype->get_name(); } - f_gen_ << "pub const " << rust_upper_case(name) << ": " << to_rust_type(ttype) << " = "; - render_const_value(ttype, tvalue); + f_gen_ << "pub const " << rust_upper_case(name) << ": " << to_rust_const_type(ttype) << " = "; + render_const_value(ttype, tvalue, false); f_gen_ << ";" << endl; f_gen_ << endl; } @@ -673,15 +676,22 @@ void t_rs_generator::render_const_value_holder(const string& name, t_type* ttype f_gen_ << endl; } -void t_rs_generator::render_const_value(t_type* ttype, t_const_value* tvalue) { +void t_rs_generator::render_const_value(t_type* ttype, t_const_value* tvalue, bool is_owned) { if (ttype->is_base_type()) { t_base_type* tbase_type = (t_base_type*)ttype; switch (tbase_type->get_base()) { case t_base_type::TYPE_STRING: if (tbase_type->is_binary()) { - f_gen_ << "\"" << tvalue->get_string() << "\""<< ".to_owned().into_bytes()"; + if (is_owned) { + f_gen_ << "\"" << tvalue->get_string() << "\""<< ".to_owned().into_bytes()"; + } else { + f_gen_ << "b\"" << tvalue->get_string() << "\""; + } } else { - f_gen_ << "\"" << tvalue->get_string() << "\""<< ".to_owned()"; + f_gen_ << "\"" << tvalue->get_string() << "\""; + if (is_owned) { + f_gen_ << ".to_owned()"; + } } break; case t_base_type::TYPE_BOOL: @@ -3039,6 +3049,21 @@ string t_rs_generator::to_rust_type(t_type* ttype, bool ordered_float) { throw "cannot find rust type for " + ttype->get_name(); } +string t_rs_generator::to_rust_const_type(t_type* ttype, bool ordered_float) { + if (ttype->is_base_type()) { + t_base_type* tbase_type = ((t_base_type*)ttype); + if (tbase_type->get_base() == t_base_type::TYPE_STRING) { + if (tbase_type->is_binary()) { + return "&[u8]"; + } else { + return "&str"; + } + } + } + + return to_rust_type(ttype, ordered_float); +} + string t_rs_generator::to_rust_field_type_enum(t_type* ttype) { ttype = get_true_type(ttype); if (ttype->is_base_type()) { diff --git a/lib/rs/test/thrifts/Base_One.thrift b/lib/rs/test/thrifts/Base_One.thrift index 3da083d..c5fa6c2 100644 --- a/lib/rs/test/thrifts/Base_One.thrift +++ b/lib/rs/test/thrifts/Base_One.thrift @@ -37,6 +37,9 @@ const list<double> CommonTemperatures = [300.0, 450.0] const double MealsPerDay = 2.5; +const string DefaultRecipeName = "Soup-rise of the Day" +const binary DefaultRecipeBinary = "Soup-rise of the 01010101" + struct Noodle { 1: string flourType 2: Temperature cookTemp