anda_db_utils 0.11.0

A utility Rust library for Anda DB.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
//! Shared utility types used by the AndaDB workspace.
//!
//! The crate intentionally stays small and dependency-light. It currently
//! provides:
//!
//! - [`UniqueVec`], an insertion-ordered vector that rejects duplicates.
//! - [`CountingWriter`], a writer that counts serialized bytes without storing
//!   the payload.
//! - [`Pipe`], a small functional-style chaining trait.
//!
//! # Hashing
//!
//! Hash-based structures in this crate use `rustc-hash` (FxHash) without a
//! random seed, matching the rest of the AndaDB workspace. FxHash has no
//! collision resistance: do not use these types where an adversary controls
//! the hashed keys and hash-flooding (O(n²) degradation) is a concern.

use core::ops::Deref;
use rustc_hash::{FxBuildHasher, FxHashSet};
use serde::{
    de::{Deserialize, Deserializer},
    ser::{Serialize, Serializer},
};
use std::{borrow::Borrow, hash::Hash};

/// A trait for functional-style method chaining.
///
/// Allows any value to be passed through a function, enabling
/// fluent interfaces and functional programming patterns.
pub trait Pipe<T> {
    /// Passes the value through a function.
    ///
    /// # Arguments
    ///
    /// * `f` - Function to apply to the value
    ///
    /// # Returns
    ///
    /// The result of applying the function to the value
    fn pipe<F, R>(self, f: F) -> R
    where
        F: FnOnce(Self) -> R,
        Self: Sized;
}

impl<T> Pipe<T> for T {
    fn pipe<F, R>(self, f: F) -> R
    where
        F: FnOnce(Self) -> R,
    {
        f(self)
    }
}

/// A helper utility to efficiently push or extend a `Vec` with unique items.
///
/// This struct maintains an internal `HashSet` to keep track of existing items,
/// providing an optimized way to perform multiple non-existent insertions.
/// It is designed to be used with a `Vec` that it helps manage.
///
/// # Memory cost
///
/// Every element is stored **twice** — once in the ordered `Vec` and once in
/// the membership `HashSet` — trading roughly 2x memory for O(1) duplicate
/// checks. For large owned elements (e.g. long `String` keys) this doubles
/// the payload memory; callers holding many large collections should weigh
/// this against a plain `Vec` with linear-scan deduplication.
///
/// # Examples
///
/// ```rust
/// use anda_db_utils::UniqueVec;
///
/// let vec = vec![1, 2, 3];
/// let mut extender = UniqueVec::from(vec);
///
/// // Push an item that already exists (no change)
/// extender.push(2);
/// assert_eq!(extender.as_ref(), &[1, 2, 3]);
///
/// // Push a new item
/// extender.push(4);
/// assert_eq!(extender.as_ref(), &[1, 2, 3, 4]);
///
/// // Extend with a list of items
/// extender.extend(vec![3, 5, 6]);
/// assert_eq!(extender.as_ref(), &[1, 2, 3, 4, 5, 6]);
/// ```
#[derive(Clone, Debug)]
pub struct UniqueVec<T> {
    set: FxHashSet<T>,
    vec: Vec<T>,
}

struct UniqueVecSetRebuildGuard<'a, T>
where
    T: Eq + Hash + Clone,
{
    set: &'a mut FxHashSet<T>,
    vec: &'a mut Vec<T>,
}

impl<T> Drop for UniqueVecSetRebuildGuard<'_, T>
where
    T: Eq + Hash + Clone,
{
    fn drop(&mut self) {
        // Retain-style edits can only delete elements, never duplicate them, so
        // equal lengths imply the set still mirrors the vec exactly. Rebuild only
        // on divergence (e.g. a panicking predicate or an inconsistent Hash/Eq
        // implementation interrupted the incremental set maintenance).
        if self.set.len() != self.vec.len() {
            self.set.clear();
            self.set.extend(self.vec.iter().cloned());
        }
    }
}

impl<T> Default for UniqueVec<T> {
    /// Creates an empty `UniqueVec`.
    fn default() -> Self {
        Self {
            set: FxHashSet::default(),
            vec: Vec::new(),
        }
    }
}

