Crates.io | anda_db_utils |
lib.rs | anda_db_utils |
version | 0.2.1 |
created_at | 2025-07-11 02:47:05.7573+00 |
updated_at | 2025-08-10 13:10:24.019492+00 |
description | An utility Rust library for Anda DB. |
homepage | |
repository | https://github.com/ldclabs/anda_db/tree/main/rs/anda_db_utils |
max_upload_size | |
id | 1747351 |
size | 45,045 |
anda_db_utils
is a utility Rust library for Anda DB, providing helpful extensions and data structures.
Pipe
Trait: Enables functional-style method chaining.UniqueVec<T>
: A Vec
that ensures all its elements are unique.CountingWriter
: A writer that counts bytes, useful for determining the size of serialized data without allocation.The Pipe
trait allows you to chain functions in a fluent, readable way.
use anda_db_utils::Pipe;
let result = 5.pipe(|x| x * 2).pipe(|x| x + 1);
assert_eq!(result, 11);
UniqueVec<T>
is a vector that automatically handles uniqueness of its elements, backed by a HashSet
for efficiency.
use anda_db_utils::UniqueVec;
let mut unique_vec = UniqueVec::from(vec![1, 2, 3]);
// Pushing an existing item does nothing
unique_vec.push(2);
assert_eq!(unique_vec.as_ref(), &[1, 2, 3]);
// Pushing a new item adds it to the vector
unique_vec.push(4);
assert_eq!(unique_vec.as_ref(), &[1, 2, 3, 4]);
// Extend with a mix of new and existing items
unique_vec.extend(vec![3, 5, 6]);
assert_eq!(unique_vec.as_ref(), &[1, 2, 3, 4, 5, 6]);
CountingWriter
can be used to calculate the size of data when serialized, for example with CBOR, without actually writing to memory.
use anda_db_utils::CountingWriter;
use serde::Serialize;
#[derive(Serialize)]
struct MyData {
id: u32,
name: String,
}
let data = MyData { id: 1, name: "test".to_string() };
let size = estimate_cbor_size(&data);
println!("Serialized size: {} bytes", size);
Copyright © 2025 LDC Labs.
ldclabs/anda-db
is licensed under the MIT License. See LICENSE for the full license text.