Skip to content

Commit 4a80c2c

Browse files
committed
Rename resize() to expand() and clarify documentation
Per review feedback, renamed the resize() method to expand() to make it clear that this function cannot shrink the storage capacity - it only allows expansion. Updated all function names, tests, and added documentation to clarify this behavior. The expand() function now clearly indicates that it can only increase capacity, preventing confusion about whether the buffer can be shrunk.
1 parent 731e3e9 commit 4a80c2c

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

core/patina_internal_collections/src/bst.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,18 @@ impl<'a, D> Bst<'a, D>
612612
where
613613
D: Copy + SliceKey + 'a,
614614
{
615-
/// Replaces the memory of the tree with a new slice, copying the data from the old slice to the new slice.
616-
pub fn resize(&mut self, slice: &'a mut [u8]) {
615+
/// Expands the tree's storage capacity to a new, larger buffer.
616+
///
617+
/// This function cannot shrink the storage capacity - it only allows expansion.
618+
/// All nodes are copied to the new buffer to preserve the tree structure.
619+
///
620+
/// # Panics
621+
///
622+
/// Panics if the new slice is smaller than the current capacity.
623+
pub fn expand(&mut self, slice: &'a mut [u8]) {
617624
let root = { if self.root.get().is_null() { None } else { Some(self.storage.idx(self.root.get())) } };
618625

619-
self.storage.resize(slice);
626+
self.storage.expand(slice);
620627

621628
if let Some(idx) = root {
622629
self.root.set(self.storage.get_mut(idx).expect("Pointer Exists."));
@@ -828,11 +835,11 @@ mod tests {
828835
}
829836

830837
#[test]
831-
fn test_simple_resize() {
838+
fn test_simple_expand() {
832839
let mut bst = Bst::<usize>::new();
833840

834841
let mut mem = [0; 20 * node_size::<usize>()];
835-
bst.resize(&mut mem);
842+
bst.expand(&mut mem);
836843

837844
for i in 0..10 {
838845
assert!(bst.add(i).is_ok());
@@ -844,7 +851,7 @@ mod tests {
844851
}
845852

846853
#[test]
847-
fn test_resize_with_existing_data() {
854+
fn test_expand_with_existing_data() {
848855
let mut mem = [0; 10 * node_size::<usize>()];
849856
let mut bst = Bst::<usize>::with_capacity(&mut mem);
850857

@@ -856,7 +863,7 @@ mod tests {
856863
}
857864

858865
let mut new_mem = [0; 20 * node_size::<usize>()];
859-
bst.resize(&mut new_mem);
866+
bst.expand(&mut new_mem);
860867

861868
assert_eq!(bst.len(), 10);
862869
assert_eq!(bst.capacity(), 20);

core/patina_internal_collections/src/node.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,19 @@ impl<'a, D> Storage<'a, D>
178178
where
179179
D: SliceKey + Copy,
180180
{
181-
/// Resizes the storage container to a new slice of memory.
181+
/// Expands the storage capacity by moving nodes to a new, larger buffer.
182+
///
183+
/// This function cannot shrink the storage capacity - it only allows expansion.
184+
/// All nodes (including gaps from deleted nodes) are copied to preserve the tree structure.
182185
///
183186
/// # Panics
184187
///
185-
/// Panics if the new slice is smaller than the current length of the storage container.
188+
/// Panics if the new slice is smaller than the current capacity of the storage container.
186189
///
187190
/// # Time Complexity
188191
///
189192
/// O(n)
190-
pub fn resize(&mut self, slice: &'a mut [u8]) {
193+
pub fn expand(&mut self, slice: &'a mut [u8]) {
191194
// SAFETY: This is reinterpreting a byte slice as a Node<D> slice.
192195
// 1. The alignment is handled by slice casting rules
193196
// 2. The correct number of Node<D> elements that fit in the byte slice is calculated
@@ -705,7 +708,7 @@ mod tests {
705708
}
706709

707710
#[test]
708-
fn test_resize_with_no_free_space() {
711+
fn test_expand_with_no_free_space() {
709712
const CAPACITY: usize = 5;
710713
let mut memory = [0; CAPACITY * node_size::<usize>()];
711714
let mut storage = Storage::<usize>::with_capacity(&mut memory);
@@ -717,7 +720,7 @@ mod tests {
717720

718721
// Resize to the exact same capacity (no free space)
719722
let mut new_memory = [0; CAPACITY * node_size::<usize>()];
720-
storage.resize(&mut new_memory);
723+
storage.expand(&mut new_memory);
721724

722725
// Verify that available is null indicating no free space
723726
assert!(storage.available.get().is_null());
@@ -783,8 +786,8 @@ mod tests {
783786

784787
#[test]
785788
#[should_panic(expected = "assertion failed: buffer.len() >= self.capacity()")]
786-
fn test_resize_prevents_capacity_shrink() {
787-
// Verify that resize() prevents shrinking capacity
789+
fn test_expand_prevents_capacity_shrink() {
790+
// Verify that expand() prevents shrinking capacity
788791
const INITIAL_SIZE: usize = 10;
789792
let mut initial_memory = [0; INITIAL_SIZE * node_size::<usize>()];
790793
let mut storage = Storage::<usize>::with_capacity(&mut initial_memory);
@@ -802,12 +805,12 @@ mod tests {
802805
// This should panic because we're shrinking capacity
803806
const SMALLER_SIZE: usize = 5;
804807
let mut smaller_memory = [0; SMALLER_SIZE * node_size::<usize>()];
805-
storage.resize(&mut smaller_memory); // Should panic here
808+
storage.expand(&mut smaller_memory); // Should panic here
806809
}
807810

808811
#[test]
809-
fn test_resize_copies_all_nodes_including_gaps() {
810-
// Test that resize copies ALL nodes (capacity), not just len() nodes
812+
fn test_expand_copies_all_nodes_including_gaps() {
813+
// Test that expand copies ALL nodes (capacity), not just len() nodes
811814
// Buffer layout: [VALID | VALID | INVALID | VALID | INVALID]
812815
const INITIAL_SIZE: usize = 10;
813816
let mut initial_memory = [0; INITIAL_SIZE * node_size::<usize>()];
@@ -840,7 +843,7 @@ mod tests {
840843
// Resize to larger capacity - should copy ALL nodes including invalid ones
841844
const LARGER_SIZE: usize = 20;
842845
let mut larger_memory = [0; LARGER_SIZE * node_size::<usize>()];
843-
storage.resize(&mut larger_memory);
846+
storage.expand(&mut larger_memory);
844847

845848
// Verify all 7 nodes are still accessible
846849
assert_eq!(storage.len(), 7);

core/patina_internal_collections/src/rbt.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,18 @@ impl<'a, D> Rbt<'a, D>
843843
where
844844
D: SliceKey + Copy + 'a,
845845
{
846-
/// Replaces the memory of the tree with a new slice, copying the data from the old slice to the new slice.
847-
pub fn resize(&mut self, slice: &'a mut [u8]) {
846+
/// Expands the tree's storage capacity to a new, larger buffer.
847+
///
848+
/// This function cannot shrink the storage capacity - it only allows expansion.
849+
/// All nodes are copied to the new buffer to preserve the tree structure.
850+
///
851+
/// # Panics
852+
///
853+
/// Panics if the new slice is smaller than the current capacity.
854+
pub fn expand(&mut self, slice: &'a mut [u8]) {
848855
let root = (!self.root.get().is_null()).then(|| self.storage.idx(self.root.get()));
849856

850-
self.storage.resize(slice);
857+
self.storage.expand(slice);
851858

852859
if let Some(idx) = root {
853860
self.root.set(self.storage.get_mut(idx).expect("Pointer Exists."));
@@ -1773,11 +1780,11 @@ mod tests {
17731780
}
17741781

17751782
#[test]
1776-
fn test_simple_resize() {
1783+
fn test_simple_expand() {
17771784
let mut rbt = Rbt::<usize>::new();
17781785

17791786
let mut mem = [0; 20 * node_size::<usize>()];
1780-
rbt.resize(&mut mem);
1787+
rbt.expand(&mut mem);
17811788

17821789
for i in 0..10 {
17831790
assert!(rbt.add(i).is_ok());
@@ -1789,7 +1796,7 @@ mod tests {
17891796
}
17901797

17911798
#[test]
1792-
fn test_resize_with_existing_data() {
1799+
fn test_expand_with_existing_data() {
17931800
let mut mem = [0; 10 * node_size::<usize>()];
17941801
let mut rbt = Rbt::<usize>::with_capacity(&mut mem);
17951802

@@ -1801,7 +1808,7 @@ mod tests {
18011808
}
18021809

18031810
let mut new_mem = [0; 20 * node_size::<usize>()];
1804-
rbt.resize(&mut new_mem);
1811+
rbt.expand(&mut new_mem);
18051812

18061813
assert_eq!(rbt.len(), 10);
18071814
assert_eq!(rbt.capacity(), 20);

0 commit comments

Comments
 (0)