1use crate::event::{Atom, Bytes};
2use crate::ser::boxed::unsize;
3use crate::ser::{Boxed, MapEmitter, SeqEmitter, SerializeHandle, StructEmitter};
4use crate::{State, Text};
5use alloc::string::String;
6
7pub enum Chunk<'a> {
19 Atom(Atom<'a>),
20 Struct(Boxed<dyn StructEmitter + 'a>),
21 Map(Boxed<dyn MapEmitter + 'a>),
22 Seq(Boxed<dyn SeqEmitter + 'a>),
23 Forward(SerializeHandle<'a>),
55}
56
57impl<'a> Chunk<'a> {
58 #[inline(always)]
61 pub fn seq<E: SeqEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a> {
62 Chunk::Seq(unsize(Boxed::arena(emitter, state), |x| {
63 x as *mut (dyn SeqEmitter + 'a)
64 }))
65 }
66
67 #[inline(always)]
69 pub fn map<E: MapEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a> {
70 Chunk::Map(unsize(Boxed::arena(emitter, state), |x| {
71 x as *mut (dyn MapEmitter + 'a)
72 }))
73 }
74
75 #[inline(always)]
78 pub fn structure<E: StructEmitter + 'a>(emitter: E, state: &mut State) -> Chunk<'a> {
79 Chunk::Struct(unsize(Boxed::arena(emitter, state), |x| {
80 x as *mut (dyn StructEmitter + 'a)
81 }))
82 }
83}
84
85impl<'a> From<Atom<'a>> for Chunk<'a> {
86 fn from(atom: Atom<'a>) -> Self {
87 Chunk::Atom(atom)
88 }
89}
90
91macro_rules! impl_from {
92 ($ty:ty, $atom:ident) => {
93 impl From<$ty> for Chunk<'static> {
94 fn from(value: $ty) -> Self {
95 Chunk::Atom(Atom::$atom(value as _))
96 }
97 }
98 };
99}
100
101impl_from!(u64, U64);
102impl_from!(i64, I64);
103impl_from!(usize, U64);
104impl_from!(isize, I64);
105impl_from!(bool, Bool);
106impl_from!(char, Char);
107
108impl From<f64> for Chunk<'static> {
109 fn from(value: f64) -> Self {
110 Chunk::Atom(Atom::F64(value))
111 }
112}
113
114impl From<f32> for Chunk<'static> {
115 fn from(value: f32) -> Self {
116 Chunk::Atom(Atom::F32(value))
117 }
118}
119
120impl From<()> for Chunk<'static> {
121 fn from(_: ()) -> Chunk<'static> {
122 Chunk::Atom(Atom::Null)
123 }
124}
125
126impl<'a> From<&'a str> for Chunk<'a> {
127 fn from(value: &'a str) -> Chunk<'a> {
128 Chunk::Atom(Atom::Str(Text::borrowed(value)))
129 }
130}
131
132impl<'a> From<&'a [u8]> for Chunk<'a> {
133 fn from(value: &'a [u8]) -> Chunk<'a> {
134 Chunk::Atom(Atom::Bytes(Bytes::borrowed(value)))
135 }
136}
137
138impl From<String> for Chunk<'static> {
139 fn from(value: String) -> Chunk<'static> {
140 Chunk::Atom(Atom::Str(Text::owned(value)))
141 }
142}
143
144#[cfg(target_pointer_width = "64")]
145const _: () = assert!(core::mem::size_of::<Chunk<'static>>() == 32);
146#[cfg(target_pointer_width = "32")]
147const _: () = assert!(core::mem::size_of::<Chunk<'static>>() == 24);