Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. pub trait AssocRefIterPart {
  2. type Item;
  3. type Capture: ?Sized;
  4.  
  5. fn next<'a>(&mut self, assoc: &'a Self::Capture) -> Option<&'a Self::Item>;
  6. }
  7. pub struct AssocRefIter<'a, T, P>
  8. where
  9. T: ?Sized,
  10. {
  11. inner: &'a T,
  12. part: P,
  13. }
  14.  
  15. impl<'a, T, P> AssocRefIter<'a, T, P>
  16. where
  17. T: ?Sized,
  18. {
  19. pub fn new(inner: &'a T, part: P) -> Self {
  20. Self { inner, part }
  21. }
  22. }
  23.  
  24. impl<'a, T, P> Iterator for AssocRefIter<'a, T, P>
  25. where
  26. T: ?Sized,
  27. P: 'a + AssocRefIterPart<Capture = T>,
  28. {
  29. type Item = &'a P::Item;
  30. fn next(&mut self) -> Option<Self::Item> {
  31. self.part.next(self.inner)
  32. }
  33. }
  34. pub trait ServerExample<ClientId> {
  35. type ClientsPart: AssocRefIterPart<Item = ClientId, Capture = Self>;
  36. fn clients<'a>(&'a self) -> AssocRefIter<'a, Self, Self::ClientsPart>;
  37. }
  38.  
  39. pub struct ListOf64 {
  40. v: Vec<u64>,
  41. }
  42.  
  43. pub struct LO64IterPart {
  44. pos: std::ops::Range<usize>,
  45. }
  46.  
  47. impl AssocRefIterPart for LO64IterPart {
  48. type Item = u64;
  49. type Capture = ListOf64;
  50.  
  51. fn next<'a>(&mut self, assoc: &'a Self::Capture) -> Option<&'a Self::Item> {
  52. unsafe { Some(assoc.v.get_unchecked(self.pos.next()?)) }
  53. }
  54. }
  55.  
  56. impl ServerExample<u64> for ListOf64 {
  57. type ClientsPart = LO64IterPart;
  58. fn clients<'a>(&'a self) -> AssocRefIter<'a, Self, Self::ClientsPart> {
  59. AssocRefIter::new(
  60. self,
  61. LO64IterPart {
  62. pos: 0..self.v.len(),
  63. },
  64. )
  65. }
  66. }
  67.  
  68. fn main() {
  69. let k = ListOf64 { v: vec![1, 2, 3, 4, 8, 7, 6, 5] };
  70. for x in k.clients() {
  71. println!("{}", x);
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement