1 mod pin;
2 
3 use core::time::Duration;
4 #[cfg(feature = "std")]
5 use std::collections::VecDeque;
6 #[cfg(all(feature = "std", target_family = "unix"))]
7 use std::{io, os::unix};
8 
9 pub use self::pin::Pin;
10 use crate::traits::Sealed;
11 
12 #[inline]
identity<T>(x: T) -> T13 pub const fn identity<T>(x: T) -> T {
14     x
15 }
16 
17 pub trait Unpin {}
18 impl<'a, T: ?Sized + 'a> Unpin for &'a T {}
19 impl<'a, T: ?Sized + 'a> Unpin for &'a mut T {}
20 
21 #[cfg(all(feature = "std", target_family = "unix"))]
22 pub trait UnixFileExt_v1_33: unix::fs::FileExt {
read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()>23     fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
24         while !buf.is_empty() {
25             match self.read_at(buf, offset) {
26                 Ok(0) => break,
27                 Ok(n) => {
28                     let tmp = buf;
29                     buf = &mut tmp[n..];
30                     offset += n as u64;
31                 }
32                 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
33                 Err(e) => return Err(e),
34             }
35         }
36         if !buf.is_empty() {
37             Err(io::Error::new(
38                 io::ErrorKind::UnexpectedEof,
39                 "failed to fill whole buffer",
40             ))
41         } else {
42             Ok(())
43         }
44     }
45 
write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()>46     fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
47         while !buf.is_empty() {
48             match self.write_at(buf, offset) {
49                 Ok(0) => {
50                     return Err(io::Error::new(
51                         io::ErrorKind::WriteZero,
52                         "failed to write whole buffer",
53                     ));
54                 }
55                 Ok(n) => {
56                     buf = &buf[n..];
57                     offset += n as u64
58                 }
59                 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
60                 Err(e) => return Err(e),
61             }
62         }
63         Ok(())
64     }
65 }
66 
67 #[cfg(all(feature = "std", target_family = "unix"))]
68 impl<F: unix::fs::FileExt> UnixFileExt_v1_33 for F {}
69 
70 pub trait Option_v1_33<T, E>: Sealed<Option<Result<T, E>>> {
transpose(self) -> Result<Option<T>, E>71     fn transpose(self) -> Result<Option<T>, E>;
72 }
73 
74 impl<T, E> Option_v1_33<T, E> for Option<Result<T, E>> {
75     #[inline]
transpose(self) -> Result<Option<T>, E>76     fn transpose(self) -> Result<Option<T>, E> {
77         match self {
78             Some(Ok(x)) => Ok(Some(x)),
79             Some(Err(e)) => Err(e),
80             None => Ok(None),
81         }
82     }
83 }
84 
85 pub trait Result_v1_33<T, E>: Sealed<Result<Option<T>, E>> {
transpose(self) -> Option<Result<T, E>>86     fn transpose(self) -> Option<Result<T, E>>;
87 }
88 
89 impl<T, E> Result_v1_33<T, E> for Result<Option<T>, E> {
90     #[inline]
transpose(self) -> Option<Result<T, E>>91     fn transpose(self) -> Option<Result<T, E>> {
92         match self {
93             Ok(Some(x)) => Some(Ok(x)),
94             Ok(None) => None,
95             Err(e) => Some(Err(e)),
96         }
97     }
98 }
99 
100 #[cfg(feature = "std")]
101 pub trait VecDeque_v1_33<T>: Sealed<VecDeque<T>> {
resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T)102     fn resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T);
103 }
104 
105 #[cfg(feature = "std")]
106 impl<T> VecDeque_v1_33<T> for VecDeque<T> {
resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T)107     fn resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T) {
108         let len = self.len();
109 
110         if new_len > len {
111             self.extend(core::iter::repeat_with(generator).take(new_len - len))
112         } else {
113             self.truncate(new_len);
114         }
115     }
116 }
117 
118 pub trait Duration_v1_33: Sealed<Duration> {
as_millis(&self) -> u128119     fn as_millis(&self) -> u128;
as_micros(&self) -> u128120     fn as_micros(&self) -> u128;
as_nanos(&self) -> u128121     fn as_nanos(&self) -> u128;
122 }
123 
124 impl Duration_v1_33 for Duration {
125     #[inline]
as_millis(&self) -> u128126     fn as_millis(&self) -> u128 {
127         self.as_secs() as u128 * 1_000 + (self.subsec_nanos() / 1_000_000) as u128
128     }
129 
130     #[inline]
as_micros(&self) -> u128131     fn as_micros(&self) -> u128 {
132         self.as_secs() as u128 * 1_000_000 + (self.subsec_nanos() / 1_000) as u128
133     }
134 
135     #[inline]
as_nanos(&self) -> u128136     fn as_nanos(&self) -> u128 {
137         self.as_secs() as u128 * 1_000_000_000 + self.subsec_nanos() as u128
138     }
139 }
140