qubit_io_binary/ext/
async_leb128_write_ext.rs1use core::future::Future;
11use std::io::Result;
12
13use qubit_codec_binary::Leb128Codec;
14use qubit_codec_binary::NonStrict;
15use qubit_io::AsyncOutput;
16
17use crate::util::encode_infallible_unchecked;
18use crate::util::write_all_async;
19
20macro_rules! leb128_write_method {
21 ($doc:literal, $name:ident, $ty:ty) => {
22 #[doc = $doc]
23 #[doc = ""]
24 #[doc = "# Parameters"]
25 #[doc = ""]
26 #[doc = "- `value`: Integer to encode and write."]
27 #[doc = ""]
28 #[doc = "# Returns"]
29 #[doc = ""]
30 #[doc = "Returns a future that completes after the canonical payload \
31 has been written."]
32 #[doc = ""]
33 #[doc = "# Errors"]
34 #[doc = ""]
35 #[doc = "Returns an output error, including a write-zero error when \
36 the output stops making progress."]
37 #[doc = ""]
38 #[doc = "# Cancellation safety"]
39 #[doc = ""]
40 #[doc = "This operation is not cancellation safe. Dropping the future \
41 retains any bytes already written to the output."]
42 #[inline(always)]
43 fn $name(&mut self, value: $ty) -> impl Future<Output = Result<()>> + '_
44 where
45 Self: Unpin,
46 {
47 async move {
48 let mut bytes = [0_u8; Leb128Codec::<$ty, NonStrict>::MAX_ENCODE_UNITS_PER_VALUE];
49 type Codec = Leb128Codec<$ty, NonStrict>;
50 let len = unsafe { encode_infallible_unchecked::<Codec>(value, &mut bytes, 0) };
53 write_all_async(self, &bytes[..len]).await
54 }
55 }
56 };
57}
58
59pub trait AsyncLeb128WriteExt: AsyncOutput<Item = u8> {
80 leb128_write_method!("Asynchronously writes LEB128 `u8`.", write_uleb_u8_async, u8);
81 leb128_write_method!("Asynchronously writes LEB128 `u16`.", write_uleb_u16_async, u16);
82 leb128_write_method!("Asynchronously writes LEB128 `u32`.", write_uleb_u32_async, u32);
83 leb128_write_method!("Asynchronously writes LEB128 `u64`.", write_uleb_u64_async, u64);
84 leb128_write_method!("Asynchronously writes LEB128 `u128`.", write_uleb_u128_async, u128);
85 leb128_write_method!("Asynchronously writes LEB128 `usize`.", write_uleb_usize_async, usize);
86 leb128_write_method!("Asynchronously writes LEB128 `i8`.", write_sleb_i8_async, i8);
87 leb128_write_method!("Asynchronously writes LEB128 `i16`.", write_sleb_i16_async, i16);
88 leb128_write_method!("Asynchronously writes LEB128 `i32`.", write_sleb_i32_async, i32);
89 leb128_write_method!("Asynchronously writes LEB128 `i64`.", write_sleb_i64_async, i64);
90 leb128_write_method!("Asynchronously writes LEB128 `i128`.", write_sleb_i128_async, i128);
91 leb128_write_method!("Asynchronously writes LEB128 `isize`.", write_sleb_isize_async, isize);
92}
93
94impl<W> AsyncLeb128WriteExt for W where W: AsyncOutput<Item = u8> + ?Sized {}