variable_len_reader/asynchronous/
write_varint.rs1macro_rules! write_varint_future {
2 ($primitive: ty, $future: ident) => {
3 write_varint_future!(f cfg(feature = "async_varint"), $primitive, $future, u8, WriteU8Raw);
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, W: ?Sized> {
13 value: $primitive,
14 position: usize,
15 ok: bool,
16 #[pin]
17 inner: $inner_future<'a, W>,
18 }
19 }
20 #[$feature]
21 impl<'a, W: ?Sized> WriterFuture<'a, W, $primitive> for $future<'a, W> {
22 fn new(writer: &'a mut W, buf: $primitive) -> Self {
23 let (value, current, position) = Self::_extra(buf, 0);
24 Self { value, position, ok: false, inner: $inner_future::new(writer, current) }
25 }
26 fn reset(self: Pin<&mut Self>, buf: $primitive) {
27 let me = self.project();
28 let (value, current, position) = Self::_extra(buf, 0);
29 *me.value = value;
30 *me.position = position;
31 *me.ok = false;
32 me.inner.reset(current);
33 }
34 }
35 #[$feature]
36 impl<'a, W: AsyncVariableWriter + Unpin + ?Sized> Future for $future<'a, W> {
37 type Output = ::core::result::Result<(), W::Error>;
38
39 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
40 let mut me = self.project();
41 if *me.ok { return Poll::Ready(Ok(())); }
42 loop {
43 ::core::task::ready!(me.inner.as_mut().poll(cx))?;
44 if *me.value == 0 {
45 *me.ok = true;
46 return Poll::Ready(Ok(()));
47 }
48 let (value, current, position) = Self::_extra(*me.value, *me.position);
49 *me.value = value;
50 *me.position = position;
51 me.inner.as_mut().reset(current);
52 }
53 }
54 }
55 #[$feature]
56 #[allow(arithmetic_overflow)] impl<'a, W: ?Sized> $future<'a, W> {
58 fn _extra(value: $primitive, position: usize) -> ($primitive, $internal, usize) {
59 const NUM_BITS: $internal = <$internal>::MAX >> 1;
60 const SIGN_BIT: $internal = NUM_BITS + 1;
61 const POS_OFFSET: usize = (<$internal>::BITS - 1) as usize;
62 if (value & NUM_BITS as $primitive) != value {
63 (value >> POS_OFFSET, ((value & (NUM_BITS as $primitive)) as $internal) | SIGN_BIT, position + POS_OFFSET)
64 } else {
65 (0, value as $internal, position + POS_OFFSET)
66 }
67 }
68 }
69 };
70}
71macro_rules! write_varint_func {
72 ($primitive: ty, $future: ident, $func: ident) => {
73 write_varint_func!(f cfg(feature = "async_varint"), $primitive, $future, $func);
74 };
75 (f $feature: meta, $primitive: ty, $future: ident, $func: ident) => {
76 write_wrap_func!(f $feature, $primitive, $future, $func);
77 };
78}
79
80macro_rules! define_write_varint_future {
81 () => {
82 write_varint_future!(u16, WriteU16Varint);
83 write_varint_future!(u32, WriteU32Varint);
84 write_varint_future!(u64, WriteU64Varint);
85 write_varint_future!(u128, WriteU128Varint);
86 };
87}
88macro_rules! define_write_varint_func {
89 () => {
90 write_varint_func!(u16, WriteU16Varint, write_u16_varint);
91 write_varint_func!(u32, WriteU32Varint, write_u32_varint);
92 write_varint_func!(u64, WriteU64Varint, write_u64_varint);
93 write_varint_func!(u128, WriteU128Varint, write_u128_varint);
94 };
95}
96
97define_write_varint_future!();
98
99#[cfg(all(feature = "async_varint", not(feature = "async_raw")))]
100compile_error!("developer error: please check Cargo.toml");