Skip to main content

reifydb_core/value/column/pool/
capacity.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::fmt;
5
6use reifydb_type::value::{
7	container::{
8		blob::BlobContainer, bool::BoolContainer, number::NumberContainer, row::RowNumberContainer,
9		temporal::TemporalContainer, utf8::Utf8Container, uuid::UuidContainer,
10	},
11	is::{IsNumber, IsTemporal, IsUuid},
12};
13/// Trait for containers that can be created with a specific capacity
14pub trait ContainerCapacity {
15	fn with_capacity(capacity: usize) -> Self;
16	fn clear(&mut self);
17	fn capacity(&self) -> usize;
18}
19
20// Implement ContainerCapacity for all our container types
21impl ContainerCapacity for BoolContainer {
22	fn with_capacity(capacity: usize) -> Self {
23		Self::with_capacity(capacity)
24	}
25
26	fn clear(&mut self) {
27		// Clear content but preserve capacity
28		let capacity = self.capacity();
29		*self = Self::with_capacity(capacity);
30	}
31
32	fn capacity(&self) -> usize {
33		self.capacity()
34	}
35}
36
37impl<T> ContainerCapacity for NumberContainer<T>
38where
39	T: IsNumber + Clone + fmt::Debug + Default,
40{
41	fn with_capacity(capacity: usize) -> Self {
42		Self::with_capacity(capacity)
43	}
44
45	fn clear(&mut self) {
46		// Clear content but preserve capacity
47		let capacity = self.capacity();
48		*self = Self::with_capacity(capacity);
49	}
50
51	fn capacity(&self) -> usize {
52		self.capacity()
53	}
54}
55
56impl ContainerCapacity for Utf8Container {
57	fn with_capacity(capacity: usize) -> Self {
58		Self::with_capacity(capacity)
59	}
60
61	fn clear(&mut self) {
62		// Clear content but preserve capacity
63		let capacity = self.capacity();
64		*self = Self::with_capacity(capacity);
65	}
66
67	fn capacity(&self) -> usize {
68		self.capacity()
69	}
70}
71
72impl<T> ContainerCapacity for TemporalContainer<T>
73where
74	T: IsTemporal + Clone + fmt::Debug + Default,
75{
76	fn with_capacity(capacity: usize) -> Self {
77		Self::with_capacity(capacity)
78	}
79
80	fn clear(&mut self) {
81		// Clear content but preserve capacity
82		let capacity = self.capacity();
83		*self = Self::with_capacity(capacity);
84	}
85
86	fn capacity(&self) -> usize {
87		self.capacity()
88	}
89}
90
91impl<T> ContainerCapacity for UuidContainer<T>
92where
93	T: IsUuid + Clone + fmt::Debug + Default,
94{
95	fn with_capacity(capacity: usize) -> Self {
96		Self::with_capacity(capacity)
97	}
98
99	fn clear(&mut self) {
100		// Clear content but preserve capacity
101		let capacity = self.capacity();
102		*self = Self::with_capacity(capacity);
103	}
104
105	fn capacity(&self) -> usize {
106		self.capacity()
107	}
108}
109
110impl ContainerCapacity for BlobContainer {
111	fn with_capacity(capacity: usize) -> Self {
112		Self::with_capacity(capacity)
113	}
114
115	fn clear(&mut self) {
116		// Clear content but preserve capacity
117		let capacity = self.capacity();
118		*self = Self::with_capacity(capacity);
119	}
120
121	fn capacity(&self) -> usize {
122		self.capacity()
123	}
124}
125
126impl ContainerCapacity for RowNumberContainer {
127	fn with_capacity(capacity: usize) -> Self {
128		Self::with_capacity(capacity)
129	}
130
131	fn clear(&mut self) {
132		// Clear content but preserve capacity
133		let capacity = self.capacity();
134		*self = Self::with_capacity(capacity);
135	}
136
137	fn capacity(&self) -> usize {
138		self.capacity()
139	}
140}