1#![cfg_attr(not(any(feature = "std", test)), no_std)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![cfg_attr(docsrs, allow(unused_attributes))]
5#![deny(missing_docs)]
6
7#[cfg(any(feature = "std", test))]
8extern crate std;
9
10#[cfg(all(not(feature = "std"), feature = "alloc"))]
11extern crate alloc as std;
12
13pub mod checksum;
15
16pub mod leb128;
18
19pub mod buffer;
21
22pub mod error;
24
25pub use cheap_clone::CheapClone;
26
27pub mod equivalent;
29
30pub mod equivalentor;
32
33pub mod types;
35
36pub mod state;
38
39#[doc(hidden)]
40pub mod __private {
41 pub use paste;
42}
43
44#[macro_export]
67macro_rules! builder {
68 ($(
69 $(#[$meta:meta])*
70 $wrapper_vis:vis $name:ident
71 ); +$(;)?
72 ) => {
73 $(
74 $crate::__private::paste::paste! {
75 $(#[$meta])*
76 #[derive(::core::marker::Copy, ::core::clone::Clone, ::core::fmt::Debug)]
77 $wrapper_vis struct $name <F> {
78 $wrapper_vis size: usize,
80
81 $wrapper_vis f: F,
83 }
84
85 impl<F> ::core::convert::From<(usize, F)> for $name<F> {
86 #[inline]
87 fn from((size, f): (usize, F)) -> Self {
88 Self { size, f }
89 }
90 }
91
92 impl<F> ::core::convert::From<$name<F>> for (usize, F) {
93 #[inline]
94 fn from(builder: $name<F>) -> Self {
95 (builder.size, builder.f)
96 }
97 }
98
99 impl<F> $name<F> {
100 #[doc = "Creates a new `" $name "` with the given size and builder closure."]
101 #[inline]
102 pub const fn new(size: usize, f: F) -> Self
103 {
104 Self { size, f }
105 }
106
107 #[doc = "Returns the required `" $name "` size."]
108 #[inline]
109 pub const fn size(&self) -> usize {
110 self.size
111 }
112
113 #[doc = "Returns the `" $name "` builder closure."]
114 #[inline]
115 pub const fn builder(&self) -> &F {
116 &self.f
117 }
118
119 #[inline]
121 pub fn into_components(self) -> (usize, F) {
122 (self.size, self.f)
123 }
124 }
125
126 impl<W, E> $crate::buffer::BufWriter for $name<W>
127 where
128 W: ::core::ops::Fn(&mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, E>,
129 {
130 type Error = E;
131
132 #[inline]
133 fn encoded_len(&self) -> usize {
134 self.size()
135 }
136
137 #[inline]
138 fn write(&self, buf: &mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, Self::Error> {
139 self.builder()(buf)
140 }
141 }
142
143 impl<W, E> $crate::buffer::BufWriterOnce for $name<W>
144 where
145 W: ::core::ops::FnOnce(&mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, E>,
146 {
147 type Error = E;
148
149 #[inline]
150 fn encoded_len(&self) -> usize {
151 self.size()
152 }
153
154 #[inline]
155 fn write_once(self, buf: &mut $crate::buffer::VacantBuffer<'_>) -> ::core::result::Result<usize, Self::Error> {
156 self.into_components().1(buf)
157 }
158 }
159 }
160 )*
161 };
162}
163
164#[inline(never)]
166#[cold]
167pub fn abort() -> ! {
168 #[cfg(feature = "std")]
169 {
170 std::process::abort()
171 }
172
173 #[cfg(not(feature = "std"))]
174 {
175 struct Abort;
176 impl Drop for Abort {
177 fn drop(&mut self) {
178 panic!();
179 }
180 }
181 let _a = Abort;
182 panic!("abort");
183 }
184}