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

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

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()

  1. pub enum MyType {A, B, /*等等...*/ }
  2. impl MyEnumType
  3. {
  4. pub fn print(&self)
  5. {
  6. match self
  7. {
  8. MyEnumType::A => web_sys::console::log_1("a"),
  9. MyEnumType::B => web_sys::console::log_1("b"),
  10. /*等等...*/
  11. }
  12. }
  13. pub fn from_str(input: &String) -> Result<MyEnumType, String>
  14. {
  15. match input.as_str()
  16. {
  17. "A" => Ok(MyEnumType::A),
  18. "a" => Ok(MyEnumType::A),
  19. "B" => Ok(MyEnumType::B),
  20. "b" => Ok(MyEnumType::B),
  21. /*等等*/
  22. _ => Err("bad string".to_string()),
  23. }
  24. }
  25. pub fn to_string(&self) -> String
  26. {
  27. match self
  28. {
  29. MyEnumType::A => "a".to_string(),
  30. MyEnumType::B => "b".to_string(),
  31. /*等等...*/
  32. }
  33. }
  34. }

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()

  1. pub enum MyType {A, B, /*etc...*/ }
  2. impl MyEnumType
  3. {
  4. pub fn print(&amp;self)
  5. {
  6. match &amp;self
  7. {
  8. MyEnumType::A =&gt; web_sys::console::log_1(&quot;a&quot;),
  9. MyEnumType::B =&gt; web_sys::console::log_1(&quot;b&quot;),
  10. /*etc... */
  11. }
  12. }
  13. pub fn from_str(input: &amp;String) -&gt; Result&lt;MyEnumType, String&gt;
  14. {
  15. match input.as_str()
  16. {
  17. &quot;A&quot; =&gt; Ok(MyEnumType::A),
  18. &quot;a&quot; =&gt; Ok(MyEnumType::A),
  19. &quot;B&quot; =&gt; Ok(MyEnumType::B),
  20. &quot;b&quot; =&gt; Ok(MyEnumType::B),
  21. /* etc*/
  22. _ =&gt; Err(&quot;bad string&quot;.to_string()),
  23. }
  24. }
  25. pub fn to_string(&amp;self) -&gt; String
  26. {
  27. match &amp;self
  28. {
  29. MyEnumType::A =&gt; &quot;a&quot;.to_string(),
  30. MyEnumType::B =&gt; &quot;b&quot;.to_string(),
  31. /* etc ... */
  32. }
  33. }
  34. }

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:

确定