How can I specialize a trait for a specific type?

The code below reports maybe upstream crate will add FromStr for &str and refuses to compile.
I want to know is there some resolution to make the code compiled?

trait a<T> {                                                                                                                                                                                                                                                             
    fn parse(&self) -> Result<T, String>;                                                                                                                                                                                                                                
}                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                         
impl<T: FromStr> a<T> for OptVal                                                                                                                                                                                                                                         
where                                                                                                                                                                                                                                                                    
    T::Err: fmt::Display,                                                                                                                                                                                                                                                
{                                                                                                                                                                                                                                                                        
    fn parse(&self) -> Result<T, String> {                                                                                                                                                                                                                               
        let stat = self.stat.load(Ordering::Relaxed);                                                                                                                                                                                                                    
        if stat & 4 == 0 {                                                                                                                                                                                                                                               
            return Err("can't called before main".to_owned());                                                                                                                                                                                                           
        }                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                         
        if stat & 8 != 0 {                                                                                                                                                                                                                                               
            return Err("not in subcommand context".to_owned());                                                                                                                                                                                                          
        }                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                         
        match self.name().parse() {                                                                                                                                                                                                                                      
            Ok(v) => Ok(v),                                                                                                                                                                                                                                              
            Err(e) => Err(format!("{}", e)),                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                                                
    }                                                                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                         
impl a<&'static str> for OptVal {                                                                                                                                                                                                                                        
    fn parse(&self) -> Result<&'static str, String> {                                                                                                                                                                                                                    
        let stat = self.stat.load(Ordering::Relaxed);                                                                                                                                                                                                                    
        if stat & 4 == 0 {                                                                                                                                                                                                                                               
            return Err("can't called before main".to_owned());                                                                                                                                                                                                           
        }                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                         
        if stat & 8 != 0 {                                                                                                                                                                                                                                               
            return Err("not in subcommand context".to_owned());                                                                                                                                                                                                          
        }                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                         
        Ok(self.name())                                                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                                                                    
}                                     

You can't do this.

1 Like

This is called "partial specialization" and it's still experimental(for almost 6 years), you can try it on nightly versions. Check this

This is explicitly prohibited due to something called the "Orphan Runes".

This README does a pretty good job of explaining the situation:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.