impl<T> From<Vec<T>> for UniqueVec<T>
where
    T: Eq + Hash + Clone,
{
    /// Creates a `UniqueVec` from a `Vec`.
    ///
    /// The extender is initialized with all the unique items from the vector.
    fn from(mut vec: Vec<T>) -> Self {
        let mut set = FxHashSet::with_capacity_and_hasher(vec.len(), FxBuildHasher);
        vec.retain(|item| set.insert(item.clone()));
        Self { set, vec }
    }
}

impl<T> FromIterator<T> for UniqueVec<T>
where
    T: Eq + Hash + Clone,
{
    /// Creates a `UniqueVec` from an iterator.
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let vec: Vec<T> = iter.into_iter().collect();
        vec.into()
    }
}

impl<T> From<UniqueVec<T>> for Vec<T> {
    /// Converts a `UniqueVec` into a `Vec`.
    fn from(extender: UniqueVec<T>) -> Self {
        extender.vec
    }
}

impl<T> AsRef<[T]> for UniqueVec<T> {
    /// Returns a slice containing the entire vector.
    fn as_ref(&self) -> &[T] {
        &self.vec
    }
}

impl<T> Deref for UniqueVec<T> {
    type Target = Vec<T>;

    /// Dereferences the `UniqueVec` to a `Vec`.
    fn deref(&self) -> &Self::Target {
        &self.vec
    }
}

impl<T> UniqueVec<T>
where
    T: Eq + Hash + Clone,
{
    /// Creates a new, empty `UniqueVec`.
    pub fn new() -> Self {
        UniqueVec::default()
    }

    /// Creates a new, empty `UniqueVec` with a specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        UniqueVec {
            set: FxHashSet::with_capacity_and_hasher(capacity, FxBuildHasher),
            vec: Vec::with_capacity(capacity),
        }
    }

    /// Returns `true` if the `UniqueVec` contains the specified item.
    pub fn contains<Q>(&self, item: &Q) -> bool
    where
        T: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        self.set.contains(item)
    }

    /// Pushes an item to the vector if it does not already exist.
    ///
    /// # Arguments
    ///
    /// * `item` - The item to add.
    ///
    /// # Returns
    ///
    /// `true` if the item was added, `false` otherwise.
    ///
    /// # Panic safety
    ///
    /// The set/vec invariant (`set` mirrors `vec` exactly) holds even if any
    /// step unwinds:
    ///
    /// * `contains` / `clone` panic — nothing has been mutated yet.
    /// * `vec.push` panics (capacity overflow) — `Vec::push` leaves the vector
    ///   unchanged on panic and the set has not been touched.
    /// * `set.insert` panics (a custom `Hash` impl may panic) — a drop guard
    ///   pops the element that was just pushed onto the vector. Without the
    ///   guard the vector would keep an element the set does not know about,
    ///   and a later `push` of an equal element would insert a duplicate.
    pub fn push(&mut self, item: T) -> bool {
        // Membership test first: duplicates are rejected without paying for a
        // clone.
        if self.set.contains(&item) {
            return false;
        }

        struct VecRollbackGuard<'a, T> {
            vec: &'a mut Vec<T>,
            armed: bool,
        }
        impl<T> Drop for VecRollbackGuard<'_, T> {
            fn drop(&mut self) {
                if self.armed {
                    self.vec.pop();
                }
            }
        }

        self.vec.push(item.clone());
        let mut guard = VecRollbackGuard {
            vec: &mut self.vec,
            armed: true,
        };
        let inserted = self.set.insert(item);
        // Defuse only when the set accepted the element. `inserted == false`
        // means an inconsistent `Hash`/`Eq` implementation disagreed with the
        // `contains` probe above; keep set and vec in agreement by letting the
        // guard pop the vector copy.
        guard.armed = !inserted;
        drop(guard);
        inserted
    }

    /// Extends the vector with items from an iterator that do not already exist.
    ///
    /// # Arguments
    ///
    /// * `items` - An iterator providing the items to add.
    pub fn extend(&mut self, items: impl IntoIterator<Item = T>) {
        Extend::extend(self, items);
    }

    /// Retains only the elements specified by the predicate.
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&T) -> bool,
    {
        let guard = UniqueVecSetRebuildGuard {
            set: &mut self.set,
            vec: &mut self.vec,
        };
        let set = &mut *guard.set;
        guard.vec.retain(|item| {
            if f(item) {
                true
            } else {
                set.remove(item);
                false
            }
        });
    }

    /// Removes and returns the element at `index`.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds (same semantics as
    /// [`Vec::remove`]).
    ///
    /// # Panic safety
    ///
    /// The set entry is removed **before** the vector entry: `set.remove` may
    /// panic (custom `Hash` impls), and doing it first leaves both containers
    /// untouched on unwind. The subsequent `vec.remove` cannot panic because
    /// the index was already bounds-checked.
    pub fn remove(&mut self, index: usize) -> T {
        // Bounds check (may panic) happens before any mutation.
        self.set.remove(&self.vec[index]);
        self.vec.remove(index)
    }

    /// Removes **an element** from the vector and returns it.
    /// The first element that satisfies the predicate will be removed.
    ///
    /// # Panic safety
    ///
    /// See [`Self::remove`]: the set entry is removed first so that a panic
    /// in `set.remove` (custom `Hash` impls) leaves both containers untouched.
    pub fn remove_if<P>(&mut self, mut predicate: P) -> Option<T>
    where
        P: FnMut(&T) -> bool,
    {
        if let Some(index) = self.vec.iter().position(&mut predicate) {
            self.set.remove(&self.vec[index]);
            Some(self.vec.remove(index))
        } else {
            None
        }
    }

    /// Removes **an element** from the vector and returns it.
    /// The last element is swapped into its place.
    ///
    /// # Panic safety
    ///
    /// See [`Self::remove`]: the set entry is removed first so that a panic
    /// in `set.remove` (custom `Hash` impls) leaves both containers untouched.
    pub fn swap_remove_if<P>(&mut self, mut predicate: P) -> Option<T>
    where
        P: FnMut(&T) -> bool,
    {
        if let Some(index) = self.vec.iter().position(&mut predicate) {
            self.set.remove(&self.vec[index]);
            Some(self.vec.swap_remove(index))
        } else {
            None
        }
    }

    /// Intersects the `UniqueVec` with another `UniqueVec`.
    pub fn intersect_with(&mut self, other: &UniqueVec<T>) {
        let guard = UniqueVecSetRebuildGuard {
            set: &mut self.set,
            vec: &mut self.vec,
        };
        let set = &mut *guard.set;
        guard.vec.retain(|item| {
            if other.set.contains(item) {
                true
            } else {
                set.remove(item);
                false
            }
        });
    }

    /// Returns the inner `Vec` of the `UniqueVec`.
    pub fn into_vec(self) -> Vec<T> {
        self.vec
    }

    /// Returns the inner `FxHashSet` of the `UniqueVec`.
    pub fn into_set(self) -> FxHashSet<T> {
        self.set
    }

    /// Converts the `UniqueVec` to a `Vec`.
    pub fn to_vec(&self) -> Vec<T> {
        self.vec.clone()
    }

    /// Converts the `UniqueVec` to a `FxHashSet`.
    pub fn to_set(&self) -> FxHashSet<T> {
        self.set.clone()
    }
}

