Skip to content

Instantly share code, notes, and snippets.

@little-dude
Created February 1, 2022 11:30
Show Gist options
  • Save little-dude/ab57e5b6f13a103b4005ae59d8ae16ad to your computer and use it in GitHub Desktop.
Save little-dude/ab57e5b6f13a103b4005ae59d8ae16ad to your computer and use it in GitHub Desktop.
use std::convert::Into;
use std::error::Error;
pub trait MyTrait<T> {
type E: Into<Box<dyn Error>>;
fn method(&self, _: T);
fn boxed(self) -> Box<dyn MyTrait<T, E = Self::E>>
where
Self: Sized + 'static,
{
Box::new(self)
}
}
impl<T, E> MyTrait<T> for Box<dyn MyTrait<T, E = E>>
where
E: Into<Box<dyn Error>>,
{
type E = E;
fn method(&self, t: T) {
(**self).method(t)
}
fn boxed(self) -> Box<dyn MyTrait<T, E = E>> {
self
}
}
pub trait MyTraitRefInput<T>: for<'t> MyTrait<&'t T> {
fn boxed(self) -> Box<dyn for<'t> MyTrait<&'t T, E = <Self as MyTrait<&'t T>>::E>>
where
Self: Sized + 'static,
{
Box::new(self)
}
}
struct Wrapper<T, E>(Box<dyn for<'t> MyTrait<&'t T, E = E>>);
impl<'t, T, E> MyTrait<&'t T> for Wrapper<T, E>
where
E: Into<Box<dyn Error>>,
{
type E = E;
fn method(&self, t: &'t T) {
self.0.method(t)
}
fn boxed(self) -> Box<dyn for<'a> MyTrait<&'a T, E = Self::E>> {
self.0
}
}
impl<E, T> MyTraitRefInput<T> for Wrapper<T, E>
where
E: Into<Box<dyn Error>>,
{
fn boxed(self) -> Box<dyn for<'t> MyTrait<&'t T, E = E>> {
self.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment