Guest User

Untitled

a guest
Jul 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. use std::marker::PhantomData;
  2.  
  3. struct Qux<'a> {
  4. _used_lifetime: PhantomData<&'a str>,
  5. }
  6.  
  7. struct Bar<'a, S> {
  8. phantom: PhantomData<S>,
  9. qux: Qux<'a>,
  10. }
  11.  
  12. impl<'a, S> Bar<'a, S>
  13. where
  14. // How to create lifetime bound higher-rank trait bounds?
  15. S: for<'b, 'c> FooMaker<'b, 'c>,
  16. {
  17. fn new() -> Self {
  18. Bar {
  19. phantom: PhantomData,
  20. qux: Qux {
  21. _used_lifetime: PhantomData,
  22. }
  23. }
  24. }
  25. fn foo_method(&'a mut self) {
  26. S::Foo::new(&mut self.qux).do_stuff();
  27. S::Foo::new(&mut self.qux).do_stuff();
  28. }
  29. }
  30.  
  31. trait FooMaker<'a, 'b: 'a> {
  32. type Foo: Foo<'a, 'b>;
  33. }
  34.  
  35. trait Foo<'a, 'b: 'a> {
  36. fn new(qux: &'a mut Qux<'b>) -> Self;
  37. fn do_stuff(&mut self);
  38. }
  39.  
  40. struct FooAMaker {}
  41.  
  42. impl<'a, 'b: 'a> FooMaker<'a, 'b> for FooAMaker {
  43. type Foo = FooA<'a, 'b>;
  44. }
  45.  
  46. struct FooA<'a, 'b: 'a> {
  47. qux: &'a mut Qux<'b>,
  48. }
  49.  
  50. impl<'a, 'b> Foo<'a, 'b> for FooA<'a, 'b> {
  51. fn new(qux: &'a mut Qux<'b>) -> FooA<'a, 'b> {
  52. FooA {
  53. qux,
  54. }
  55. }
  56. fn do_stuff(&mut self) {
  57. }
  58. }
  59.  
  60. fn main() {
  61. let mut v = Vec::<u8>::new();
  62. Bar::<FooAMaker>::new(&mut v).foo_method();
  63. }
Add Comment
Please, Sign In to add comment