将Rust中的枚举类型转换为字符串,不使用std或fmt。

huangapple go评论68阅读模式
英文:

rust wasm convert enum type to string without std or fmt

问题

Is there a stable way to convert rust enum types to strings (and from strings to enum types) without the use of std or fmt?

The reason I am asking is because std appears to account for and 'bloat' the final binary size by around 33% (using cargo bloat) - leaving me with an executable almost 1MB in size for some very simple code.

Note I've already done all cargo.toml profile.release optimizations as outlined here: https://rustwasm.github.io/docs/book/reference/code-size.html

英文:

Is there a stable way to convert rust enum types to strings (and from strings to enum types) without the use of std or fmt?

The reason I am asking is because std appears to account for and 'bloat' the final binary size by around 33% (using cargo bloat) - leaving me with an executable almost 1MB in size for some very simple code.

Note I've already done all cargo.toml profile.release optimizations as outlined here: https://rustwasm.github.io/docs/book/reference/code-size.html

答案1

得分: 0

Sure, here's the translated code portion:

也许这有点傻,但我刚刚去掉了std/fmt,并在枚举上使用了impl,然后做了相同的事情。二进制大小尚未减小,但我尚未从所有依赖项中删除所有std / fmt。让我感到傻,因为我一开始就使用std / fmt,因为无论哪种方式,都需要相同的工作量。我认为唯一的区别是,如果我的MYType枚举在&format!宏内部使用,我只需要调用.to_string()

pub enum MyType {A, B, /*等等...*/ }

impl MyEnumType 
{
    pub fn print(&self)
    {
        match self 
        {
            MyEnumType::A => web_sys::console::log_1("a"),
            MyEnumType::B => web_sys::console::log_1("b"),
            /*等等...*/ 
        }
    }

    pub fn from_str(input: &String) -> Result<MyEnumType, String> 
    {
        match input.as_str() 
        {
            "A" => Ok(MyEnumType::A),
            "a" => Ok(MyEnumType::A),
            "B" => Ok(MyEnumType::B),
            "b" => Ok(MyEnumType::B),
            /*等等*/ 
            _ => Err("bad string".to_string()),
        }
    }

    pub fn to_string(&self) -> String 
    {
        match self 
        {			
            MyEnumType::A => "a".to_string(), 	
            MyEnumType::B => "b".to_string(), 	
            /*等等...*/
        }
    }
}

Please note that I've removed the HTML entities and made the necessary adjustments to the code for clarity.

英文:

Maybe this is silly but I've just took out std/fmt and used impl on the enum and did the same thing. binary size hasn't decreased yet but I haven't removed all std / fmt from all dependencies yet. Makes me feel silly for using std / fmt in the first place since it's the same amount of work either way. I think the only difference is if my MYType enum were used inside the &format! macro I'd just need to call .to_string()

pub enum MyType {A, B, /*etc...*/ }

impl MyEnumType 
{
   pub fn print(&amp;self)
   {
      match &amp;self 
      {
         MyEnumType::A =&gt; web_sys::console::log_1(&quot;a&quot;),
         MyEnumType::B =&gt; web_sys::console::log_1(&quot;b&quot;),
         /*etc... */ 
      }
   }

   pub fn from_str(input: &amp;String) -&gt; Result&lt;MyEnumType, String&gt; 
   {
      match input.as_str() 
      {
        &quot;A&quot; =&gt; Ok(MyEnumType::A),
        &quot;a&quot; =&gt; Ok(MyEnumType::A),
        &quot;B&quot; =&gt; Ok(MyEnumType::B),
        &quot;b&quot; =&gt; Ok(MyEnumType::B),
        /* etc*/ 
         _  =&gt; Err(&quot;bad string&quot;.to_string()),
	  }
    }

    pub fn to_string(&amp;self) -&gt; String 
    {
      match &amp;self 
      {			
	    MyEnumType::A =&gt; &quot;a&quot;.to_string(), 	
        MyEnumType::B =&gt; &quot;b&quot;.to_string(), 	
        /* etc ... */
      }
    }
  }

huangapple
  • 本文由 发表于 2023年5月10日 22:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定