Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct CovCov<'a>(fn(fn(&'a ()))); // Two wrongs make a right
- struct ConCon<'a>(fn(fn(fn(&'a ())))); // Three wrongs make a left
- fn verify_covariance<'a, 'b:'a>(c: CovCov<'b>) -> CovCov<'a> { c }
- fn verify_contravariance<'a, 'b:'a>(c: ConCon<'a>) -> ConCon<'b> { c }
- struct Struct;
- impl Struct {
- fn concon_mut(&mut self) -> ConCon { ConCon(|_| ()) }
- fn covcov_mut(&mut self) -> CovCov { CovCov(|_| ()) }
- }
- fn main() {
- let mut x = Struct;
- {
- // This is forbidden. `a` and `b` both borrow from `x`.
- let a = x.covcov_mut();
- let b = x.covcov_mut();
- }
- {
- // This is okay. `a` and `b` do not borrow from `x`.
- let _a = x.concon_mut();
- let _b = x.concon_mut();
- }
- }
Add Comment
Please, Sign In to add comment