impl<T: PartialEq> PartialEq for UniqueVec<T> {
    /// Two `UniqueVec`s are equal when their vectors are equal (same elements
    /// in the same order). The membership set mirrors the vector, so it does
    /// not need to be compared.
    fn eq(&self, other: &Self) -> bool {
        self.vec == other.vec
    }
}

impl<T: Eq> Eq for UniqueVec<T> {}

impl<T> Extend<T> for UniqueVec<T>
where
    T: Eq + Hash + Clone,
{
    /// Extends the vector with items from an iterator that do not already exist.
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        // Element-by-element via `push` so the set/vec invariant holds after
        // every step, even if the iterator (or an allocation) panics midway.
        for item in iter {
            self.push(item);
        }
    }
}

impl<T> Serialize for UniqueVec<T>
where
    T: Serialize,
{
    /// Serializes the `UniqueVec` as a sequence.
    #[inline]
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_seq(self.vec.iter())
    }
}

impl<'de, T> Deserialize<'de> for UniqueVec<T>
where
    T: Eq + Hash + Clone + Deserialize<'de>,
{
    /// Deserializes a sequence into a `UniqueVec`.
    #[inline]
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let vec: Vec<T> = Deserialize::deserialize(deserializer)?;
        Ok(UniqueVec::from(vec))
    }
}

/// Utility for counting the size of serialized CBOR data.
///
/// Note: for computing the encoded size of a CBOR value, prefer
/// `cbor2::serialized_size` (the workspace convention); it avoids driving a
/// full serializer through the `Write` trait. This type is kept as a
/// general-purpose byte-counting `Write` sink for other serialization
/// formats and for backwards compatibility.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CountingWriter {
    count: usize,
}

