Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. use std::{fmt::Display, ops::{Deref, DerefMut, Index, IndexMut}, slice::SliceIndex, sync::Arc};
  2.  
  3. fn main() {
  4. struct NoisyDrop<T: Display>(T);
  5.  
  6. impl<T: Display> Drop for NoisyDrop<T> {
  7. fn drop(&mut self) {
  8. println!("I'm dropping this: {}!!!", self.0);
  9. }
  10. }
  11.  
  12. let v = ["apotheke", "potheke", "otheke", "theke", "heke", "eke", "ke", "e"].iter()
  13. .map(|s| NoisyDrop(s.to_owned())).collect::<Vec<_>>();
  14.  
  15. let (left,right) = v.split_into(4);
  16. let (left, middle) = left.split_into(2);
  17.  
  18. println!("{}", left[0].0);
  19. for s in middle {
  20. println!("{}", s.0);
  21. }
  22. println!("{}", right[0].0);
  23. }
  24.  
  25. trait SplitExt {
  26. type Shard;
  27.  
  28. /// Split this array into two shards at the given index
  29. fn split_into(self, at: usize) -> (Self::Shard, Self::Shard);
  30. }
  31.  
  32. /// The raw guts of a Vec<T>
  33. struct VecDropper<T> {
  34. ptr: *mut T,
  35. length: usize,
  36. capacity: usize,
  37. }
  38.  
  39. impl<T> Drop for VecDropper<T> {
  40. fn drop(&mut self) {
  41. unsafe {
  42. std::mem::drop(Vec::from_raw_parts(self.ptr, self.length, self.capacity));
  43. }
  44. }
  45. }
  46.  
  47. pub struct VecShard<T> {
  48. dropper: Arc<VecDropper<T>>,
  49.  
  50. data: *mut T,
  51. len: usize,
  52. }
  53.  
  54. impl<T> SplitExt for VecShard<T> {
  55. type Shard = VecShard<T>;
  56.  
  57. fn split_into(self, at: usize) -> (Self::Shard, Self::Shard) {
  58. assert!(at <= self.len);
  59.  
  60. let left = VecShard {
  61. dropper: self.dropper.clone(),
  62. data: self.data,
  63. len: at,
  64. };
  65.  
  66. let right = VecShard {
  67. dropper: self.dropper,
  68. data: unsafe{ self.data.add(at) },
  69. len: self.len - at,
  70. };
  71.  
  72. (left, right)
  73. }
  74. }
  75.  
  76. impl<T> Deref for VecShard<T> {
  77. type Target = [T];
  78.  
  79. fn deref(&self) -> &[T] {
  80. unsafe { std::slice::from_raw_parts(self.data, self.len) }
  81. }
  82. }
  83.  
  84. impl<T> DerefMut for VecShard<T> {
  85. fn deref_mut(&mut self) -> &mut [T] {
  86. unsafe { std::slice::from_raw_parts_mut(self.data, self.len) }
  87. }
  88. }
  89.  
  90. impl<T, I: SliceIndex<[T]>> Index<I> for VecShard<T> {
  91. type Output = <I as std::slice::SliceIndex<[T]>>::Output;
  92.  
  93. fn index(&self, idx: I) -> &Self::Output {
  94. &((**self)[idx])
  95. }
  96. }
  97.  
  98. impl<T, I: SliceIndex<[T]>> IndexMut<I> for VecShard<T> {
  99. fn index_mut(&mut self, idx: I) -> &mut Self::Output {
  100. &mut((**self)[idx])
  101. }
  102. }
  103.  
  104. impl<T> Iterator for VecShard<T> {
  105. type Item = T;
  106.  
  107. fn next(&mut self) -> Option<T> {
  108. // oh yes, very clever
  109. if self.len > 0 {
  110. let res = unsafe { self.data.read() };
  111. self.len -= 1;
  112. self.data = unsafe { self.data.add(1) };
  113. Some(res)
  114. } else {
  115. None
  116. }
  117. }
  118. }
  119.  
  120. impl<T> From<Vec<T>> for VecShard<T> {
  121. fn from(mut v: Vec<T>) -> Self {
  122. let res = VecShard {
  123. dropper: Arc::new(VecDropper {
  124. ptr: v.as_mut_ptr(),
  125. length: v.len(),
  126. capacity: v.capacity(),
  127. }),
  128. data: v.as_mut_ptr(),
  129. len: v.len(),
  130. };
  131. std::mem::forget(v);
  132. res
  133. }
  134. }
  135.  
  136. impl<T> SplitExt for Vec<T> {
  137. type Shard = VecShard<T>;
  138.  
  139. fn split_into(self, at: usize) -> (Self::Shard, Self::Shard) {
  140. VecShard::from(self).split_into(at)
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement