Specialization in rust

Trait:

pub trait IntoConfig<T, U>
    where T: AsRef<[u8]> + AsRef<Path>,
          U: IntoIterator<Item=T>,
          <Self as IntoConfig<T, U>>::Matches: AsRef<[u8]>,
          <Self as IntoConfig<T, U>>::Path: AsRef<Path>
{
    type Path;
    type Matches;
    type Error;
    fn to_config(self) -> Result<Config<Self::Path, Self::Matches>, Self::Error>;
}

I needed to make optimized implementations for my config creator trait. But specialization in the rust
works very poorly with associated types.

my realisation:

auto trait NonOptConfGen {}
impl<T> ! NonOptConfGen for &[T] {}
impl<T, U> IntoConfig<T, U> for U
    where T: AsRef<[u8]> + AsRef<Path> + AsRef<str>,
          U: IntoIterator<Item=T>,
          Self: NonOptConfGen 
{
    type Path = T;
    type Matches = String;
    type Error = &'static str;

    fn to_config(self) -> Result<Config<Self::Path, Self::Matches>, Self::Error> {
  ...
    }

}
impl<'a, T> IntoConfig<&'a T, &'a [T]> for &'a [T]
    where T: AsRef<[u8]> + AsRef<Path> + AsRef<str>,

{
    type Path = &'a T;
    type Matches = &'a str;
    type Error = &'static str;

    fn to_config(self) -> Result<Config<Self::Path, Self::Matches>, Self::Error> {
     ...
    }
}

but when I tried to introduce the second level of Specialization,everything
became much compliacated due to the lack of "negative bound" in the language.

My decision:

auto trait NonOptConfGenT1 {}
auto trait NonOptConfGen {}
impl<T> ! NonOptConfGen for &[T] {}
impl<T: Copy> ! NonOptConfGenT1 for &[T] {}

and

impl<T, U> IntoConfig<T, U> for U
    where T: AsRef<[u8]> + AsRef<Path> + AsRef<str>,
          U: IntoIterator<Item=T>,
          Self: NonOptConfGen + NonOptConfGenT1

,

impl<'a, T> IntoConfig<&'a T, &'a [T]> for &'a [T]
    where T: AsRef<[u8]> + AsRef<Path> + AsRef<str>,
          Self: NonOptConfGenT1

As for me , my decision is suboptimal. What else can be done?

Cross post I saw on SO today:

As a result, I solved the problem without using specialization or something like that. I changed the approach to implementation, I changed the input arguments and their analysis in the function

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.