[ 
https://issues.apache.org/jira/browse/THRIFT-4662?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16683755#comment-16683755
 ] 

ASF GitHub Bot commented on THRIFT-4662:
----------------------------------------

jeking3 closed pull request #1624: THRIFT-4662: Rust const string calls 
function at compile time
URL: https://github.com/apache/thrift/pull/1624
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/compiler/cpp/src/thrift/generate/t_rs_generator.cc 
b/compiler/cpp/src/thrift/generate/t_rs_generator.cc
index dc11fd3ee2..eccdd275e0 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 @@ class t_rs_generator : public t_generator {
   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 @@ class t_rs_generator : public t_generator {
   // 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 3da083d451..c5fa6c20df 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


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Rust const string calls function at compile time
> ------------------------------------------------
>
>                 Key: THRIFT-4662
>                 URL: https://issues.apache.org/jira/browse/THRIFT-4662
>             Project: Thrift
>          Issue Type: Bug
>          Components: Rust - Compiler
>    Affects Versions: 0.11.0
>         Environment: C:\Users\jake>rustup show
> Default host: x86_64-pc-windows-msvc
> stable-x86_64-pc-windows-msvc (default)
> rustc 1.30.0 (da5f414c2 2018-10-24)
>            Reporter: J W
>            Assignee: Allen George
>            Priority: Major
>             Fix For: 1.0
>
>
> *For this thrift:*
> const string broker_playback_message = "mmi.developer.playback"
> *Generates:*
> // thrift -gen rs -out ../rust/thrift/src const_string.thrift
> pub const BROKER_PLAYBACK_MESSAGE: String = 
> "mmi.developer.playback".to_owned();
> *Fails to compile:*
> error[E0015]: calls in constants are limited to tuple structs and tuple 
> variants
> note: a limited form of compile-time function evaluation is available on a 
> nightly compiler via `const fn`
> *Fix:*
> Probably want to output:
> pub const BROKER_PLAYBACK_MESSAGE: &str = "mmi.developer.playback";
>  
> Looking at render_const_value() it looks like byte arrays will have the same 
> issue.
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to