英文:
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(&self)
   {
      match &self 
      {
         MyEnumType::A => web_sys::console::log_1("a"),
         MyEnumType::B => web_sys::console::log_1("b"),
         /*etc... */ 
      }
   }
   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),
        /* etc*/ 
         _  => Err("bad string".to_string()),
	  }
    }
    pub fn to_string(&self) -> String 
    {
      match &self 
      {			
	    MyEnumType::A => "a".to_string(), 	
        MyEnumType::B => "b".to_string(), 	
        /* etc ... */
      }
    }
  }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论