Skip to main content

qubit_io_binary/ext/
async_zig_zag_write_ext.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Asynchronous ZigZag writes.
9
10use core::future::Future;
11use std::io::Result;
12
13use qubit_codec_binary::{
14    NonStrict,
15    ZigZagCodec,
16};
17use qubit_io::AsyncOutput;
18
19use crate::util::{
20    encode_infallible_unchecked,
21    write_all_async,
22};
23
24macro_rules! zig_zag_write_method {
25    ($doc:literal, $name:ident, $ty:ty) => {
26        #[doc = $doc]
27        #[doc = ""]
28        #[doc = "# Parameters"]
29        #[doc = ""]
30        #[doc = "- `value`: Signed integer to encode and write."]
31        #[doc = ""]
32        #[doc = "# Returns"]
33        #[doc = ""]
34        #[doc = "Returns a future that completes after the encoded payload \
35                 has been written."]
36        #[doc = ""]
37        #[doc = "# Errors"]
38        #[doc = ""]
39        #[doc = "Returns an output error, including a write-zero error when \
40                 the output stops making progress."]
41        #[doc = ""]
42        #[doc = "# Cancellation safety"]
43        #[doc = ""]
44        #[doc = "This operation is not cancellation safe. Dropping the future \
45                 retains any bytes already written to the output."]
46        #[inline(always)]
47        fn $name(&mut self, value: $ty) -> impl Future<Output = Result<()>> + '_
48        where
49            Self: Unpin,
50        {
51            async move {
52                let mut bytes = [0_u8;
53                    ZigZagCodec::<$ty, NonStrict>::MAX_ENCODE_UNITS_PER_VALUE];
54                type Codec = ZigZagCodec<$ty, NonStrict>;
55                // SAFETY: The local buffer has the codec's maximum payload
56                // size.
57                let len = unsafe {
58                    encode_infallible_unchecked::<Codec>(value, &mut bytes, 0)
59                };
60                write_all_async(self, &bytes[..len]).await
61            }
62        }
63    };
64}
65
66/// Future-based ZigZag plus unsigned-LEB128 writes.
67///
68/// # Cancellation safety
69///
70/// These writes are not cancellation safe. Dropping a pending future leaves
71/// an already-written prefix in the output.
72pub trait AsyncZigZagWriteExt: AsyncOutput<Item = u8> {
73    zig_zag_write_method!(
74        "Asynchronously writes ZigZag `i8`.",
75        write_zig_zag_i8_async,
76        i8
77    );
78    zig_zag_write_method!(
79        "Asynchronously writes ZigZag `i16`.",
80        write_zig_zag_i16_async,
81        i16
82    );
83    zig_zag_write_method!(
84        "Asynchronously writes ZigZag `i32`.",
85        write_zig_zag_i32_async,
86        i32
87    );
88    zig_zag_write_method!(
89        "Asynchronously writes ZigZag `i64`.",
90        write_zig_zag_i64_async,
91        i64
92    );
93    zig_zag_write_method!(
94        "Asynchronously writes ZigZag `i128`.",
95        write_zig_zag_i128_async,
96        i128
97    );
98    zig_zag_write_method!(
99        "Asynchronously writes ZigZag `isize`.",
100        write_zig_zag_isize_async,
101        isize
102    );
103}
104
105impl<W> AsyncZigZagWriteExt for W where W: AsyncOutput<Item = u8> + ?Sized {}