1 mod slice; 2 3 use core::iter::Peekable; 4 #[cfg(feature = "std")] 5 use core::mem; 6 #[cfg(feature = "std")] 7 use std::io::{Seek, SeekFrom}; 8 #[cfg(feature = "std")] 9 use std::sync::Arc; 10 11 use crate::task::Poll; 12 #[cfg(__standback_before_1_40)] 13 use crate::v1_40::Option_v1_40_; 14 #[cfg(__standback_before_1_50)] 15 use crate::v1_50::Bool_v1_50; 16 17 #[cfg(feature = "std")] 18 pub trait Arc_v1_51<T> { decrement_strong_count(ptr: *const T)19 unsafe fn decrement_strong_count(ptr: *const T); increment_strong_count(ptr: *const T)20 unsafe fn increment_strong_count(ptr: *const T); 21 } 22 23 #[cfg(feature = "std")] 24 impl<T> Arc_v1_51<T> for Arc<T> { 25 #[inline] decrement_strong_count(ptr: *const T)26 unsafe fn decrement_strong_count(ptr: *const T) { 27 drop(Arc::from_raw(ptr)); 28 } 29 30 #[inline] increment_strong_count(ptr: *const T)31 unsafe fn increment_strong_count(ptr: *const T) { 32 let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(ptr)); 33 let _arc_clone: mem::ManuallyDrop<_> = arc.clone(); 34 } 35 } 36 37 pub trait Peekable_v1_51<I: Iterator> { next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item>38 fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item>; next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item> where T: ?Sized, I::Item: PartialEq<T>39 fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item> 40 where 41 T: ?Sized, 42 I::Item: PartialEq<T>; 43 } 44 45 impl<I: Iterator> Peekable_v1_51<I> for Peekable<I> { next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item>46 fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item> { 47 func(self.peek()?).then(|| self.next()).flatten() 48 } 49 next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item> where T: ?Sized, I::Item: PartialEq<T>,50 fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item> 51 where 52 T: ?Sized, 53 I::Item: PartialEq<T>, 54 { 55 self.next_if(|next| next == expected) 56 } 57 } 58 59 #[cfg(feature = "std")] 60 pub trait Seek_v1_51 { stream_position(&mut self) -> std::io::Result<u64>61 fn stream_position(&mut self) -> std::io::Result<u64>; 62 } 63 64 #[cfg(feature = "std")] 65 impl<T: Seek> Seek_v1_51 for T { stream_position(&mut self) -> std::io::Result<u64>66 fn stream_position(&mut self) -> std::io::Result<u64> { 67 self.seek(SeekFrom::Current(0)) 68 } 69 } 70 71 pub trait Slice_v1_51<T> { fill_with<F>(&mut self, f: F) where F: FnMut() -> T72 fn fill_with<F>(&mut self, f: F) 73 where 74 F: FnMut() -> T; split_inclusive_mut<F>(&mut self, pred: F) -> slice::SplitInclusiveMut<'_, T, F> where F: FnMut(&T) -> bool75 fn split_inclusive_mut<F>(&mut self, pred: F) -> slice::SplitInclusiveMut<'_, T, F> 76 where 77 F: FnMut(&T) -> bool; split_inclusive<F>(&self, pred: F) -> slice::SplitInclusive<'_, T, F> where F: FnMut(&T) -> bool78 fn split_inclusive<F>(&self, pred: F) -> slice::SplitInclusive<'_, T, F> 79 where 80 F: FnMut(&T) -> bool; strip_prefix(&self, prefix: &[T]) -> Option<&[T]> where T: PartialEq81 fn strip_prefix(&self, prefix: &[T]) -> Option<&[T]> 82 where 83 T: PartialEq; strip_suffix(&self, suffix: &[T]) -> Option<&[T]> where T: PartialEq84 fn strip_suffix(&self, suffix: &[T]) -> Option<&[T]> 85 where 86 T: PartialEq; 87 } 88 89 impl<T> Slice_v1_51<T> for [T] { fill_with<F>(&mut self, mut f: F) where F: FnMut() -> T,90 fn fill_with<F>(&mut self, mut f: F) 91 where 92 F: FnMut() -> T, 93 { 94 for el in self { 95 *el = f(); 96 } 97 } 98 99 #[inline] split_inclusive_mut<F>(&mut self, pred: F) -> slice::SplitInclusiveMut<'_, T, F> where F: FnMut(&T) -> bool,100 fn split_inclusive_mut<F>(&mut self, pred: F) -> slice::SplitInclusiveMut<'_, T, F> 101 where 102 F: FnMut(&T) -> bool, 103 { 104 slice::SplitInclusiveMut::new(self, pred) 105 } 106 107 #[inline] split_inclusive<F>(&self, pred: F) -> slice::SplitInclusive<'_, T, F> where F: FnMut(&T) -> bool,108 fn split_inclusive<F>(&self, pred: F) -> slice::SplitInclusive<'_, T, F> 109 where 110 F: FnMut(&T) -> bool, 111 { 112 slice::SplitInclusive::new(self, pred) 113 } 114 115 #[must_use = "returns the subslice without modifying the original"] strip_prefix(&self, prefix: &[T]) -> Option<&[T]> where T: PartialEq,116 fn strip_prefix(&self, prefix: &[T]) -> Option<&[T]> 117 where 118 T: PartialEq, 119 { 120 let n = prefix.len(); 121 if n <= self.len() { 122 let (head, tail) = self.split_at(n); 123 if head == prefix { 124 return Some(tail); 125 } 126 } 127 None 128 } 129 130 #[must_use = "returns the subslice without modifying the original"] strip_suffix(&self, suffix: &[T]) -> Option<&[T]> where T: PartialEq,131 fn strip_suffix(&self, suffix: &[T]) -> Option<&[T]> 132 where 133 T: PartialEq, 134 { 135 let (len, n) = (self.len(), suffix.len()); 136 if n <= len { 137 let (head, tail) = self.split_at(len - n); 138 if tail == suffix { 139 return Some(head); 140 } 141 } 142 None 143 } 144 } 145 146 #[cfg(all(__standback_since_1_33, feature = "std"))] 147 pub trait Wake { wake(self: Arc<Self>)148 fn wake(self: Arc<Self>); 149 #[cfg(__standback_since_v1_41)] wake_by_ref(self: &Arc<Self>)150 fn wake_by_ref(self: &Arc<Self>) { 151 self.clone().wake(); 152 } 153 } 154 155 pub trait Integer_v1_51 { 156 type __StandbackUnsigned; unsigned_abs(self) -> Self::__StandbackUnsigned157 fn unsigned_abs(self) -> Self::__StandbackUnsigned; 158 } 159 160 macro_rules! impl_integer { 161 ($($int:ty => $uint:ty)*) => {$( 162 impl Integer_v1_51 for $int { 163 type __StandbackUnsigned = $uint; 164 #[inline] 165 fn unsigned_abs(self) -> Self::__StandbackUnsigned { 166 self.wrapping_abs() as $uint 167 } 168 } 169 )*}; 170 } 171 172 impl_integer! { 173 i8 => u8 174 i16 => u16 175 i32 => u32 176 i64 => u64 177 i128 => u128 178 } 179 180 pub trait Poll_v1_51<T, E> { map_ok<U, F>(self, f: F) -> Poll<Result<U, E>> where F: FnOnce(T) -> U181 fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>> 182 where 183 F: FnOnce(T) -> U; map_err<U, F>(self, f: F) -> Poll<Result<T, U>> where F: FnOnce(E) -> U184 fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>> 185 where 186 F: FnOnce(E) -> U; 187 } 188 189 impl<T, E> Poll_v1_51<T, E> for Poll<Result<T, E>> { map_ok<U, F>(self, f: F) -> Poll<Result<U, E>> where F: FnOnce(T) -> U,190 fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>> 191 where 192 F: FnOnce(T) -> U, 193 { 194 match self { 195 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))), 196 Poll::Ready(Err(e)) => Poll::Ready(Err(e)), 197 Poll::Pending => Poll::Pending, 198 } 199 } 200 map_err<U, F>(self, f: F) -> Poll<Result<T, U>> where F: FnOnce(E) -> U,201 fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>> 202 where 203 F: FnOnce(E) -> U, 204 { 205 match self { 206 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)), 207 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))), 208 Poll::Pending => Poll::Pending, 209 } 210 } 211 } 212