Skip to main content

smol_bytes/
macros.rs

1macro_rules! method_forward {
2  ($repr:tt { $(
3    $(#[$meta:meta])*
4    fn $name:ident($( $arg:ident : $arg_ty:ty ),*) $(-> $ret:ty)?
5  );+$(;)? }) => {
6    $(
7      $(#[$meta])*
8      #[cfg_attr(not(coverage), inline(always))]
9      fn $name(&mut self, $( $arg : $arg_ty ),*) $(-> $ret)? {
10        match &mut self.$repr {
11          Repr::Inline(buffer) => buffer.$name($( $arg ),*),
12          Repr::Heap(bytes) => bytes.$name($( $arg ),*),
13        }
14      }
15    )*
16  };
17  ($(
18    $(#[$meta:meta])*
19    fn $name:ident($($arg:ident : $arg_ty:ty ),*) $(-> $ret:ty)?
20  );+$(;)?) => {
21    $(
22      $(#[$meta])*
23      #[cfg_attr(not(coverage), inline(always))]
24      fn $name(&mut self, $( $arg : $arg_ty ),*) $(-> $ret)? {
25        Self::$name(self, $( $arg ),*)
26      }
27    )*
28  };
29}
30
31macro_rules! forward_buf_mut {
32  ($repr:tt { $($suffix:ident), +$(,)? }) => {
33    paste::paste! {
34      $(
35        $crate::macros::method_forward! { $repr {
36          fn [< put_ $suffix >](val: $suffix);
37          fn [< put_ $suffix _le >](val: $suffix);
38          fn [< put_ $suffix _ne >](val: $suffix);
39        }}
40      )*
41
42      $crate::macros::method_forward! {$repr {
43        fn put_i8(val: i8);
44        fn put_u8(val: u8);
45
46        fn put_uint(n: u64, nbytes: usize);
47        fn put_uint_le(n: u64, nbytes: usize);
48        fn put_uint_ne(n: u64, nbytes: usize);
49        fn put_int(n: i64, nbytes: usize);
50        fn put_int_le(n: i64, nbytes: usize);
51        fn put_int_ne(n: i64, nbytes: usize);
52      }}
53    }
54  };
55  ($($suffix:ident), +$(,)?) => {
56    paste::paste! {
57      $(
58        $crate::macros::method_forward! {
59          fn [< put_ $suffix >](val: $suffix);
60          fn [< put_ $suffix _le >](val: $suffix);
61          fn [< put_ $suffix _ne >](val: $suffix);
62        }
63      )*
64
65      $crate::macros::method_forward! {
66        fn put_i8(val: i8);
67        fn put_u8(val: u8);
68
69        fn put_uint(n: u64, nbytes: usize);
70        fn put_uint_le(n: u64, nbytes: usize);
71        fn put_uint_ne(n: u64, nbytes: usize);
72
73        fn put_int(n: i64, nbytes: usize);
74        fn put_int_le(n: i64, nbytes: usize);
75        fn put_int_ne(n: i64, nbytes: usize);
76      }
77    }
78  };
79}
80
81pub(super) use forward_buf_mut;
82pub(super) use method_forward;
83
84/// Formats arguments into the default shared [`Utf8Bytes`](crate::Utf8Bytes).
85///
86/// This is similar to `format!`, but returns a value that retains heap storage
87/// when later shortened. Use `format_compact_utf8bytes!` when short results
88/// should use inline storage.
89///
90/// # Examples
91///
92/// ```
93/// use smol_bytes::{format_utf8bytes, Utf8Bytes};
94///
95/// let name = "Ada";
96/// let greeting: Utf8Bytes = format_utf8bytes!("hello, {name}!");
97/// assert_eq!(greeting.as_str(), "hello, Ada!");
98/// ```
99#[macro_export]
100macro_rules! format_utf8bytes {
101  ($($tt:tt)*) => {{
102    let mut value = $crate::Utf8BytesMut::new();
103    ::core::fmt::Write::write_fmt(&mut value, ::core::format_args!($($tt)*))
104      .expect("a formatting trait implementation returned an error");
105    value.freeze_shared()
106  }};
107}
108
109/// Formats arguments into compact [`Utf8Bytes`](crate::compact::Utf8Bytes).
110///
111/// This is similar to `format!`, but returns a value that uses inline storage
112/// when its contents fit, including after a larger value is later shortened.
113/// Use `format_utf8bytes!` when retaining heap storage after shrinking is preferred.
114///
115/// # Examples
116///
117/// ```
118/// use smol_bytes::{compact, format_compact_utf8bytes};
119///
120/// let name = "Ada";
121/// let greeting: compact::Utf8Bytes = format_compact_utf8bytes!("hello, {name}!");
122/// assert_eq!(greeting.as_str(), "hello, Ada!");
123/// ```
124#[macro_export]
125macro_rules! format_compact_utf8bytes {
126  ($($tt:tt)*) => {{
127    let mut value = $crate::Utf8BytesMut::new();
128    ::core::fmt::Write::write_fmt(&mut value, ::core::format_args!($($tt)*))
129      .expect("a formatting trait implementation returned an error");
130    value.freeze_compact()
131  }};
132}