reifydb-core 0.4.11

Core database interfaces and data structures for ReifyDB
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::fmt;

use reifydb_type::value::{
	container::{
		blob::BlobContainer, bool::BoolContainer, number::NumberContainer, row::RowNumberContainer,
		temporal::TemporalContainer, utf8::Utf8Container, uuid::UuidContainer,
	},
	is::{IsNumber, IsTemporal, IsUuid},
};
/// Trait for containers that can be created with a specific capacity
pub trait ContainerCapacity {
	fn with_capacity(capacity: usize) -> Self;
	fn clear(&mut self);
	fn capacity(&self) -> usize;
}

// Implement ContainerCapacity for all our container types
impl ContainerCapacity for BoolContainer {
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}

impl<T> ContainerCapacity for NumberContainer<T>
where
	T: IsNumber + Clone + fmt::Debug + Default,
{
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}

impl ContainerCapacity for Utf8Container {
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}

impl<T> ContainerCapacity for TemporalContainer<T>
where
	T: IsTemporal + Clone + fmt::Debug + Default,
{
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}

impl<T> ContainerCapacity for UuidContainer<T>
where
	T: IsUuid + Clone + fmt::Debug + Default,
{
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}

impl ContainerCapacity for BlobContainer {
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}

impl ContainerCapacity for RowNumberContainer {
	fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity(capacity)
	}

	fn clear(&mut self) {
		// Clear content but preserve capacity
		let capacity = self.capacity();
		*self = Self::with_capacity(capacity);
	}

	fn capacity(&self) -> usize {
		self.capacity()
	}
}