Skip to content

Instantly share code, notes, and snippets.

@cynecx
Created June 6, 2022 14:20
Show Gist options
  • Select an option

  • Save cynecx/46959cafe8a81217acea62c6ec803fe1 to your computer and use it in GitHub Desktop.

Select an option

Save cynecx/46959cafe8a81217acea62c6ec803fe1 to your computer and use it in GitHub Desktop.
Somekind of specialization in nightly using negative_impls + with_negative_coherence
#![feature(negative_impls, with_negative_coherence)]
#[derive(Debug)]
struct A;
#[derive(Debug)]
struct B;
#[derive(Debug)]
enum Foo {
A(A),
B(B),
}
impl From<A> for Foo {
fn from(val: A) -> Self {
Self::A(val)
}
}
impl From<B> for Foo {
fn from(val: B) -> Self {
Self::B(val)
}
}
#[derive(Debug)]
enum R<T> {
Foo(Foo),
Bar(T),
}
trait C {}
trait NotC {}
impl<T: NotC> !C for T {}
impl<T: C> !NotC for T {}
#[derive(Debug)]
struct C1;
impl C for C1 {}
#[derive(Debug)]
struct C2;
impl C for C2 {}
impl<T> NotC for T where Foo: From<T> {}
impl<V: NotC, T: C> From<V> for R<T>
where
Foo: From<V>,
{
fn from(val: V) -> Self {
Self::Foo(Foo::from(val))
}
}
impl<T: C> From<T> for R<T> {
fn from(val: T) -> Self {
Self::Bar(val)
}
}
fn test(v: usize) -> Result<usize, R<C1>> {
match v {
0 => Err(A)?,
1 => Err(B)?,
2 => Err(C1)?,
_ => Ok(0)
}
}
fn main() {
dbg!(test(0));
dbg!(test(1));
dbg!(test(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment