use std::cell::RefCell;
use qubit_budget::ResourceQuantity;
use serde::Serialize;
use serde::Serializer;
use super::JsonEncodeContext;
use super::JsonEncodeSerializer;
pub(in crate::encode) struct BudgetedValue<
'a,
'transaction,
'budget,
'context,
T,
R,
Q,
const VALUE_LIMITS: bool,
> where
T: ?Sized,
Q: ResourceQuantity,
{
value: &'a T,
context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
depth: usize,
}
impl<'a, 'transaction, 'budget, 'context, T, R, Q, const VALUE_LIMITS: bool>
BudgetedValue<'a, 'transaction, 'budget, 'context, T, R, Q, VALUE_LIMITS>
where
T: ?Sized,
Q: ResourceQuantity,
{
#[must_use]
#[inline(always)]
pub(super) const fn new(
value: &'a T,
context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
depth: usize,
) -> Self {
Self {
value,
context,
depth,
}
}
}
impl<T, R, Q, const VALUE_LIMITS: bool> Serialize
for BudgetedValue<'_, '_, '_, '_, T, R, Q, VALUE_LIMITS>
where
T: Serialize + ?Sized,
R: Clone,
Q: ResourceQuantity,
{
#[inline(always)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let result =
self.value
.serialize(JsonEncodeSerializer::<S, R, Q, VALUE_LIMITS>::with_context(
serializer,
self.context,
self.depth,
));
if result.is_err() {
self.context
.borrow()
.output
.borrow_mut()
.record_custom_serialization_error();
}
result
}
}