Guest User

Untitled

a guest
Apr 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. struct CovCov<'a>(fn(fn(&'a ()))); // Two wrongs make a right
  2. struct ConCon<'a>(fn(fn(fn(&'a ())))); // Three wrongs make a left
  3.  
  4. fn verify_covariance<'a, 'b:'a>(c: CovCov<'b>) -> CovCov<'a> { c }
  5. fn verify_contravariance<'a, 'b:'a>(c: ConCon<'a>) -> ConCon<'b> { c }
  6.  
  7. struct Struct;
  8. impl Struct {
  9. fn concon_mut(&mut self) -> ConCon { ConCon(|_| ()) }
  10. fn covcov_mut(&mut self) -> CovCov { CovCov(|_| ()) }
  11. }
  12.  
  13. fn main() {
  14. let mut x = Struct;
  15.  
  16. {
  17. // This is forbidden. `a` and `b` both borrow from `x`.
  18. let a = x.covcov_mut();
  19. let b = x.covcov_mut();
  20. }
  21.  
  22. {
  23. // This is okay. `a` and `b` do not borrow from `x`.
  24. let _a = x.concon_mut();
  25. let _b = x.concon_mut();
  26. }
  27. }
Add Comment
Please, Sign In to add comment