use crate::SQLError;
use uqa_core::memory::{MemoryReservation, Produced, ProductionControl};
#[derive(Debug, Default)]
pub(in crate::schema) struct SchemaMemory {
reservation: Option<MemoryReservation>,
}
impl SchemaMemory {
pub(in crate::schema) fn into_reservation(self) -> Option<MemoryReservation> {
self.reservation
}
pub(in crate::schema) fn new(memory: Option<MemoryReservation>) -> Self {
Self {
reservation: memory,
}
}
}
impl Clone for SchemaMemory {
fn clone(&self) -> Self {
Self::default()
}
}
impl PartialEq for SchemaMemory {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for SchemaMemory {}
pub(in crate::schema) fn boxed_text(
text: &str,
control: &ProductionControl<'_>,
) -> Result<Produced<Box<str>>, SQLError> {
let memory = control.reserve(text.len())?;
Ok(control.finish(Box::from(text), memory)?)
}
pub(in crate::schema) fn boxed_slice<T>(
value: Produced<Vec<T>>,
control: &ProductionControl<'_>,
) -> Result<Produced<Box<[T]>>, SQLError> {
if value.len() == value.capacity() || size_of::<T>() == 0 {
let (value, memory) = value.into_parts();
return Ok(control.finish(value.into_boxed_slice(), memory)?);
}
let replacement = control.reserve(value.len() * size_of::<T>())?;
let (value, mut memory) = value.into_parts();
let old_buffer = memory
.as_mut()
.map(|memory| memory.split(value.capacity() * size_of::<T>()));
let value = value.into_boxed_slice();
drop(old_buffer);
Ok(control.finish(value, control.combine(memory, replacement))?)
}