variable_len_reader/asynchronous/
read_varint.rs1macro_rules! read_varint_future {
2 ($primitive: ty, $future: ident) => {
3 read_varint_future!(f cfg(feature = "async_varint"), $primitive, $future, u8, ReadU8Raw);
4 };
5 (f $feature: meta, $primitive: ty, $future: ident, $internal: ty, $inner_future: ident) => {
6 #[$feature]
7 $crate::pin_project_lite::pin_project! {
8 #[cfg_attr(docsrs, doc($feature))]
9 #[derive(Debug)]
10 #[project(!Unpin)]
11 #[must_use = "futures do nothing unless you `.await` or poll them"]
12 pub struct $future<'a, R: ?Sized> {
13 value: $primitive,
14 position: usize,
15 ok: Option<bool>,
16 #[pin]
17 inner: $inner_future<'a, R>,
18 }
19 }
20 #[$feature]
21 impl<'a, R: ?Sized> ReaderFuture for $future<'a, R> {
22 fn reset(self: Pin<&mut Self>) {
23 let me = self.project();
24 *me.value = 0;
25 *me.position = 0;
26 *me.ok = None;
27 me.inner.reset();
28 }
29 }
30 #[$feature]
31 impl<'a, R: AsyncVariableReader + Unpin + ?Sized> Future for $future<'a, R> {
32 type Output = ::core::result::Result<$primitive, R::Error>;
33
34 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
35 let mut me = self.project();
36 if let Some(ok) = me.ok.as_ref() {
37 return Poll::Ready(if *ok { Ok(*me.value) } else { Err(R::read_varint_error(stringify!($future), *me.value as u128)) });
38 }
39 const SIZE: usize = ::core::mem::size_of::<$primitive>() << 3; const NUM_BITS: $internal = <$internal>::MAX >> 1;
41 const SIGN_BIT: $internal = NUM_BITS + 1;
42 const POS_OFFSET: usize = (<$internal>::BITS - 1) as usize;
43 loop {
44 let current = ::core::task::ready!(me.inner.as_mut().poll(cx))?;
45 *me.value |= ((current & NUM_BITS) as $primitive) << *me.position;
46 if current & SIGN_BIT == 0 {
47 *me.ok = Some(true);
48 return Poll::Ready(Ok(*me.value));
49 }
50 *me.position += POS_OFFSET;
51 if *me.position >= SIZE {
52 *me.ok = Some(false);
53 return Poll::Ready(Err(R::read_varint_error(stringify!($future), *me.value as u128)));
54 }
55 me.inner.as_mut().reset();
56 }
57 }
58 }
59 };
60}
61macro_rules! read_varint_func {
62 ($func: ident, $future: ident) => {
63 read_varint_func!(f cfg(feature = "async_varint"), $func, $future, read_u8_raw);
64 };
65 (f $feature: meta, $future: ident, $func: ident, $inner_func: ident) => {
66 #[$feature]
67 #[cfg_attr(docsrs, doc($feature))]
68 #[inline]
69 fn $func(&mut self) -> $future<Self> where Self: Unpin {
70 $future { value: 0, position: 0, ok: None, inner: self.$inner_func() }
71 }
72 };
73}
74
75macro_rules! define_read_varint_future {
76 () => {
77 read_varint_future!(u16, ReadU16Varint);
78 read_varint_future!(u32, ReadU32Varint);
79 read_varint_future!(u64, ReadU64Varint);
80 read_varint_future!(u128, ReadU128Varint);
81 };
82}
83macro_rules! define_read_varint_func {
84 () => {
85 #[cfg(feature = "async_varint")]
86 #[cfg_attr(docsrs, doc(cfg(feature = "async_varint")))]
87 fn read_varint_error(future_name: &'static str, value: u128) -> Self::Error;
88
89 read_varint_func!(ReadU16Varint, read_u16_varint);
90 read_varint_func!(ReadU32Varint, read_u32_varint);
91 read_varint_func!(ReadU64Varint, read_u64_varint);
92 read_varint_func!(ReadU128Varint, read_u128_varint);
93 };
94}
95
96define_read_varint_future!();
97
98#[cfg(all(feature = "async_varint", not(feature = "async_raw")))]
99compile_error!("developer error: please check Cargo.toml");