Guest User

Untitled

a guest
Jul 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. use std::marker::PhantomData;
  2.  
  3. struct Bar<'a, S> {
  4. phantom: PhantomData<S>,
  5. data: &'a mut Vec<u8>,
  6. }
  7.  
  8. impl<'a, S> Bar<'a, S>
  9. where
  10. S: Foo<'a>,
  11. {
  12. fn new(data: &'a mut Vec<u8>) -> Self {
  13. Bar {
  14. phantom: PhantomData,
  15. data,
  16. }
  17. }
  18. fn foo_method(&mut self) {
  19. S::new().do_stuff(&mut self.data);
  20. }
  21. }
  22.  
  23. trait Foo<'a> {
  24. fn new() -> Self;
  25. fn do_stuff(&self, _name: &'a mut Vec<u8>);
  26. }
  27.  
  28. struct FooA {}
  29.  
  30. impl<'a> Foo<'a> for FooA {
  31. fn new() -> FooA {
  32. FooA {}
  33. }
  34. fn do_stuff(&self, _name: &'a mut Vec<u8>) {}
  35. }
  36.  
  37. fn main() {
  38. let mut v = Vec::<u8>::new();
  39. Bar::<FooA>::new(&mut v).foo_method();
  40. }
Add Comment
Please, Sign In to add comment