1 use core::cell::Cell;
2 use core::ops::{Bound, RangeBounds};
3 use core::ptr;
4 
5 use crate::traits::Sealed;
6 
7 pub trait Cell_v1_37<T>: Sealed<Cell<T>> {
8     fn from_mut(t: &mut T) -> &Cell<T>;
9 }
10 
11 impl<T> Cell_v1_37<T> for Cell<T> {
12     #[inline]
new(data: *const (), vtable: &'static RawWakerVTable) -> RawWaker13     fn from_mut(t: &mut T) -> &Cell<T> {
14         unsafe { &*(t as *mut T as *const Cell<T>) }
15     }
16 }
17 
18 pub trait Cell_v1_37_<T>: Sealed<Cell<[T]>> {
19     fn as_slice_of_cells(&self) -> &[Cell<T>];
20 }
21 
22 impl<T> Cell_v1_37_<T> for Cell<[T]> {
23     fn as_slice_of_cells(&self) -> &[Cell<T>] {
24         unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
25     }
26 }
new( clone: unsafe fn(*const ()) -> RawWaker, wake: unsafe fn(*const ()), wake_by_ref: unsafe fn(*const ()), drop: unsafe fn(*const ()), ) -> Self27 
28 pub trait Option_v1_37<T>: Sealed<Option<T>> {
29     fn xor(self, optb: Option<T>) -> Option<T>;
30 }
31 
32 impl<T> Option_v1_37<T> for Option<T> {
33     #[inline]
34     fn xor(self, optb: Option<T>) -> Option<T> {
35         match (self, optb) {
36             (Some(a), None) => Some(a),
37             (None, Some(b)) => Some(b),
38             _ => None,
39         }
40     }
41 }
42 
43 pub trait Slice_v1_37<T>: Sealed<[T]> {
44     fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize)
45     where
46         T: Copy;
47 }
48 
from_waker(waker: &'a Waker) -> Self49 impl<T> Slice_v1_37<T> for [T] {
50     fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize)
51     where
52         T: Copy,
53     {
54         let src_start = match src.start_bound() {
55             Bound::Included(&n) => n,
56             Bound::Excluded(&n) => n
57                 .checked_add(1)
58                 .unwrap_or_else(|| slice_index_overflow_fail()),
59             Bound::Unbounded => 0,
60         };
61         let src_end = match src.end_bound() {
62             Bound::Included(&n) => n
63                 .checked_add(1)
64                 .unwrap_or_else(|| slice_index_overflow_fail()),
65             Bound::Excluded(&n) => n,
66             Bound::Unbounded => self.len(),
67         };
68         assert!(src_start <= src_end, "src end is before src start");
69         assert!(src_end <= self.len(), "src is out of bounds");
70         let count = src_end - src_start;
71         assert!(dest <= self.len() - count, "dest is out of bounds");
72         unsafe {
73             ptr::copy(
74                 self.as_ptr().add(src_start),
75                 self.as_mut_ptr().add(dest),
76                 count,
77             );
78         }
79     }
80 }
wake(self)81 
82 #[inline(never)]
83 #[cold]
84 fn slice_index_overflow_fail() -> ! {
85     panic!("attempted to index slice up to maximum usize");
86 }
87 
88 pub trait DoubleEndedIterator_v1_37: DoubleEndedIterator {
89     fn nth_back(&mut self, n: usize) -> Option<Self::Item>;
90 }
wake_by_ref(&self)91 
92 impl<Iter: DoubleEndedIterator> DoubleEndedIterator_v1_37 for Iter {
93     #[inline]
94     fn nth_back(&mut self, mut n: usize) -> Option<Self::Item> {
95         for x in self.rev() {
96             if n == 0 {
97                 return Some(x);
98             }
99             n -= 1;
100         }
101         None
102     }
103 }
104