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?