impl Default for CountingWriter {
    /// Creates a new `CountingWriter` with a count of 0.
    fn default() -> Self {
        Self::new()
    }
}

impl CountingWriter {
    /// Creates a new `CountingWriter`.
    pub const fn new() -> Self {
        CountingWriter { count: 0 }
    }

    /// Returns the current count of bytes written.
    pub const fn size(&self) -> usize {
        self.count
    }
}

impl std::io::Write for CountingWriter {
    /// Implements the write method for the Write trait.
    /// This simply counts the bytes without actually writing them.
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let len = buf.len();
        self.count = self
            .count
            .checked_add(len)
            .ok_or_else(|| std::io::Error::other("byte count overflow"))?;
        Ok(len)
    }

    /// Implements the flush method for the Write trait.
    /// This is a no-op since we're not actually writing data.
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{borrow::Cow, io::Write};

    #[test]
    fn test_pipe_trait() {
        // Test basic pipe functionality
        let result = 5.pipe(|x| x * 2).pipe(|x| x + 1);
        assert_eq!(result, 11);

        // Test pipe with different types
        let string_result = "hello"
            .pipe(|s| s.to_uppercase())
            .pipe(|s| format!("{} world", s));
        assert_eq!(string_result, "HELLO world");

        // Test pipe with closure that changes type
        let vec_result = vec![1, 2, 3].pipe(|v| v.len()).pipe(|len| len as f64);
        assert_eq!(vec_result, 3.0);
    }

    #[test]
    fn test_unique_vec_new() {
        let uv: UniqueVec<i32> = UniqueVec::new();
        assert_eq!(uv.len(), 0);
        assert!(uv.is_empty());
    }

    #[test]
    fn test_unique_vec_with_capacity() {
        let uv: UniqueVec<i32> = UniqueVec::with_capacity(10);
        assert_eq!(uv.len(), 0);
        assert_eq!(uv.capacity(), 10);
    }

    #[test]
    fn test_unique_vec_from_vec() {
        let vec = vec![1, 2, 2, 3, 2, 1];
        let uv = UniqueVec::from(vec);
        assert_eq!(uv.len(), 3);
        assert!(uv.contains(&1));
        assert!(uv.contains(&2));
        assert!(uv.contains(&3));
    }

    #[test]
    fn test_unique_vec_from_iterator() {
        let uv: UniqueVec<i32> = [2, 2, 1, 3, 2, 1].iter().cloned().collect();
        assert_eq!(uv.len(), 3);
        assert!(uv.contains(&2));
        assert!(uv.contains(&1));
        assert!(uv.contains(&3));
    }

    #[test]
    fn test_unique_vec_push() {
        let mut uv = UniqueVec::new();

        // Push new items
        assert!(uv.push(1));
        assert!(uv.push(2));
        assert!(uv.push(3));
        assert_eq!(uv.len(), 3);

        // Push duplicate items
        assert!(!uv.push(1));
        assert!(!uv.push(2));
        assert_eq!(uv.len(), 3);

        // Verify order is maintained
        assert_eq!(uv.as_ref(), &[1, 2, 3]);
    }

    #[test]
    fn test_unique_vec_extend() {
        let mut uv = UniqueVec::from(vec![1, 2, 3]);

        // Extend with mix of new and existing items
        uv.extend(vec![3, 4, 5, 2, 6]);

        assert_eq!(uv.len(), 6);
        assert_eq!(uv.as_ref(), &[1, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn test_unique_vec_retain() {
        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);

        // Retain only even numbers
        uv.retain(|&x| x % 2 == 0);

        assert_eq!(uv.len(), 2);
        assert_eq!(uv.as_ref(), &[2, 4]);
        assert!(uv.contains(&2));
        assert!(uv.contains(&4));
        assert!(!uv.contains(&1));
        assert!(!uv.contains(&3));
        assert!(!uv.contains(&5));
    }

    #[test]
    fn test_unique_vec_retain_keeps_set_consistent_after_panic() {
        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.retain(|&x| {
                if x == 3 {
                    panic!("intentional retain panic");
                }

                x % 2 == 0
            });
        }));

        assert!(result.is_err());
        for value in 1..=5 {
            assert_eq!(uv.contains(&value), uv.as_ref().contains(&value));
        }
    }

    #[test]
    fn test_unique_vec_retain_without_removal_keeps_set_consistent() {
        let mut uv = UniqueVec::from(vec![1, 2, 3]);

        uv.retain(|_| true);

        assert_eq!(uv.as_ref(), &[1, 2, 3]);
        // The set must still reject duplicates and accept new items.
        assert!(!uv.push(2));
        assert!(uv.push(4));
        assert_eq!(uv.as_ref(), &[1, 2, 3, 4]);
    }

    #[test]
    fn test_unique_vec_intersect_with_superset_and_disjoint() {
        let mut uv = UniqueVec::from(vec![1, 2, 3]);
        let superset = UniqueVec::from(vec![1, 2, 3, 4, 5]);

        // Intersecting with a superset removes nothing.
        uv.intersect_with(&superset);
        assert_eq!(uv.as_ref(), &[1, 2, 3]);
        assert!(!uv.push(3));

        // Intersecting with a disjoint set removes everything; removed items
        // must be insertable again afterwards.
        let disjoint = UniqueVec::from(vec![7, 8]);
        uv.intersect_with(&disjoint);
        assert!(uv.is_empty());
        assert!(!uv.contains(&1));
        assert!(uv.push(1));
    }

    #[test]
    fn test_unique_vec_remove() {
        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);

        let removed = uv.remove(2); // Remove element at index 2 (value 3)
        assert_eq!(removed, 3);
        assert_eq!(uv.len(), 4);
        assert_eq!(uv.as_ref(), &[1, 2, 4, 5]);
        assert!(!uv.contains(&3));
    }

    #[test]
    #[should_panic]
    fn test_unique_vec_remove_out_of_bounds() {
        let mut uv = UniqueVec::from(vec![1, 2, 3]);
        uv.remove(5); // Should panic
    }

    #[test]
    fn test_unique_vec_remove_if() {
        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);

        // Remove first even number
        let removed = uv.remove_if(|&x| x % 2 == 0);
        assert_eq!(removed, Some(2));
        assert_eq!(uv.len(), 4);
        assert_eq!(uv.as_ref(), &[1, 3, 4, 5]);
        assert!(!uv.contains(&2));

        // Try to remove non-existent condition
        let removed = uv.remove_if(|&x| x > 10);
        assert_eq!(removed, None);
        assert_eq!(uv.len(), 4);
    }

    #[test]
    fn test_unique_vec_swap_remove_if() {
        let mut uv = UniqueVec::from(vec![1, 2, 3, 4, 5]);

        // Remove first even number (swap with last)
        let removed = uv.swap_remove_if(|&x| x % 2 == 0);
        assert_eq!(removed, Some(2));
        assert_eq!(uv.len(), 4);
        // After swap_remove, the last element (5) should be in position of removed element
        assert_eq!(uv.as_ref(), &[1, 5, 3, 4]);
        assert!(!uv.contains(&2));
    }

    #[test]
    fn test_unique_vec_contains() {
        let uv = UniqueVec::from(vec![1, 2, 3]);

        assert!(uv.contains(&1));
        assert!(uv.contains(&2));
        assert!(uv.contains(&3));
        assert!(!uv.contains(&4));
    }

    #[test]
    fn test_unique_vec_intersect_with() {
        let mut uv1 = UniqueVec::from(vec![1, 2, 3, 4, 5]);
        let uv2 = UniqueVec::from(vec![3, 4, 5, 6, 7]);

        uv1.intersect_with(&uv2);

        assert_eq!(uv1.len(), 3);
        assert!(uv1.contains(&3));
        assert!(uv1.contains(&4));
        assert!(uv1.contains(&5));
        assert!(!uv1.contains(&1));
        assert!(!uv1.contains(&2));
    }

    #[test]
    fn test_unique_vec_to_vec() {
        let uv = UniqueVec::from(vec![1, 2, 2, 3]);
        let vec = uv.to_vec();
        assert_eq!(vec, vec![1, 2, 3]);
    }

    #[test]
    fn test_unique_vec_to_set() {
        let uv = UniqueVec::from(vec![1, 2, 3]);
        let set = uv.to_set();
        assert_eq!(set.len(), 3);
        assert!(set.contains(&1));
        assert!(set.contains(&2));
        assert!(set.contains(&3));
    }

    #[test]
    fn test_unique_vec_as_ref() {
        let uv = UniqueVec::from(vec![1, 2, 3]);
        let slice: &[i32] = uv.as_ref();
        assert_eq!(slice, &[1, 2, 3]);
    }

    #[test]
    fn test_unique_vec_deref() {
        let uv = UniqueVec::from(vec![1, 2, 3]);
        // Test deref by calling Vec methods directly
        assert_eq!(uv.len(), 3);
        assert_eq!(uv[0], 1);
        assert_eq!(uv[1], 2);
        assert_eq!(uv[2], 3);
    }

    #[test]
    fn test_unique_vec_into_vec() {
        let uv = UniqueVec::from(vec![1, 2, 3]);
        let vec: Vec<i32> = uv.into();
        assert_eq!(vec, vec![1, 2, 3]);
    }

    #[test]
    fn test_unique_vec_serialize_deserialize() {
        let uv = UniqueVec::from(vec![1, 2, 2, 3, 2, 1]); // Duplicates should be removed

        // Serialize
        let json = serde_json::to_string(&uv).unwrap();
        assert_eq!(json, "[1,2,3]");

        // Deserialize
        let deserialized: UniqueVec<i32> = serde_json::from_str("[1,3,2,3,3,2,1]").unwrap();
        assert_eq!(deserialized.len(), 3);
        assert_eq!(deserialized.as_ref(), &[1, 3, 2]);
    }

    #[test]
    fn test_unique_vec_deserialize_borrowed_values() {
        fn deserialize_unique_vec_cow<'a>(json: &'a str) -> UniqueVec<Cow<'a, str>> {
            serde_json::from_str(json).unwrap()
        }

        let json = String::from(r#"["alpha","beta","alpha"]"#);
        let deserialized = deserialize_unique_vec_cow(&json);

        assert_eq!(
            deserialized.as_ref(),
            &[Cow::Borrowed("alpha"), Cow::Borrowed("beta")]
        );
    }

    #[test]
    fn test_unique_vec_clone() {
        let uv1 = UniqueVec::from(vec![1, 2, 3]);
        let uv2 = uv1.clone();

        assert_eq!(uv1.len(), uv2.len());
        assert_eq!(uv1.as_ref(), uv2.as_ref());
    }

    #[test]
    fn test_counting_writer_new() {
        let writer = CountingWriter::new();
        assert_eq!(writer.size(), 0);
    }

    #[test]
    fn test_counting_writer_default() {
        let writer = CountingWriter::default();
        assert_eq!(writer.size(), 0);
    }

    #[test]
    fn test_counting_writer_write() {
        let mut writer = CountingWriter::new();

        let result = writer.write(b"hello");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 5);
        assert_eq!(writer.size(), 5);

        let result = writer.write(b" world");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 6);
        assert_eq!(writer.size(), 11);
    }

    #[test]
    fn test_counting_writer_flush() {
        let mut writer = CountingWriter::new();
        let result = writer.flush();
        assert!(result.is_ok());
        assert_eq!(writer.size(), 0); // Flush doesn't change size
    }

    #[test]
    fn test_counting_writer_multiple_writes() {
        let mut writer = CountingWriter::new();

        // Multiple writes should accumulate
        writer.write_all(b"a").unwrap();
        assert_eq!(writer.size(), 1);

        writer.write_all(b"bc").unwrap();
        assert_eq!(writer.size(), 3);

        writer.write_all(b"defg").unwrap();
        assert_eq!(writer.size(), 7);
    }

    #[test]
    fn test_counting_writer_empty_write() {
        let mut writer = CountingWriter::new();

        let result = writer.write(b"");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 0);
        assert_eq!(writer.size(), 0);
    }

    #[test]
    fn test_unique_vec_edge_cases() {
        // Test with empty vector
        let uv = UniqueVec::from(vec![] as Vec<i32>);
        assert_eq!(uv.len(), 0);
        assert!(uv.is_empty());

        // Test with single element
        let mut uv = UniqueVec::from(vec![42]);
        assert_eq!(uv.len(), 1);
        assert!(uv.contains(&42));

        // Test removing the only element
        let removed = uv.remove(0);
        assert_eq!(removed, 42);
        assert_eq!(uv.len(), 0);
        assert!(!uv.contains(&42));
    }

    #[test]
    fn test_unique_vec_string_type() {
        let mut uv = UniqueVec::new();

        uv.push("hello".to_string());
        uv.push("world".to_string());
        uv.push("hello".to_string()); // Duplicate

        assert_eq!(uv.len(), 2);
        assert!(uv.contains("hello"));
        assert!(uv.contains("world"));
    }

    #[test]
    fn test_counting_writer_overflow() {
        let mut writer = CountingWriter { count: usize::MAX };
        let err = writer.write(b"x").unwrap_err();

        assert_eq!(err.kind(), std::io::ErrorKind::Other);
        assert_eq!(writer.size(), usize::MAX);
    }

    #[test]
    fn test_unique_vec_extend_panicking_iterator_keeps_set_consistent() {
        let mut uv = UniqueVec::from(vec![1, 2]);

        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.extend((3..10).inspect(|&x| {
                if x == 5 {
                    panic!("intentional extend panic");
                }
            }));
        }));
        assert!(result.is_err());

        // Elements yielded before the panic are applied; set and vec agree.
        assert_eq!(uv.as_ref(), &[1, 2, 3, 4]);
        for value in 1..10 {
            assert_eq!(uv.contains(&value), uv.as_ref().contains(&value));
        }
        // The set still rejects duplicates and accepts new items.
        assert!(!uv.push(4));
        assert!(uv.push(5));
        assert_eq!(uv.as_ref(), &[1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_unique_vec_cbor_round_trip_and_dedup() {
        // Round-trip through CBOR (the workspace's on-disk format).
        let uv = UniqueVec::from(vec![1u64, 2, 3]);
        let mut buf = Vec::new();
        cbor2::to_writer(&uv, &mut buf).unwrap();
        let decoded: UniqueVec<u64> = cbor2::from_reader(&buf[..]).unwrap();
        assert_eq!(decoded, uv);

        // CBOR input containing duplicates (e.g. a corrupted or crash-window
        // bucket file in anda_db_btree) is deduplicated on load, preserving
        // first-occurrence order.
        let mut buf = Vec::new();
        cbor2::to_writer(&vec![5u64, 1, 5, 2, 1], &mut buf).unwrap();
        let decoded: UniqueVec<u64> = cbor2::from_reader(&buf[..]).unwrap();
        assert_eq!(decoded.as_ref(), &[5, 1, 2]);
        assert!(decoded.contains(&2));
        assert!(!decoded.contains(&9));
    }

    #[test]
    fn test_counting_writer_matches_cbor2_serialized_size() {
        let value = (42u64, "hello".to_string(), vec![1u8, 2, 3]);
        let mut writer = CountingWriter::new();
        cbor2::to_writer(&value, &mut writer).unwrap();
        assert_eq!(
            writer.size() as u64,
            cbor2::serialized_size(&value).unwrap()
        );
    }

    #[test]
    fn test_unique_vec_partial_eq() {
        let a = UniqueVec::from(vec![1, 2, 3]);
        let b = UniqueVec::from(vec![1, 2, 2, 3]);
        let c = UniqueVec::from(vec![3, 2, 1]);
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    use std::cell::Cell;
    use std::hash::{Hash, Hasher};

    thread_local! {
        /// Remaining `Hash::hash` calls before the next one panics.
        /// `None` disables panicking.
        static HASH_PANIC_COUNTDOWN: Cell<Option<usize>> = const { Cell::new(None) };
        /// When `true`, the next `Clone::clone` call panics.
        static CLONE_PANIC: Cell<bool> = const { Cell::new(false) };
    }

    /// Element type whose `Hash` / `Clone` impls can be armed to panic,
    /// simulating adversarial or buggy user types.
    #[derive(Debug, PartialEq, Eq)]
    struct Evil(u32);

    impl Hash for Evil {
        fn hash<H: Hasher>(&self, state: &mut H) {
            HASH_PANIC_COUNTDOWN.with(|c| {
                if let Some(n) = c.get() {
                    if n == 0 {
                        c.set(None);
                        panic!("intentional Hash panic");
                    }
                    c.set(Some(n - 1));
                }
            });
            self.0.hash(state);
        }
    }

    impl Clone for Evil {
        fn clone(&self) -> Self {
            CLONE_PANIC.with(|c| {
                if c.get() {
                    c.set(false);
                    panic!("intentional Clone panic");
                }
            });
            Evil(self.0)
        }
    }

    fn assert_evil_invariant(uv: &UniqueVec<Evil>, universe: std::ops::RangeInclusive<u32>) {
        // set and vec must agree exactly, and vec must have no duplicates.
        for value in universe {
            let item = Evil(value);
            assert_eq!(
                uv.contains(&item),
                uv.as_ref().contains(&item),
                "set/vec diverged for {value}"
            );
            assert!(
                uv.as_ref().iter().filter(|x| **x == item).count() <= 1,
                "duplicate element {value} in vec"
            );
        }
    }

    #[test]
    fn test_unique_vec_push_hash_panic_keeps_invariant() {
        let mut uv: UniqueVec<Evil> = UniqueVec::new();
        assert!(uv.push(Evil(1)));
        assert!(uv.push(Evil(2)));

        // `push` hashes twice: once in `contains`, once in `set.insert`.
        // Arm the panic for the second hash so `vec.push` has already
        // succeeded when `set.insert` unwinds — the historical window where
        // vec ⊋ set let a later push insert a duplicate.
        HASH_PANIC_COUNTDOWN.with(|c| c.set(Some(1)));
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.push(Evil(3));
        }));
        HASH_PANIC_COUNTDOWN.with(|c| c.set(None));
        assert!(result.is_err());

        // The drop guard must have rolled the vector back.
        assert_eq!(uv.as_ref(), &[Evil(1), Evil(2)]);
        assert_evil_invariant(&uv, 1..=3);

        // Re-pushing the same element must add it exactly once.
        assert!(uv.push(Evil(3)));
        assert!(!uv.push(Evil(3)));
        assert_eq!(uv.as_ref(), &[Evil(1), Evil(2), Evil(3)]);
        assert_evil_invariant(&uv, 1..=3);
    }

    #[test]
    fn test_unique_vec_push_clone_panic_keeps_invariant() {
        let mut uv: UniqueVec<Evil> = UniqueVec::new();
        assert!(uv.push(Evil(1)));

        CLONE_PANIC.with(|c| c.set(true));
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.push(Evil(2));
        }));
        CLONE_PANIC.with(|c| c.set(false));
        assert!(result.is_err());

        assert_eq!(uv.as_ref(), &[Evil(1)]);
        assert_evil_invariant(&uv, 1..=2);
        assert!(uv.push(Evil(2)));
        assert_evil_invariant(&uv, 1..=2);
    }

    #[test]
    fn test_unique_vec_remove_hash_panic_keeps_invariant() {
        let mut uv: UniqueVec<Evil> = UniqueVec::new();
        for v in 1..=3 {
            assert!(uv.push(Evil(v)));
        }

        // `remove` / `remove_if` / `swap_remove_if` hash once (`set.remove`).
        // A panic there must leave both containers untouched instead of the
        // historical set ⊋ vec state where the element could never be
        // re-inserted.
        HASH_PANIC_COUNTDOWN.with(|c| c.set(Some(0)));
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.remove(1);
        }));
        HASH_PANIC_COUNTDOWN.with(|c| c.set(None));
        assert!(result.is_err());
        assert_eq!(uv.as_ref(), &[Evil(1), Evil(2), Evil(3)]);
        assert_evil_invariant(&uv, 1..=3);

        HASH_PANIC_COUNTDOWN.with(|c| c.set(Some(0)));
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.remove_if(|x| x.0 == 2);
        }));
        HASH_PANIC_COUNTDOWN.with(|c| c.set(None));
        assert!(result.is_err());
        assert_eq!(uv.as_ref(), &[Evil(1), Evil(2), Evil(3)]);
        assert_evil_invariant(&uv, 1..=3);

        HASH_PANIC_COUNTDOWN.with(|c| c.set(Some(0)));
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            uv.swap_remove_if(|x| x.0 == 2);
        }));
        HASH_PANIC_COUNTDOWN.with(|c| c.set(None));
        assert!(result.is_err());
        assert_eq!(uv.as_ref(), &[Evil(1), Evil(2), Evil(3)]);
        assert_evil_invariant(&uv, 1..=3);

        // With panics disarmed, removal still works normally.
        assert_eq!(uv.remove_if(|x| x.0 == 2), Some(Evil(2)));
        assert_eq!(uv.as_ref(), &[Evil(1), Evil(3)]);
        assert_evil_invariant(&uv, 1..=3);
        assert!(uv.push(Evil(2)));
        assert_evil_invariant(&uv, 1..=3);
    }
}