Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. trait FnLt<'a> {
  2. fn apply(self, input: &'a u8) -> &'a u8;
  3. }
  4.  
  5. // Struct impl
  6. struct Foo;
  7. impl<'a> FnLt<'a> for Foo {
  8. fn apply(self, input: &'a u8) -> &'a u8 {
  9. input
  10. }
  11. }
  12.  
  13. // Closure impl
  14. impl<'a, T> FnLt<'a> for T
  15. where
  16. T: FnOnce(&'a u8) -> &'a u8,
  17. {
  18. fn apply(self, input: &'a u8) -> &'a u8 {
  19. (self)(input)
  20. }
  21. }
  22.  
  23. fn take_fn_lt(_: impl for<'a> FnLt<'a>) {}
  24.  
  25. fn fix<F: FnOnce(&u8) -> &u8>(x: F) -> F {
  26. x
  27. }
  28.  
  29. fn main() {
  30. take_fn_lt(Foo); // Works
  31.  
  32. fn foo(x: &u8) -> &u8 { x }
  33. take_fn_lt(foo); // Works
  34.  
  35. //take_fn_lt(|x: &u8| -> &u8 { x }); // Doesn't work
  36. take_fn_lt(fix(|x: &u8| -> &u8 { x })); // Doesn work
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement