Skip to content

Instantly share code, notes, and snippets.

@TethysSvensson
Created June 19, 2016 11:11
Show Gist options
  • Save TethysSvensson/22da19effdac4532dee6528d74f480cd to your computer and use it in GitHub Desktop.
Save TethysSvensson/22da19effdac4532dee6528d74f480cd to your computer and use it in GitHub Desktop.
use std::ops::Add;
trait X<Rhs, Output>
where Self: Add<Rhs, Output = Output>,
Self: for<'rhs> Add<&'rhs Rhs, Output = Output>
{}
// First try, does not work
trait Y1 { fn foo(&self); }
trait Z1: Sized
where for<'a> &'a Self: X<Self, Self>
{}
// error: the trait bound `for<'a> &'a T: X<T, T>` is not satisfied
// impl<T: Z1> Y1 for T {}
// Second try, add bound is not there
trait Y2 { fn foo(&self); }
trait Z2<'a>: Sized+ 'a
where &'a Self: X<Self, Self>
{}
impl<T: for<'a> Z2<'a>> Y2 for T {
fn foo(&self) {
// error: binary operation `+` cannot be applied to type `&T`
// note: an implementation of `std::ops::Add` might be missing for `&T`
// let x = self + self;
}
}
// Third try, this works:
trait Y3 { fn foo(&self); }
trait Z3: Sized
where for<'a> &'a Self: X<Self, Self> {}
impl<T: Z3> Y3 for T
where for<'a> &'a T: X<T, T> {
fn foo(&self) {
let x = self + self;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment